KProperty

KPropertyFactory.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2008-2015 Jarosław Staniek <[email protected]>
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 
26 class Q_DECL_HIDDEN KPropertyFactoryManager::Private
27 {
28 public:
29  Private()
30  {
31  }
32  ~Private()
33  {
34  qDeleteAll(factories);
35  }
36 
37  QSet<KPropertyFactory*> factories;
38  QHash<int, KComposedPropertyCreatorInterface*> composedPropertyCreators;
40 };
41 
42 Q_GLOBAL_STATIC(KPropertyFactoryManager, _self)
43 
44 class Q_DECL_HIDDEN KPropertyFactory::Private
46 {
47 public:
48  Private()
49  {
50  }
51  ~Private()
52  {
53  qDeleteAll(valueDisplaysSet);
54  }
55 
56  QHash<int, KComposedPropertyCreatorInterface*> composedPropertyCreators;
57  QSet<KComposedPropertyCreatorInterface*> composedPropertyCreatorsSet;
60 };
61 
64 Q_GLOBAL_STATIC(InitFunctions, _initFunctions)
65 
66 KPropertyFactory::KPropertyFactory()
67  : d( new Private )
68 {
69 }
70 
71 KPropertyFactory::~KPropertyFactory()
72 {
73  delete d;
74 }
75 
76 QHash<int, KComposedPropertyCreatorInterface*> KPropertyFactory::composedPropertyCreators() const
77 {
78  return d->composedPropertyCreators;
79 }
80 
81 QHash<int, KPropertyValueDisplayInterface*> KPropertyFactory::valueDisplays() const
82 {
83  return d->valueDisplays;
84 }
85 
86 void KPropertyFactory::addComposedPropertyCreator( int type, KComposedPropertyCreatorInterface* creator )
87 {
88  addComposedPropertyCreatorInternal( type, creator, true );
89 }
90 
91 void 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 
98 void 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 
110 void 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 
120 class Q_DECL_HIDDEN KPropertyValueDisplayInterface::Private
121 {
122 public:
123  Private() {}
124 };
125 
126 KPropertyValueDisplayInterface::KPropertyValueDisplayInterface()
127  : d(new Private)
128 {
129 }
130 
131 KPropertyValueDisplayInterface::~KPropertyValueDisplayInterface()
132 {
133  delete d;
134 }
135 
136 //static
138 {
139  return 250;
140 }
141 
142 //static
144 {
145  QString s(value.toString());
148  return QObject::tr("%1...", "Truncated string").arg(s);
149  }
150  return s;
151 }
152 
153 //------------
154 
155 KPropertyFactoryManager::KPropertyFactoryManager()
156  : QObject(nullptr)
157  , d(new Private)
158 {
159  setObjectName(QLatin1String("KPropertyFactoryManager"));
160 }
161 
162 KPropertyFactoryManager::~KPropertyFactoryManager()
163 {
164  delete d;
165 }
166 
167 KPropertyFactoryManager* 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 
180 void 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();
192  for (QHash<int, KPropertyValueDisplayInterface*>::ConstIterator it( factory->valueDisplays().constBegin() );
193  it != valueDisplaysItEnd; ++it)
194  {
195  d->valueDisplays.insert(it.key(), it.value());
196  }
197 }
198 
199 KComposedPropertyInterface* 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
206 void KPropertyFactoryManager::addInitFunction(void (*initFunction)())
207 {
208  _initFunctions->append(initFunction);
209 }
210 
211 bool KPropertyFactoryManager::canConvertValueToText(int type) const
212 {
213  return d->valueDisplays.value(type) != nullptr;
214 }
215 
216 bool KPropertyFactoryManager::canConvertValueToText(const KProperty* property) const
217 {
218  return canConvertValueToText(property->type());
219 }
220 
221 QString 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 
227 QString 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 
233 QString 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 
239 QString 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 
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 
279 class Q_DECL_HIDDEN KComposedPropertyInterface::Private
280 {
281 public:
282  Private() {}
283  bool childValueChangedEnabled = true;
284 };
285 
286 KComposedPropertyInterface::KComposedPropertyInterface(KProperty *parent)
287  : d(new Private)
288 {
289  Q_UNUSED(parent)
290 }
291 
292 KComposedPropertyInterface::~KComposedPropertyInterface()
293 {
294  delete d;
295 }
296 
297 void KComposedPropertyInterface::childValueChangedInternal(KProperty *child, const QVariant &value,
298  KProperty::ValueOptions valueOptions)
299 {
300  if (d->childValueChangedEnabled) {
301  childValueChanged(child, value, valueOptions);
302  }
303 }
304 
305 void KComposedPropertyInterface::setChildValueChangedEnabled(bool set)
306 {
307  d->childValueChangedEnabled = set;
308 }
309 
310 bool KComposedPropertyInterface::childValueChangedEnabled() const
311 {
312  return d->childValueChangedEnabled;
313 }
314 
315 //---------------
316 
317 class Q_DECL_HIDDEN KComposedPropertyCreatorInterface::Private
318 {
319 public:
320  Private() {}
321 };
322 
323 KComposedPropertyCreatorInterface::KComposedPropertyCreatorInterface()
324  : d(new Private)
325 {
326 }
327 
328 KComposedPropertyCreatorInterface::~KComposedPropertyCreatorInterface()
329 {
330  delete d;
331 }
void truncate(int position)
static QString valueToLocalizedString(const QVariant &value)
static int maxStringValueLength()
Maximum length of strings to display in valueToString(), propertyValueToString() and KPropertyValuePa...
QString tr(const char *sourceText, const char *disambiguation, int n)
QLocale c()
An interface for for composed property handlers.
QVariant property(const char *name) const const
QHash::const_iterator constEnd() const const
void setObjectName(const QString &name)
The base class representing a single property.
Definition: KProperty.h:95
int type() const
Definition: KProperty.cpp:366
QObject(QObject *parent)
QVariant value() const
Definition: KProperty.cpp:394
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QObject * parent() const const
QString toString() const const
Provides a specialized conversion of value to string depending on type.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 23:10:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.