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-2017 Jarosław Staniek <staniek@kde.org>
5    Copyright (C) 2010 Adam Pigg <adam@piggz.co.uk>
6 
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Library General Public
9    License as published by the Free Software Foundation; either
10    version 2 of the License, or (at your option) any later version.
11 
12    This library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Library General Public License for more details.
16 
17    You should have received a copy of the GNU Library General Public License
18    along with this library; see the file COPYING.LIB.  If not, write to
19    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21 */
22 
23 #include "pointfedit.h"
24 #include "KPropertyUtils_p.h"
25 
26 #include <QPointF>
27 
KPropertyPointFDelegate()28 KPropertyPointFDelegate::KPropertyPointFDelegate()
29 {
30 }
31 
propertyValueToString(const KProperty * property,const QLocale & locale) const32 QString KPropertyPointFDelegate::propertyValueToString(const KProperty *property,
33                                                        const QLocale &locale) const
34 {
35     const KPropertyUtilsPrivate::ValueOptionsHandler options(*property);
36     return options.valueWithPrefixAndSuffix(valueToString(property->value(), locale), locale);
37 }
38 
valueToString(const QVariant & value,const QLocale & locale) const39 QString KPropertyPointFDelegate::valueToString(const QVariant& value, const QLocale &locale) const
40 {
41     const QPointF p(value.toPointF());
42     if (locale.language() == QLocale::C) {
43         return QString::fromLatin1("%1, %2").arg(p.x()).arg(p.y());
44     }
45     return QObject::tr("%1, %2", "Point")
46         .arg(locale.toString(p.x()))
47         .arg(locale.toString(p.y()));
48 }
49 
50 //------------
51 
KPointFComposedProperty(KProperty * property)52 KPointFComposedProperty::KPointFComposedProperty(KProperty *property)
53         : KComposedPropertyInterface(property)
54 {
55     (void)new KProperty("x",
56         QVariant(), QObject::tr("X", "Property: X coordinate"),
57         QObject::tr("X Coordinate", "Property: X coordinate"), KProperty::Double, property);
58     (void)new KProperty("y",
59         QVariant(), QObject::tr("Y", "Property: Y coordinate"),
60         QObject::tr("Y Coordinate", "Property: Y coordinate"), KProperty::Double, property);
61 }
62 
setValue(KProperty * property,const QVariant & value,KProperty::ValueOptions valueOptions)63 void KPointFComposedProperty::setValue(KProperty *property,
64     const QVariant &value, KProperty::ValueOptions valueOptions)
65 {
66     const QPointF p( value.toPointF() );
67     property->child("x")->setValue(p.x(), valueOptions);
68     property->child("y")->setValue(p.y(), valueOptions);
69 }
70 
childValueChanged(KProperty * child,const QVariant & value,KProperty::ValueOptions valueOptions)71 void KPointFComposedProperty::childValueChanged(KProperty *child,
72     const QVariant &value, KProperty::ValueOptions valueOptions)
73 {
74     QPointF p( child->parent()->value().toPointF() );
75 
76     if (child->name() == "x")
77         p.setX(value.toDouble());
78     else if (child->name() == "y")
79         p.setY(value.toDouble());
80 
81     child->parent()->setValue(p, valueOptions);
82 }
83