libs/flake

KoCanvasResourceProvider.cpp

Go to the documentation of this file.
00001 /*
00002    Copyright (c) 2006 Boudewijn Rempt (boud@valdyas.org)
00003    Copyright (C) 2007 Thomas Zander <zander@kde.org>
00004    Copyright (c) 2008 Carlos Licea <carlos.licea@kdemail.net>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019    Boston, MA 02110-1301, USA.
00020  */
00021 #include "KoCanvasResourceProvider.h"
00022 
00023 #include <QVariant>
00024 
00025 #include "KoShape.h"
00026 #include "KoLineBorder.h"
00027 
00028 class KoCanvasResourceProvider::Private
00029 {
00030 public:
00031     QHash<int, QVariant> resources;
00032 };
00033 
00034 KoCanvasResourceProvider::KoCanvasResourceProvider(QObject *parent)
00035         : QObject(parent),
00036         d(new Private())
00037 {
00038     setGrabSensitivity(3);
00039 }
00040 
00041 KoCanvasResourceProvider::~KoCanvasResourceProvider()
00042 {
00043     delete d;
00044 }
00045 
00046 void KoCanvasResourceProvider::setResource(int key, const QVariant &value)
00047 {
00048     if (d->resources.contains(key)) {
00049         if (d->resources.value(key) == value)
00050             return;
00051         d->resources[key] = value;
00052     } else {
00053         d->resources.insert(key, value);
00054     }
00055     emit resourceChanged(key, value);
00056 }
00057 
00058 QVariant KoCanvasResourceProvider::resource(int key)
00059 {
00060     if (!d->resources.contains(key)) {
00061         QVariant empty;
00062         return empty;
00063     } else
00064         return d->resources.value(key);
00065 }
00066 
00067 void KoCanvasResourceProvider::setResource(int key, const KoColor &color)
00068 {
00069     QVariant v;
00070     v.setValue(color);
00071     setResource(key, v);
00072 }
00073 
00074 void KoCanvasResourceProvider::setResource(int key, const KoID &id)
00075 {
00076     QVariant v;
00077     v.setValue(id);
00078     setResource(key, v);
00079 }
00080 
00081 void KoCanvasResourceProvider::setResource(int key, KoShape *shape)
00082 {
00083     QVariant v;
00084     v.setValue(shape);
00085     setResource(key, v);
00086 }
00087 
00088 KoColor KoCanvasResourceProvider::koColorResource(int key)
00089 {
00090     if (! d->resources.contains(key)) {
00091         KoColor empty;
00092         return empty;
00093     }
00094     return resource(key).value<KoColor>();
00095 }
00096 
00097 
00098 void KoCanvasResourceProvider::setForegroundColor(const KoColor &color)
00099 {
00100     setResource(KoCanvasResource::ForegroundColor, color);
00101 }
00102 
00103 KoColor KoCanvasResourceProvider::foregroundColor()
00104 {
00105     return koColorResource(KoCanvasResource::ForegroundColor);
00106 }
00107 
00108 
00109 void KoCanvasResourceProvider::setBackgroundColor(const KoColor &color)
00110 {
00111     setResource(KoCanvasResource::BackgroundColor, color);
00112 }
00113 
00114 KoColor KoCanvasResourceProvider::backgroundColor()
00115 {
00116     return koColorResource(KoCanvasResource::BackgroundColor);
00117 }
00118 
00119 KoID KoCanvasResourceProvider::koIDResource(int key)
00120 {
00121     return resource(key).value<KoID>();
00122 }
00123 
00124 KoShape * KoCanvasResourceProvider::koShapeResource(int key)
00125 {
00126     if (! d->resources.contains(key))
00127         return 0;
00128 
00129     return resource(key).value<KoShape *>();
00130 }
00131 
00132 void KoCanvasResourceProvider::setHandleRadius(int handleRadius)
00133 {
00134     // do not allow arbitrary small handles
00135     if (handleRadius < 3)
00136         handleRadius = 3;
00137     setResource(KoCanvasResource::HandleRadius, QVariant(handleRadius));
00138 }
00139 
00140 int KoCanvasResourceProvider::handleRadius()
00141 {
00142     if (d->resources.contains(KoCanvasResource::HandleRadius))
00143         return d->resources.value(KoCanvasResource::HandleRadius).toInt();
00144     return 3; // default value.
00145 }
00146 
00147 void KoCanvasResourceProvider::setGrabSensitivity(uint grabSensitivity)
00148 {
00149     // do not allow arbitrary small handles
00150     if (grabSensitivity < 1)
00151         grabSensitivity = 1;
00152     setResource(KoCanvasResource::GrabSensitivity, QVariant(grabSensitivity));
00153 }
00154 
00155 uint KoCanvasResourceProvider::grabSensitivity()
00156 {
00157     return resource(KoCanvasResource::GrabSensitivity).toUInt();
00158 }
00159 
00160 void KoCanvasResourceProvider::setActiveBorder( const KoLineBorder &border )
00161 {
00162     QVariant v;
00163     v.setValue(border);
00164     setResource(KoCanvasResource::ActiveBorder, v);
00165 }
00166 
00167 KoLineBorder KoCanvasResourceProvider::activeBorder()
00168 {
00169     if (! d->resources.contains(KoCanvasResource::ActiveBorder)) {
00170         KoLineBorder empty;
00171         return empty;
00172     }
00173     return resource(KoCanvasResource::ActiveBorder).value<KoLineBorder>();
00174 }
00175 
00176 void KoCanvasResourceProvider::setUnitChanged()
00177 {
00178     setResource(KoCanvasResource::Unit, true);
00179 }
00180 
00181 bool KoCanvasResourceProvider::boolResource(int key) const
00182 {
00183     if (! d->resources.contains(key))
00184         return false;
00185     return d->resources[key].toBool();
00186 }
00187 
00188 int KoCanvasResourceProvider::intResource(int key) const
00189 {
00190     if (! d->resources.contains(key))
00191         return 0;
00192     return d->resources[key].toInt();
00193 }
00194 
00195 qreal KoCanvasResourceProvider::doubleResource(int key) const
00196 {
00197     if (! d->resources.contains(key))
00198         return 0.;
00199     return d->resources[key].toDouble();
00200 }
00201 
00202 QString KoCanvasResourceProvider::stringResource(int key)
00203 {
00204     if (! d->resources.contains(key)) {
00205         QString empty;
00206         return empty;
00207     }
00208     return qvariant_cast<QString>(resource(key));
00209 }
00210 
00211 QSizeF KoCanvasResourceProvider::sizeResource(int key)
00212 {
00213     if (! d->resources.contains(key)) {
00214         QSizeF empty;
00215         return empty;
00216     }
00217     return qvariant_cast<QSizeF>(resource(key));
00218 }
00219 
00220 bool KoCanvasResourceProvider::hasResource(int key)
00221 {
00222     return d->resources.contains(key);
00223 }
00224 
00225 void KoCanvasResourceProvider::clearResource(int key)
00226 {
00227     if (! d->resources.contains(key))
00228         return;
00229     d->resources.remove(key);
00230     QVariant empty;
00231     emit resourceChanged(key, empty);
00232 }
00233 
00234 #include "KoCanvasResourceProvider.moc"