Changeset View
Standalone View
src/editors/spinbox.cpp
1 | /* This file is part of the KDE project | 1 | /* This file is part of the KDE project | ||
---|---|---|---|---|---|
2 | Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr> | 2 | Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr> | ||
3 | Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net> | 3 | Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net> | ||
4 | Copyright (C) 2008-2015 Jarosław Staniek <staniek@kde.org> | 4 | Copyright (C) 2008-2017 Jarosław Staniek <staniek@kde.org> | ||
5 | 5 | | |||
6 | This library is free software; you can redistribute it and/or | 6 | This library is free software; you can redistribute it and/or | ||
7 | modify it under the terms of the GNU Library General Public | 7 | modify it under the terms of the GNU Library General Public | ||
8 | License as published by the Free Software Foundation; either | 8 | License as published by the Free Software Foundation; either | ||
9 | version 2 of the License, or (at your option) any later version. | 9 | version 2 of the License, or (at your option) any later version. | ||
10 | 10 | | |||
11 | This library is distributed in the hope that it will be useful, | 11 | This library is distributed in the hope that it will be useful, | ||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | Library General Public License for more details. | 14 | Library General Public License for more details. | ||
15 | 15 | | |||
16 | You should have received a copy of the GNU Library General Public License | 16 | You should have received a copy of the GNU Library General Public License | ||
17 | along with this library; see the file COPYING.LIB. If not, write to | 17 | along with this library; see the file COPYING.LIB. If not, write to | ||
18 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 18 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
19 | * Boston, MA 02110-1301, USA. | 19 | * Boston, MA 02110-1301, USA. | ||
20 | */ | 20 | */ | ||
21 | 21 | | |||
22 | #include "spinbox.h" | 22 | #include "spinbox.h" | ||
23 | #include "KProperty.h" | 23 | #include "KProperty.h" | ||
24 | #include "KProperty_p.h" | 24 | #include "KProperty_p.h" | ||
25 | #include "KPropertyEditorView.h" | 25 | #include "KPropertyEditorView.h" | ||
26 | #include "KPropertyUnit_p.h" | | |||
27 | #include "KPropertyUtils.h" | 26 | #include "KPropertyUtils.h" | ||
28 | #include "KPropertyUtils_p.h" | 27 | #include "KPropertyUtils_p.h" | ||
29 | #include "KPropertyWidgetsFactory.h" | 28 | #include "KPropertyWidgetsFactory.h" | ||
30 | #include "kproperty_debug.h" | 29 | #include "kproperty_debug.h" | ||
31 | 30 | | |||
32 | #include <QVariant> | 31 | #include <QVariant> | ||
33 | #include <QLineEdit> | 32 | #include <QLineEdit> | ||
34 | #include <QLocale> | 33 | #include <QLocale> | ||
Show All 19 Lines | 49 | return QString::fromLatin1( | |||
54 | "QLineEdit { border-width:0px; } " | 53 | "QLineEdit { border-width:0px; } " | ||
55 | ) | 54 | ) | ||
56 | .arg(itemHeight/2 - 1).arg(itemHeight - itemHeight/2 - 1) | 55 | .arg(itemHeight/2 - 1).arg(itemHeight - itemHeight/2 - 1) | ||
57 | .arg(fontSizeForCSS(font)) | 56 | .arg(fontSizeForCSS(font)) | ||
58 | .arg(QLatin1String((itemHeight/2 <= 9) ? "bottom: 2px;" : "bottom: 0px;")) | 57 | .arg(QLatin1String((itemHeight/2 <= 9) ? "bottom: 2px;" : "bottom: 0px;")) | ||
59 | .arg(QLatin1String(_class)); | 58 | .arg(QLatin1String(_class)); | ||
60 | } | 59 | } | ||
61 | 60 | | |||
61 | namespace { | ||||
62 | | ||||
63 | bool intRangeValue(const KProperty &property, QVariant *min, QVariant *max) | ||||
64 | { | ||||
65 | Q_ASSERT(min); | ||||
66 | Q_ASSERT(max); | ||||
67 | *min = property.option("min", (property.type() == KProperty::UInt) ? 0 : -INT_MAX); | ||||
68 | *max = property.option("max", INT_MAX); | ||||
69 | if (!min->canConvert(QMetaType::Int)) { | ||||
70 | min->clear(); | ||||
71 | } | ||||
72 | if (!max->canConvert(QMetaType::Int)) { | ||||
73 | max->clear(); | ||||
74 | } | ||||
75 | return min->isValid() && max->isValid() && min->toInt() <= max->toInt(); | ||||
76 | } | ||||
77 | | ||||
78 | } // namespace | ||||
79 | | ||||
62 | KPropertyIntSpinBox::KPropertyIntSpinBox(const KProperty* prop, QWidget *parent, int itemHeight) | 80 | KPropertyIntSpinBox::KPropertyIntSpinBox(const KProperty* prop, QWidget *parent, int itemHeight) | ||
63 | : QSpinBox(parent) | 81 | : QSpinBox(parent) | ||
64 | , m_unsigned(prop->type() == KProperty::UInt) | 82 | , m_unsigned(prop->type() == KProperty::UInt) | ||
65 | { | 83 | { | ||
66 | QLineEdit* le = findChild<QLineEdit*>(); | 84 | QLineEdit* le = findChild<QLineEdit*>(); | ||
67 | setContentsMargins(0,0,0,0); | 85 | setContentsMargins(0,0,0,0); | ||
68 | if (le) { | 86 | if (le) { | ||
69 | le->setAlignment(Qt::AlignLeft); | 87 | le->setAlignment(Qt::AlignLeft); | ||
70 | le->setContentsMargins(0,0,0,0); | 88 | le->setContentsMargins(0,0,0,0); | ||
71 | } | 89 | } | ||
72 | setFrame(true); | 90 | setFrame(true); | ||
73 | QString css = cssForSpinBox("QSpinBox", font(), itemHeight); | 91 | QString css = cssForSpinBox("QSpinBox", font(), itemHeight); | ||
74 | KPropertyWidgetsFactory::setTopAndBottomBordersUsingStyleSheet(this, css); | 92 | KPropertyWidgetsFactory::setTopAndBottomBordersUsingStyleSheet(this, css); | ||
75 | setStyleSheet(css); | 93 | setStyleSheet(css); | ||
76 | 94 | | |||
77 | QVariant minVal(prop->option("min", m_unsigned ? 0 : -INT_MAX)); | 95 | QVariant minVal; | ||
78 | QVariant maxVal(prop->option("max", INT_MAX)); | 96 | QVariant maxVal; | ||
79 | setMinimum(minVal.toInt()); | 97 | if (intRangeValue(*prop, &minVal, &maxVal)) { | ||
80 | setMaximum(maxVal.toInt()); | 98 | setRange(minVal.toInt(), maxVal.toInt()); | ||
81 | QString minValueText(prop->option("minValueText").toString()); | 99 | } | ||
82 | if (!minValueText.isEmpty()) | 100 | const KPropertyUtilsPrivate::ValueOptionsHandler options(*prop); | ||
83 | setSpecialValueText(minValueText); | 101 | if (!options.minValueText.isEmpty()) { | ||
102 | setSpecialValueText(options.minValueText); | ||||
103 | } | ||||
104 | if (!options.prefix.isEmpty()) { | ||||
105 | setPrefix(options.prefix + QLatin1Char(' ')); | ||||
106 | } | ||||
107 | if (!options.suffix.isEmpty()) { | ||||
108 | setSuffix(QLatin1Char(' ') + options.suffix); | ||||
109 | } | ||||
84 | connect(this, SIGNAL(valueChanged(int)), this, SLOT(slotValueChanged(int))); | 110 | connect(this, SIGNAL(valueChanged(int)), this, SLOT(slotValueChanged(int))); | ||
85 | } | 111 | } | ||
86 | 112 | | |||
87 | KPropertyIntSpinBox::~KPropertyIntSpinBox() | 113 | KPropertyIntSpinBox::~KPropertyIntSpinBox() | ||
88 | { | 114 | { | ||
89 | } | 115 | } | ||
90 | 116 | | |||
91 | QVariant KPropertyIntSpinBox::value() const | 117 | QVariant KPropertyIntSpinBox::value() const | ||
Show All 19 Lines | 135 | { | |||
111 | emit commitData(this); | 137 | emit commitData(this); | ||
112 | } | 138 | } | ||
113 | 139 | | |||
114 | //----------------------- | 140 | //----------------------- | ||
115 | 141 | | |||
116 | class Q_DECL_HIDDEN KPropertyDoubleSpinBox::Private | 142 | class Q_DECL_HIDDEN KPropertyDoubleSpinBox::Private | ||
117 | { | 143 | { | ||
118 | public: | 144 | public: | ||
119 | KPropertyUnit unit; | 145 | bool dummy = false; | ||
120 | bool hasUnit; | | |||
121 | }; | 146 | }; | ||
122 | 147 | | |||
123 | namespace { | 148 | namespace { | ||
124 | 149 | | |||
125 | void decodeUnit(const KProperty &property, KPropertyUnit *unit, bool *hasUnit) | 150 | bool doubleRangeValue(const KProperty &property, QVariant *min, QVariant *max) | ||
126 | { | 151 | { | ||
127 | const QString unitString = property.option("unit").toString(); | 152 | Q_ASSERT(min); | ||
128 | if (unitString.isEmpty()) { | 153 | Q_ASSERT(max); | ||
129 | *hasUnit = false; | 154 | *min = property.option("min", 0.0); | ||
155 | *max = property.option("max", DBL_MAX / 100.0); | ||||
piggz: why / 100? | |||||
Checked for you - comes from
BUT it's still present today. Test: enter DBL_MAX i.e. 1.79769e+308 as value eg. in window.cpp: m_set.addProperty(p = new KProperty("Double", DBL_MAX, "Double"), group) Then you get this value in the GUI: 1797693134862315633301262629765050630866728936377003738459752501701914801454763796210449641241516314779480883561305033792001456611676070178718250902275612892141556670084334558762242165238180420033094130920939320073544954448635079731752693526288593899415905695239791325170347763968341437224060469123940876288.0000 which is (try to paster in a python shell): 1.7976931348623156e+306 which is a bit more than DBL_MAX/100.0 == 1.79769e+306. I'll add this explanation to the code... ;) staniek: Checked for you - comes from
> 85447da427b41 2006-02-21 - Qt 4 issue probably :)
> - fix… | |||||
156 | if (!min->canConvert(QMetaType::Double)) { | ||||
157 | min->clear(); | ||||
130 | } | 158 | } | ||
131 | else { | 159 | if (!max->canConvert(QMetaType::Double)) { | ||
132 | *unit = KPropertyUnit::fromSymbol(unitString, hasUnit); | 160 | max->clear(); | ||
133 | } | 161 | } | ||
162 | return min->isValid() && max->isValid() && min->toDouble() <= max->toDouble(); | ||||
134 | } | 163 | } | ||
135 | 164 | | |||
136 | //! Helper for testing property options | 165 | QVariant precisionValue(const KProperty &property) | ||
137 | struct DoubleValueOptions { | | |||
138 | explicit DoubleValueOptions(const KProperty &property) | | |||
139 | { | | |||
140 | minVal = property.option("min", 0.0); | | |||
141 | maxVal = property.option("max", DBL_MAX / 100.0); | | |||
142 | if (!minVal.canConvert(QMetaType::Double) || !maxVal.canConvert(QMetaType::Double) | | |||
143 | || minVal.toReal() > maxVal.toReal()) | | |||
144 | { | 166 | { | ||
145 | minVal.clear(); | 167 | QVariant result = property.option("precision", KPROPERTY_DEFAULT_DOUBLE_VALUE_PRECISION); | ||
146 | maxVal.clear(); | 168 | if (result.canConvert(QMetaType::Int) && result.toInt() >= 0) { | ||
147 | } | 169 | return result; | ||
148 | step = property.option("step", KPROPERTY_DEFAULT_DOUBLE_VALUE_STEP); | | |||
149 | if (!step.canConvert(QMetaType::Double) || step.toDouble() <= 0.0) { | | |||
150 | step.clear(); | | |||
151 | } | 170 | } | ||
152 | precision = property.option("precision", KPROPERTY_DEFAULT_DOUBLE_VALUE_PRECISION); | 171 | return QVariant(); | ||
153 | if (!precision.canConvert(QMetaType::Int) || precision.toInt() < 0) { | | |||
154 | precision.clear(); | | |||
155 | } | 172 | } | ||
156 | minValueText = property.option("minValueText").toString(); | 173 | | ||
157 | } | | |||
158 | QVariant minVal; | | |||
159 | QVariant maxVal; | | |||
160 | QVariant step; | | |||
161 | QVariant precision; | | |||
162 | QString minValueText; | | |||
163 | }; | | |||
164 | } // namespace | 174 | } // namespace | ||
165 | 175 | | |||
166 | KPropertyDoubleSpinBox::KPropertyDoubleSpinBox(const KProperty* prop, QWidget *parent, int itemHeight) | 176 | KPropertyDoubleSpinBox::KPropertyDoubleSpinBox(const KProperty* prop, QWidget *parent, int itemHeight) | ||
167 | : QDoubleSpinBox(parent) | 177 | : QDoubleSpinBox(parent) | ||
168 | , d(new Private) | 178 | , d(new Private) | ||
169 | { | 179 | { | ||
170 | setFrame(false); | 180 | setFrame(false); | ||
171 | QLineEdit* le = findChild<QLineEdit*>(); | 181 | QLineEdit* le = findChild<QLineEdit*>(); | ||
172 | if (le) { | 182 | if (le) { | ||
173 | le->setAlignment(Qt::AlignLeft); | 183 | le->setAlignment(Qt::AlignLeft); | ||
174 | le->setContentsMargins(0,0,0,0); | 184 | le->setContentsMargins(0,0,0,0); | ||
175 | le->setFrame(false); | 185 | le->setFrame(false); | ||
176 | } | 186 | } | ||
177 | /* KPropertyFactory::setTopAndBottomBordersUsingStyleSheet(sb, | 187 | /* KPropertyFactory::setTopAndBottomBordersUsingStyleSheet(sb, | ||
178 | QString::fromLatin1( | 188 | QString::fromLatin1( | ||
179 | "QDoubleSpinBox { border-left: 0; border-right: 0; } " | 189 | "QDoubleSpinBox { border-left: 0; border-right: 0; } " | ||
180 | "QDoubleSpinBox::down-button { height: %1px; } " | 190 | "QDoubleSpinBox::down-button { height: %1px; } " | ||
181 | "QDoubleSpinBox::up-button { height: %2px; }" | 191 | "QDoubleSpinBox::up-button { height: %2px; }" | ||
182 | ).arg(itemHeight/2).arg(itemHeight - itemHeight/2) | 192 | ).arg(itemHeight/2).arg(itemHeight - itemHeight/2) | ||
183 | );*/ | 193 | );*/ | ||
184 | QString css = cssForSpinBox("QDoubleSpinBox", font(), itemHeight); | 194 | QString css = cssForSpinBox("QDoubleSpinBox", font(), itemHeight); | ||
185 | KPropertyWidgetsFactory::setTopAndBottomBordersUsingStyleSheet(this, css); | 195 | KPropertyWidgetsFactory::setTopAndBottomBordersUsingStyleSheet(this, css); | ||
186 | setStyleSheet(css); | 196 | setStyleSheet(css); | ||
187 | 197 | | |||
188 | const DoubleValueOptions options(*prop); | 198 | QVariant minVal; | ||
189 | if (options.minVal.isValid() && options.maxVal.isValid()) { | 199 | QVariant maxVal; | ||
190 | setRange(options.minVal.toDouble(), options.maxVal.toDouble()); | 200 | if (doubleRangeValue(*prop, &minVal, &maxVal)) { | ||
201 | setRange(minVal.toDouble(), maxVal.toDouble()); | ||||
191 | } | 202 | } | ||
192 | if (options.step.isValid()) { | 203 | QVariant step = prop->option("step", KPROPERTY_DEFAULT_DOUBLE_VALUE_STEP); | ||
193 | setSingleStep(options.step.toDouble()); | 204 | if (step.canConvert(QMetaType::Double) && step.toDouble() > 0.0) { | ||
205 | setSingleStep(step.toDouble()); | ||||
206 | } | ||||
207 | const QVariant precision = precisionValue(*prop); | ||||
208 | if (precision.isValid()) { | ||||
209 | setDecimals(precision.toInt()); | ||||
194 | } | 210 | } | ||
195 | //! @todo implement slider | 211 | //! @todo implement slider | ||
196 | // bool slider = prop->option("slider", false).toBool(); | 212 | // bool slider = prop->option("slider", false).toBool(); | ||
197 | if (options.precision.isValid()) { | 213 | const KPropertyUtilsPrivate::ValueOptionsHandler options(*prop); | ||
198 | setDecimals(options.precision.toInt()); | | |||
199 | } | | |||
200 | if (!options.minValueText.isEmpty()) { | 214 | if (!options.minValueText.isEmpty()) { | ||
201 | setSpecialValueText(options.minValueText); | 215 | setSpecialValueText(options.minValueText); | ||
202 | } | 216 | } | ||
203 | decodeUnit(*prop, &d->unit, &d->hasUnit); | 217 | if (!options.prefix.isEmpty()) { | ||
204 | if (d->hasUnit) { | 218 | setPrefix(options.prefix + QLatin1Char(' ')); | ||
205 | setSuffix( | 219 | } | ||
206 | QObject::tr("%1 %2", "<value> <unit>") // this adds necessary space | 220 | if (!options.suffix.isEmpty()) { | ||
207 | .arg(QString()).arg(d->unit.toString())); | 221 | setSuffix(QLatin1Char(' ') + options.suffix); | ||
208 | } | 222 | } | ||
209 | connect(this, SIGNAL(valueChanged(double)), this, SLOT(slotValueChanged(double))); | 223 | connect(this, SIGNAL(valueChanged(double)), this, SLOT(slotValueChanged(double))); | ||
210 | } | 224 | } | ||
211 | 225 | | |||
212 | KPropertyDoubleSpinBox::~KPropertyDoubleSpinBox() | 226 | KPropertyDoubleSpinBox::~KPropertyDoubleSpinBox() | ||
213 | { | 227 | { | ||
214 | delete d; | 228 | delete d; | ||
215 | } | 229 | } | ||
216 | 230 | | |||
217 | void KPropertyDoubleSpinBox::resizeEvent( QResizeEvent * event ) | 231 | void KPropertyDoubleSpinBox::resizeEvent( QResizeEvent * event ) | ||
218 | { | 232 | { | ||
219 | setFixedHeight(height() + 1); | 233 | setFixedHeight(height() + 1); | ||
220 | QDoubleSpinBox::resizeEvent(event); | 234 | QDoubleSpinBox::resizeEvent(event); | ||
221 | } | 235 | } | ||
222 | 236 | | |||
223 | void KPropertyDoubleSpinBox::setValue(double v) | | |||
224 | { | | |||
225 | if (d->hasUnit) { | | |||
226 | QDoubleSpinBox::setValue(d->unit.toUserValue(v)); | | |||
227 | return; | | |||
228 | } | | |||
229 | QDoubleSpinBox::setValue(v); | | |||
230 | } | | |||
231 | | ||||
232 | double KPropertyDoubleSpinBox::value() const | | |||
233 | { | | |||
234 | if (d->hasUnit) { | | |||
235 | return d->unit.fromUserValue(QDoubleSpinBox::value()); | | |||
236 | } | | |||
237 | return QDoubleSpinBox::value(); | | |||
238 | } | | |||
239 | | ||||
240 | void KPropertyDoubleSpinBox::slotValueChanged(double value) | 237 | void KPropertyDoubleSpinBox::slotValueChanged(double value) | ||
241 | { | 238 | { | ||
242 | Q_UNUSED(value); | 239 | Q_UNUSED(value); | ||
243 | emit commitData(this); | 240 | emit commitData(this); | ||
244 | } | 241 | } | ||
245 | 242 | | |||
246 | //----------------------- | 243 | //----------------------- | ||
247 | 244 | | |||
248 | KPropertyIntSpinBoxDelegate::KPropertyIntSpinBoxDelegate() | 245 | KPropertyIntSpinBoxDelegate::KPropertyIntSpinBoxDelegate() | ||
249 | { | 246 | { | ||
250 | } | 247 | } | ||
251 | 248 | | |||
252 | QString KPropertyIntSpinBoxDelegate::propertyValueToString(const KProperty* prop, | 249 | QString KPropertyIntSpinBoxDelegate::propertyValueToString(const KProperty* prop, | ||
253 | const QLocale &locale) const | 250 | const QLocale &locale) const | ||
254 | { | 251 | { | ||
255 | KPropertyUnit unit; | | |||
256 | bool hasUnit; | | |||
257 | decodeUnit(*prop, &unit, &hasUnit); | | |||
258 | if (prop->hasOptions()) { | | |||
259 | //replace min value with minValueText if defined | 252 | //replace min value with minValueText if defined | ||
260 | QVariant minValue(prop->option("min")); | 253 | QVariant minVal; | ||
261 | QString minValueText(prop->option("minValueText").toString()); | 254 | QVariant maxVal; | ||
262 | if (!minValue.isNull() && !minValueText.isEmpty() | 255 | const KPropertyUtilsPrivate::ValueOptionsHandler options(*prop); | ||
263 | && minValue.toInt() == prop->value().toInt()) | 256 | (void)intRangeValue(*prop, &minVal, &maxVal); | ||
264 | { | 257 | if (minVal.isValid() && minVal.toInt() == prop->value().toInt()) { | ||
265 | return minValueText; | 258 | return options.minValueText; | ||
266 | } | | |||
267 | } | | |||
268 | if (!hasUnit) { | | |||
269 | return valueToString(prop->value(), locale); | | |||
270 | } | | |||
271 | if (locale.language() == QLocale::C) { | | |||
272 | return QString::fromLatin1("%1 %2") | | |||
273 | .arg(valueToString(prop->value(), locale)) | | |||
274 | .arg(unit.toString()); | | |||
275 | } | 259 | } | ||
276 | return QObject::tr("%1 %2", "<value> <unit>") | 260 | return options.valueWithPrefixAndSuffix(valueToString(prop->value(), locale), locale); | ||
277 | .arg(valueToString(unit.toUserValue(prop->value().toDouble()), locale)) | | |||
278 | .arg(unit.toString()); | | |||
279 | } | 261 | } | ||
280 | 262 | | |||
281 | QString KPropertyIntSpinBoxDelegate::valueToString(const QVariant& value, const QLocale &locale) const | 263 | QString KPropertyIntSpinBoxDelegate::valueToString(const QVariant& value, const QLocale &locale) const | ||
282 | { | 264 | { | ||
283 | return locale.toString(value.toDouble(), 'f', 0); | 265 | return locale.toString(value.toReal(), 'f', 0); | ||
284 | } | 266 | } | ||
285 | 267 | | |||
286 | QWidget* KPropertyIntSpinBoxDelegate::createEditor( int type, QWidget *parent, | 268 | QWidget* KPropertyIntSpinBoxDelegate::createEditor( int type, QWidget *parent, | ||
287 | const QStyleOptionViewItem & option, const QModelIndex & index ) const | 269 | const QStyleOptionViewItem & option, const QModelIndex & index ) const | ||
288 | { | 270 | { | ||
289 | Q_UNUSED(type); | 271 | Q_UNUSED(type); | ||
290 | 272 | | |||
291 | KProperty *prop = KPropertyUtils::propertyForIndex(index); | 273 | KProperty *prop = KPropertyUtils::propertyForIndex(index); | ||
292 | if (!prop) { | 274 | if (!prop) { | ||
293 | return 0; | 275 | return 0; | ||
294 | } | 276 | } | ||
295 | return new KPropertyIntSpinBox(prop, parent, option.rect.height() - 2); | 277 | return new KPropertyIntSpinBox(prop, parent, option.rect.height() - 2); | ||
296 | } | 278 | } | ||
297 | 279 | | |||
298 | //----------------------- | 280 | //----------------------- | ||
299 | 281 | | |||
300 | KPropertyDoubleSpinBoxDelegate::KPropertyDoubleSpinBoxDelegate() | 282 | KPropertyDoubleSpinBoxDelegate::KPropertyDoubleSpinBoxDelegate() | ||
301 | { | 283 | { | ||
302 | } | 284 | } | ||
303 | 285 | | |||
304 | QString KPropertyDoubleSpinBoxDelegate::propertyValueToString(const KProperty* prop, | 286 | QString KPropertyDoubleSpinBoxDelegate::propertyValueToString(const KProperty* prop, | ||
305 | const QLocale &locale) const | 287 | const QLocale &locale) const | ||
306 | { | 288 | { | ||
307 | const DoubleValueOptions options(*prop); | | |||
308 | //replace min value with minValueText if defined | 289 | //replace min value with minValueText if defined | ||
309 | if (options.minVal.isValid() && !options.minValueText.isEmpty() | 290 | QVariant minVal; | ||
310 | && options.minVal.toDouble() == prop->value().toDouble()) | 291 | QVariant maxVal; | ||
311 | { | 292 | const KPropertyUtilsPrivate::ValueOptionsHandler options(*prop); | ||
293 | (void)doubleRangeValue(*prop, &minVal, &maxVal); | ||||
294 | if (minVal.isValid() && minVal.toDouble() == prop->value().toDouble()) { | ||||
312 | return options.minValueText; | 295 | return options.minValueText; | ||
313 | } | 296 | } | ||
314 | KPropertyUnit unit; | | |||
315 | bool hasUnit; | | |||
316 | decodeUnit(*prop, &unit, &hasUnit); | | |||
317 | const qreal realValue = hasUnit ? unit.toUserValue(prop->value().toReal()) : prop->value().toReal(); | | |||
318 | QString valueString; | 297 | QString valueString; | ||
319 | if (options.precision.isValid()) { | 298 | const QVariant precision = precisionValue(*prop); | ||
320 | valueString = locale.toString(realValue, 'f', options.precision.toInt()); | 299 | if (precision.isValid()) { | ||
300 | valueString = locale.toString(prop->value().toReal(), 'f', precision.toInt()); | ||||
321 | } else { | 301 | } else { | ||
322 | valueString = valueToString(realValue, locale); | 302 | valueString = valueToString(prop->value().toReal(), locale); | ||
323 | } | | |||
324 | | ||||
325 | if (!hasUnit) { | | |||
326 | return valueString; | | |||
327 | } | | |||
328 | if (locale.language() == QLocale::C) { | | |||
329 | return QString::fromLatin1("%1 %2").arg(valueString).arg(unit.toString()); | | |||
330 | } | 303 | } | ||
331 | return QObject::tr("%1 %2", "<value> <unit>").arg(valueString).arg(unit.toString()); | 304 | return options.valueWithPrefixAndSuffix(valueString, locale); | ||
332 | } | 305 | } | ||
333 | 306 | | |||
334 | QString KPropertyDoubleSpinBoxDelegate::valueToString(const QVariant& value, const QLocale &locale) const | 307 | QString KPropertyDoubleSpinBoxDelegate::valueToString(const QVariant& value, const QLocale &locale) const | ||
335 | { | 308 | { | ||
336 | return locale.toString(value.toDouble()); | 309 | return locale.toString(value.toReal()); | ||
337 | } | 310 | } | ||
338 | 311 | | |||
339 | QWidget* KPropertyDoubleSpinBoxDelegate::createEditor( int type, QWidget *parent, | 312 | QWidget* KPropertyDoubleSpinBoxDelegate::createEditor( int type, QWidget *parent, | ||
340 | const QStyleOptionViewItem & option, const QModelIndex & index ) const | 313 | const QStyleOptionViewItem & option, const QModelIndex & index ) const | ||
341 | { | 314 | { | ||
342 | Q_UNUSED(type); | 315 | Q_UNUSED(type); | ||
343 | 316 | | |||
344 | KProperty *prop = KPropertyUtils::propertyForIndex(index); | 317 | KProperty *prop = KPropertyUtils::propertyForIndex(index); | ||
345 | if (!prop) { | 318 | if (!prop) { | ||
346 | return 0; | 319 | return 0; | ||
347 | } | 320 | } | ||
348 | return new KPropertyDoubleSpinBox(prop, parent, option.rect.height() - 2 - 1); | 321 | return new KPropertyDoubleSpinBox(prop, parent, option.rect.height() - 2 - 1); | ||
349 | } | 322 | } |
why / 100?