1 /* This file is part of the KDE project
2 Copyright (C) 2008-2015 Jarosław Staniek <staniek@kde.org>
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 "rectedit.h"
21
22 #include <QRect>
23
KPropertyRectDelegate()24 KPropertyRectDelegate::KPropertyRectDelegate()
25 {
26 }
27
valueToString(const QVariant & value,const QLocale & locale) const28 QString KPropertyRectDelegate::valueToString(const QVariant& value, const QLocale &locale) const
29 {
30 const QRect r(value.toRect());
31 if (locale.language() == QLocale::C) {
32 return QString::fromLatin1("%1, %2, %3x%4").arg(r.x()).arg(r.y()).arg(r.width()).arg(r.height());
33 }
34 return QObject::tr("%1, %2, %3x%4", "Rectangle")
35 .arg(locale.toString(r.x()))
36 .arg(locale.toString(r.y()))
37 .arg(locale.toString(r.width()))
38 .arg(locale.toString(r.height()));
39 }
40
41 //------------
42
KRectComposedProperty(KProperty * property)43 KRectComposedProperty::KRectComposedProperty(KProperty *property)
44 : KComposedPropertyInterface(property)
45 {
46 (void)new KProperty("x",
47 QVariant(), QObject::tr("X", "Property: X coordinate"),
48 QObject::tr("X Coordinate", "Property: X coordinate"), KProperty::Int, property);
49 (void)new KProperty("y",
50 QVariant(), QObject::tr("Y", "Property: Y coordinate"),
51 QObject::tr("Y Coordinate", "Property: Y coordinate"), KProperty::Int, property);
52 (void)new KProperty("width",
53 QVariant(), QObject::tr("Width", "Property: width of rectangle"),
54 QObject::tr("Width", "Property: width of rectangle"), KProperty::UInt, property);
55 (void)new KProperty("height",
56 QVariant(), QObject::tr("Height", "Property: height of rectangle"),
57 QObject::tr("Height", "Property: height of rectangle"), KProperty::UInt, property);
58 }
59
setValue(KProperty * property,const QVariant & value,KProperty::ValueOptions valueOptions)60 void KRectComposedProperty::setValue(KProperty *property,
61 const QVariant &value, KProperty::ValueOptions valueOptions)
62 {
63 const QRect r( value.toRect() );
64 property->child("x")->setValue(r.x(), valueOptions);
65 property->child("y")->setValue(r.y(), valueOptions);
66 property->child("width")->setValue(r.width(), valueOptions);
67 property->child("height")->setValue(r.height(), valueOptions);
68 }
69
childValueChanged(KProperty * child,const QVariant & value,KProperty::ValueOptions valueOptions)70 void KRectComposedProperty::childValueChanged(KProperty *child,
71 const QVariant &value, KProperty::ValueOptions valueOptions)
72 {
73 QRect r( child->parent()->value().toRect() );
74
75 if (child->name() == "x")
76 r.moveLeft(value.toInt());
77 else if (child->name() == "y")
78 r.moveTop(value.toInt());
79 else if (child->name() == "width")
80 r.setWidth(value.toInt());
81 else if (child->name() == "height")
82 r.setHeight(value.toInt());
83
84 child->parent()->setValue(r, valueOptions);
85 }
86