lib

customproperty.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "customproperty.h"
00021 #include "property.h"
00022 
00023 #include <qsize.h>
00024 #include <qrect.h>
00025 #include <qsizepolicy.h>
00026 #include <qpoint.h>
00027 
00028 #include <klocale.h>
00029 #include <kdebug.h>
00030 
00031 using namespace KoProperty;
00032 
00033 CustomProperty::CustomProperty(Property *parent)
00034  : m_property(parent)
00035 {
00036 }
00037 
00038 CustomProperty::~CustomProperty()
00039 {
00040 }
00041 
00042 void
00043 CustomProperty::emitPropertyChanged()
00044 {
00045     m_property->emitPropertyChanged();
00046 }
00047 
00049 
00050 SizeCustomProperty::SizeCustomProperty(Property *property)
00051 : CustomProperty(property)
00052 {
00053     if(property && (property->type() == Size) ) {
00054         QSize s = property->value().toSize();
00055         new Property("width", s.width(), i18n("Width"), i18n("Width"), Size_Width, property);
00056         new Property("height", s.height(), i18n("Height"), i18n("Height"), Size_Height, property);
00057     }
00058 }
00059 
00060 SizeCustomProperty::~SizeCustomProperty()
00061 {}
00062 
00063 bool
00064 SizeCustomProperty::handleValue() const
00065 {
00066     if(!m_property)
00067         return false;
00068 
00069     switch(m_property->type()) {
00070         case Size_Width: case Size_Height:
00071             return true;
00072         default:
00073             return false;
00074     }
00075 }
00076 
00077 void
00078 SizeCustomProperty::setValue(const QVariant &value, bool rememberOldValue)
00079 {
00080     if(!m_property)
00081         return;
00082 
00083     if(m_property->parent()) {
00084         QSize s = m_property->parent()->value().toSize();
00085 
00086         if(m_property->type() == Size_Height)
00087             s.setHeight(value.toInt());
00088         else if(m_property->type() == Size_Width)
00089             s.setWidth(value.toInt());
00090 
00091         m_property->parent()->setValue(s, true, false);
00092     }
00093     else{
00094         QSize s = value.toSize();
00095         m_property->child("width")->setValue(s.width(), rememberOldValue, false);
00096         m_property->child("height")->setValue(s.height(), rememberOldValue, false);
00097     }
00098 }
00099 
00100 QVariant
00101 SizeCustomProperty::value() const
00102 {
00103     if(!m_property || !m_property->parent())
00104         return QVariant();
00105 
00106     if(m_property->type() == Size_Height)
00107         return m_property->parent()->value().toSize().height();
00108     else if(m_property->type() == Size_Width)
00109         return m_property->parent()->value().toSize().width();
00110 
00111     return QVariant();
00112 }
00113 
00115 
00116 PointCustomProperty::PointCustomProperty(Property *property)
00117 : CustomProperty(property)
00118 {
00119     if(property && (property->type() == Point) ) {
00120         QPoint p = property->value().toPoint();
00121         new Property("x", p.x(), i18n("X"), i18n("X"), Point_X, property);
00122         new Property("y", p.y(), i18n("Y"), i18n("Y"), Point_Y, property);
00123     }
00124 }
00125 
00126 PointCustomProperty::~PointCustomProperty()
00127 {}
00128 
00129 bool
00130 PointCustomProperty::handleValue() const
00131 {
00132     if(!m_property)
00133         return false;
00134 
00135     switch(m_property->type()) {
00136         case Point_X: case Point_Y:
00137             return true;
00138         default:
00139             return false;
00140     }
00141 }
00142 
00143 void
00144 PointCustomProperty::setValue(const QVariant &value, bool rememberOldValue)
00145 {
00146     if(!m_property)
00147         return;
00148 
00149     if(m_property->parent()) {
00150         QPoint p = m_property->parent()->value().toPoint();
00151 
00152         if(m_property->type() == Point_X)
00153             p.setX(value.toInt());
00154         else if(m_property->type() == Point_Y)
00155             p.setY(value.toInt());
00156 
00157         m_property->parent()->setValue(p, true, false);
00158     }
00159     else {
00160         QPoint p = value.toPoint();
00161         m_property->child("x")->setValue(p.x(), rememberOldValue, false);
00162         m_property->child("y")->setValue(p.y(), rememberOldValue, false);
00163     }
00164 }
00165 
00166 QVariant
00167 PointCustomProperty::value() const
00168 {
00169     if(!m_property || !m_property->parent())
00170         return QVariant();
00171 
00172     if(m_property->type() == Point_X)
00173         return m_property->parent()->value().toPoint().x();
00174     else if(m_property->type() == Point_Y)
00175         return m_property->parent()->value().toPoint().y();
00176 
00177     return QVariant();
00178 }
00179 
00181 
00182 RectCustomProperty::RectCustomProperty(Property *property)
00183 : CustomProperty(property)
00184 {
00185     if(property && (property->type() == Rect) ) {
00186         QRect r = property->value().toRect();
00187         new Property("x", r.x(), i18n("X"), i18n("X"), Rect_X, property);
00188         new Property("y", r.y(), i18n("Y"), i18n("Y"), Rect_Y, property);
00189         new Property("width", r.width(), i18n("Width"), i18n("Width"), Rect_Width, property);
00190         new Property("height", r.height(), i18n("Height"), i18n("Height"), Rect_Height, property);
00191     }
00192 }
00193 
00194 RectCustomProperty::~RectCustomProperty()
00195 {}
00196 
00197 bool
00198 RectCustomProperty::handleValue() const
00199 {
00200     if(!m_property)
00201         return false;
00202 
00203     switch(m_property->type()) {
00204         case Rect_X: case Rect_Y: case Rect_Width: case Rect_Height:
00205             return true;
00206         default:
00207             return false;
00208     }
00209 }
00210 
00211 void
00212 RectCustomProperty::setValue(const QVariant &value, bool rememberOldValue)
00213 {
00214     if(!m_property)
00215         return;
00216 
00217     if(m_property->parent()) {
00218         QRect r = m_property->parent()->value().toRect();
00219 
00220         if(m_property->type() == Rect_X) {
00221             //changing x component of Rect shouldn't change width
00222             const int delta = value.toInt() - r.x();
00223             r.setX(value.toInt());
00224             r.setWidth(r.width()+delta);
00225         }
00226         else if(m_property->type() == Rect_Y) {
00227             //changing y component of Rect shouldn't change height
00228             const int delta = value.toInt() - r.y();
00229             r.setY(value.toInt());
00230             r.setHeight(r.height()+delta);
00231         }
00232         else if(m_property->type() == Rect_Width)
00233             r.setWidth(value.toInt());
00234         else if(m_property->type() == Rect_Height)
00235             r.setHeight(value.toInt());
00236 
00237         m_property->parent()->setValue(r, true, false);
00238     }
00239     else {
00240         QRect r = value.toRect();
00241         m_property->child("x")->setValue(r.x(), rememberOldValue, false);
00242         m_property->child("y")->setValue(r.y(), rememberOldValue, false);
00243         m_property->child("width")->setValue(r.width(), rememberOldValue, false);
00244         m_property->child("height")->setValue(r.height(), rememberOldValue, false);
00245     }
00246 }
00247 
00248 QVariant
00249 RectCustomProperty::value() const
00250 {
00251     if(!m_property || !m_property->parent())
00252         return QVariant();
00253 
00254     if(m_property->type() == Rect_X)
00255         return m_property->parent()->value().toRect().x();
00256     else if(m_property->type() == Rect_Y)
00257         return m_property->parent()->value().toRect().y();
00258     else if(m_property->type() == Rect_Width)
00259         return m_property->parent()->value().toRect().width();
00260     else if(m_property->type() == Rect_Height)
00261         return m_property->parent()->value().toRect().height();
00262 
00263     return QVariant();
00264 }
00265 
00266 
00268 
00269 SizePolicyCustomProperty::SizePolicyCustomProperty(Property *property)
00270 : CustomProperty(property)
00271 {
00272     if(property && (property->type() == SizePolicy) ) {
00273 //      QMap<QString, QVariant> spValues;
00274         QValueList<QVariant> keys;
00275         keys << QSizePolicy::Fixed
00276             << QSizePolicy::Minimum
00277             << QSizePolicy::Maximum
00278             << QSizePolicy::Preferred
00279             << QSizePolicy::Expanding
00280             << QSizePolicy::MinimumExpanding
00281             << QSizePolicy::Ignored;
00282         QStringList strings;
00283         strings << i18n("Size Policy", "Fixed")
00284             << i18n("Size Policy", "Minimum")
00285             << i18n("Size Policy", "Maximum")
00286             << i18n("Size Policy", "Preferred")
00287             << i18n("Size Policy", "Expanding")
00288             << i18n("Size Policy", "Minimum Expanding")
00289             << i18n("Size Policy", "Ignored");
00290 
00291         new Property("hSizeType", new Property::ListData(keys, strings),
00292             (int)property->value().toSizePolicy().horData(), 
00293             i18n("Horz. Size Type"),i18n("Horizontal Size Type"),
00294             SizePolicy_HorData, property);
00295         new Property("vSizeType", new Property::ListData(keys, strings),
00296             (int)property->value().toSizePolicy().verData(), 
00297             i18n("Vert. Size Type"), i18n("Vertical Size Type"),
00298             SizePolicy_VerData, property);
00299         new Property("hStretch", 
00300             property->value().toSizePolicy().horStretch(), 
00301             i18n("Horz. Stretch"), i18n("Horizontal Stretch"),
00302             SizePolicy_HorStretch, property);
00303         new Property("vStretch", 
00304             property->value().toSizePolicy().verStretch(), 
00305             i18n("Vert. Stretch"), i18n("Vertical Stretch"),
00306             SizePolicy_VerStretch, property);
00307     }
00308 }
00309 
00310 SizePolicyCustomProperty::~SizePolicyCustomProperty()
00311 {
00312 }
00313 
00314 bool
00315 SizePolicyCustomProperty::handleValue() const
00316 {
00317     if(!m_property)
00318         return false;
00319 
00320     switch(m_property->type()) {
00321         case SizePolicy_HorData:
00322         case SizePolicy_VerData:
00323         case SizePolicy_HorStretch:
00324         case SizePolicy_VerStretch:
00325             return true;
00326         default:
00327             return false;
00328     }
00329 }
00330 
00331 void
00332 SizePolicyCustomProperty::setValue(const QVariant &value, bool rememberOldValue)
00333 {
00334     if(!m_property)
00335         return;
00336 
00337     if(m_property->parent()) {
00338         QSizePolicy v = m_property->parent()->value().toSizePolicy();
00339 
00340         if(m_property->type() == SizePolicy_HorData)
00341             v.setHorData(QSizePolicy::SizeType(value.toInt()));
00342         else if(m_property->type() == SizePolicy_VerData)
00343             v.setVerData(QSizePolicy::SizeType(value.toInt()));
00344         else if(m_property->type() == SizePolicy_HorStretch)
00345             v.setHorStretch(value.toInt());
00346         else if(m_property->type() == SizePolicy_VerStretch)
00347             v.setVerStretch(value.toInt());
00348 
00349         m_property->parent()->setValue(v, true, false);
00350     }
00351     else {
00352         QSizePolicy v = value.toSizePolicy();
00353         m_property->child("hSizeType")->setValue(v.horData(), rememberOldValue, false);
00354         m_property->child("vSizeType")->setValue(v.verData(), rememberOldValue, false);
00355         m_property->child("hStretch")->setValue(v.horStretch(), rememberOldValue, false);
00356         m_property->child("vStretch")->setValue(v.verStretch(), rememberOldValue, false);
00357     }
00358 }
00359 
00360 QVariant
00361 SizePolicyCustomProperty::value() const
00362 {
00363     if(!m_property || !m_property->parent())
00364         return QVariant();
00365 
00366     if(m_property->type() == SizePolicy_HorData)
00367         return m_property->parent()->value().toSizePolicy().horData();
00368     else if(m_property->type() == SizePolicy_VerData)
00369         return m_property->parent()->value().toSizePolicy().verData();
00370     else if(m_property->type() == SizePolicy_HorStretch)
00371         return m_property->parent()->value().toSizePolicy().horStretch();
00372     else if(m_property->type() == SizePolicy_VerStretch)
00373         return m_property->parent()->value().toSizePolicy().verStretch();
00374 
00375     return QVariant();
00376 }
KDE Home | KDE Accessibility Home | Description of Access Keys