KProperty

KPropertyRectFEditor.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2015-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 "KPropertyRectFEditor.h"
21 #include "KPropertyUtils_p.h"
22 
23 #include <QRectF>
24 
25 KPropertyRectFDelegate::KPropertyRectFDelegate()
26 {
27 }
28 
29 QString KPropertyRectFDelegate::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 KPropertyRectFDelegate::valueToString(const QVariant& value, const QLocale &locale) const
37 {
38  const QRectF r(value.toRectF());
39  if (locale.language() == QLocale::C) {
40  return QString::fromLatin1("%1, %2, %3x%4").arg(r.x()).arg(r.y()).arg(r.width()).arg(r.height());
41  }
42  return QObject::tr("%1, %2, %3x%4", "Rectangle")
43  .arg(locale.toString(r.x()))
44  .arg(locale.toString(r.y()))
45  .arg(locale.toString(r.width()))
46  .arg(locale.toString(r.height()));
47 }
48 
49 //------------
50 
51 KRectFComposedProperty::KRectFComposedProperty(KProperty *property)
52  : KComposedPropertyInterface(property)
53 {
54  (void)new KProperty("x",
55  QVariant(), QObject::tr("X", "Property: X coordinate"),
56  QObject::tr("X Coordinate", "Property: X coordinate"), KProperty::Double, property);
57  (void)new KProperty("y",
58  QVariant(), QObject::tr("Y", "Property: Y coordinate"),
59  QObject::tr("Y Coordinate", "Property: Y coordinate"), KProperty::Double, property);
60  (void)new KProperty("width",
61  QVariant(), QObject::tr("Width", "Property: width of rectangle"),
62  QObject::tr("Width", "Property: width of rectangle"), KProperty::Double, property);
63  (void)new KProperty("height",
64  QVariant(), QObject::tr("Height", "Property: height of rectangle"),
65  QObject::tr("Height", "Property: height of rectangle"), KProperty::Double, property);
66 }
67 
68 void KRectFComposedProperty::setValue(KProperty *property,
69  const QVariant &value, KProperty::ValueOptions valueOptions)
70 {
71  const QRectF r(value.toRectF());
72  property->child("x")->setValue(r.x(), valueOptions);
73  property->child("y")->setValue(r.y(), valueOptions);
74  property->child("width")->setValue(r.width(), valueOptions);
75  property->child("height")->setValue(r.height(), valueOptions);
76 }
77 
78 void KRectFComposedProperty::childValueChanged(KProperty *child, const QVariant &value,
79  KProperty::ValueOptions valueOptions)
80 {
81  QRectF r(child->parent()->value().toRectF());
82 
83  if (child->name() == "x")
84  r.moveLeft(value.toReal());
85  else if (child->name() == "y")
86  r.moveTop(value.toReal());
87  else if (child->name() == "width")
88  r.setWidth(value.toReal());
89  else if (child->name() == "height")
90  r.setHeight(value.toReal());
91 
92  child->parent()->setValue(r, valueOptions);
93 }
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.
QVariant property(const char *name) const const
void moveLeft(qreal x)
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)
qreal toReal(bool *ok) const const
QRectF toRectF() 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.