Changeset View
Changeset View
Standalone View
Standalone View
kcms/touchpadx/src/kcm/xlib/customconfigdialogmanager.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2013 Alexander Mezin <mezin.alexander@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program 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 | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public License | ||||
15 | * along with this program; if not, write to the Free Software | ||||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #include "customconfigdialogmanager.h" | ||||
20 | | ||||
21 | #include <cmath> | ||||
22 | | ||||
23 | #include <QWidget> | ||||
24 | #include <QGroupBox> | ||||
25 | | ||||
26 | #include <KConfigSkeleton> | ||||
27 | #include <KComboBox> | ||||
28 | #include <QDebug> | ||||
29 | | ||||
30 | #include "customslider.h" | ||||
31 | | ||||
32 | CustomConfigDialogManager::CustomConfigDialogManager(QWidget *parent, | ||||
33 | KCoreConfigSkeleton *conf, | ||||
34 | const QStringList &supported) | ||||
35 | : KConfigDialogManager(parent, conf), m_config(conf) | ||||
36 | { | ||||
37 | static const QString kcfgPrefix("kcfg_"); | ||||
38 | | ||||
39 | Q_FOREACH(KConfigSkeletonItem *i, conf->items()) { | ||||
40 | QString name(i->name()); | ||||
41 | | ||||
42 | QWidget *child = parent->findChild<QWidget*>(kcfgPrefix + name); | ||||
43 | if (!child) { | ||||
44 | continue; | ||||
45 | } | ||||
46 | m_widgets[name] = child; | ||||
47 | | ||||
48 | /* FIXME: this should probably be less hackish */ | ||||
49 | if (name == "Tapping" && | ||||
50 | !supported.contains("Tapping")) | ||||
51 | qobject_cast<QGroupBox *>(child)->setCheckable(false); | ||||
52 | else if (!supported.contains(name)) { | ||||
53 | child->setEnabled(false); | ||||
54 | } | ||||
55 | | ||||
56 | KCoreConfigSkeleton::ItemEnum *asEnum = | ||||
57 | dynamic_cast<KCoreConfigSkeleton::ItemEnum *>(i); | ||||
58 | if (!asEnum) { | ||||
59 | continue; | ||||
60 | } | ||||
61 | | ||||
62 | QStringList choiceList; | ||||
63 | Q_FOREACH(const KCoreConfigSkeleton::ItemEnum::Choice &c, | ||||
64 | asEnum->choices()) | ||||
65 | { | ||||
66 | if (c.label.isEmpty()) { | ||||
67 | choiceList.append(c.name); | ||||
68 | } else { | ||||
69 | choiceList.append(c.label); | ||||
70 | } | ||||
71 | } | ||||
72 | | ||||
73 | KComboBox *asComboBox = qobject_cast<KComboBox *>(child); | ||||
74 | if (asComboBox) { | ||||
75 | asComboBox->addItems(choiceList); | ||||
76 | } | ||||
77 | } | ||||
78 | } | ||||
79 | | ||||
80 | CustomConfigDialogManager::~CustomConfigDialogManager() | ||||
81 | { | ||||
82 | } | ||||
83 | | ||||
84 | QVariantHash CustomConfigDialogManager::currentWidgetProperties() const | ||||
85 | { | ||||
86 | QVariantHash r; | ||||
87 | for (QMap<QString, QWidget *>::ConstIterator i = m_widgets.begin(); | ||||
88 | i != m_widgets.end(); ++i) | ||||
89 | { | ||||
90 | r[i.key()] = property(i.value()); | ||||
91 | } | ||||
92 | return r; | ||||
93 | } | ||||
94 | | ||||
95 | void CustomConfigDialogManager::setWidgetProperties(const QVariantHash &p) | ||||
96 | { | ||||
97 | for (QVariantHash::ConstIterator i = p.begin(); i != p.end(); ++i) { | ||||
98 | QMap<QString, QWidget *>::ConstIterator j = m_widgets.constFind(i.key()); | ||||
99 | if (j != m_widgets.constEnd()) { | ||||
100 | setProperty(j.value(), i.value()); | ||||
101 | } | ||||
102 | } | ||||
103 | } | ||||
104 | | ||||
105 | static bool variantFuzzyCompare(const QVariant &a, const QVariant &b) | ||||
106 | { | ||||
107 | if (a == b) { | ||||
108 | return true; | ||||
109 | } | ||||
110 | | ||||
111 | bool isDouble_a = false, isDouble_b = false; | ||||
112 | float d_a = static_cast<float>(a.toDouble(&isDouble_a)), | ||||
113 | d_b = static_cast<float>(b.toDouble(&isDouble_b)); | ||||
114 | if (!isDouble_a || !isDouble_b) { | ||||
115 | return false; | ||||
116 | } | ||||
117 | | ||||
118 | return (qFuzzyIsNull(d_a) && qFuzzyIsNull(d_b)) || qFuzzyCompare(d_a, d_b); | ||||
119 | } | ||||
120 | | ||||
121 | QVariant CustomConfigDialogManager::fixup(QWidget *widget, QVariant v) const | ||||
122 | { | ||||
123 | bool isDouble = false; | ||||
124 | double value = v.toDouble(&isDouble); | ||||
125 | if (!isDouble) { | ||||
126 | return v; | ||||
127 | } | ||||
128 | | ||||
129 | QVariant decimals(widget->property("decimals")); | ||||
130 | if (decimals.type() != QVariant::Int) { | ||||
131 | CustomSlider *asSlider = qobject_cast<CustomSlider *>(widget); | ||||
132 | if (asSlider) { | ||||
133 | return asSlider->fixup(value); | ||||
134 | } | ||||
135 | return value; | ||||
136 | } | ||||
137 | | ||||
138 | double k = std::pow(10.0, decimals.toInt()); | ||||
139 | return std::floor(value * k + 0.5) / k; //round | ||||
140 | } | ||||
141 | | ||||
142 | bool CustomConfigDialogManager::compareWidgetProperties(const QVariantHash &p) const | ||||
143 | { | ||||
144 | bool result = true; | ||||
145 | for (QVariantHash::ConstIterator i = p.begin(); i != p.end(); ++i) { | ||||
146 | QMap<QString, QWidget *>::ConstIterator j = m_widgets.find(i.key()); | ||||
147 | if (j == m_widgets.end()) { | ||||
148 | continue; | ||||
149 | } | ||||
150 | | ||||
151 | QWidget *widget = j.value(); | ||||
152 | QVariant widgetValue(fixup(widget, property(widget))); | ||||
153 | QVariant fixed(fixup(widget, i.value())); | ||||
154 | if (!variantFuzzyCompare(widgetValue, fixed)) { | ||||
155 | result = false; | ||||
156 | qDebug() << "Config mismatch:" | ||||
157 | << widget->objectName() << widgetValue << fixed; | ||||
158 | } | ||||
159 | } | ||||
160 | return result; | ||||
161 | } | ||||
162 | | ||||
163 | bool CustomConfigDialogManager::hasChangedFuzzy() const | ||||
164 | { | ||||
165 | for (QMap<QString, QWidget *>::ConstIterator i = m_widgets.begin(); | ||||
166 | i != m_widgets.end(); ++i) | ||||
167 | { | ||||
168 | KConfigSkeletonItem *config = m_config->findItem(i.key()); | ||||
169 | QWidget *widget = i.value(); | ||||
170 | QVariant widgetValue(fixup(widget, property(widget))); | ||||
171 | QVariant configValue(fixup(widget, config->property())); | ||||
172 | if (!variantFuzzyCompare(widgetValue, configValue)) { | ||||
173 | return true; | ||||
174 | } | ||||
175 | } | ||||
176 | return false; | ||||
177 | } |