From b50ce88c28e47f961abb86ff266522747533ad4c Mon Sep 17 00:00:00 2001 From: cutefishd Date: Fri, 19 Mar 2021 11:12:38 +0800 Subject: [PATCH] Screen: Support settings --- screen/confighandler.h | 7 ++++++- screen/outputmodel.cpp | 16 +++++++--------- screen/outputmodel.h | 6 +++++- screen/plugin.cpp | 2 +- screen/screen.cpp | 18 ++++++++++++++++++ screen/screen.h | 1 + 6 files changed, 38 insertions(+), 12 deletions(-) diff --git a/screen/confighandler.h b/screen/confighandler.h index 834ddcd..c7c00c5 100644 --- a/screen/confighandler.h +++ b/screen/confighandler.h @@ -14,7 +14,9 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . *********************************************************************/ -#pragma once + +#ifndef CONFIGHANDLER_H +#define CONFIGHANDLER_H #include "./common/control.h" @@ -27,6 +29,7 @@ class OutputModel; class ConfigHandler : public QObject { Q_OBJECT + public: explicit ConfigHandler (QObject *parent = nullptr); ~ConfigHandler() override = default; @@ -93,3 +96,5 @@ private: Undefined; QSize m_lastNormalizedScreenSize; }; + +#endif \ No newline at end of file diff --git a/screen/outputmodel.cpp b/screen/outputmodel.cpp index 488c078..3409ac6 100644 --- a/screen/outputmodel.cpp +++ b/screen/outputmodel.cpp @@ -14,6 +14,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . *********************************************************************/ + #include "outputmodel.h" #include "./common/utils.h" @@ -173,7 +174,8 @@ bool OutputModel::setData(const QModelIndex &index, return false; } -QHash OutputModel::roleNames() const { +QHash OutputModel::roleNames() const +{ QHash roles = QAbstractItemModel::roleNames(); roles[EnabledRole] = "enabled"; roles[InternalRole] = "internal"; @@ -483,6 +485,7 @@ static int greatestCommonDivisor(int a, int b) { QVariantList OutputModel::resolutionsStrings(const KScreen::OutputPtr &output) const { QVariantList ret; + for (const QSize &size : resolutions(output)) { int divisor = greatestCommonDivisor(size.width(), size.height()); @@ -491,17 +494,12 @@ QVariantList OutputModel::resolutionsStrings(const KScreen::OutputPtr &output) c divisor /= 2; } - const QString text; - - // const QString text = i18nc("Width x height (aspect ratio)", "%1x%2 (%3:%4)", - // // Explicitly not have it add thousand-separators. - // QString::number(size.width()), - // QString::number(size.height()), - // size.width() / divisor, - // size.height() / divisor); + const QString text = QString("%1x%2").arg(QString::number(size.width())) + .arg(QString::number(size.height())); ret << text; } + return ret; } diff --git a/screen/outputmodel.h b/screen/outputmodel.h index 76fded3..49cd5bb 100644 --- a/screen/outputmodel.h +++ b/screen/outputmodel.h @@ -14,7 +14,9 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . *********************************************************************/ -#pragma once + +#ifndef OUTPUTMODEL_H +#define OUTPUTMODEL_H #include #include @@ -148,3 +150,5 @@ private: ConfigHandler *m_config; }; + +#endif \ No newline at end of file diff --git a/screen/plugin.cpp b/screen/plugin.cpp index 80345a3..d377dca 100644 --- a/screen/plugin.cpp +++ b/screen/plugin.cpp @@ -11,7 +11,7 @@ class QmlPlugins : public QQmlExtensionPlugin public: void registerTypes(const char * uri) override { - // qmlRegisterType(uri, 1, 0, "Screen"); + // qmlRegisterType(uri, 1, 0, "OutputModel"); qmlRegisterType(uri, 1, 0, "Screen"); qmlRegisterType(uri, 1, 0, "Output"); } diff --git a/screen/screen.cpp b/screen/screen.cpp index 26badc9..73b431d 100644 --- a/screen/screen.cpp +++ b/screen/screen.cpp @@ -1,6 +1,8 @@ #include "screen.h" #include "outputmodel.h" +#include + #include #include @@ -29,6 +31,22 @@ void Screen::load() connect(new KScreen::GetConfigOperation(), &KScreen::GetConfigOperation::finished, this, &Screen::configReady); } +void Screen::save() +{ + auto config = m_config->config(); + bool atLeastOneEnabledOutput = false; + + for (const KScreen::OutputPtr &output : config->outputs()) { + KScreen::ModePtr mode = output->currentMode(); + atLeastOneEnabledOutput |= output->isEnabled(); + } + + m_config->writeControl(); + + auto *op = new KScreen::SetConfigOperation(config); + op->exec(); +} + OutputModel *Screen::outputModel() const { if (!m_config) { diff --git a/screen/screen.h b/screen/screen.h index d419424..0f79913 100644 --- a/screen/screen.h +++ b/screen/screen.h @@ -20,6 +20,7 @@ public: OutputModel *outputModel() const; void load(); + Q_INVOKABLE void save(); private: void configReady(KScreen::ConfigOperation *op);