Changeset View
Changeset View
Standalone View
Standalone View
src/qmlcontrols/kcmcontrols/settingstateproxy.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2020 Kevin Ottens <kevin.ottens@enioka.com> | ||||
3 | * | ||||
4 | * This library is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU Library General Public | ||||
6 | * License as published by the Free Software Foundation; either | ||||
7 | * version 2 of the License, or (at your option) any later version. | ||||
8 | * | ||||
9 | * This library is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
12 | * Library General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU Library General Public License | ||||
15 | * along with this library; see the file COPYING.LIB. If not, write to | ||||
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
17 | * Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include "settingstateproxy.h" | ||||
21 | | ||||
22 | #include <QDebug> | ||||
23 | #include <QMetaMethod> | ||||
24 | | ||||
25 | KCoreConfigSkeleton *SettingStateProxy::configObject() const | ||||
26 | { | ||||
27 | return m_configObject; | ||||
28 | } | ||||
29 | | ||||
30 | void SettingStateProxy::setConfigObject(KCoreConfigSkeleton *configObject) | ||||
31 | { | ||||
32 | if (m_configObject == configObject) { | ||||
33 | return; | ||||
34 | } | ||||
35 | | ||||
36 | if (m_configObject) { | ||||
37 | m_configObject->disconnect(this); | ||||
38 | } | ||||
39 | | ||||
40 | m_configObject = configObject; | ||||
41 | emit configObjectChanged(); | ||||
42 | updateState(); | ||||
43 | connectSetting(); | ||||
44 | } | ||||
45 | | ||||
46 | QString SettingStateProxy::itemName() const | ||||
47 | { | ||||
48 | return m_itemName; | ||||
49 | } | ||||
50 | | ||||
51 | void SettingStateProxy::setItemName(const QString &itemName) | ||||
52 | { | ||||
53 | if (m_itemName == itemName) { | ||||
54 | return; | ||||
55 | } | ||||
56 | | ||||
57 | if (m_configObject) { | ||||
58 | m_configObject->disconnect(this); | ||||
59 | } | ||||
60 | | ||||
61 | m_itemName = itemName; | ||||
62 | emit itemNameChanged(); | ||||
63 | updateState(); | ||||
64 | connectSetting(); | ||||
65 | } | ||||
66 | | ||||
67 | bool SettingStateProxy::isImmutable() const | ||||
68 | { | ||||
69 | return m_immutable; | ||||
70 | } | ||||
71 | | ||||
72 | bool SettingStateProxy::isDefaulted() const | ||||
73 | { | ||||
74 | return m_defaulted; | ||||
75 | } | ||||
76 | | ||||
77 | void SettingStateProxy::resetToDefault() | ||||
78 | { | ||||
79 | const auto item = m_configObject ? m_configObject->findItem(m_itemName) : nullptr; | ||||
80 | if (item) { | ||||
81 | item->setDefault(); | ||||
82 | } | ||||
83 | } | ||||
84 | | ||||
85 | void SettingStateProxy::updateState() | ||||
86 | { | ||||
87 | const auto item = m_configObject ? m_configObject->findItem(m_itemName) : nullptr; | ||||
88 | const auto immutable = item ? item->isImmutable() : false; | ||||
89 | const auto defaulted = item ? item->isDefault() : true; | ||||
90 | | ||||
91 | if (m_immutable != immutable) { | ||||
92 | m_immutable = immutable; | ||||
93 | emit immutableChanged(); | ||||
94 | } | ||||
95 | | ||||
96 | if (m_defaulted != defaulted) { | ||||
97 | m_defaulted = defaulted; | ||||
98 | emit defaultedChanged(); | ||||
99 | } | ||||
100 | } | ||||
101 | | ||||
102 | void SettingStateProxy::connectSetting() | ||||
103 | { | ||||
104 | const auto item = m_configObject ? m_configObject->findItem(m_itemName) : nullptr; | ||||
105 | if (!item) { | ||||
106 | return; | ||||
107 | } | ||||
108 | | ||||
109 | const auto updateStateSlotIndex = metaObject()->indexOfMethod("updateState()"); | ||||
110 | Q_ASSERT(updateStateSlotIndex >= 0); | ||||
111 | const auto updateStateSlot = metaObject()->method(updateStateSlotIndex); | ||||
112 | Q_ASSERT(updateStateSlot.isValid()); | ||||
113 | | ||||
114 | const auto itemHasSignals = dynamic_cast<KConfigCompilerSignallingItem*>(item) || dynamic_cast<KPropertySkeletonItem*>(item); | ||||
115 | if (!itemHasSignals) { | ||||
116 | qWarning() << "Attempting to use SettingStateProxy with a non signalling item:" << m_itemName; | ||||
117 | return; | ||||
118 | } | ||||
119 | | ||||
120 | const auto propertyName = [this] { | ||||
121 | auto name = m_itemName; | ||||
122 | if (name.at(0).isUpper()) { | ||||
123 | name[0] = name[0].toLower(); | ||||
124 | } | ||||
125 | return name.toUtf8(); | ||||
126 | }(); | ||||
127 | | ||||
128 | const auto metaObject = m_configObject->metaObject(); | ||||
129 | const auto propertyIndex = metaObject->indexOfProperty(propertyName.constData()); | ||||
130 | Q_ASSERT(propertyIndex >= 0); | ||||
131 | const auto property = metaObject->property(propertyIndex); | ||||
132 | Q_ASSERT(property.isValid()); | ||||
133 | if (!property.hasNotifySignal()) { | ||||
134 | qWarning() << "Attempting to use SettingStateProxy with a non notifying property:" << propertyName; | ||||
135 | return; | ||||
136 | } | ||||
137 | | ||||
138 | const auto changedSignal = property.notifySignal(); | ||||
139 | Q_ASSERT(changedSignal.isValid()); | ||||
140 | connect(m_configObject, changedSignal, this, updateStateSlot); | ||||
141 | connect(m_configObject, &KCoreConfigSkeleton::configChanged, | ||||
142 | this, &SettingStateProxy::updateState); | ||||
143 | } |