From 9ac1cc139e1b55a15d0fa827baffd20494bd5078 Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Wed, 14 Jul 2021 15:39:43 +0200 Subject: [PATCH] Move QuickSettingsModel to C++ --- .../mobilehomescreencomponents/CMakeLists.txt | 3 +- .../mobilehomescreencomponentsplugin.cpp | 6 +- .../quicksettingsmodel.cpp | 94 ++++++++++++ .../quicksettingsmodel.h | 89 +++++++++++ .../panel/package/contents/ui/main.qml | 5 +- .../ui/quicksettings/QuickSetting.qml | 17 --- .../ui/quicksettings/QuickSettingsModel.qml | 138 ----------------- .../ui/quicksettings/QuickSettingsPanel.qml | 4 +- .../ui/quicksettings/SettingsModel.qml | 139 ++++++++++++++++++ 9 files changed, 334 insertions(+), 161 deletions(-) create mode 100644 components/mobilehomescreencomponents/quicksettingsmodel.cpp create mode 100644 components/mobilehomescreencomponents/quicksettingsmodel.h delete mode 100644 containments/panel/package/contents/ui/quicksettings/QuickSetting.qml delete mode 100644 containments/panel/package/contents/ui/quicksettings/QuickSettingsModel.qml create mode 100644 containments/panel/package/contents/ui/quicksettings/SettingsModel.qml diff --git a/components/mobilehomescreencomponents/CMakeLists.txt b/components/mobilehomescreencomponents/CMakeLists.txt index 70f020b4..f9114281 100644 --- a/components/mobilehomescreencomponents/CMakeLists.txt +++ b/components/mobilehomescreencomponents/CMakeLists.txt @@ -5,7 +5,8 @@ set(mobilehomescreencomponentsplugin_SRCS applicationlistmodel.cpp favoritesmodel.cpp homescreenutils.cpp - ) + quicksettingsmodel.cpp +) add_library(mobilehomescreencomponentsplugin ${mobilehomescreencomponentsplugin_SRCS}) diff --git a/components/mobilehomescreencomponents/mobilehomescreencomponentsplugin.cpp b/components/mobilehomescreencomponents/mobilehomescreencomponentsplugin.cpp index c7058cdb..793e2b7b 100644 --- a/components/mobilehomescreencomponents/mobilehomescreencomponentsplugin.cpp +++ b/components/mobilehomescreencomponents/mobilehomescreencomponentsplugin.cpp @@ -22,14 +22,18 @@ #include #include -#include "favoritesmodel.h" #include "applicationlistmodel.h" +#include "favoritesmodel.h" #include "homescreenutils.h" +#include "quicksettingsmodel.h" void MobileHomeScreenComponentsPlugin::registerTypes(const char *uri) { Q_ASSERT(QLatin1String(uri) == QLatin1String("org.kde.plasma.private.mobilehomescreencomponents")); + qmlRegisterType(uri, 0, 1, "QuickSetting"); + qmlRegisterType(uri, 0, 1, "QuickSettingsModel"); + qmlRegisterSingletonType(uri, 0, 1, "HomeScreenUtils", [](QQmlEngine *, QJSEngine *) { return new HomeScreenUtils{}; diff --git a/components/mobilehomescreencomponents/quicksettingsmodel.cpp b/components/mobilehomescreencomponents/quicksettingsmodel.cpp new file mode 100644 index 00000000..71e6f171 --- /dev/null +++ b/components/mobilehomescreencomponents/quicksettingsmodel.cpp @@ -0,0 +1,94 @@ +/* + * SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez + * + * SPDX-License-Identifier: LGPL-2.0-or-later + */ + +#include "quicksettingsmodel.h" + +QuickSettingsModel::QuickSettingsModel(QObject *parent) + : QAbstractListModel(parent) +{ +} + +QHash QuickSettingsModel::roleNames() const +{ + return {{Qt::UserRole, "modelData"}}; +} + +QQmlListProperty QuickSettingsModel::children() +{ + return QQmlListProperty(this, &m_children); +} + +int QuickSettingsModel::rowCount(const QModelIndex &parent) const +{ + if (parent.isValid()) + return 0; + + return m_children.count(); +} + +QVariant QuickSettingsModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid() || index.row() >= m_children.count() || role != Qt::UserRole) { + return {}; + } + + return QVariant::fromValue(m_children[index.row()]); +} + +//////////////////////// + +QuickSetting::QuickSetting(QObject *parent) + : QObject(parent) +{ +} + +void QuickSetting::setEnabled(bool enabled) +{ + if (m_enabled == enabled) + return; + + m_enabled = enabled; + Q_EMIT enabledChanged(enabled); +} + +void QuickSetting::setSettingsCommand(const QString &settingsCommand) +{ + if (m_settingsCommand == settingsCommand) + return; + + m_settingsCommand = settingsCommand; + Q_EMIT settingsCommandChanged(settingsCommand); +} + +void QuickSetting::setIconName(const QString &iconName) +{ + if (m_iconName == iconName) + return; + + m_iconName = iconName; + Q_EMIT iconNameChanged(iconName); +} + +void QuickSetting::setText(const QString &text) +{ + if (m_text == text) + return; + + m_text = text; + Q_EMIT textChanged(text); +} + +QQmlListProperty QuickSetting::children() +{ + return QQmlListProperty(this, &m_children); +} + +void QuickSettingsModel::include(QuickSetting *item) +{ + beginInsertRows({}, m_children.count(), m_children.count()); + m_children.append(item); + endInsertRows(); +} diff --git a/components/mobilehomescreencomponents/quicksettingsmodel.h b/components/mobilehomescreencomponents/quicksettingsmodel.h new file mode 100644 index 00000000..7a07c885 --- /dev/null +++ b/components/mobilehomescreencomponents/quicksettingsmodel.h @@ -0,0 +1,89 @@ +/* + * SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez + * + * SPDX-License-Identifier: LGPL-2.0-or-later + */ + +#ifndef QUICKSETTINGSMODEL_H +#define QUICKSETTINGSMODEL_H + +#include "qqml.h" +#include +#include + +class QuickSetting : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString text READ text WRITE setText REQUIRED NOTIFY textChanged) + Q_PROPERTY(QString icon READ iconName WRITE setIconName REQUIRED NOTIFY iconNameChanged) + Q_PROPERTY(QString settingsCommand READ settingsCommand WRITE setSettingsCommand NOTIFY settingsCommandChanged) + Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged) + Q_PROPERTY(QQmlListProperty children READ children CONSTANT) + Q_CLASSINFO("DefaultProperty", "children") + QML_NAMED_ELEMENT("QuickSetting") +public: + QuickSetting(QObject *parent = nullptr); + + QString text() const + { + return m_text; + } + QString iconName() const + { + return m_iconName; + } + QString settingsCommand() const + { + return m_settingsCommand; + } + bool isEnabled() const + { + return m_enabled; + } + + void setText(const QString &text); + void setIconName(const QString &iconName); + void setSettingsCommand(const QString &settingsCommand); + void setEnabled(bool enabled); + QQmlListProperty children(); + +Q_SIGNALS: + void enabledChanged(bool enabled); + void textChanged(const QString &text); + void iconNameChanged(const QString &icon); + void settingsCommandChanged(const QString &settingsCommand); + +private: + bool m_enabled = true; + QString m_text; + QString m_iconName; + QString m_settingsCommand; + QList m_children; +}; + +class QuickSettingsModel : public QAbstractListModel +{ + Q_OBJECT + Q_PROPERTY(QQmlListProperty children READ children NOTIFY childrenChanged) + Q_CLASSINFO("DefaultProperty", "children") + QML_ELEMENT + +public: + QuickSettingsModel(QObject *parent = nullptr); + + QVariant data(const QModelIndex &index, int role) const override; + int rowCount(const QModelIndex &parent) const override; + QHash roleNames() const override; + + QQmlListProperty children(); + + Q_SCRIPTABLE void include(QuickSetting *item); + +Q_SIGNALS: + void childrenChanged(); + +private: + QList m_children; +}; + +#endif // QUICKSETTINGSMODEL_H diff --git a/containments/panel/package/contents/ui/main.qml b/containments/panel/package/contents/ui/main.qml index 633d48c9..00df23b0 100644 --- a/containments/panel/package/contents/ui/main.qml +++ b/containments/panel/package/contents/ui/main.qml @@ -21,6 +21,7 @@ import org.kde.taskmanager 0.1 as TaskManager import org.kde.plasma.private.nanoshell 2.0 as NanoShell import org.kde.plasma.private.mobileshell 1.0 as MobileShell +import org.kde.plasma.private.mobilehomescreencomponents 0.1 as HomeScreenComponents import "LayoutManager.js" as LayoutManager @@ -148,7 +149,7 @@ Item { IndicatorProviders.VolumeProvider { id: volumeProvider - readonly property var soundQuickSetting: QuickSetting { + readonly property var setting: HomeScreenComponents.QuickSetting { text: i18n("Sound") icon: "audio-speakers-symbolic" enabled: false @@ -156,8 +157,8 @@ Item { function toggle() { volumeProvider.showVolumeOverlay() } - Component.onCompleted: quickSettings.quickSettingsModel.model.push(volumeProvider.soundQuickSetting) } + Component.onCompleted: quickSettings.quickSettingsModel.include(setting) } IndicatorProviders.WifiProvider { id: wifiProvider diff --git a/containments/panel/package/contents/ui/quicksettings/QuickSetting.qml b/containments/panel/package/contents/ui/quicksettings/QuickSetting.qml deleted file mode 100644 index 8f4f60ba..00000000 --- a/containments/panel/package/contents/ui/quicksettings/QuickSetting.qml +++ /dev/null @@ -1,17 +0,0 @@ -/* -* SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez - * - * SPDX-License-Identifier: LGPL-2.0-or-later - */ - -import QtQuick 2.10 - -QtObject -{ - required property string text - required property string icon - property string settingsCommand - property bool enabled: true - - default property var children: [] -} diff --git a/containments/panel/package/contents/ui/quicksettings/QuickSettingsModel.qml b/containments/panel/package/contents/ui/quicksettings/QuickSettingsModel.qml deleted file mode 100644 index 9b40b835..00000000 --- a/containments/panel/package/contents/ui/quicksettings/QuickSettingsModel.qml +++ /dev/null @@ -1,138 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015 Marco Martin - * SPDX-FileCopyrightText: 2021 Devin Lin - * - * SPDX-License-Identifier: LGPL-2.0-or-later - */ - -import QtQuick 2.14 -import QtQuick.Layouts 1.1 -import QtQuick.Window 2.2 -import org.kde.plasma.core 2.0 as PlasmaCore -import org.kde.plasma.components 2.0 as PlasmaComponents -import org.kde.plasma.networkmanagement 0.2 as PlasmaNM -import org.kde.bluezqt 1.0 as BluezQt -import org.kde.colorcorrect 0.1 as CC -import org.kde.plasma.private.nanoshell 2.0 as NanoShell - -import org.kde.plasma.components 3.0 as PC3 - -Item { - id: modelItem - property bool screenshotRequested: false - - signal panelClosed() - - onPanelClosed: { - if (screenshotRequested) { - plasmoid.nativeInterface.takeScreenshot(); - screenshotRequested = false; - } - } - - readonly property list model: [ - QuickSetting { - text: i18n("Settings") - icon: "configure" - enabled: false - settingsCommand: "plasma-settings" - }, - QuickSetting { - text: i18n("Wifi") - icon: "network-wireless-signal" - settingsCommand: "plasma-settings -m kcm_mobile_wifi" - function toggle() { - nmHandler.enableWireless(!enabledConnections.wirelessEnabled) - } - enabled: enabledConnections.wirelessEnabled - }, - QuickSetting { - text: i18n("Bluetooth") - icon: "network-bluetooth" - settingsCommand: "plasma-settings -m kcm_bluetooth" - function toggle() { - var enable = !BluezQt.Manager.bluetoothOperational; - BluezQt.Manager.bluetoothBlocked = !enable; - - for (var i = 0; i < BluezQt.Manager.adapters.length; ++i) { - var adapter = BluezQt.Manager.adapters[i]; - adapter.powered = enable; - } - } - enabled: BluezQt.Manager.bluetoothOperational - }, - QuickSetting { - text: i18n("Mobile Data") - icon: "network-modem" - settingsCommand: "plasma-settings -m kcm_mobile_broadband" - enabled: enabledConnections.wwanEnabled - function toggle() { - nmHandler.enableWwan(!enabledConnections.wwanEnabled) - } - }, - QuickSetting { - text: i18n("Battery") - icon: "battery-full" - enabled: false - settingsCommand: "plasma-settings -m kcm_mobile_power" - }, - QuickSetting { - text: i18n("Flashlight") - icon: "flashlight-on" - enabled: plasmoid.nativeInterface.torchEnabled - function toggle() { - plasmoid.nativeInterface.toggleTorch() - } - }, - QuickSetting { - text: i18n("Location") - icon: "gps" - enabled: false - }, - QuickSetting { - text: i18n("Screenshot") - icon: "spectacle" - enabled: false - function toggle() { - modelItem.screenshotRequested = true; - root.closeRequested(); - } - }, - QuickSetting { - text: i18n("Auto-rotate") - icon: "rotation-allowed" - enabled: plasmoid.nativeInterface.autoRotateEnabled - function toggle() { - plasmoid.nativeInterface.autoRotateEnabled = !enabled - } - }, - QuickSetting { - text: i18n("Night Color") - icon: "redshift-status-on" - enabled: compositorAdaptor.active - settingsCommand: "plasma-settings -m kcm_nightcolor" - - CC.CompositorAdaptor { - id: compositorAdaptor - } - function toggle() { - if (compositorAdaptor.active) { - compositorAdaptor.activeStaged = false; - } else { - compositorAdaptor.activeStaged = true; - compositorAdaptor.modeStaged = 3; // always on - } - compositorAdaptor.sendConfigurationAll(); - enabled = compositorAdaptor.active; - } - } - ] - - PlasmaNM.Handler { - id: nmHandler - } - - PlasmaNM.EnabledConnections { - id: enabledConnections - } -} diff --git a/containments/panel/package/contents/ui/quicksettings/QuickSettingsPanel.qml b/containments/panel/package/contents/ui/quicksettings/QuickSettingsPanel.qml index 9446d0ec..dec1823d 100644 --- a/containments/panel/package/contents/ui/quicksettings/QuickSettingsPanel.qml +++ b/containments/panel/package/contents/ui/quicksettings/QuickSettingsPanel.qml @@ -70,7 +70,7 @@ Item { onClosed: quickSettingsModel.panelClosed() - property QuickSettingsModel quickSettingsModel: QuickSettingsModel {} + readonly property SettingsModel quickSettingsModel: SettingsModel {} PlasmaCore.FrameSvgItem { id: background @@ -132,7 +132,7 @@ Item { spacing: 0 Repeater { - model: quickSettingsModel.model + model: quickSettingsModel delegate: Delegate { id: delegateItem required property var modelData diff --git a/containments/panel/package/contents/ui/quicksettings/SettingsModel.qml b/containments/panel/package/contents/ui/quicksettings/SettingsModel.qml new file mode 100644 index 00000000..2b596a65 --- /dev/null +++ b/containments/panel/package/contents/ui/quicksettings/SettingsModel.qml @@ -0,0 +1,139 @@ +/* + * SPDX-FileCopyrightText: 2015 Marco Martin + * SPDX-FileCopyrightText: 2021 Devin Lin + * SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez + * + * SPDX-License-Identifier: LGPL-2.0-or-later + */ + +import QtQuick 2.14 +import QtQuick.Layouts 1.1 +import QtQuick.Window 2.2 +import org.kde.plasma.core 2.0 as PlasmaCore +import org.kde.plasma.components 2.0 as PlasmaComponents +import org.kde.plasma.networkmanagement 0.2 as PlasmaNM +import org.kde.bluezqt 1.0 as BluezQt +import org.kde.colorcorrect 0.1 as CC +import org.kde.plasma.private.nanoshell 2.0 as NanoShell +import org.kde.plasma.private.mobilehomescreencomponents 0.1 as HomeScreenComponents + +import org.kde.plasma.components 3.0 as PC3 + +HomeScreenComponents.QuickSettingsModel +{ + id: modelItem + property bool screenshotRequested: false + + signal panelClosed() + + onPanelClosed: { + if (screenshotRequested) { + plasmoid.nativeInterface.takeScreenshot(); + screenshotRequested = false; + } + } + + HomeScreenComponents.QuickSetting { + text: i18n("Settings") + icon: "configure" + enabled: false + settingsCommand: "plasma-settings" + } + HomeScreenComponents.QuickSetting { + PlasmaNM.Handler { + id: nmHandler + } + + PlasmaNM.EnabledConnections { + id: enabledConnections + } + + text: i18n("Wifi") + icon: "network-wireless-signal" + settingsCommand: "plasma-settings -m kcm_mobile_wifi" + function toggle() { + nmHandler.enableWireless(!enabledConnections.wirelessEnabled) + } + enabled: enabledConnections.wirelessEnabled + } + HomeScreenComponents.QuickSetting { + text: i18n("Bluetooth") + icon: "network-bluetooth" + settingsCommand: "plasma-settings -m kcm_bluetooth" + function toggle() { + var enable = !BluezQt.Manager.bluetoothOperational; + BluezQt.Manager.bluetoothBlocked = !enable; + + for (var i = 0; i < BluezQt.Manager.adapters.length; ++i) { + var adapter = BluezQt.Manager.adapters[i]; + adapter.powered = enable; + } + } + enabled: BluezQt.Manager.bluetoothOperational + } + HomeScreenComponents.QuickSetting { + text: i18n("Mobile Data") + icon: "network-modem" + settingsCommand: "plasma-settings -m kcm_mobile_broadband" + enabled: enabledConnections.wwanEnabled + function toggle() { + nmHandler.enableWwan(!enabledConnections.wwanEnabled) + } + } + HomeScreenComponents.QuickSetting { + text: i18n("Battery") + icon: "battery-full" + enabled: false + settingsCommand: "plasma-settings -m kcm_mobile_power" + } + HomeScreenComponents.QuickSetting { + text: i18n("Flashlight") + icon: "flashlight-on" + enabled: plasmoid.nativeInterface.torchEnabled + function toggle() { + plasmoid.nativeInterface.toggleTorch() + } + } + HomeScreenComponents.QuickSetting { + text: i18n("Location") + icon: "gps" + enabled: false + } + HomeScreenComponents.QuickSetting { + text: i18n("Screenshot") + icon: "spectacle" + enabled: false + function toggle() { + modelItem.screenshotRequested = true; + root.closeRequested(); + } + } + HomeScreenComponents.QuickSetting { + text: i18n("Auto-rotate") + icon: "rotation-allowed" + enabled: plasmoid.nativeInterface.autoRotateEnabled + function toggle() { + plasmoid.nativeInterface.autoRotateEnabled = !enabled + } + } + HomeScreenComponents.QuickSetting { + text: i18n("Night Color") + icon: "redshift-status-on" + enabled: compositorAdaptor.active + settingsCommand: "plasma-settings -m kcm_nightcolor" + + CC.CompositorAdaptor { + id: compositorAdaptor + } + function toggle() { + if (compositorAdaptor.active) { + compositorAdaptor.activeStaged = false; + } else { + compositorAdaptor.activeStaged = true; + compositorAdaptor.modeStaged = 3; // always on + } + compositorAdaptor.sendConfigurationAll(); + enabled = compositorAdaptor.active; + } + } +}