KProperty

KPropertyFactory.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2008-2015 Jarosław Staniek <staniek@kde.org>
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
21#include "KPropertyFactory.h"
22
23//---------------
24
25//! @internal
26class Q_DECL_HIDDEN KPropertyFactoryManager::Private
27{
28public:
29 Private()
30 {
31 }
32 ~Private()
33 {
34 qDeleteAll(factories);
35 }
36
40};
41
42Q_GLOBAL_STATIC(KPropertyFactoryManager, _self)
43
44//! @internal
45class Q_DECL_HIDDEN KPropertyFactory::Private
46{
47public:
48 Private()
49 {
50 }
51 ~Private()
52 {
53 qDeleteAll(valueDisplaysSet);
54 }
55
57 QSet<KComposedPropertyCreatorInterface*> composedPropertyCreatorsSet;
60};
61
62typedef QList<void (*)()> InitFunctions;
63//! @internal Used by KPropertyFactoryManager::addInitFunction()
64Q_GLOBAL_STATIC(InitFunctions, _initFunctions)
65
66KPropertyFactory::KPropertyFactory()
67 : d( new Private )
68{
69}
70
71KPropertyFactory::~KPropertyFactory()
72{
73 delete d;
74}
75
76QHash<int, KComposedPropertyCreatorInterface*> KPropertyFactory::composedPropertyCreators() const
77{
78 return d->composedPropertyCreators;
79}
80
81QHash<int, KPropertyValueDisplayInterface*> KPropertyFactory::valueDisplays() const
82{
83 return d->valueDisplays;
84}
85
86void KPropertyFactory::addComposedPropertyCreator( int type, KComposedPropertyCreatorInterface* creator )
87{
88 addComposedPropertyCreatorInternal( type, creator, true );
89}
90
91void KPropertyFactory::addComposedPropertyCreatorInternal(int type, KComposedPropertyCreatorInterface* creator, bool own)
92{
93 if (own)
94 d->composedPropertyCreatorsSet.insert(creator);
95 d->composedPropertyCreators.insert(type, creator);
96}
97
98void KPropertyFactory::addDisplay(int type, KPropertyValueDisplayInterface *display)
99{
100 addDisplayInternal(type, display, true);
101 if (dynamic_cast<KComposedPropertyCreatorInterface*>(display)) {
102 addComposedPropertyCreatorInternal( type,
103 dynamic_cast<KComposedPropertyCreatorInterface*>(display), false/* !own*/ );
104 }
105 if (dynamic_cast<KPropertyValueDisplayInterface*>(display)) {
106 addDisplayInternal( type, dynamic_cast<KPropertyValueDisplayInterface*>(display), false/* !own*/ );
107 }
108}
109
110void KPropertyFactory::addDisplayInternal(int type, KPropertyValueDisplayInterface *display, bool own)
111{
112 if (own) {
113 d->valueDisplaysSet.insert(display);
114 }
115 d->valueDisplays.insert(type, display);
116}
117
118//------------
119
120class Q_DECL_HIDDEN KPropertyValueDisplayInterface::Private
121{
122public:
123 Private() {}
124};
125
126KPropertyValueDisplayInterface::KPropertyValueDisplayInterface()
127 : d(new Private)
128{
129}
130
131KPropertyValueDisplayInterface::~KPropertyValueDisplayInterface()
132{
133 delete d;
134}
135
136//static
141
142//static
152
153//------------
154
155KPropertyFactoryManager::KPropertyFactoryManager()
156 : QObject(nullptr)
157 , d(new Private)
158{
159 setObjectName(QLatin1String("KPropertyFactoryManager"));
160}
161
162KPropertyFactoryManager::~KPropertyFactoryManager()
163{
164 delete d;
165}
166
167KPropertyFactoryManager* KPropertyFactoryManager::self()
168{
169 if (_self.exists()) { // avoid recursion: initFunctions below may call self()
170 return _self;
171 }
172 _self(); // KPropertyFactoryManager should exist as initFunctions may need it
173 foreach(void (*initFunction)(), *_initFunctions) {
174 initFunction();
175 }
176 _initFunctions->clear();
177 return _self;
178}
179
180void KPropertyFactoryManager::registerFactory(KPropertyFactory *factory)
181{
182 d->factories.insert(factory);
184 = factory->composedPropertyCreators().constEnd();
185 for (QHash<int, KComposedPropertyCreatorInterface*>::ConstIterator it( factory->composedPropertyCreators().constBegin() );
186 it != composedPropertyCreatorsItEnd; ++it)
187 {
188 d->composedPropertyCreators.insert(it.key(), it.value());
189 }
191 = factory->valueDisplays().constEnd();
193 it != valueDisplaysItEnd; ++it)
194 {
195 d->valueDisplays.insert(it.key(), it.value());
196 }
197}
198
199KComposedPropertyInterface* KPropertyFactoryManager::createComposedProperty(KProperty *parent)
200{
201 const KComposedPropertyCreatorInterface *creator = d->composedPropertyCreators.value( parent->type() );
202 return creator ? creator->createComposedProperty(parent) : nullptr;
203}
204
205//static
206void KPropertyFactoryManager::addInitFunction(void (*initFunction)())
207{
208 _initFunctions->append(initFunction);
209}
210
211bool KPropertyFactoryManager::canConvertValueToText(int type) const
212{
213 return d->valueDisplays.value(type) != nullptr;
214}
215
216bool KPropertyFactoryManager::canConvertValueToText(const KProperty* property) const
217{
218 return canConvertValueToText(property->type());
219}
220
221QString KPropertyFactoryManager::propertyValueToString(const KProperty* property) const
222{
223 const KPropertyValueDisplayInterface *display = d->valueDisplays.value(property->type());
224 return display ? display->propertyValueToString(property, QLocale::c()) : property->value().toString();
225}
226
227QString KPropertyFactoryManager::valueToString(int type, const QVariant &value) const
228{
229 const KPropertyValueDisplayInterface *display = d->valueDisplays.value(type);
230 return display ? display->valueToString(value, QLocale::c()) : value.toString();
231}
232
233QString KPropertyFactoryManager::propertyValueToLocalizedString(const KProperty* property) const
234{
235 const KPropertyValueDisplayInterface *display = d->valueDisplays.value(property->type());
236 return display ? display->propertyValueToString(property, QLocale()) : KPropertyValueDisplayInterface::valueToLocalizedString(property->value());
237}
238
239QString KPropertyFactoryManager::valueToLocalizedString(int type, const QVariant &value) const
240{
241 const KPropertyValueDisplayInterface *display = d->valueDisplays.value(type);
242 return display ? display->valueToString(value, QLocale()) : KPropertyValueDisplayInterface::valueToLocalizedString(value.toString());
243}
244
245//! @todo
246#if 0
247 const int type = parent->type();
248/*
249 CustomPropertyFactory *factory = d->registeredWidgets[type];
250 if (factory)
251 return factory->createCustomProperty(parent);
252*/
253 switch (type) {
254 case Size:
255 case Size_Width:
256 case Size_Height:
257 return new SizeCustomProperty(parent);
258 case Point:
259 case Point_X:
260 case Point_Y:
261 return new PointCustomProperty(parent);
262 case Rect:
263 case Rect_X:
264 case Rect_Y:
265 case Rect_Width:
266 case Rect_Height:
267 return new RectCustomProperty(parent);
268 case SizePolicy:
269/* case SizePolicy_HorizontalStretch:
270 case SizePolicy_VerticalStretch:
271 case SizePolicy_HorizontalPolicy:
272 case SizePolicy_VerticalPolicy:*/
273 return new SizePolicyCustomProperty(parent);
274 default:;
275 }
276 return 0;
277#endif
278
279class Q_DECL_HIDDEN KComposedPropertyInterface::Private
280{
281public:
282 Private() {}
283 bool childValueChangedEnabled = true;
284};
285
286KComposedPropertyInterface::KComposedPropertyInterface(KProperty *parent)
287 : d(new Private)
288{
289 Q_UNUSED(parent)
290}
291
292KComposedPropertyInterface::~KComposedPropertyInterface()
293{
294 delete d;
295}
296
297void KComposedPropertyInterface::childValueChangedInternal(KProperty *child, const QVariant &value,
298 KProperty::ValueOptions valueOptions)
299{
300 if (d->childValueChangedEnabled) {
301 childValueChanged(child, value, valueOptions);
302 }
303}
304
305void KComposedPropertyInterface::setChildValueChangedEnabled(bool set)
306{
307 d->childValueChangedEnabled = set;
308}
309
310bool KComposedPropertyInterface::childValueChangedEnabled() const
311{
312 return d->childValueChangedEnabled;
313}
314
315//---------------
316
317class Q_DECL_HIDDEN KComposedPropertyCreatorInterface::Private
318{
319public:
320 Private() {}
321};
322
323KComposedPropertyCreatorInterface::KComposedPropertyCreatorInterface()
324 : d(new Private)
325{
326}
327
328KComposedPropertyCreatorInterface::~KComposedPropertyCreatorInterface()
329{
330 delete d;
331}
An interface for for composed property handlers.
Provides a specialized conversion of value to string depending on type.
static QString valueToLocalizedString(const QVariant &value)
static int maxStringValueLength()
Maximum length of strings to display in valueToString(), propertyValueToString() and KPropertyValuePa...
The base class representing a single property.
Definition KProperty.h:96
int type() const
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
const_iterator constBegin() const const
const_iterator constEnd() const const
iterator insert(const Key &key, const T &value)
T value(const Key &key) const const
QLocale c()
QObject * parent() const const
QVariant property(const char *name) const const
QString tr(const char *sourceText, const char *disambiguation, int n)
iterator insert(const T &value)
QString arg(Args &&... args) const const
qsizetype length() const const
void truncate(qsizetype position)
Type type() const const
QString toString() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:12:37 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.