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 #ifdef QT_ONLY
00029 // \todo
00030 #else
00031 #include <klocale.h>
00032 #include <kdebug.h>
00033 #endif
00034 
00035 using namespace KoProperty;
00036 
00037 CustomProperty::CustomProperty(Property *parent)
00038  : m_property(parent)
00039 {
00040 }
00041 
00042 CustomProperty::~CustomProperty()
00043 {
00044 }
00045 
00047 
00048 SizeCustomProperty::SizeCustomProperty(Property *property)
00049 : CustomProperty(property)
00050 {
00051     if(property && (property->type() == Size) ) {
00052         QSize s = property->value().toSize();
00053         new Property("width", s.width(), i18n("Width"), i18n("Width"), Size_Width, property);
00054         new Property("height", s.height(), i18n("Height"), i18n("Height"), Size_Height, property);
00055     }
00056 }
00057 
00058 SizeCustomProperty::~SizeCustomProperty()
00059 {}
00060 
00061 bool
00062 SizeCustomProperty::handleValue() const
00063 {
00064     if(!m_property)
00065         return false;
00066 
00067     switch(m_property->type()) {
00068         case Size_Width: case Size_Height:
00069             return true;
00070         default:
00071             return false;
00072     }
00073 }
00074 
00075 void
00076 SizeCustomProperty::setValue(const QVariant &value, bool rememberOldValue)
00077 {
00078     if(!m_property)
00079         return;
00080 
00081     if(m_property->parent()) {
00082         QSize s = m_property->parent()->value().toSize();
00083 
00084         if(m_property->type() == Size_Height)
00085             s.setHeight(value.toInt());
00086         else if(m_property->type() == Size_Width)
00087             s.setWidth(value.toInt());
00088 
00089         m_property->parent()->setValue(s, true, false);
00090     }
00091     else{
00092         QSize s = value.toSize();
00093         m_property->child("width")->setValue(s.width(), rememberOldValue, false);
00094         m_property->child("height")->setValue(s.height(), rememberOldValue, false);
00095     }
00096 }
00097 
00098 QVariant
00099 SizeCustomProperty::value() const
00100 {
00101     if(!m_property || !m_property->parent())
00102         return QVariant();
00103 
00104     if(m_property->type() == Size_Height)
00105         return m_property->parent()->value().toSize().height();
00106     else if(m_property->type() == Size_Width)
00107         return m_property->parent()->value().toSize().width();
00108 
00109     return QVariant();
00110 }
00111 
00113 
00114 PointCustomProperty::PointCustomProperty(Property *property)
00115 : CustomProperty(property)
00116 {
00117     if(property && (property->type() == Point) ) {
00118         QPoint p = property->value().toPoint();
00119         new Property("x", p.x(), i18n("X"), i18n("X"), Point_X, property);
00120         new Property("y", p.y(), i18n("Y"), i18n("Y"), Point_Y, property);
00121     }
00122 }
00123 
00124 PointCustomProperty::~PointCustomProperty()
00125 {}
00126 
00127 bool
00128 PointCustomProperty::handleValue() const
00129 {
00130     if(!m_property)
00131         return false;
00132 
00133     switch(m_property->type()) {
00134         case Point_X: case Point_Y:
00135             return true;
00136         default:
00137             return false;
00138     }
00139 }
00140 
00141 void
00142 PointCustomProperty::setValue(const QVariant &value, bool rememberOldValue)
00143 {
00144     if(!m_property)
00145         return;
00146 
00147     if(m_property->parent()) {
00148         QPoint p = m_property->parent()->value().toPoint();
00149 
00150         if(m_property->type() == Point_X)
00151             p.setX(value.toInt());
00152         else if(m_property->type() == Point_Y)
00153             p.setY(value.toInt());
00154 
00155         m_property->parent()->setValue(p, true, false);
00156     }
00157     else {
00158         QPoint p = value.toPoint();
00159         m_property->child("x")->setValue(p.x(), rememberOldValue, false);
00160         m_property->child("y")->setValue(p.y(), rememberOldValue, false);
00161     }
00162 }
00163 
00164 QVariant
00165 PointCustomProperty::value() const
00166 {
00167     if(!m_property || !m_property->parent())
00168         return QVariant();
00169 
00170     if(m_property->type() == Point_X)
00171         return m_property->parent()->value().toPoint().x();
00172     else if(m_property->type() == Point_Y)
00173         return m_property->parent()->value().toPoint().y();
00174 
00175     return QVariant();
00176 }
00177 
00179 
00180 RectCustomProperty::RectCustomProperty(Property *property)
00181 : CustomProperty(property)
00182 {
00183     if(property && (property->type() == Rect) ) {
00184         QRect r = property->value().toRect();
00185         new Property("x", r.x(), i18n("X"), i18n("X"), Rect_X, property);
00186         new Property("y", r.y(), i18n("Y"), i18n("Y"), Rect_Y, property);
00187         new Property("width", r.width(), i18n("Width"), i18n("Width"), Rect_Width, property);
00188         new Property("height", r.height(), i18n("Height"), i18n("Height"), Rect_Height, property);
00189     }
00190 }
00191 
00192 RectCustomProperty::~RectCustomProperty()
00193 {}
00194 
00195 bool
00196 RectCustomProperty::handleValue() const
00197 {
00198     if(!m_property)
00199         return false;
00200 
00201     switch(m_property->type()) {
00202         case Rect_X: case Rect_Y: case Rect_Width: case Rect_Height:
00203             return true;
00204         default:
00205             return false;
00206     }
00207 }
00208 
00209 void
00210 RectCustomProperty::setValue(const QVariant &value, bool rememberOldValue)
00211 {
00212     if(!m_property)
00213         return;
00214 
00215     if(m_property->parent()) {
00216         QRect r = m_property->parent()->value().toRect();
00217 
00218         if(m_property->type() == Rect_X) {
00219             //changing x component of Rect shouldn't change width
00220             const int delta = value.toInt() - r.x();
00221             r.setX(value.toInt());
00222             r.setWidth(r.width()+delta);
00223         }
00224         else if(m_property->type() == Rect_Y) {
00225             //changing y component of Rect shouldn't change height
00226             const int delta = value.toInt() - r.y();
00227             r.setY(value.toInt());
00228             r.setHeight(r.height()+delta);
00229         }
00230         else if(m_property->type() == Rect_Width)
00231             r.setWidth(value.toInt());
00232         else if(m_property->type() == Rect_Height)
00233             r.setHeight(value.toInt());
00234 
00235         m_property->parent()->setValue(r, true, false);
00236     }
00237     else {
00238         QRect r = value.toRect();
00239         m_property->child("x")->setValue(r.x(), rememberOldValue, false);
00240         m_property->child("y")->setValue(r.y(), rememberOldValue, false);
00241         m_property->child("width")->setValue(r.width(), rememberOldValue, false);
00242         m_property->child("height")->setValue(r.height(), rememberOldValue, false);
00243     }
00244 }
00245 
00246 QVariant
00247 RectCustomProperty::value() const
00248 {
00249     if(!m_property || !m_property->parent())
00250         return QVariant();
00251 
00252     if(m_property->type() == Rect_X)
00253         return m_property->parent()->value().toRect().x();
00254     else if(m_property->type() == Rect_Y)
00255         return m_property->parent()->value().toRect().y();
00256     else if(m_property->type() == Rect_Width)
00257         return m_property->parent()->value().toRect().width();
00258     else if(m_property->type() == Rect_Height)
00259         return m_property->parent()->value().toRect().height();
00260 
00261     return QVariant();
00262 }
00263 
00264 
00266 
00267 SizePolicyCustomProperty::SizePolicyCustomProperty(Property *property)
00268 : CustomProperty(property)
00269 {
00270     if(property && (property->type() == SizePolicy) ) {
00271 //      QMap<QString, QVariant> spValues;
00272         QValueList<QVariant> keys;
00273         keys << QSizePolicy::Fixed
00274             << QSizePolicy::Minimum
00275             << QSizePolicy::Maximum
00276             << QSizePolicy::Preferred
00277             << QSizePolicy::Expanding
00278             << QSizePolicy::MinimumExpanding
00279             << QSizePolicy::Ignored;
00280         QStringList strings;
00281         strings << i18n("Size Policy", "Fixed")
00282             << i18n("Size Policy", "Minimum")
00283             << i18n("Size Policy", "Maximum")
00284             << i18n("Size Policy", "Preferred")
00285             << i18n("Size Policy", "Expanding")
00286             << i18n("Size Policy", "Minimum Expanding")
00287             << i18n("Size Policy", "Ignored");
00288 
00289         new Property("hSizeType", new Property::ListData(keys, strings),
00290             (int)property->value().toSizePolicy().horData(), 
00291             i18n("Horz. Size Type"),i18n("Horizontal Size Type"),
00292             SizePolicy_HorData, property);
00293         new Property("vSizeType", new Property::ListData(keys, strings),
00294             (int)property->value().toSizePolicy().verData(), 
00295             i18n("Vert. Size Type"), i18n("Vertical Size Type"),
00296             SizePolicy_VerData, property);
00297         new Property("hStretch", 
00298             property->value().toSizePolicy().horStretch(), 
00299             i18n("Horz. Stretch"), i18n("Horizontal Stretch"),
00300             SizePolicy_HorStretch, property);
00301         new Property("vStretch", 
00302             property->value().toSizePolicy().verStretch(), 
00303             i18n("Vert. Stretch"), i18n("Vertical Stretch"),
00304             SizePolicy_VerStretch, property);
00305     }
00306 }
00307 
00308 SizePolicyCustomProperty::~SizePolicyCustomProperty()
00309 {
00310 }
00311 
00312 bool
00313 SizePolicyCustomProperty::handleValue() const
00314 {
00315     if(!m_property)
00316         return false;
00317 
00318     switch(m_property->type()) {
00319         case SizePolicy_HorData:
00320         case SizePolicy_VerData:
00321         case SizePolicy_HorStretch:
00322         case SizePolicy_VerStretch:
00323             return true;
00324         default:
00325             return false;
00326     }
00327 }
00328 
00329 void
00330 SizePolicyCustomProperty::setValue(const QVariant &value, bool rememberOldValue)
00331 {
00332     if(!m_property)
00333         return;
00334 
00335     if(m_property->parent()) {
00336         QSizePolicy v = m_property->parent()->value().toSizePolicy();
00337 
00338         if(m_property->type() == SizePolicy_HorData)
00339             v.setHorData(QSizePolicy::SizeType(value.toInt()));
00340         else if(m_property->type() == SizePolicy_VerData)
00341             v.setVerData(QSizePolicy::SizeType(value.toInt()));
00342         else if(m_property->type() == SizePolicy_HorStretch)
00343             v.setHorStretch(value.toInt());
00344         else if(m_property->type() == SizePolicy_VerStretch)
00345             v.setVerStretch(value.toInt());
00346 
00347         m_property->parent()->setValue(v, true, false);
00348     }
00349     else {
00350         QSizePolicy v = value.toSizePolicy();
00351         m_property->child("hSizeType")->setValue(v.horData(), rememberOldValue, false);
00352         m_property->child("vSizeType")->setValue(v.verData(), rememberOldValue, false);
00353         m_property->child("hStretch")->setValue(v.horStretch(), rememberOldValue, false);
00354         m_property->child("vStretch")->setValue(v.verStretch(), rememberOldValue, false);
00355     }
00356 }
00357 
00358 QVariant
00359 SizePolicyCustomProperty::value() const
00360 {
00361     if(!m_property || !m_property->parent())
00362         return QVariant();
00363 
00364     if(m_property->type() == SizePolicy_HorData)
00365         return m_property->parent()->value().toSizePolicy().horData();
00366     else if(m_property->type() == SizePolicy_VerData)
00367         return m_property->parent()->value().toSizePolicy().verData();
00368     else if(m_property->type() == SizePolicy_HorStretch)
00369         return m_property->parent()->value().toSizePolicy().horStretch();
00370     else if(m_property->type() == SizePolicy_VerStretch)
00371         return m_property->parent()->value().toSizePolicy().verStretch();
00372 
00373     return QVariant();
00374 }
KDE Home | KDE Accessibility Home | Description of Access Keys