Changeset View
Changeset View
Standalone View
Standalone View
src/KPropertyWidgetsFactory.h
- This file was copied from src/KPropertyFactory.h.
Show All 11 Lines | 1 | /* This file is part of the KDE project | |||
---|---|---|---|---|---|
12 | Library General Public License for more details. | 12 | Library General Public License for more details. | ||
13 | 13 | | |||
14 | You should have received a copy of the GNU Library General Public License | 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 | 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, | 16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
17 | * Boston, MA 02110-1301, USA. | 17 | * Boston, MA 02110-1301, USA. | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #ifndef KPROPERTY_FACTORY_H | 20 | #ifndef KPROPERTYWIDGETS_FACTORY_H | ||
21 | #define KPROPERTY_FACTORY_H | 21 | #define KPROPERTYWIDGETS_FACTORY_H | ||
22 | 22 | | |||
23 | #include "kpropertywidgets_export.h" | ||||
23 | #include "KProperty.h" | 24 | #include "KProperty.h" | ||
25 | #include "KPropertyFactory.h" | ||||
24 | 26 | | |||
25 | #include <QObject> | 27 | #include <QObject> | ||
26 | #include <QVariant> | 28 | #include <QVariant> | ||
27 | #include <QHash> | 29 | #include <QHash> | ||
30 | | ||||
28 | #include <QLabel> | 31 | #include <QLabel> | ||
29 | #include <QPainter> | 32 | #include <QPainter> | ||
30 | #include <QStyleOptionViewItem> | 33 | #include <QStyleOptionViewItem> | ||
31 | 34 | | |||
32 | //! An interface for for composed property handlers | | |||
33 | /*! You have to subclass KComposedPropertyInterface to override the behaviour of a property type.\n | | |||
34 | In the constructor, you should create the child properties (if needed). | | |||
35 | Then, you need to implement the functions concerning values.\n | | |||
36 | | ||||
37 | Example implementation of composed properties can be found in editors/ directory. | | |||
38 | */ | | |||
39 | class KPROPERTY_EXPORT KComposedPropertyInterface | | |||
40 | { | | |||
41 | public: | | |||
42 | explicit KComposedPropertyInterface(KProperty *parent); | | |||
43 | virtual ~KComposedPropertyInterface(); | | |||
44 | | ||||
45 | /*! This function modifies the child properties for parent value @a value. | | |||
46 | It is called by @ref Property::setValue() when | | |||
47 | the property is composed. | | |||
48 | You don't have to modify the property value, it is done by Property class. | | |||
49 | Note that when calling Property::setValue, you <b>need</b> to set | | |||
50 | useComposedProperty (the third parameter) to false, or there will be infinite recursion. */ | | |||
51 | virtual void setValue(KProperty *property, const QVariant &value, bool rememberOldValue) = 0; | | |||
52 | | ||||
53 | void childValueChangedInternal(KProperty *child, const QVariant &value, bool rememberOldValue) { | | |||
54 | if (m_childValueChangedEnabled) | | |||
55 | childValueChanged(child, value, rememberOldValue); | | |||
56 | } | | |||
57 | | ||||
58 | void setChildValueChangedEnabled(bool set) { m_childValueChangedEnabled = set; } | | |||
59 | | ||||
60 | protected: | | |||
61 | virtual void childValueChanged(KProperty *child, const QVariant &value, bool rememberOldValue) = 0; | | |||
62 | | ||||
63 | /*! This method emits the \a Set::propertyChanged() signal for all | | |||
64 | sets our parent-property is registered in. */ | | |||
65 | void emitPropertyChanged(); | | |||
66 | bool m_childValueChangedEnabled : 1; | | |||
67 | }; | | |||
68 | | ||||
69 | class KPROPERTY_EXPORT KComposedPropertyCreatorInterface | | |||
70 | { | | |||
71 | public: | | |||
72 | KComposedPropertyCreatorInterface(); | | |||
73 | | ||||
74 | virtual ~KComposedPropertyCreatorInterface(); | | |||
75 | | ||||
76 | virtual KComposedPropertyInterface* createComposedProperty(KProperty *parent) const = 0; | | |||
77 | }; | | |||
78 | | ||||
79 | //! An interface for editor widget creators. | 35 | //! An interface for editor widget creators. | ||
80 | /*! Options can be set in the options attribute in order to customize | 36 | /*! Options can be set in the options attribute in order to customize | ||
81 | widget creation process. Do this in the EditorCreatorInterface constructor. | 37 | widget creation process. Do this in the EditorCreatorInterface constructor. | ||
82 | */ | 38 | */ | ||
83 | class KPROPERTY_EXPORT KPropertyEditorCreatorInterface | 39 | class KPROPERTYWIDGETS_EXPORT KPropertyEditorCreatorInterface | ||
84 | { | 40 | { | ||
85 | public: | 41 | public: | ||
86 | KPropertyEditorCreatorInterface(); | 42 | KPropertyEditorCreatorInterface(); | ||
87 | 43 | | |||
88 | virtual ~KPropertyEditorCreatorInterface(); | 44 | virtual ~KPropertyEditorCreatorInterface(); | ||
89 | 45 | | |||
90 | virtual QWidget * createEditor( int type, QWidget *parent, | 46 | virtual QWidget * createEditor( int type, QWidget *parent, | ||
91 | const QStyleOptionViewItem & option, const QModelIndex & index ) const = 0; | 47 | const QStyleOptionViewItem & option, const QModelIndex & index ) const = 0; | ||
92 | 48 | | |||
93 | /*! Options for altering the editor widget creation process, | 49 | /*! Options for altering the editor widget creation process, | ||
94 | used by KPropertyFactoryManager::createEditor(). */ | 50 | used by KPropertyFactoryManager::createEditor(). */ | ||
95 | class Options { | 51 | class Options { | ||
96 | public: | 52 | public: | ||
97 | Options(); | 53 | Options(); | ||
98 | /*! In order to have better look of the widget within the property editor view, | 54 | /*! In order to have better look of the widget within the property editor view, | ||
99 | we usually remove borders from the widget (see FactoryManager::createEditor()). | 55 | we usually remove borders from the widget (see FactoryManager::createEditor()). | ||
100 | and adding 1 pixel 'gray border' on the top. Default value is true. */ | 56 | and adding 1 pixel 'gray border' on the top. Default value is true. */ | ||
101 | bool removeBorders; | 57 | bool removeBorders; | ||
102 | }; | 58 | }; | ||
103 | 59 | | |||
104 | //! Options for altering the editor widget creation process | 60 | //! Options for altering the editor widget creation process | ||
105 | Options options; | 61 | Options options; | ||
106 | }; | 62 | }; | ||
107 | 63 | | |||
108 | class KPROPERTY_EXPORT KPropertyValuePainterInterface | 64 | class KPROPERTYWIDGETS_EXPORT KPropertyValuePainterInterface | ||
109 | { | 65 | { | ||
110 | public: | 66 | public: | ||
111 | KPropertyValuePainterInterface(); | 67 | KPropertyValuePainterInterface(); | ||
112 | virtual ~KPropertyValuePainterInterface(); | 68 | virtual ~KPropertyValuePainterInterface(); | ||
113 | virtual void paint( QPainter * painter, | 69 | virtual void paint( QPainter * painter, | ||
114 | const QStyleOptionViewItem & option, const QModelIndex & index ) const = 0; | 70 | const QStyleOptionViewItem & option, const QModelIndex & index ) const = 0; | ||
115 | }; | 71 | }; | ||
116 | 72 | | |||
117 | class KPROPERTY_EXPORT KPropertyValueDisplayInterface | 73 | class KPROPERTYWIDGETS_EXPORT KPropertyValueDisplayInterface | ||
118 | { | 74 | { | ||
119 | public: | 75 | public: | ||
120 | KPropertyValueDisplayInterface(); | 76 | KPropertyValueDisplayInterface(); | ||
121 | virtual ~KPropertyValueDisplayInterface(); | 77 | virtual ~KPropertyValueDisplayInterface(); | ||
122 | virtual QString displayTextForProperty( const KProperty* property ) const | 78 | virtual QString displayTextForProperty( const KProperty* property ) const | ||
123 | { return displayText(property->value()); } | 79 | { return displayText(property->value()); } | ||
124 | virtual QString displayText( const QVariant& value ) const | 80 | virtual QString displayText( const QVariant& value ) const | ||
125 | { return value.toString(); } | 81 | { return value.toString(); } | ||
126 | }; | 82 | }; | ||
127 | 83 | | |||
84 | | ||||
128 | //! Label widget that can be used for displaying text-based read-only items | 85 | //! Label widget that can be used for displaying text-based read-only items | ||
129 | //! Used in LabelCreator. | 86 | //! Used in LabelCreator. | ||
130 | class KPROPERTY_EXPORT KPropertyLabel : public QLabel | 87 | class KPROPERTYWIDGETS_EXPORT KPropertyLabel : public QLabel | ||
131 | { | 88 | { | ||
132 | Q_OBJECT | 89 | Q_OBJECT | ||
133 | Q_PROPERTY(QVariant value READ value WRITE setValue USER true) | 90 | Q_PROPERTY(QVariant value READ value WRITE setValue USER true) | ||
134 | public: | 91 | public: | ||
135 | KPropertyLabel(QWidget *parent, const KPropertyValueDisplayInterface *iface); | 92 | KPropertyLabel(QWidget *parent, const KPropertyValueDisplayInterface *iface); | ||
136 | QVariant value() const; | 93 | QVariant value() const; | ||
137 | Q_SIGNALS: | 94 | Q_SIGNALS: | ||
138 | void commitData( QWidget * editor ); | 95 | void commitData( QWidget * editor ); | ||
139 | public Q_SLOTS: | 96 | public Q_SLOTS: | ||
140 | void setValue(const QVariant& value); | 97 | void setValue(const QVariant& value); | ||
141 | 98 | | |||
142 | protected: | 99 | protected: | ||
143 | virtual void paintEvent( QPaintEvent * event ); | 100 | virtual void paintEvent( QPaintEvent * event ); | ||
144 | 101 | | |||
145 | private: | 102 | private: | ||
146 | const KPropertyValueDisplayInterface *m_iface; | 103 | const KPropertyValueDisplayInterface *m_iface; | ||
147 | QVariant m_value; | 104 | QVariant m_value; | ||
148 | }; | 105 | }; | ||
149 | 106 | | |||
150 | //! Creator returning editor | 107 | //! Creator returning editor | ||
151 | template<class Widget> | 108 | template<class Widget> | ||
152 | class KPROPERTY_EXPORT KPropertyEditorCreator : public KPropertyEditorCreatorInterface, | 109 | class KPROPERTYWIDGETS_EXPORT KPropertyEditorCreator : public KPropertyEditorCreatorInterface, | ||
153 | public KPropertyValueDisplayInterface, | 110 | public KPropertyValueDisplayInterface, | ||
154 | public KPropertyValuePainterInterface | 111 | public KPropertyValuePainterInterface | ||
155 | { | 112 | { | ||
156 | public: | 113 | public: | ||
157 | KPropertyEditorCreator() : KPropertyEditorCreatorInterface() {} | 114 | KPropertyEditorCreator() : KPropertyEditorCreatorInterface() {} | ||
158 | 115 | | |||
159 | virtual ~KPropertyEditorCreator() {} | 116 | virtual ~KPropertyEditorCreator() {} | ||
160 | 117 | | |||
Show All 15 Lines | 129 | { | |||
176 | painter->drawText( r, Qt::AlignLeft | Qt::AlignVCenter, | 133 | painter->drawText( r, Qt::AlignLeft | Qt::AlignVCenter, | ||
177 | displayText( index.data(Qt::EditRole) ) ); | 134 | displayText( index.data(Qt::EditRole) ) ); | ||
178 | painter->restore(); | 135 | painter->restore(); | ||
179 | } | 136 | } | ||
180 | }; | 137 | }; | ||
181 | 138 | | |||
182 | typedef KPropertyEditorCreator<KPropertyLabel> KPropertyLabelCreator; | 139 | typedef KPropertyEditorCreator<KPropertyLabel> KPropertyLabelCreator; | ||
183 | 140 | | |||
184 | //! Creator returning composed property object | | |||
185 | template<class ComposedProperty> | | |||
186 | class KPROPERTY_EXPORT KComposedPropertyCreator : public KComposedPropertyCreatorInterface | | |||
187 | { | | |||
188 | public: | | |||
189 | KComposedPropertyCreator() : KComposedPropertyCreatorInterface() {} | | |||
190 | | ||||
191 | virtual ~KComposedPropertyCreator() {} | | |||
192 | | ||||
193 | virtual ComposedProperty* createComposedProperty(KProperty *parent) const { | | |||
194 | return new ComposedProperty(parent); | | |||
195 | } | | |||
196 | }; | | |||
197 | 141 | | |||
198 | class KPROPERTY_EXPORT KPropertyFactory | 142 | class KPROPERTYWIDGETS_EXPORT KPropertyWidgetsFactory : public KPropertyFactory | ||
199 | { | 143 | { | ||
200 | public: | 144 | public: | ||
201 | KPropertyFactory(); | 145 | KPropertyWidgetsFactory(); | ||
202 | virtual ~KPropertyFactory(); | 146 | virtual ~KPropertyWidgetsFactory(); | ||
203 | QHash<int, KComposedPropertyCreatorInterface*> composedPropertyCreators() const; | 147 | | ||
204 | QHash<int, KPropertyEditorCreatorInterface*> editorCreators() const; | 148 | QHash<int, KPropertyEditorCreatorInterface*> editorCreators() const; | ||
205 | QHash<int, KPropertyValuePainterInterface*> valuePainters() const; | 149 | QHash<int, KPropertyValuePainterInterface*> valuePainters() const; | ||
206 | QHash<int, KPropertyValueDisplayInterface*> valueDisplays() const; | 150 | QHash<int, KPropertyValueDisplayInterface*> valueDisplays() const; | ||
207 | 151 | | |||
208 | //! Adds editor creator @a creator for type @a type. | 152 | //! Adds editor creator @a creator for type @a type. | ||
209 | //! The creator becomes owned by the factory. | 153 | //! The creator becomes owned by the factory. | ||
210 | void addEditor(int type, KPropertyEditorCreatorInterface *creator); | 154 | void addEditor(int type, KPropertyEditorCreatorInterface *creator); | ||
211 | 155 | | |||
212 | void addComposedPropertyCreator( int type, KComposedPropertyCreatorInterface* creator ); | | |||
213 | | ||||
214 | void addPainter(int type, KPropertyValuePainterInterface *painter); | 156 | void addPainter(int type, KPropertyValuePainterInterface *painter); | ||
215 | 157 | | |||
216 | void addDisplay(int type, KPropertyValueDisplayInterface *display); | 158 | void addDisplay(int type, KPropertyValueDisplayInterface *display); | ||
217 | 159 | | |||
218 | static void paintTopGridLine(QWidget *widget); | 160 | static void paintTopGridLine(QWidget *widget); | ||
219 | static void setTopAndBottomBordersUsingStyleSheet(QWidget *widget, QWidget* parent, | 161 | static void setTopAndBottomBordersUsingStyleSheet(QWidget *widget, QWidget* parent, | ||
220 | const QString& extraStyleSheet = QString()); | 162 | const QString& extraStyleSheet = QString()); | ||
221 | 163 | | |||
222 | protected: | 164 | protected: | ||
223 | void addEditorInternal(int type, KPropertyEditorCreatorInterface *editor, bool own = true); | 165 | void addEditorInternal(int type, KPropertyEditorCreatorInterface *editor, bool own = true); | ||
224 | 166 | | |||
225 | void addComposedPropertyCreatorInternal(int type, | | |||
226 | KComposedPropertyCreatorInterface* creator, bool own = true); | | |||
227 | | ||||
228 | //! Adds value painter @a painter for type @a type. | 167 | //! Adds value painter @a painter for type @a type. | ||
229 | //! The painter becomes owned by the factory. | 168 | //! The painter becomes owned by the factory. | ||
230 | void addPainterInternal(int type, KPropertyValuePainterInterface *painter, bool own = true); | 169 | void addPainterInternal(int type, KPropertyValuePainterInterface *painter, bool own = true); | ||
231 | 170 | | |||
232 | //! Adds value-to-text converted @a painter for type @a type. | 171 | //! Adds value-to-text converted @a painter for type @a type. | ||
233 | //! The converter becomes owned by the factory. | 172 | //! The converter becomes owned by the factory. | ||
234 | void addDisplayInternal(int type, KPropertyValueDisplayInterface *display, bool own = true); | 173 | void addDisplayInternal(int type, KPropertyValueDisplayInterface *display, bool own = true); | ||
235 | 174 | | |||
236 | class Private; | 175 | class Private; | ||
237 | Private * const d; | 176 | Private * const d; | ||
238 | }; | 177 | }; | ||
239 | 178 | | |||
240 | class KProperty; | 179 | class KProperty; | ||
241 | class KCustomProperty; | 180 | class KCustomProperty; | ||
242 | 181 | | |||
243 | class KPROPERTY_EXPORT KPropertyFactoryManager : public QObject | 182 | class KPROPERTYWIDGETS_EXPORT KPropertyWidgetsFactoryManager : public KPropertyFactoryManager | ||
244 | { | 183 | { | ||
245 | Q_OBJECT | 184 | Q_OBJECT | ||
246 | public: | 185 | public: | ||
247 | bool isEditorForTypeAvailable( int type ) const; | 186 | bool isEditorForTypeAvailable( int type ) const; | ||
248 | 187 | | |||
249 | QWidget * createEditor( | 188 | QWidget * createEditor( | ||
250 | int type, | 189 | int type, | ||
251 | QWidget *parent, | 190 | QWidget *parent, | ||
Show All 9 Lines | |||||
261 | 200 | | |||
262 | bool canConvertValueToText( int type ) const; | 201 | bool canConvertValueToText( int type ) const; | ||
263 | 202 | | |||
264 | bool canConvertValueToText( const KProperty* property ) const; | 203 | bool canConvertValueToText( const KProperty* property ) const; | ||
265 | 204 | | |||
266 | QString convertValueToText( const KProperty* property ) const; | 205 | QString convertValueToText( const KProperty* property ) const; | ||
267 | 206 | | |||
268 | //! Registers factory @a factory. It becomes owned by the manager. | 207 | //! Registers factory @a factory. It becomes owned by the manager. | ||
269 | void registerFactory(KPropertyFactory *factory); | 208 | void registerFactory(KPropertyWidgetsFactory *factory); | ||
270 | 209 | | |||
271 | KCustomProperty* createCustomProperty( KProperty *parent ); | 210 | KCustomProperty* createCustomProperty( KProperty *parent ); | ||
272 | 211 | | |||
273 | /*! \return a pointer to a factory manager instance.*/ | 212 | /*! \return a pointer to a factory manager instance.*/ | ||
274 | static KPropertyFactoryManager* self(); | 213 | static KPropertyWidgetsFactoryManager* self(); | ||
275 | 214 | | |||
276 | KPropertyFactoryManager(); | 215 | KPropertyWidgetsFactoryManager(); | ||
277 | ~KPropertyFactoryManager(); | 216 | ~KPropertyWidgetsFactoryManager(); | ||
278 | private: | 217 | private: | ||
279 | 218 | | |||
280 | class Private; | 219 | class Private; | ||
281 | Private * const d; | 220 | Private * const d; | ||
282 | }; | 221 | }; | ||
283 | 222 | | |||
284 | #endif | 223 | #endif |