1 /* This file is part of the KDE project
2    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
3    Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
4    Copyright (C) 2008-2015 Jarosław Staniek <staniek@kde.org>
5 
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public
8    License as published by the Free Software Foundation; either
9    version 2 of the License, or (at your option) any later version.
10 
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15 
16    You should have received a copy of the GNU Library General Public License
17    along with this library; see the file COPYING.LIB.  If not, write to
18    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20 */
21 
22 #include "pointedit.h"
23 
24 #include <QPoint>
25 
KPropertyPointDelegate()26 KPropertyPointDelegate::KPropertyPointDelegate()
27 {
28 }
29 
valueToString(const QVariant & value,const QLocale & locale) const30 QString KPropertyPointDelegate::valueToString(const QVariant& value, const QLocale &locale) const
31 {
32     const QPoint p(value.toPoint());
33     if (locale.language() == QLocale::C) {
34         return QString::fromLatin1("%1, %2").arg(p.x()).arg(p.y());
35     }
36     return QObject::tr("%1, %2", "Point")
37         .arg(locale.toString(p.x()))
38         .arg(locale.toString(p.y()));
39 }
40 
41 //------------
42 
KPointComposedProperty(KProperty * property)43 KPointComposedProperty::KPointComposedProperty(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 }
53 
setValue(KProperty * property,const QVariant & value,KProperty::ValueOptions valueOptions)54 void KPointComposedProperty::setValue(KProperty *property,
55     const QVariant &value, KProperty::ValueOptions valueOptions)
56 {
57     const QPoint p( value.toPoint() );
58     property->child("x")->setValue(p.x(), valueOptions);
59     property->child("y")->setValue(p.y(), valueOptions);
60 }
61 
childValueChanged(KProperty * child,const QVariant & value,KProperty::ValueOptions valueOptions)62 void KPointComposedProperty::childValueChanged(KProperty *child,
63     const QVariant &value, KProperty::ValueOptions valueOptions)
64 {
65     QPoint p( child->parent()->value().toPoint() );
66 
67     if (child->name() == "x")
68         p.setX(value.toInt());
69     else if (child->name() == "y")
70         p.setY(value.toInt());
71 
72     child->parent()->setValue(p, valueOptions);
73 }
74