File indexing completed on 2024-04-21 04:41:02

0001 /* This file is part of the KDE project
0002    Copyright (C) 2008-2015 Jarosław Staniek <staniek@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "rectedit.h"
0021 
0022 #include <QRect>
0023 
0024 KPropertyRectDelegate::KPropertyRectDelegate()
0025 {
0026 }
0027 
0028 QString KPropertyRectDelegate::valueToString(const QVariant& value, const QLocale &locale) const
0029 {
0030     const QRect r(value.toRect());
0031     if (locale.language() == QLocale::C) {
0032         return QString::fromLatin1("%1, %2, %3x%4").arg(r.x()).arg(r.y()).arg(r.width()).arg(r.height());
0033     }
0034     return QObject::tr("%1, %2, %3x%4", "Rectangle")
0035         .arg(locale.toString(r.x()))
0036         .arg(locale.toString(r.y()))
0037         .arg(locale.toString(r.width()))
0038         .arg(locale.toString(r.height()));
0039 }
0040 
0041 //------------
0042 
0043 KRectComposedProperty::KRectComposedProperty(KProperty *property)
0044         : KComposedPropertyInterface(property)
0045 {
0046     (void)new KProperty("x",
0047         QVariant(), QObject::tr("X", "Property: X coordinate"),
0048         QObject::tr("X Coordinate", "Property: X coordinate"), KProperty::Int, property);
0049     (void)new KProperty("y",
0050         QVariant(), QObject::tr("Y", "Property: Y coordinate"),
0051         QObject::tr("Y Coordinate", "Property: Y coordinate"), KProperty::Int, property);
0052     (void)new KProperty("width",
0053         QVariant(), QObject::tr("Width", "Property: width of rectangle"),
0054         QObject::tr("Width", "Property: width of rectangle"), KProperty::UInt, property);
0055     (void)new KProperty("height",
0056         QVariant(), QObject::tr("Height", "Property: height of rectangle"),
0057         QObject::tr("Height", "Property: height of rectangle"), KProperty::UInt, property);
0058 }
0059 
0060 void KRectComposedProperty::setValue(KProperty *property,
0061     const QVariant &value, KProperty::ValueOptions valueOptions)
0062 {
0063     const QRect r( value.toRect() );
0064     property->child("x")->setValue(r.x(), valueOptions);
0065     property->child("y")->setValue(r.y(), valueOptions);
0066     property->child("width")->setValue(r.width(), valueOptions);
0067     property->child("height")->setValue(r.height(), valueOptions);
0068 }
0069 
0070 void KRectComposedProperty::childValueChanged(KProperty *child,
0071     const QVariant &value, KProperty::ValueOptions valueOptions)
0072 {
0073     QRect r( child->parent()->value().toRect() );
0074 
0075     if (child->name() == "x")
0076         r.moveLeft(value.toInt());
0077     else if (child->name() == "y")
0078         r.moveTop(value.toInt());
0079     else if (child->name() == "width")
0080         r.setWidth(value.toInt());
0081     else if (child->name() == "height")
0082         r.setHeight(value.toInt());
0083 
0084     child->parent()->setValue(r, valueOptions);
0085 }