[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kproperty] /: Make values converted to user-defined units for Double values
From:       Jaroslaw Staniek <staniek () kde ! org>
Date:       2015-12-28 19:22:59
Message-ID: E1aDdNj-0005Nh-AS () scm ! kde ! org
[Download RAW message or body]

Git commit 8742068b72d1a0008a90b01d9aa680caa807e44a by Jaroslaw Staniek.
Committed on 28/12/2015 at 19:16.
Pushed by staniek into branch 'master'.

Make values converted to user-defined units for Double values

Summary:
Make values converted to user-defined units for Double values

Extra:
- handle all combination of (hasUnit) and (locale is C) cases
- round double to ints using QLocale::toString()
- add suffix to the editor spin box based on the "unit" option
- add suitable "Double cm" property to the example app

Test Plan: Try the "Double cm" property in the KPropertyExample

Reviewers: piggz
Differential Revision: https://phabricator.kde.org/D713
Fixes T1235

M  +8    -0    examples/window.cpp
M  +29   -13   src/editors/spinbox.cpp
M  +0    -1    src/editors/spinbox.h

http://commits.kde.org/kproperty/8742068b72d1a0008a90b01d9aa680caa807e44a

diff --git a/examples/window.cpp b/examples/window.cpp
index ffbfd82..b86a0e4 100644
--- a/examples/window.cpp
+++ b/examples/window.cpp
@@ -22,6 +22,7 @@
 
 #include <KProperty>
 #include <KPropertyEditorView>
+#include <KPropertyUnit_p.h>
 
 #include <QDate>
 #include <QDateTime>
@@ -87,6 +88,13 @@ Window::Window()
         m_set.addProperty(p = new KProperty("Double", 3.14159, "Double"), group);
         p->setOption("precision", 4); // will round to 3.1416
     }
+    if (singleProperty.isEmpty() || singleProperty=="cm") {
+        const qreal cm = 1.0; // 28.3465058 points
+        const qreal points = \
KPropertyUnit(KPropertyUnit::Centimeter).fromUserValue(cm); +        \
m_set.addProperty(p = new KProperty("cm", points, "Double cm"), group); +        \
p->setOption("unit", "cm"); +        // default precision == 2
+    }
     if (singleProperty.isEmpty() || singleProperty=="Bool") {
         m_set.addProperty(new KProperty("Bool", QVariant(true), "Bool"), group);
     }
diff --git a/src/editors/spinbox.cpp b/src/editors/spinbox.cpp
index 53c17ae..1de789c 100644
--- a/src/editors/spinbox.cpp
+++ b/src/editors/spinbox.cpp
@@ -176,6 +176,11 @@ KPropertyDoubleSpinBox::KPropertyDoubleSpinBox(const KProperty* \
prop, QWidget *p  if (!minValueText.isEmpty())
         setSpecialValueText(minValueText);
     decodeUnit(*prop, &d->unit, &d->hasUnit);
+    if (d->hasUnit) {
+        setSuffix(
+            QObject::tr("%1 %2", "<value> <unit>") // this adds necessary space
+                    .arg(QString()).arg(d->unit.toString()));
+    }
     connect(this, SIGNAL(valueChanged(double)), this, \
SLOT(slotValueChanged(double)));  }
 
@@ -235,16 +240,22 @@ QString \
KPropertyIntSpinBoxDelegate::propertyValueToString(const KProperty* prop  return \
minValueText;  }
     }
-    if (hasUnit && locale.language() != QLocale::C) {
-        return QObject::tr("%1 %2", "<text> <unit>")
-                .arg(valueToString(prop->value(), locale)).arg(unit.toString());
+    if (!hasUnit) {
+        return valueToString(prop->value(), locale);
+    }
+    if (locale.language() == QLocale::C) {
+        return QString::fromLatin1("%1 %2")
+                                   .arg(valueToString(prop->value(), locale))
+                                   .arg(unit.toString());
     }
-    return valueToString(prop->value(), locale);
+    return QObject::tr("%1 %2", "<value> <unit>")
+                       \
.arg(valueToString(unit.toUserValue(prop->value().toDouble()), locale)) +             \
.arg(unit.toString());  }
 
 QString KPropertyIntSpinBoxDelegate::valueToString(const QVariant& value, const \
QLocale &locale) const  {
-    return locale.toString(value.toInt());
+    return locale.toString(value.toDouble(), 'f', 0);
 }
 
 QWidget* KPropertyIntSpinBoxDelegate::createEditor( int type, QWidget *parent,
@@ -284,16 +295,21 @@ QString \
KPropertyDoubleSpinBoxDelegate::propertyValueToString(const KProperty* p  precision = \
prop->option("precision").toInt();  }
     }
-//! @todo precision?
-//! @todo rounding using KLocale::formatNumber(const QString &numStr, bool round = \
                true,int precision = 2)?
-    if (hasUnit && locale.language() != QLocale::C) {
-        return QObject::tr("%1 %2", "<text> <unit>")
-                .arg(valueToString(prop->value(), locale)).arg(unit.toString());
+    const qreal realValue = hasUnit ? unit.toUserValue(prop->value().toReal()) : \
prop->value().toReal(); +    QString valueString;
+    if (precision >= 0) {
+        valueString = locale.toString(realValue, 'f', precision);
+    } else {
+        valueString = valueToString(realValue, locale);
+    }
+
+    if (!hasUnit) {
+        return valueString;
     }
-    if (prop->value().canConvert(QMetaType::Double) && precision >= 0) {
-        return locale.toString(prop->value().toDouble(), 'f', precision);
+    if (locale.language() == QLocale::C) {
+        return QString::fromLatin1("%1 %2").arg(valueString).arg(unit.toString());
     }
-    return valueToString(prop->value(), locale);
+    return QObject::tr("%1 %2", "<value> \
<unit>").arg(valueString).arg(unit.toString());  }
 
 QString KPropertyDoubleSpinBoxDelegate::valueToString(const QVariant& value, const \
                QLocale &locale) const
diff --git a/src/editors/spinbox.h b/src/editors/spinbox.h
index 7b0688f..f1a5bbe 100644
--- a/src/editors/spinbox.h
+++ b/src/editors/spinbox.h
@@ -68,7 +68,6 @@ class KPROPERTYWIDGETS_EXPORT KPropertyDoubleSpinBox : public \
QDoubleSpinBox  Q_PROPERTY(double value READ value WRITE setValue USER true)
 
 public:
-//! @todo Support setting precision limits, step, etc.
     KPropertyDoubleSpinBox(const KProperty* prop, QWidget *parent, int itemHeight);
     virtual ~KPropertyDoubleSpinBox();
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic