diff --git a/autotests/KPropertyTest.cpp b/autotests/KPropertyTest.cpp --- a/autotests/KPropertyTest.cpp +++ b/autotests/KPropertyTest.cpp @@ -186,20 +186,6 @@ KPropertyFactory::addDisplay(int, KPropertyValueDisplayInterface*) KPropertyFactory::composedPropertyCreators() const KPropertyFactory::valueDisplays() const -KPropertyUnit::convertFromUnitToUnit(double, KPropertyUnit const&, KPropertyUnit const&, double) -KPropertyUnit::fromListForUi(int, QFlags, double) -KPropertyUnit::fromSymbol(QString const&, bool*) -KPropertyUnit::fromUserValue(QString const&, bool*) const -KPropertyUnit::fromUserValue(double) const -KPropertyUnit::indexInListForUi(QFlags) const -KPropertyUnit::listOfUnitNameForUi(QFlags) -KPropertyUnit::parseAngle(QString const&, double) -KPropertyUnit::parseValue(QString const&, double) -KPropertyUnit::ptToUnit(double, KPropertyUnit const&) -KPropertyUnit::symbol() const -KPropertyUnit::toUserStringValue(double) const -KPropertyUnit::toUserValue(double) const -KPropertyUnit::unitDescription(KPropertyUnit::Type) KPropertyUtils::keyForEnumValue(char const*, int) KPropertyValueDisplayInterface::~KPropertyValueDisplayInterface() KPropertyValueDisplayInterface::KPropertyValueDisplayInterface() diff --git a/examples/window.cpp b/examples/window.cpp --- a/examples/window.cpp +++ b/examples/window.cpp @@ -22,7 +22,6 @@ #include #include -#include #include #include @@ -76,8 +75,8 @@ QByteArray group; const bool addGroups = !m_parser.isSet(m_flatOption); if (addGroups) { - group = "SimpleGroup"; - m_set.setGroupCaption(group, "Simple Group"); + group = "BasicGroup"; + m_set.setGroupCaption(group, "Basic Properties"); } if (singleProperty.isEmpty() || singleProperty=="String") { m_set.addProperty(p = new KProperty("String", "String"), group); @@ -99,13 +98,6 @@ 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::Type::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); } @@ -148,31 +140,70 @@ // Complex if (addGroups) { - group = "ComplexGroup"; - m_set.setGroupCaption(group, "Complex Group"); - } - if (singleProperty.isEmpty() || singleProperty=="Rect") { - m_set.addProperty(new KProperty("Rect", QRect(5,11,100,200), "Rect"), group); + group = "ComposedGroup"; + m_set.setGroupCaption(group, "Composed Properties"); } if (singleProperty.isEmpty() || singleProperty=="Point") { m_set.addProperty(new KProperty("Point", QPoint(3, 4), "Point"), group); } if (singleProperty.isEmpty() || singleProperty=="Size") { m_set.addProperty(new KProperty("Size", QSize(10, 20), "Size"), group); } - if (singleProperty.isEmpty() || singleProperty=="RectF") { - m_set.addProperty(new KProperty("RectF", QRectF(0.1, 0.5, 10.72, 18.21), "RectF"), group); + if (singleProperty.isEmpty() || singleProperty=="Rect") { + m_set.addProperty(new KProperty("Rect", QRect(5,11,100,200), "Rect"), group); } if (singleProperty.isEmpty() || singleProperty=="PointF") { m_set.addProperty(new KProperty("PointF", QPointF(3.14, 4.15), "PointF"), group); } if (singleProperty.isEmpty() || singleProperty=="SizeF") { m_set.addProperty(new KProperty("SizeF", QSizeF(1.1, 2.45), "SizeF"), group); } + if (singleProperty.isEmpty() || singleProperty == "RectF") { + m_set.addProperty( + new KProperty("RectF", QRectF(0.1, 0.5, 10.72, 18.21), "RectF"), group); + } + +// With suffixes and prefixes + if (addGroups) { + group = "PrefixesSuffixesGroup"; + m_set.setGroupCaption(group, "Prefixes & Suffixes"); + } + if (singleProperty.isEmpty() || singleProperty=="dollars") { + m_set.addProperty(p = new KProperty("dollars", 100, "Dollars"), group); + p->setOption("prefix", "$"); + } + if (singleProperty.isEmpty() || singleProperty == "billions") { + m_set.addProperty(p = new KProperty("billions", 5.0, "Billions"), group); + p->setOption("prefix", "£"); + p->setOption("suffix", "bn"); + // default precision == 2 and step == 0.01 + } + if (singleProperty.isEmpty() || singleProperty == "PointF-mm") { + m_set.addProperty( + p = new KProperty("PointF-mm", QPointF(2.5, 3.5), "PointF [mm]"), group); + p->setOption("suffix", "mm"); + p->setOption("step", 0.1); + p->setOption("precision", 2); + } + if (singleProperty.isEmpty() || singleProperty == "SizeF-dm") { + m_set.addProperty( + p = new KProperty("SizeF-dm", QSizeF(7.0, 6.5), "SizeF [dm]"), group); + p->setOption("suffix", "dm"); + p->setOption("step", 0.001); + p->setOption("precision", 3); + } + if (singleProperty.isEmpty() || singleProperty=="RectF-px") { + m_set.addProperty( + p = new KProperty("RectF-px", QRectF(21.2, 22.2, 9.1, 1.0), "RectF [px]"), + group); + p->setOption("suffix", "px"); + p->setOption("step", 0.1); + p->setOption("precision", 1); + } // Appearance if (addGroups) { - group = "Appearance Group"; + group = "AppearanceGroup"; m_set.setGroupCaption(group, "Appearance Group"); m_set.setGroupIconName(group, "appearance"); } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,7 +4,6 @@ set(kpropertycore_LIB_SRCS KProperty.cpp KPropertySet.cpp - KPropertyUnit_p.cpp KPropertyFactory.cpp KPropertyCoreUtils.cpp kproperty_debug.cpp diff --git a/src/KProperty.h b/src/KProperty.h --- a/src/KProperty.h +++ b/src/KProperty.h @@ -408,12 +408,23 @@ This is most visible in property editor widget. Currently supported options are: -
  • min: integer value describing minimum value for properties of integer and - double types. The default is 0.
  • -
  • minValueText: user-visible translated string to be displayed in editor for integer - type when minimum is set for the property. +
      +
    • min: value describing minimum value for properties of integer, double, + date, date/time and time types. Default is 0 for double and unsigned integer types, + -INT_MAX for signed integer type. Defaults for date, date/time and time types are + specified in documentation of QDateEdit::minimumDate, + QDateTimeEdit::minimumDateTime and QTime::minimumTime, respectively. + The value is ignored if it is larger than the value of "max" option.
    • +
    • minValueText: user-visible translated string to be displayed in editor for integer, + double, date, date/time and time types when the value is equal to the value of + "min" option. @see QAbstractSpinBox::specialValueText
    • -
    • max: integer describing minimum value for properties of integer type. Default is 0xffff.
    • +
    • max: value describing minimum value for properties of integer type. + Default is 1.79769e+308 DBL_MAX / 100.0 i.e. 1.79769e+308 for double type, + INT_MAX for integer type. Defaults for date, date/time and time types are + specified in documentation of QDateEdit::maximumDate, + QDateTimeEdit::maximumDateTime and QTime::maximumTime, respectively. + The value is ignored if it is smaller than the value of "min" option.
    • precision: integer value >= 0 describing the number of decimals after the decimal point for double type. Default value is 2. @see QDoubleSpinBox::decimals
    • @@ -451,6 +462,14 @@
    • multiLine: boolean value used for string type. If @c true, a multi-line QPlainTextEdit-based widget is used for editor; otherwise a single-line QLineEdit widget is used. @c false by default. Added in version 3.1.
    • +
    • prefix: string to display before the value, e.g. '$'. Supported for double and integer + types and composed types based on double and integer types (Point*, Size*, Rect*). + @see QDoubleSpinBox::prefix QSpinBox::prefix
    • +
    • suffix: string to display after the value, e.g. unit such as 'mm'. + Supported for double and integer types and composed types based on double and + integer types (Point*, Size*, Rect*). Note that only display is affected, value + is not converted to any unit. + @see QDoubleSpinBox::suffix QSpinBox::suffix
    */ void setOption(const char* name, const QVariant& val); diff --git a/src/KPropertyUnit_p.h b/src/KPropertyUnit_p.h deleted file mode 100644 --- a/src/KPropertyUnit_p.h +++ /dev/null @@ -1,258 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 1998, 1999 Reginald Stadlbauer - Copyright (C) 1998, 1999 Torben Weis - Copyright (C) 2004, Nicolas GOUTTE - Copyright (C) 2010 Thomas Zander - Copyright 2012 Friedrich W. H. Kossebau - - This library 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 of the License, or (at your option) any later version. - - This library 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 library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. -*/ - -#ifndef KPROPERTYUNIT_H -#define KPROPERTYUNIT_H - -#include "kpropertycore_export.h" - -#include -#include -#include -#include - -#include // for floor - -// 1 inch ^= 72 pt -// 1 inch ^= 25.399956 mm (-pedantic ;p) -// 1 pt = 1/12 pi -// 1 pt ^= 0.0077880997 cc -// 1 cc = 12 dd -// Note: I don't use division but multiplication with the inverse value -// because it's faster ;p (Werner) -#define POINT_TO_MM(px) qreal((px)*0.352777167) -#define MM_TO_POINT(mm) qreal((mm)*2.83465058) -#define POINT_TO_CM(px) qreal((px)*0.0352777167) -#define CM_TO_POINT(cm) qreal((cm)*28.3465058) -#define POINT_TO_DM(px) qreal((px)*0.00352777167) -#define DM_TO_POINT(dm) qreal((dm)*283.465058) -#define POINT_TO_INCH(px) qreal((px)*0.01388888888889) -#define INCH_TO_POINT(inch) qreal((inch)*72.0) -#define MM_TO_INCH(mm) qreal((mm)*0.039370147) -#define INCH_TO_MM(inch) qreal((inch)*25.399956) -#define POINT_TO_PI(px) qreal((px)*0.083333333) -#define POINT_TO_CC(px) qreal((px)*0.077880997) -#define PI_TO_POINT(pi) qreal((pi)*12) -#define CC_TO_POINT(cc) qreal((cc)*12.840103) -/** - * %KProperty stores everything in pt (using "qreal") internally. - * When displaying a value to the user, the value is converted to the user's unit - * of choice, and rounded to a reasonable precision to avoid 0.999999 - * - * For implementing the selection of a unit type in the UI use the *ForUi() methods. - * They ensure the same order of the unit types in all places, with the order not - * bound to the order in the enum (so ABI-compatible extension is possible) and - * with the order and scope of listed types controlled by the @c ListOptions parameter. - */ -class KPROPERTYCORE_EXPORT KPropertyUnit -{ -public: - /** Length units supported by %KProperty. */ - enum class Type { - Millimeter = 0, - Point, //!< Postscript point, 1/72th of an Inco - Inch, - Centimeter, - Decimeter, - Pica, - Cicero, - Pixel, - TypeCount //!< @internal - }; - - //! Used to control the scope of the unit types listed in the UI - enum class ListOption { - All = 0, - HidePixel = 1 - }; - Q_DECLARE_FLAGS(ListOptions, ListOption) - - /** Returns a KPropertyUnit instance with the type at the @p index of the UI list with the given @p listOptions. */ - static KPropertyUnit fromListForUi(int index, ListOptions listOptions = ListOption::All, qreal factor = 1.0); - - //! Convert a unit symbol string into a KPropertyUnit - //! @param symbol symbol to convert - //! @param ok if set, it will be true if the unit was known, false if unknown - static KPropertyUnit fromSymbol(const QString &symbol, bool *ok = 0); - - /** Construction requires initialization. The factor is for variable factor units like pixel */ - explicit KPropertyUnit(Type unit = Type::Point, qreal factor = 1.0) { - m_type = unit; - m_pixelConversion = factor; - } - - KPropertyUnit& operator=(Type unit) { - m_type = unit; m_pixelConversion = 1.0; return *this; - } - - bool operator!=(const KPropertyUnit &other) const { - return !operator==(other); - } - - bool operator==(const KPropertyUnit &other) const { - return m_type == other.m_type && - (m_type != Type::Pixel || - qFuzzyCompare(m_pixelConversion, other.m_pixelConversion)); - } - - KPropertyUnit::Type type() const { - return m_type; - } - - void setFactor(qreal factor) { - m_pixelConversion = factor; - } - /** - * Prepare ptValue to be displayed in pt - * This method will round to 0.001 precision - */ - static qreal toPoint(qreal ptValue) { - // No conversion, only rounding (to 0.001 precision) - return floor(ptValue * 1000.0) / 1000.0; - } - - /** - * Prepare ptValue to be displayed in mm - * This method will round to 0.0001 precision, use POINT_TO_MM() for lossless conversion. - */ - static qreal toMillimeter(qreal ptValue) { - // "mm" values are rounded to 0.0001 millimeters - return floor(POINT_TO_MM(ptValue) * 10000.0) / 10000.0; - } - - /** - * Prepare ptValue to be displayed in cm - * This method will round to 0.0001 precision, use POINT_TO_CM() for lossless conversion. - */ - static qreal toCentimeter(qreal ptValue) { - return floor(POINT_TO_CM(ptValue) * 10000.0) / 10000.0; - } - - /** - * Prepare ptValue to be displayed in dm - * This method will round to 0.0001 precision, use POINT_TO_DM() for lossless conversion. - */ - static qreal toDecimeter(qreal ptValue) { - return floor(POINT_TO_DM(ptValue) * 10000.0) / 10000.0; - } - - /** - * Prepare ptValue to be displayed in inch - * This method will round to 0.00001 precision, use POINT_TO_INCH() for lossless conversion. - */ - static qreal toInch(qreal ptValue) { - // "in" values are rounded to 0.00001 inches - return floor(POINT_TO_INCH(ptValue) * 100000.0) / 100000.0; - } - - /** - * Prepare ptValue to be displayed in pica - * This method will round to 0.00001 precision, use POINT_TO_PI() for lossless conversion. - */ - static qreal toPica(qreal ptValue) { - // "pi" values are rounded to 0.00001 inches - return floor(POINT_TO_PI(ptValue) * 100000.0) / 100000.0; - } - - /** - * Prepare ptValue to be displayed in cicero - * This method will round to 0.00001 precision, use POINT_TO_CC() for lossless conversion. - */ - static qreal toCicero(qreal ptValue) { - // "cc" values are rounded to 0.00001 inches - return floor(POINT_TO_CC(ptValue) * 100000.0) / 100000.0; - } - - /** - * convert the given value directly from one unit to another - */ - static qreal convertFromUnitToUnit(qreal value, const KPropertyUnit &fromUnit, const KPropertyUnit &toUnit, qreal factor = 1.0); - - /** - * This method is the one to use to display a value in a dialog - * \return the value @p ptValue converted to unit and rounded, ready to be displayed - */ - qreal toUserValue(qreal ptValue) const; - - /** - * Convert the value @p ptValue to a given unit @p unit - * Unlike KPropertyUnit::ptToUnit the return value remains unrounded, so that it can be used in complex calculation - * \return the converted value - */ - static qreal ptToUnit(qreal ptValue, const KPropertyUnit &unit); - - //! This method is the one to use to display a value in a dialog - //! @return the value @p ptValue converted the unit and rounded, ready to be displayed - QString toUserStringValue(qreal ptValue) const; - - //! This method is the one to use to read a value from a dialog - //! @return the value converted to points for internal use - qreal fromUserValue(qreal value) const; - - //! This method is the one to use to read a value from a dialog - //! @param value value entered by the user - //! @param ok if set, the pointed bool is set to true if the value could be - //! converted to a qreal, and to false otherwise. - //! @return the value converted to points for internal use - qreal fromUserValue(const QString &value, bool *ok = 0) const; - - //! Get the description string of the given unit - static QString unitDescription(KPropertyUnit::Type type); - - //! Get the symbol string of the unit - QString symbol() const; - - //! Returns the list of unit types for the UI, controlled with the given @p listOptions. - static QStringList listOfUnitNameForUi(ListOptions listOptions = ListOption::All); - - //! Get the index of this unit in the list of unit types for the UI, - //! if it is controlled with the given @p listOptions. - int indexInListForUi(ListOptions listOptions = ListOption::All) const; - - /** - * Parses common %KProperty and ODF values, like "10cm", "5mm" to pt. - * If no unit is specified, pt is assumed. - */ - static qreal parseValue(const QString &value, qreal defaultVal = 0.0); - - //! Parse an angle to its value in degrees - static qreal parseAngle(const QString &value, qreal defaultVal = 0.0); - - //! Equal to symbol(): returns the symbol string of the unit. - inline QString toString() const { - return symbol(); - } - -private: - Type m_type; - qreal m_pixelConversion; -}; - -#ifndef QT_NO_DEBUG_STREAM -KPROPERTYCORE_EXPORT QDebug operator<<(QDebug, const KPropertyUnit &); -#endif - -Q_DECLARE_METATYPE(KPropertyUnit) -Q_DECLARE_OPERATORS_FOR_FLAGS(KPropertyUnit::ListOptions) - -#endif diff --git a/src/KPropertyUnit_p.cpp b/src/KPropertyUnit_p.cpp deleted file mode 100644 --- a/src/KPropertyUnit_p.cpp +++ /dev/null @@ -1,386 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 2001 David Faure - Copyright (C) 2004, Nicolas GOUTTE - Copyright 2012 Friedrich W. H. Kossebau - - This library 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 of the License, or (at your option) any later version. - - This library 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 library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. -*/ - -#include "KPropertyUnit_p.h" -#include "kproperty_debug.h" - -#include - -// ensure the same order as in KPropertyUnit::Unit -static const char* const unitNameList[int(KPropertyUnit::Type::TypeCount)] = -{ - "mm", - "pt", - "in", - "cm", - "dm", - "pi", - "cc", - "px" -}; - -QString KPropertyUnit::unitDescription(KPropertyUnit::Type type) -{ - switch (type) { - case KPropertyUnit::Type::Millimeter: - return QObject::tr("Millimeters (mm)"); - case KPropertyUnit::Type::Centimeter: - return QObject::tr("Centimeters (cm)"); - case KPropertyUnit::Type::Decimeter: - return QObject::tr("Decimeters (dm)"); - case KPropertyUnit::Type::Inch: - return QObject::tr("Inches (in)"); - case KPropertyUnit::Type::Pica: - return QObject::tr("Pica (pi)"); - case KPropertyUnit::Type::Cicero: - return QObject::tr("Cicero (cc)"); - case KPropertyUnit::Type::Point: - return QObject::tr("Points (pt)"); - case KPropertyUnit::Type::Pixel: - return QObject::tr("Pixels (px)"); - default: - return QObject::tr("Unsupported unit"); - } -} - -// grouped by units which are similar -static const KPropertyUnit::Type typesInUi[int(KPropertyUnit::Type::TypeCount)] = -{ - KPropertyUnit::Type::Millimeter, - KPropertyUnit::Type::Centimeter, - KPropertyUnit::Type::Decimeter, - KPropertyUnit::Type::Inch, - KPropertyUnit::Type::Pica, - KPropertyUnit::Type::Cicero, - KPropertyUnit::Type::Point, - KPropertyUnit::Type::Pixel, -}; - -QStringList KPropertyUnit::listOfUnitNameForUi(ListOptions listOptions) -{ - QStringList lst; - for (int i = 0; i < int(KPropertyUnit::Type::TypeCount); ++i) { - const Type type = typesInUi[i]; - if (type != Type::Pixel || !(listOptions & ListOption::HidePixel)) { - lst.append(unitDescription(type)); - } - } - return lst; -} - -KPropertyUnit KPropertyUnit::fromListForUi(int index, ListOptions listOptions, qreal factor) -{ - KPropertyUnit::Type type = KPropertyUnit::Type::Point; - - if ((0 <= index) && (index < int(KPropertyUnit::Type::TypeCount))) { - // iterate through all enums and skip the Pixel enum if needed - for (int i = 0; i < int(KPropertyUnit::Type::TypeCount); ++i) { - if ((listOptions & ListOption::HidePixel) && (typesInUi[i] == Type::Pixel)) { - ++index; - continue; - } - if (i == index) { - type = typesInUi[i]; - break; - } - } - } - - return KPropertyUnit(type, factor); -} - -int KPropertyUnit::indexInListForUi(ListOptions listOptions) const -{ - if ((listOptions & ListOption::HidePixel) && (m_type == Type::Pixel)) { - return -1; - } - - int result = -1; - - int skipped = 0; - for (int i = 0; i < int(KPropertyUnit::Type::TypeCount); ++i) { - if ((listOptions & ListOption::HidePixel) && (typesInUi[i] == Type::Pixel)) { - ++skipped; - continue; - } - if (typesInUi[i] == m_type) { - result = i - skipped; - break; - } - } - - return result; -} - -qreal KPropertyUnit::toUserValue(qreal ptValue) const -{ - switch (m_type) { - case Type::Millimeter: - return toMillimeter(ptValue); - case Type::Centimeter: - return toCentimeter(ptValue); - case Type::Decimeter: - return toDecimeter(ptValue); - case Type::Inch: - return toInch(ptValue); - case Type::Pica: - return toPica(ptValue); - case Type::Cicero: - return toCicero(ptValue); - case Type::Pixel: - return ptValue * m_pixelConversion; - case Type::Point: - default: - return toPoint(ptValue); - } -} - -qreal KPropertyUnit::ptToUnit(qreal ptValue, const KPropertyUnit &unit) -{ - switch (unit.m_type) { - case Type::Millimeter: - return POINT_TO_MM(ptValue); - case Type::Centimeter: - return POINT_TO_CM(ptValue); - case Type::Decimeter: - return POINT_TO_DM(ptValue); - case Type::Inch: - return POINT_TO_INCH(ptValue); - case Type::Pica: - return POINT_TO_PI(ptValue); - case Type::Cicero: - return POINT_TO_CC(ptValue); - case Type::Pixel: - return ptValue * unit.m_pixelConversion; - case Type::Point: - default: - return ptValue; - } -} - -QString KPropertyUnit::toUserStringValue(qreal ptValue) const -{ - return QLocale::system().toString(toUserValue(ptValue)); -} - -qreal KPropertyUnit::fromUserValue(qreal value) const -{ - switch (m_type) { - case Type::Millimeter: - return MM_TO_POINT(value); - case Type::Centimeter: - return CM_TO_POINT(value); - case Type::Decimeter: - return DM_TO_POINT(value); - case Type::Inch: - return INCH_TO_POINT(value); - case Type::Pica: - return PI_TO_POINT(value); - case Type::Cicero: - return CC_TO_POINT(value); - case Type::Pixel: - return value / m_pixelConversion; - case Type::Point: - default: - return value; - } -} - -qreal KPropertyUnit::fromUserValue(const QString &value, bool *ok) const -{ - return fromUserValue(QLocale::system().toDouble(value, ok)); -} - -qreal KPropertyUnit::parseValue(const QString& _value, qreal defaultVal) -{ - if (_value.isEmpty()) - return defaultVal; - - QString value(_value.simplified()); - value.remove(QLatin1Char(' ')); - - int firstLetter = -1; - for (int i = 0; i < value.length(); ++i) { - if (value.at(i).isLetter()) { - if (value.at(i) == QLatin1Char('e')) - continue; - firstLetter = i; - break; - } - } - - bool ok; - if (firstLetter == -1) { - const qreal result = QVariant(value).toReal(&ok); - return ok ? result : defaultVal; - } - - const QByteArray symbol = value.mid(firstLetter).toLatin1(); - value.truncate(firstLetter); - const qreal val = value.toDouble(); - - if (symbol == "pt" || symbol.isEmpty()) { - return val; - } - - KPropertyUnit u = KPropertyUnit::fromSymbol(QLatin1String(symbol), &ok); - if (ok) - return u.fromUserValue(val); - - if (symbol == "m") { - return DM_TO_POINT(val * 10.0); - } else if (symbol == "km") { - return DM_TO_POINT(val * 10000.0); - } - kprWarning() << "KPropertyUnit::parseValue: Unit " << symbol << " is not supported, please report."; - - //! @todo add support for mi/ft ? - return defaultVal; -} - -KPropertyUnit KPropertyUnit::fromSymbol(const QString &symbol, bool *ok) -{ - Type result = Type::Point; - - if (symbol == QLatin1String("inch") /*compat*/) { - result = Type::Inch; - if (ok) - *ok = true; - } else { - if (ok) - *ok = false; - - for (int i = 0; i < int(Type::TypeCount); ++i) { - if (symbol == QLatin1String(unitNameList[i])) { - result = static_cast(i); - if (ok) - *ok = true; - } - } - } - - return KPropertyUnit(result); -} - -qreal KPropertyUnit::convertFromUnitToUnit(qreal value, const KPropertyUnit &fromUnit, const KPropertyUnit &toUnit, qreal factor) -{ - qreal pt; - switch (fromUnit.type()) { - case Type::Millimeter: - pt = MM_TO_POINT(value); - break; - case Type::Centimeter: - pt = CM_TO_POINT(value); - break; - case Type::Decimeter: - pt = DM_TO_POINT(value); - break; - case Type::Inch: - pt = INCH_TO_POINT(value); - break; - case Type::Pica: - pt = PI_TO_POINT(value); - break; - case Type::Cicero: - pt = CC_TO_POINT(value); - break; - case Type::Pixel: - pt = value / factor; - break; - case Type::Point: - default: - pt = value; - } - - switch (toUnit.type()) { - case Type::Millimeter: - return POINT_TO_MM(pt); - case Type::Centimeter: - return POINT_TO_CM(pt); - case Type::Decimeter: - return POINT_TO_DM(pt); - case Type::Inch: - return POINT_TO_INCH(pt); - case Type::Pica: - return POINT_TO_PI(pt); - case Type::Cicero: - return POINT_TO_CC(pt); - case Type::Pixel: - return pt * factor; - case Type::Point: - default: - return pt; - } - -} - -QString KPropertyUnit::symbol() const -{ - return QLatin1String(unitNameList[int(m_type)]); -} - -qreal KPropertyUnit::parseAngle(const QString& _value, qreal defaultVal) -{ - if (_value.isEmpty()) - return defaultVal; - - QString value(_value.simplified()); - value.remove(QLatin1Char(' ')); - - int firstLetter = -1; - for (int i = 0; i < value.length(); ++i) { - if (value.at(i).isLetter()) { - if (value.at(i) == QLatin1Char('e')) - continue; - firstLetter = i; - break; - } - } - - if (firstLetter == -1) - return value.toDouble(); - - const QString type = value.mid(firstLetter); - value.truncate(firstLetter); - const qreal val = value.toDouble(); - - if (type == QLatin1String("deg")) - return val; - else if (type == QLatin1String("rad")) - return val * 180 / M_PI; - else if (type == QLatin1String("grad")) - return val * 0.9; - - return defaultVal; -} - -#ifndef QT_NO_DEBUG_STREAM -QDebug operator<<(QDebug debug, const KPropertyUnit &unit) -{ -#ifndef NDEBUG - debug.nospace() << unit.symbol(); -#else - Q_UNUSED(unit); -#endif - return debug.space(); - -} -#endif diff --git a/src/KPropertyUtils_p.h b/src/KPropertyUtils_p.h --- a/src/KPropertyUtils_p.h +++ b/src/KPropertyUtils_p.h @@ -1,5 +1,5 @@ /* This file is part of the KDE project - Copyright (C) 2010-2016 Jarosław Staniek + Copyright (C) 2010-2017 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -23,6 +23,8 @@ #include #include +class KProperty; + namespace KPropertyUtilsPrivate { @@ -54,6 +56,20 @@ //! Warns on failure and returns @c false. bool setupGlobalIconTheme(); +//! Helper for handling minValueText, prefix and suffix property options +class ValueOptionsHandler +{ +public: + explicit ValueOptionsHandler(const KProperty &property); + + //! @return @a valueString value with prefix and suffix, if present + QString valueWithPrefixAndSuffix(const QString &valueString, const QLocale &locale) const; + + QString minValueText; + QString prefix; + QString suffix; +}; + //! @short Manages the QPainter::save()/QPainter::restore() block using RAII /*! The PainterSaver class makes sure that restore() is called when exiting from the block of code. diff --git a/src/KPropertyUtils_p.cpp b/src/KPropertyUtils_p.cpp --- a/src/KPropertyUtils_p.cpp +++ b/src/KPropertyUtils_p.cpp @@ -1,5 +1,5 @@ /* This file is part of the KDE project - Copyright (C) 2010-2016 Jarosław Staniek + Copyright (C) 2010-2017 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -19,6 +19,7 @@ #include "KPropertyUtils_p.h" #include "KPropertyEditorView.h" +#include "KProperty.h" #include #if defined KPropertyCore_EXPORTS || defined KPropertyWidgets_EXPORTS @@ -379,6 +380,47 @@ // ---- +ValueOptionsHandler::ValueOptionsHandler(const KProperty &property) +{ + minValueText = property.option("minValueText").toString(); + prefix = property.option("prefix").toString().trimmed(); + suffix = property.option("suffix").toString().trimmed(); +} + +QString ValueOptionsHandler::valueWithPrefixAndSuffix(const QString &valueString, const QLocale &locale) const +{ + if (prefix.isEmpty()) { + if (suffix.isEmpty()) { // value only + return valueString; + } else { // value+suffix + if (locale.language() == QLocale::C) { + return QString::fromLatin1("%1 %2").arg(valueString).arg(suffix); + } else { + return QObject::tr("%1 %2", " ") + .arg(valueString).arg(suffix); + } + } + } + if (suffix.isEmpty()) { // prefix + value + if (locale.language() == QLocale::C) { + return QString::fromLatin1("%1 %2").arg(prefix).arg(valueString); + } else { + return QObject::tr("%1 %2", " ") + .arg(prefix).arg(valueString); + } + } + // prefix+value+suffix + if (locale.language() == QLocale::C) { + return QString::fromLatin1("%1 %2 %3") + .arg(prefix).arg(valueString).arg(suffix); + } else { + return QObject::tr("%1 %2 %3", " ") + .arg(prefix).arg(valueString).arg(suffix); + } +} + +// ---- + PainterSaver::PainterSaver(QPainter *p) : m_painter(p) { diff --git a/src/KPropertyWidgetsFactory.h b/src/KPropertyWidgetsFactory.h --- a/src/KPropertyWidgetsFactory.h +++ b/src/KPropertyWidgetsFactory.h @@ -20,9 +20,9 @@ #ifndef KPROPERTYWIDGETS_FACTORY_H #define KPROPERTYWIDGETS_FACTORY_H -#include "kpropertywidgets_export.h" #include "KProperty.h" #include "KPropertyFactory.h" +#include "KPropertyUtils.h" #include #include @@ -82,7 +82,7 @@ Q_OBJECT Q_PROPERTY(QVariant value READ value WRITE setValue USER true) public: - KPropertyLabel(QWidget *parent, const KPropertyValueDisplayInterface *iface); + KPropertyLabel(QWidget *parent, const KProperty *property, const KPropertyValueDisplayInterface *iface); QVariant value() const; Q_SIGNALS: void commitData( QWidget * editor ); @@ -93,6 +93,7 @@ virtual void paintEvent( QPaintEvent * event ); private: + const KProperty *m_property; const KPropertyValueDisplayInterface *m_iface; QVariant m_value; }; @@ -113,8 +114,8 @@ { Q_UNUSED(type); Q_UNUSED(option); - Q_UNUSED(index); - return new Widget(parent, this); + KProperty *prop = KPropertyUtils::propertyForIndex(index); + return new Widget(parent, prop, this); } virtual void paint(QPainter *painter, diff --git a/src/KPropertyWidgetsFactory.cpp b/src/KPropertyWidgetsFactory.cpp --- a/src/KPropertyWidgetsFactory.cpp +++ b/src/KPropertyWidgetsFactory.cpp @@ -1,5 +1,5 @@ /* This file is part of the KDE project - Copyright (C) 2008-2016 Jarosław Staniek + Copyright (C) 2008-2017 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -23,23 +23,25 @@ #include "KDefaultPropertyFactory.h" #include "KPropertyEditorView.h" #include "KPropertyStringEditor.h" +#include "KPropertyUtils.h" #include "KPropertyUtils_p.h" -KPropertyLabel::KPropertyLabel(QWidget *parent, const KPropertyValueDisplayInterface *iface) - : QLabel(parent) - , m_iface(iface) +KPropertyLabel::KPropertyLabel(QWidget *parent, const KProperty *property, + const KPropertyValueDisplayInterface *iface) + : QLabel(parent), m_property(property), m_iface(iface) { - setAutoFillBackground(true); + setAutoFillBackground(true); - KPropertyEditorView* view = 0; - if (parent) { - view = qobject_cast(parent->parentWidget()); - } - const QColor gridLineColor(view ? view->gridLineColor() : KPropertyEditorView::defaultGridLineColor()); - const int top = 1 + (gridLineColor.isValid() ? 1 : 0); + KPropertyEditorView *view = 0; + if (parent) { + view = qobject_cast(parent->parentWidget()); + } + const QColor gridLineColor(view ? view->gridLineColor() + : KPropertyEditorView::defaultGridLineColor()); + const int top = 1 + (gridLineColor.isValid() ? 1 : 0); - setContentsMargins(0, top, 0, 0); - setIndent(1); + setContentsMargins(0, top, 0, 0); + setIndent(1); } QVariant KPropertyLabel::value() const @@ -49,8 +51,8 @@ void KPropertyLabel::setValue(const QVariant& value) { - setText( m_iface->valueToString(value, QLocale()) ); m_value = value; + setText(m_iface->propertyValueToString(m_property, QLocale())); } void KPropertyLabel::paintEvent( QPaintEvent * event ) @@ -223,7 +225,8 @@ { const KPropertyUtilsPrivate::PainterSaver saver(painter); QRect r(option.rect); - r.setLeft(r.left()+1); - painter->drawText( r, Qt::AlignLeft | Qt::AlignVCenter, - iface->valueToString(index.data(Qt::EditRole), QLocale())); + r.setLeft(r.left() + 1); + KProperty *prop = KPropertyUtils::propertyForIndex(index); + painter->drawText(r, Qt::AlignLeft | Qt::AlignVCenter, + iface->propertyValueToString(prop, QLocale())); } diff --git a/src/editors/KPropertyRectFEditor.h b/src/editors/KPropertyRectFEditor.h --- a/src/editors/KPropertyRectFEditor.h +++ b/src/editors/KPropertyRectFEditor.h @@ -1,5 +1,5 @@ /* This file is part of the KDE project - Copyright (C) 2015 Jarosław Staniek + Copyright (C) 2015-2017 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -40,6 +40,9 @@ public: KPropertyRectFDelegate(); + QString propertyValueToString(const KProperty *property, + const QLocale &locale) const override; + virtual QString valueToString(const QVariant& value, const QLocale &locale) const; }; diff --git a/src/editors/KPropertyRectFEditor.cpp b/src/editors/KPropertyRectFEditor.cpp --- a/src/editors/KPropertyRectFEditor.cpp +++ b/src/editors/KPropertyRectFEditor.cpp @@ -1,5 +1,5 @@ /* This file is part of the KDE project - Copyright (C) 2015 Jarosław Staniek + Copyright (C) 2015-2017 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -18,13 +18,21 @@ */ #include "KPropertyRectFEditor.h" +#include "KPropertyUtils_p.h" #include KPropertyRectFDelegate::KPropertyRectFDelegate() { } +QString KPropertyRectFDelegate::propertyValueToString(const KProperty *property, + const QLocale &locale) const +{ + const KPropertyUtilsPrivate::ValueOptionsHandler options(*property); + return options.valueWithPrefixAndSuffix(valueToString(property->value(), locale), locale); +} + QString KPropertyRectFDelegate::valueToString(const QVariant& value, const QLocale &locale) const { const QRectF r(value.toRectF()); diff --git a/src/editors/pointfedit.h b/src/editors/pointfedit.h --- a/src/editors/pointfedit.h +++ b/src/editors/pointfedit.h @@ -43,6 +43,9 @@ public: KPropertyPointFDelegate(); + QString propertyValueToString(const KProperty *property, + const QLocale &locale) const override; + virtual QString valueToString(const QVariant& value, const QLocale &locale) const; }; diff --git a/src/editors/pointfedit.cpp b/src/editors/pointfedit.cpp --- a/src/editors/pointfedit.cpp +++ b/src/editors/pointfedit.cpp @@ -1,7 +1,7 @@ /* This file is part of the KDE project Copyright (C) 2004 Cedric Pasteur Copyright (C) 2004 Alexander Dymo - Copyright (C) 2008-2015 Jarosław Staniek + Copyright (C) 2008-2017 Jarosław Staniek Copyright (C) 2010 Adam Pigg This library is free software; you can redistribute it and/or @@ -21,13 +21,21 @@ */ #include "pointfedit.h" +#include "KPropertyUtils_p.h" #include KPropertyPointFDelegate::KPropertyPointFDelegate() { } +QString KPropertyPointFDelegate::propertyValueToString(const KProperty *property, + const QLocale &locale) const +{ + const KPropertyUtilsPrivate::ValueOptionsHandler options(*property); + return options.valueWithPrefixAndSuffix(valueToString(property->value(), locale), locale); +} + QString KPropertyPointFDelegate::valueToString(const QVariant& value, const QLocale &locale) const { const QPointF p(value.toPointF()); diff --git a/src/editors/sizefedit.h b/src/editors/sizefedit.h --- a/src/editors/sizefedit.h +++ b/src/editors/sizefedit.h @@ -1,7 +1,7 @@ /* This file is part of the KDE project Copyright (C) 2004 Cedric Pasteur Copyright (C) 2004 Alexander Dymo - Copyright (C) 2008-2015 Jarosław Staniek + Copyright (C) 2008-2017 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -42,6 +42,9 @@ public: KPropertySizeFDelegate(); + QString propertyValueToString(const KProperty *property, + const QLocale &locale) const override; + virtual QString valueToString(const QVariant& value, const QLocale &locale) const; }; diff --git a/src/editors/sizefedit.cpp b/src/editors/sizefedit.cpp --- a/src/editors/sizefedit.cpp +++ b/src/editors/sizefedit.cpp @@ -1,5 +1,5 @@ /* This file is part of the KDE project - Copyright (C) 2008-2015 Jarosław Staniek + Copyright (C) 2008-2017 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -18,13 +18,21 @@ */ #include "sizefedit.h" +#include "KPropertyUtils_p.h" #include KPropertySizeFDelegate::KPropertySizeFDelegate() { } +QString KPropertySizeFDelegate::propertyValueToString(const KProperty *property, + const QLocale &locale) const +{ + const KPropertyUtilsPrivate::ValueOptionsHandler options(*property); + return options.valueWithPrefixAndSuffix(valueToString(property->value(), locale), locale); +} + QString KPropertySizeFDelegate::valueToString(const QVariant& value, const QLocale &locale) const { const QSizeF s(value.toSizeF()); diff --git a/src/editors/spinbox.h b/src/editors/spinbox.h --- a/src/editors/spinbox.h +++ b/src/editors/spinbox.h @@ -1,7 +1,7 @@ /* This file is part of the KDE project Copyright (C) 2004 Cedric Pasteur Copyright (C) 2004 Alexander Dymo - Copyright (C) 2008-2015 Jarosław Staniek + Copyright (C) 2008-2017 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -74,14 +74,9 @@ return QDoubleSpinBox::lineEdit(); }*/ - double value() const; - Q_SIGNALS: void commitData(QWidget* editor); -public Q_SLOTS: - void setValue(double value); - protected Q_SLOTS: void slotValueChanged(double value); diff --git a/src/editors/spinbox.cpp b/src/editors/spinbox.cpp --- a/src/editors/spinbox.cpp +++ b/src/editors/spinbox.cpp @@ -1,7 +1,7 @@ /* This file is part of the KDE project Copyright (C) 2004 Cedric Pasteur Copyright (C) 2004 Alexander Dymo - Copyright (C) 2008-2015 Jarosław Staniek + Copyright (C) 2008-2017 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -23,7 +23,6 @@ #include "KProperty.h" #include "KProperty_p.h" #include "KPropertyEditorView.h" -#include "KPropertyUnit_p.h" #include "KPropertyUtils.h" #include "KPropertyUtils_p.h" #include "KPropertyWidgetsFactory.h" @@ -59,6 +58,25 @@ .arg(QLatin1String(_class)); } +namespace { + +bool intRangeValue(const KProperty &property, QVariant *min, QVariant *max) +{ + Q_ASSERT(min); + Q_ASSERT(max); + *min = property.option("min", (property.type() == KProperty::UInt) ? 0 : -INT_MAX); + *max = property.option("max", INT_MAX); + if (!min->canConvert(QMetaType::Int)) { + min->clear(); + } + if (!max->canConvert(QMetaType::Int)) { + max->clear(); + } + return min->isValid() && max->isValid() && min->toInt() <= max->toInt(); +} + +} // namespace + KPropertyIntSpinBox::KPropertyIntSpinBox(const KProperty* prop, QWidget *parent, int itemHeight) : QSpinBox(parent) , m_unsigned(prop->type() == KProperty::UInt) @@ -74,13 +92,21 @@ KPropertyWidgetsFactory::setTopAndBottomBordersUsingStyleSheet(this, css); setStyleSheet(css); - QVariant minVal(prop->option("min", m_unsigned ? 0 : -INT_MAX)); - QVariant maxVal(prop->option("max", INT_MAX)); - setMinimum(minVal.toInt()); - setMaximum(maxVal.toInt()); - QString minValueText(prop->option("minValueText").toString()); - if (!minValueText.isEmpty()) - setSpecialValueText(minValueText); + QVariant minVal; + QVariant maxVal; + if (intRangeValue(*prop, &minVal, &maxVal)) { + setRange(minVal.toInt(), maxVal.toInt()); + } + const KPropertyUtilsPrivate::ValueOptionsHandler options(*prop); + if (!options.minValueText.isEmpty()) { + setSpecialValueText(options.minValueText); + } + if (!options.prefix.isEmpty()) { + setPrefix(options.prefix + QLatin1Char(' ')); + } + if (!options.suffix.isEmpty()) { + setSuffix(QLatin1Char(' ') + options.suffix); + } connect(this, SIGNAL(valueChanged(int)), this, SLOT(slotValueChanged(int))); } @@ -116,51 +142,35 @@ class Q_DECL_HIDDEN KPropertyDoubleSpinBox::Private { public: - KPropertyUnit unit; - bool hasUnit; + bool dummy = false; }; namespace { -void decodeUnit(const KProperty &property, KPropertyUnit *unit, bool *hasUnit) +bool doubleRangeValue(const KProperty &property, QVariant *min, QVariant *max) { - const QString unitString = property.option("unit").toString(); - if (unitString.isEmpty()) { - *hasUnit = false; + Q_ASSERT(min); + Q_ASSERT(max); + *min = property.option("min", 0.0); + *max = property.option("max", DBL_MAX / 100.0); + if (!min->canConvert(QMetaType::Double)) { + min->clear(); } - else { - *unit = KPropertyUnit::fromSymbol(unitString, hasUnit); + if (!max->canConvert(QMetaType::Double)) { + max->clear(); } + return min->isValid() && max->isValid() && min->toDouble() <= max->toDouble(); } -//! Helper for testing property options -struct DoubleValueOptions { - explicit DoubleValueOptions(const KProperty &property) - { - minVal = property.option("min", 0.0); - maxVal = property.option("max", DBL_MAX / 100.0); - if (!minVal.canConvert(QMetaType::Double) || !maxVal.canConvert(QMetaType::Double) - || minVal.toReal() > maxVal.toReal()) - { - minVal.clear(); - maxVal.clear(); - } - step = property.option("step", KPROPERTY_DEFAULT_DOUBLE_VALUE_STEP); - if (!step.canConvert(QMetaType::Double) || step.toDouble() <= 0.0) { - step.clear(); - } - precision = property.option("precision", KPROPERTY_DEFAULT_DOUBLE_VALUE_PRECISION); - if (!precision.canConvert(QMetaType::Int) || precision.toInt() < 0) { - precision.clear(); - } - minValueText = property.option("minValueText").toString(); +QVariant precisionValue(const KProperty &property) +{ + QVariant result = property.option("precision", KPROPERTY_DEFAULT_DOUBLE_VALUE_PRECISION); + if (result.canConvert(QMetaType::Int) && result.toInt() >= 0) { + return result; } - QVariant minVal; - QVariant maxVal; - QVariant step; - QVariant precision; - QString minValueText; -}; + return QVariant(); +} + } // namespace KPropertyDoubleSpinBox::KPropertyDoubleSpinBox(const KProperty* prop, QWidget *parent, int itemHeight) @@ -185,26 +195,30 @@ KPropertyWidgetsFactory::setTopAndBottomBordersUsingStyleSheet(this, css); setStyleSheet(css); - const DoubleValueOptions options(*prop); - if (options.minVal.isValid() && options.maxVal.isValid()) { - setRange(options.minVal.toDouble(), options.maxVal.toDouble()); + QVariant minVal; + QVariant maxVal; + if (doubleRangeValue(*prop, &minVal, &maxVal)) { + setRange(minVal.toDouble(), maxVal.toDouble()); + } + QVariant step = prop->option("step", KPROPERTY_DEFAULT_DOUBLE_VALUE_STEP); + if (step.canConvert(QMetaType::Double) && step.toDouble() > 0.0) { + setSingleStep(step.toDouble()); } - if (options.step.isValid()) { - setSingleStep(options.step.toDouble()); + const QVariant precision = precisionValue(*prop); + if (precision.isValid()) { + setDecimals(precision.toInt()); } //! @todo implement slider // bool slider = prop->option("slider", false).toBool(); - if (options.precision.isValid()) { - setDecimals(options.precision.toInt()); - } + const KPropertyUtilsPrivate::ValueOptionsHandler options(*prop); if (!options.minValueText.isEmpty()) { setSpecialValueText(options.minValueText); } - decodeUnit(*prop, &d->unit, &d->hasUnit); - if (d->hasUnit) { - setSuffix( - QObject::tr("%1 %2", " ") // this adds necessary space - .arg(QString()).arg(d->unit.toString())); + if (!options.prefix.isEmpty()) { + setPrefix(options.prefix + QLatin1Char(' ')); + } + if (!options.suffix.isEmpty()) { + setSuffix(QLatin1Char(' ') + options.suffix); } connect(this, SIGNAL(valueChanged(double)), this, SLOT(slotValueChanged(double))); } @@ -220,23 +234,6 @@ QDoubleSpinBox::resizeEvent(event); } -void KPropertyDoubleSpinBox::setValue(double v) -{ - if (d->hasUnit) { - QDoubleSpinBox::setValue(d->unit.toUserValue(v)); - return; - } - QDoubleSpinBox::setValue(v); -} - -double KPropertyDoubleSpinBox::value() const -{ - if (d->hasUnit) { - return d->unit.fromUserValue(QDoubleSpinBox::value()); - } - return QDoubleSpinBox::value(); -} - void KPropertyDoubleSpinBox::slotValueChanged(double value) { Q_UNUSED(value); @@ -252,35 +249,20 @@ QString KPropertyIntSpinBoxDelegate::propertyValueToString(const KProperty* prop, const QLocale &locale) const { - KPropertyUnit unit; - bool hasUnit; - decodeUnit(*prop, &unit, &hasUnit); - if (prop->hasOptions()) { - //replace min value with minValueText if defined - QVariant minValue(prop->option("min")); - QString minValueText(prop->option("minValueText").toString()); - if (!minValue.isNull() && !minValueText.isEmpty() - && minValue.toInt() == prop->value().toInt()) - { - return minValueText; - } - } - 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()); + //replace min value with minValueText if defined + QVariant minVal; + QVariant maxVal; + const KPropertyUtilsPrivate::ValueOptionsHandler options(*prop); + (void)intRangeValue(*prop, &minVal, &maxVal); + if (minVal.isValid() && minVal.toInt() == prop->value().toInt()) { + return options.minValueText; } - return QObject::tr("%1 %2", " ") - .arg(valueToString(unit.toUserValue(prop->value().toDouble()), locale)) - .arg(unit.toString()); + return options.valueWithPrefixAndSuffix(valueToString(prop->value(), locale), locale); } QString KPropertyIntSpinBoxDelegate::valueToString(const QVariant& value, const QLocale &locale) const { - return locale.toString(value.toDouble(), 'f', 0); + return locale.toString(value.toReal(), 'f', 0); } QWidget* KPropertyIntSpinBoxDelegate::createEditor( int type, QWidget *parent, @@ -304,36 +286,27 @@ QString KPropertyDoubleSpinBoxDelegate::propertyValueToString(const KProperty* prop, const QLocale &locale) const { - const DoubleValueOptions options(*prop); //replace min value with minValueText if defined - if (options.minVal.isValid() && !options.minValueText.isEmpty() - && options.minVal.toDouble() == prop->value().toDouble()) - { + QVariant minVal; + QVariant maxVal; + const KPropertyUtilsPrivate::ValueOptionsHandler options(*prop); + (void)doubleRangeValue(*prop, &minVal, &maxVal); + if (minVal.isValid() && minVal.toDouble() == prop->value().toDouble()) { return options.minValueText; } - KPropertyUnit unit; - bool hasUnit; - decodeUnit(*prop, &unit, &hasUnit); - const qreal realValue = hasUnit ? unit.toUserValue(prop->value().toReal()) : prop->value().toReal(); QString valueString; - if (options.precision.isValid()) { - valueString = locale.toString(realValue, 'f', options.precision.toInt()); + const QVariant precision = precisionValue(*prop); + if (precision.isValid()) { + valueString = locale.toString(prop->value().toReal(), 'f', precision.toInt()); } else { - valueString = valueToString(realValue, locale); - } - - if (!hasUnit) { - return valueString; - } - if (locale.language() == QLocale::C) { - return QString::fromLatin1("%1 %2").arg(valueString).arg(unit.toString()); + valueString = valueToString(prop->value().toReal(), locale); } - return QObject::tr("%1 %2", " ").arg(valueString).arg(unit.toString()); + return options.valueWithPrefixAndSuffix(valueString, locale); } QString KPropertyDoubleSpinBoxDelegate::valueToString(const QVariant& value, const QLocale &locale) const { - return locale.toString(value.toDouble()); + return locale.toString(value.toReal()); } QWidget* KPropertyDoubleSpinBoxDelegate::createEditor( int type, QWidget *parent,