KProperty

sizefedit.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2008-2017 Jarosław Staniek <[email protected]>
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 "sizefedit.h"
21 #include "KPropertyUtils_p.h"
22 
23 #include <QSize>
24 
25 KPropertySizeFDelegate::KPropertySizeFDelegate()
26 {
27 }
28 
29 QString KPropertySizeFDelegate::propertyValueToString(const KProperty *property,
30  const QLocale &locale) const
31 {
32  const KPropertyUtilsPrivate::ValueOptionsHandler options(*property);
33  return options.valueWithPrefixAndSuffix(valueToString(property->value(), locale), locale);
34 }
35 
36 QString KPropertySizeFDelegate::valueToString(const QVariant& value, const QLocale &locale) const
37 {
38  const QSizeF s(value.toSizeF());
39  if (locale.language() == QLocale::C) {
40  return QString::fromLatin1("%1x%2").arg(s.width()).arg(s.height());
41  }
42  return QObject::tr("%1x%2", "Size")
43  .arg(locale.toString(s.width()))
44  .arg(locale.toString(s.height()));
45 }
46 
47 //------------
48 
49 KSizeFComposedProperty::KSizeFComposedProperty(KProperty *property)
50  : KComposedPropertyInterface(property)
51 {
52  (void)new KProperty("width",
53  QVariant(), QObject::tr("Width"), QObject::tr("Width"), KProperty::Double, property);
54  (void)new KProperty("height",
55  QVariant(), QObject::tr("Height"), QObject::tr("Height"), KProperty::Double, property);
56 }
57 
58 void KSizeFComposedProperty::setValue(KProperty *property,
59  const QVariant &value, KProperty::ValueOptions valueOptions)
60 {
61  const QSizeF s( value.toSizeF() );
62  property->child("width")->setValue(s.width(), valueOptions);
63  property->child("height")->setValue(s.height(), valueOptions);
64 }
65 
66 void KSizeFComposedProperty::childValueChanged(KProperty *child,
67  const QVariant &value, KProperty::ValueOptions valueOptions)
68 {
69  QSizeF s( child->parent()->value().toSizeF() );
70  if (child->name() == "width")
71  s.setWidth(value.toDouble());
72  else if (child->name() == "height")
73  s.setHeight(value.toDouble());
74 
75  child->parent()->setValue(s, valueOptions);
76 }
QString toString(qlonglong i) const const
KProperty * parent() const
Definition: KProperty.cpp:673
QString tr(const char *sourceText, const char *disambiguation, int n)
An interface for for composed property handlers.
void setWidth(qreal width)
QSizeF toSizeF() const const
QLocale::Language language() const const
QByteArray name() const
Definition: KProperty.cpp:324
The base class representing a single property.
Definition: KProperty.h:95
QVariant value() const
Definition: KProperty.cpp:394
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromLatin1(const char *str, int size)
double toDouble(bool *ok) const const
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
Sets value of the property.
Definition: KProperty.cpp:405
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 23:10:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.