KProperty

KPropertyFactory.h
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 #ifndef KPROPERTY_FACTORY_H
21 #define KPROPERTY_FACTORY_H
22 
23 #include "KProperty.h"
24 
25 //! An interface for for composed property handlers
26 /*! KComposedPropertyInterface should be subclassed to override the behaviour of a property type.
27  In the constructor child property objects should be created if there are any.
28  Then functions handling values should be implemented.
29 
30  Example implementation of composed properties can be found in editors/ directory.
31 */
32 class KPROPERTYCORE_EXPORT KComposedPropertyInterface
33 {
34 public:
35  explicit KComposedPropertyInterface(KProperty *parent);
36  virtual ~KComposedPropertyInterface();
37 
38  /*! This function modifies the child properties for parent value @a value.
39  It is called by @ref KProperty::setValue() when
40  the property is composed.
41  It is not necessary to modify the property value, it is done by KProperty.
42  @note When this method is called valueOptions should have
43  the KProperty::ValueOption::IgnoreComposedProperty flag set or else there will be infinite
44  recursion. The KProperty class makes sure this is the case but developers of custom
45  properties should take special care about this.
46  */
47  virtual void setValue(KProperty *property, const QVariant &value, KProperty::ValueOptions valueOptions) = 0;
48 
49  void childValueChangedInternal(KProperty *child, const QVariant &value, KProperty::ValueOptions valueOptions);
50 
51  bool childValueChangedEnabled() const;
52 
53  void setChildValueChangedEnabled(bool set);
54 
55  /*! @return true if values @a first and @a second are equal. Used in KProperty::setValue()
56  to check if value has been changed before setting value.
57  Default implementation uses operator==. */
58  inline virtual bool valuesEqual(const QVariant &first, const QVariant &second) { return first == second; }
59 
60 protected:
61  virtual void childValueChanged(KProperty *child, const QVariant &value, KProperty::ValueOptions valueOptions) = 0;
62 
63  /*! This method emits the \a KPropertySet::propertyChanged() signal for all
64  sets our parent-property is registered in. */
65  void emitPropertyChanged();
66 
67 private:
68  Q_DISABLE_COPY(KComposedPropertyInterface)
69  class Private;
70  Private * const d;
71 };
72 
73 class KPROPERTYCORE_EXPORT KComposedPropertyCreatorInterface
74 {
75 public:
76  KComposedPropertyCreatorInterface();
77 
78  virtual ~KComposedPropertyCreatorInterface();
79 
80  virtual KComposedPropertyInterface* createComposedProperty(KProperty *parent) const = 0;
81 
82 private:
83  Q_DISABLE_COPY(KComposedPropertyCreatorInterface)
84  class Private;
85  Private * const d;
86 };
87 
88 //! Creator returning composed property object
89 template<class ComposedProperty>
90 class KComposedPropertyCreator : public KComposedPropertyCreatorInterface
91 {
92 public:
93  KComposedPropertyCreator() : KComposedPropertyCreatorInterface() {}
94 
95  ~KComposedPropertyCreator() override {}
96 
97  ComposedProperty* createComposedProperty(KProperty *parent) const override {
98  return new ComposedProperty(parent);
99  }
100 
101  Q_DISABLE_COPY(KComposedPropertyCreator)
102 };
103 
104 //! Provides a specialized conversion of value to string depending on type
105 class KPROPERTYCORE_EXPORT KPropertyValueDisplayInterface
106 {
107 public:
109 
111 
112  virtual QString propertyValueToString(const KProperty* property, const QLocale &locale) const
113  { return valueToString(property->value(), locale); }
114 
115  virtual QString valueToString(const QVariant& value, const QLocale &locale) const = 0;
116 
117  //! Maximum length of strings to display in valueToString(), propertyValueToString()
118  //! and KPropertyValuePainterInterface::paint().
119  //! Used to avoid inefficiences. Equal to 250.
120  //! @todo Make configurable?
121  static int maxStringValueLength();
122 
123  //! @return @a value converted to string using QVariant::toString(), truncated if it's longer than @ref maxStringValueLength()
124  //! @see maxStringValueLength();
125  static QString valueToLocalizedString(const QVariant& value);
126 
127 private:
128  Q_DISABLE_COPY(KPropertyValueDisplayInterface)
129  class Private;
130  Private * const d;
131 };
132 
133 class KPROPERTYCORE_EXPORT KPropertyFactory
134 {
135 public:
136  KPropertyFactory();
137 
138  virtual ~KPropertyFactory();
139 
140  QHash<int, KComposedPropertyCreatorInterface*> composedPropertyCreators() const;
141  QHash<int, KPropertyValueDisplayInterface*> valueDisplays() const;
142 
143  void addComposedPropertyCreator(int type, KComposedPropertyCreatorInterface* creator);
144 
145  void addDisplay(int type, KPropertyValueDisplayInterface *display);
146 
147 protected:
148  void addComposedPropertyCreatorInternal(int type,
149  KComposedPropertyCreatorInterface* creator, bool own = true);
150 
151  //! Adds value-to-text converted @a painter for type @a type.
152  //! The converter becomes owned by the factory.
153  void addDisplayInternal(int type, KPropertyValueDisplayInterface *display, bool own = true);
154 
155  Q_DISABLE_COPY(KPropertyFactory)
156  class Private;
157  Private * const d;
158 };
159 
160 class KPROPERTYCORE_EXPORT KPropertyFactoryManager : public QObject
161 {
162  Q_OBJECT
163 public:
164  KComposedPropertyInterface* createComposedProperty(KProperty *parent);
165 
166  //! Registers factory @a factory. It becomes owned by the manager.
167  void registerFactory(KPropertyFactory *factory);
168 
169  /*! \return a pointer to a factory manager instance.*/
170  static KPropertyFactoryManager* self();
171 
172  bool canConvertValueToText(int type) const;
173 
174  bool canConvertValueToText(const KProperty* property) const;
175 
176  QString propertyValueToString(const KProperty* property) const;
177 
178  QString valueToString(int type, const QVariant &value) const;
179 
180  QString propertyValueToLocalizedString(const KProperty* property) const;
181 
182  QString valueToLocalizedString(int type, const QVariant &value) const;
183 
184  KPropertyFactoryManager();
185  ~KPropertyFactoryManager() override;
186 
187  //! Adds function @a initFunction that will be called after the manager is created.
188  //! Useful for creation custom factories.
189  static void addInitFunction(void (*initFunction)());
190 
191 private:
192  Q_DISABLE_COPY(KPropertyFactoryManager)
193  class Private;
194  Private * const d;
195 };
196 
197 #endif
virtual bool valuesEqual(const QVariant &first, const QVariant &second)
An interface for for composed property handlers.
The base class representing a single property.
Definition: KProperty.h:95
QVariant value() const
Definition: KProperty.cpp:394
Creator returning composed property object.
Provides a specialized conversion of value to string depending on type.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Mar 7 2021 23:02:25 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.