lib

customproperty.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "customproperty.h"
21#include "property.h"
22
23#include <tqsize.h>
24#include <tqrect.h>
25#include <tqsizepolicy.h>
26#include <tqpoint.h>
27
28#include <tdelocale.h>
29#include <kdebug.h>
30
31using namespace KoProperty;
32
33CustomProperty::CustomProperty(Property *parent)
34 : m_property(parent)
35{
36}
37
38CustomProperty::~CustomProperty()
39{
40}
41
42void
44{
45 m_property->emitPropertyChanged();
46}
47
49
50SizeCustomProperty::SizeCustomProperty(Property *property)
51: CustomProperty(property)
52{
53 if(property && (property->type() == Size) ) {
54 TQSize s = property->value().toSize();
55 new Property("width", s.width(), i18n("Width"), i18n("Width"), Size_Width, property);
56 new Property("height", s.height(), i18n("Height"), i18n("Height"), Size_Height, property);
57 }
58}
59
60SizeCustomProperty::~SizeCustomProperty()
61{}
62
63bool
65{
66 if(!m_property)
67 return false;
68
69 switch(m_property->type()) {
70 case Size_Width: case Size_Height:
71 return true;
72 default:
73 return false;
74 }
75}
76
77void
78SizeCustomProperty::setValue(const TQVariant &value, bool rememberOldValue)
79{
80 if(!m_property)
81 return;
82
83 if(m_property->parent()) {
84 TQSize s = m_property->parent()->value().toSize();
85
86 if(m_property->type() == Size_Height)
87 s.setHeight(value.toInt());
88 else if(m_property->type() == Size_Width)
89 s.setWidth(value.toInt());
90
91 m_property->parent()->setValue(s, true, false);
92 }
93 else{
94 TQSize s = value.toSize();
95 m_property->child("width")->setValue(s.width(), rememberOldValue, false);
96 m_property->child("height")->setValue(s.height(), rememberOldValue, false);
97 }
98}
99
100TQVariant
102{
103 if(!m_property || !m_property->parent())
104 return TQVariant();
105
106 if(m_property->type() == Size_Height)
107 return m_property->parent()->value().toSize().height();
108 else if(m_property->type() == Size_Width)
109 return m_property->parent()->value().toSize().width();
110
111 return TQVariant();
112}
113
115
116PointCustomProperty::PointCustomProperty(Property *property)
117: CustomProperty(property)
118{
119 if(property && (property->type() == Point) ) {
120 TQPoint p = property->value().toPoint();
121 new Property("x", p.x(), i18n("X"), i18n("X"), Point_X, property);
122 new Property("y", p.y(), i18n("Y"), i18n("Y"), Point_Y, property);
123 }
124}
125
126PointCustomProperty::~PointCustomProperty()
127{}
128
129bool
131{
132 if(!m_property)
133 return false;
134
135 switch(m_property->type()) {
136 case Point_X: case Point_Y:
137 return true;
138 default:
139 return false;
140 }
141}
142
143void
144PointCustomProperty::setValue(const TQVariant &value, bool rememberOldValue)
145{
146 if(!m_property)
147 return;
148
149 if(m_property->parent()) {
150 TQPoint p = m_property->parent()->value().toPoint();
151
152 if(m_property->type() == Point_X)
153 p.setX(value.toInt());
154 else if(m_property->type() == Point_Y)
155 p.setY(value.toInt());
156
157 m_property->parent()->setValue(p, true, false);
158 }
159 else {
160 TQPoint p = value.toPoint();
161 m_property->child("x")->setValue(p.x(), rememberOldValue, false);
162 m_property->child("y")->setValue(p.y(), rememberOldValue, false);
163 }
164}
165
166TQVariant
168{
169 if(!m_property || !m_property->parent())
170 return TQVariant();
171
172 if(m_property->type() == Point_X)
173 return m_property->parent()->value().toPoint().x();
174 else if(m_property->type() == Point_Y)
175 return m_property->parent()->value().toPoint().y();
176
177 return TQVariant();
178}
179
181
182RectCustomProperty::RectCustomProperty(Property *property)
183: CustomProperty(property)
184{
185 if(property && (property->type() == Rect) ) {
186 TQRect r = property->value().toRect();
187 new Property("x", r.x(), i18n("X"), i18n("X"), Rect_X, property);
188 new Property("y", r.y(), i18n("Y"), i18n("Y"), Rect_Y, property);
189 new Property("width", r.width(), i18n("Width"), i18n("Width"), Rect_Width, property);
190 new Property("height", r.height(), i18n("Height"), i18n("Height"), Rect_Height, property);
191 }
192}
193
194RectCustomProperty::~RectCustomProperty()
195{}
196
197bool
199{
200 if(!m_property)
201 return false;
202
203 switch(m_property->type()) {
204 case Rect_X: case Rect_Y: case Rect_Width: case Rect_Height:
205 return true;
206 default:
207 return false;
208 }
209}
210
211void
212RectCustomProperty::setValue(const TQVariant &value, bool rememberOldValue)
213{
214 if(!m_property)
215 return;
216
217 if(m_property->parent()) {
218 TQRect r = m_property->parent()->value().toRect();
219
220 if(m_property->type() == Rect_X) {
221 //changing x component of Rect shouldn't change width
222 const int delta = value.toInt() - r.x();
223 r.setX(value.toInt());
224 r.setWidth(r.width()+delta);
225 }
226 else if(m_property->type() == Rect_Y) {
227 //changing y component of Rect shouldn't change height
228 const int delta = value.toInt() - r.y();
229 r.setY(value.toInt());
230 r.setHeight(r.height()+delta);
231 }
232 else if(m_property->type() == Rect_Width)
233 r.setWidth(value.toInt());
234 else if(m_property->type() == Rect_Height)
235 r.setHeight(value.toInt());
236
237 m_property->parent()->setValue(r, true, false);
238 }
239 else {
240 TQRect r = value.toRect();
241 m_property->child("x")->setValue(r.x(), rememberOldValue, false);
242 m_property->child("y")->setValue(r.y(), rememberOldValue, false);
243 m_property->child("width")->setValue(r.width(), rememberOldValue, false);
244 m_property->child("height")->setValue(r.height(), rememberOldValue, false);
245 }
246}
247
248TQVariant
250{
251 if(!m_property || !m_property->parent())
252 return TQVariant();
253
254 if(m_property->type() == Rect_X)
255 return m_property->parent()->value().toRect().x();
256 else if(m_property->type() == Rect_Y)
257 return m_property->parent()->value().toRect().y();
258 else if(m_property->type() == Rect_Width)
259 return m_property->parent()->value().toRect().width();
260 else if(m_property->type() == Rect_Height)
261 return m_property->parent()->value().toRect().height();
262
263 return TQVariant();
264}
265
266
268
269SizePolicyCustomProperty::SizePolicyCustomProperty(Property *property)
270: CustomProperty(property)
271{
272 if(property && (property->type() == SizePolicy) ) {
273// TQStringVariantMap spValues;
274 TQValueList<TQVariant> keys;
275 keys << TQSizePolicy::Fixed
276 << TQSizePolicy::Minimum
277 << TQSizePolicy::Maximum
278 << TQSizePolicy::Preferred
279 << TQSizePolicy::Expanding
280 << TQSizePolicy::MinimumExpanding
281 << TQSizePolicy::Ignored;
282 TQStringList strings;
283 strings << i18n("Size Policy", "Fixed")
284 << i18n("Size Policy", "Minimum")
285 << i18n("Size Policy", "Maximum")
286 << i18n("Size Policy", "Preferred")
287 << i18n("Size Policy", "Expanding")
288 << i18n("Size Policy", "Minimum Expanding")
289 << i18n("Size Policy", "Ignored");
290
291 new Property("hSizeType", new Property::ListData(keys, strings),
292 (int)property->value().toSizePolicy().horData(),
293 i18n("Horz. Size Type"),i18n("Horizontal Size Type"),
294 SizePolicy_HorData, property);
295 new Property("vSizeType", new Property::ListData(keys, strings),
296 (int)property->value().toSizePolicy().verData(),
297 i18n("Vert. Size Type"), i18n("Vertical Size Type"),
298 SizePolicy_VerData, property);
299 new Property("hStretch",
300 property->value().toSizePolicy().horStretch(),
301 i18n("Horz. Stretch"), i18n("Horizontal Stretch"),
302 SizePolicy_HorStretch, property);
303 new Property("vStretch",
304 property->value().toSizePolicy().verStretch(),
305 i18n("Vert. Stretch"), i18n("Vertical Stretch"),
306 SizePolicy_VerStretch, property);
307 }
308}
309
310SizePolicyCustomProperty::~SizePolicyCustomProperty()
311{
312}
313
314bool
316{
317 if(!m_property)
318 return false;
319
320 switch(m_property->type()) {
321 case SizePolicy_HorData:
322 case SizePolicy_VerData:
323 case SizePolicy_HorStretch:
324 case SizePolicy_VerStretch:
325 return true;
326 default:
327 return false;
328 }
329}
330
331void
332SizePolicyCustomProperty::setValue(const TQVariant &value, bool rememberOldValue)
333{
334 if(!m_property)
335 return;
336
337 if(m_property->parent()) {
338 TQSizePolicy v = m_property->parent()->value().toSizePolicy();
339
340 if(m_property->type() == SizePolicy_HorData)
341 v.setHorData(TQSizePolicy::SizeType(value.toInt()));
342 else if(m_property->type() == SizePolicy_VerData)
343 v.setVerData(TQSizePolicy::SizeType(value.toInt()));
344 else if(m_property->type() == SizePolicy_HorStretch)
345 v.setHorStretch(value.toInt());
346 else if(m_property->type() == SizePolicy_VerStretch)
347 v.setVerStretch(value.toInt());
348
349 m_property->parent()->setValue(v, true, false);
350 }
351 else {
352 TQSizePolicy v = value.toSizePolicy();
353 m_property->child("hSizeType")->setValue(v.horData(), rememberOldValue, false);
354 m_property->child("vSizeType")->setValue(v.verData(), rememberOldValue, false);
355 m_property->child("hStretch")->setValue(v.horStretch(), rememberOldValue, false);
356 m_property->child("vStretch")->setValue(v.verStretch(), rememberOldValue, false);
357 }
358}
359
360TQVariant
362{
363 if(!m_property || !m_property->parent())
364 return TQVariant();
365
366 if(m_property->type() == SizePolicy_HorData)
367 return m_property->parent()->value().toSizePolicy().horData();
368 else if(m_property->type() == SizePolicy_VerData)
369 return m_property->parent()->value().toSizePolicy().verData();
370 else if(m_property->type() == SizePolicy_HorStretch)
371 return m_property->parent()->value().toSizePolicy().horStretch();
372 else if(m_property->type() == SizePolicy_VerStretch)
373 return m_property->parent()->value().toSizePolicy().verStretch();
374
375 return TQVariant();
376}
Base class for custom properties.
void setValue(const TQVariant &value, bool rememberOldValue)
The base class representing a single property.
Definition property.h:177
void setValue(const TQVariant &value, bool rememberOldValue)
void setValue(const TQVariant &value, bool rememberOldValue)
void setValue(const TQVariant &value, bool rememberOldValue)
Namespace for a set of classes implementing generic properties framework.
@ Point
point (x,y)
Definition property.h:82
@ Rect
rectangle (x,y, width, height)
Definition property.h:76
@ Size
size (width, height)
Definition property.h:77
@ SizePolicy
size policy (horizontal, vertical)
Definition property.h:93