diff --git a/org.kde.desktop/ComboBox.qml b/org.kde.desktop/ComboBox.qml --- a/org.kde.desktop/ComboBox.qml +++ b/org.kde.desktop/ComboBox.qml @@ -53,27 +53,28 @@ property bool separatorVisible: false Kirigami.Theme.colorSet: controlRoot.Kirigami.Theme.inherit ? controlRoot.Kirigami.Theme.colorSet : Kirigami.Theme.View Kirigami.Theme.inherit: controlRoot.Kirigami.Theme.inherit - onClicked: { - controlRoot.currentIndex = index; - controlRoot.popup.visible = false; - } } indicator: Item {} + StylePrivate.PropertyWriter { + id: controlRootWriter + target: controlRoot + propertyName: 'currentIndex' + } + contentItem: MouseArea { id: mouseArea anchors.fill: parent acceptedButtons: Qt.LeftButton preventStealing: true property int indexUnderMouse: -1 onWheel: { if (wheel.pixelDelta.y < 0 || wheel.angleDelta.y < 0) { - controlRoot.currentIndex = Math.min(controlRoot.currentIndex + 1, delegateModel.count -1); + controlRoot.incrementCurrentIndex(); } else { - controlRoot.currentIndex = Math.max(controlRoot.currentIndex - 1, 0); + controlRoot.decrementCurrentIndex(); } - controlRoot.activated(controlRoot.currentIndex); } onPressed: { indexUnderMouse = -1; @@ -89,7 +90,7 @@ controlRoot.popup.visible = false; } if (indexUnderMouse > -1) { - controlRoot.currentIndex = indexUnderMouse; + controlRootWriter.writeProperty(indexUnderMouse); controlRoot.activated(indexUnderMouse); } } diff --git a/plugin/CMakeLists.txt b/plugin/CMakeLists.txt --- a/plugin/CMakeLists.txt +++ b/plugin/CMakeLists.txt @@ -3,6 +3,7 @@ set(qqc2desktopstyle_SRCS qqc2desktopstyleplugin.cpp kquickstyleitem.cpp + kpropertywriter.cpp ) if(Qt5_VERSION VERSION_LESS "5.8.0") diff --git a/plugin/kpropertywriter.cpp b/plugin/kpropertywriter.cpp new file mode 100644 --- /dev/null +++ b/plugin/kpropertywriter.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2019 Cyril Rossi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "kpropertywriter_p.h" + +QObject *KPropertyWriter::target() const +{ + return m_target; +} + +QString KPropertyWriter::propertyName() const +{ + return m_propertyName; +} + +bool KPropertyWriter::writeProperty(const QVariant &value) +{ + if (!m_target) { + return false; + } + + return m_target->setProperty(qUtf8Printable(m_propertyName), value); +} + +void KPropertyWriter::setTarget(QObject *target) +{ + if (m_target == target) { + return; + } + + m_target = target; + emit targetChanged(m_target); +} + +void KPropertyWriter::setPropertyName(const QString &propertyName) +{ + if (m_propertyName == propertyName) { + return; + } + + m_propertyName = propertyName; + emit propertyNameChanged(m_propertyName); +} + +#include "moc_kpropertywriter_p.cpp" diff --git a/plugin/kpropertywriter_p.h b/plugin/kpropertywriter_p.h new file mode 100644 --- /dev/null +++ b/plugin/kpropertywriter_p.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2019 Cyril Rossi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef KPROPERTYWRITER_H +#define KPROPERTYWRITER_H + +#include +#include + +class KPropertyWriter : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QObject *target READ target WRITE setTarget NOTIFY targetChanged) + Q_PROPERTY(QString propertyName READ propertyName WRITE setPropertyName NOTIFY propertyNameChanged) + +public: + using QObject::QObject; + + QObject *target() const; + QString propertyName() const; + + Q_INVOKABLE bool writeProperty(const QVariant &value); + +public Q_SLOTS: + void setTarget(QObject *target); + void setPropertyName(const QString &propertyName); + +Q_SIGNALS: + void targetChanged(QObject *target); + void propertyNameChanged(const QString &propertyName); + +private: + QObject *m_target = nullptr; + QString m_propertyName; +}; + +#endif // KPROPERTYWRITER_H diff --git a/plugin/qqc2desktopstyleplugin.cpp b/plugin/qqc2desktopstyleplugin.cpp --- a/plugin/qqc2desktopstyleplugin.cpp +++ b/plugin/qqc2desktopstyleplugin.cpp @@ -19,6 +19,7 @@ #include "qqc2desktopstyleplugin.h" #include "kquickstyleitem_p.h" +#include "kpropertywriter_p.h" #include #include @@ -30,6 +31,7 @@ Q_ASSERT(QLatin1String(uri) == QLatin1String("org.kde.qqc2desktopstyle.private")); qmlRegisterType(uri, 1, 0, "StyleItem"); + qmlRegisterType(uri, 1, 0, "PropertyWriter"); qmlProtectModule(uri, 2); }