vizkit3d
qtpropertybrowser.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 **
6 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 **
8 ** This file is part of a Qt Solutions component.
9 **
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
15 ** * Redistributions of source code must retain the above copyright
16 ** notice, this list of conditions and the following disclaimer.
17 ** * Redistributions in binary form must reproduce the above copyright
18 ** notice, this list of conditions and the following disclaimer in
19 ** the documentation and/or other materials provided with the
20 ** distribution.
21 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 ** the names of its contributors may be used to endorse or promote
23 ** products derived from this software without specific prior written
24 ** permission.
25 **
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 **
38 ****************************************************************************/
39 
40 
41 #ifndef QTPROPERTYBROWSER_H
42 #define QTPROPERTYBROWSER_H
43 
44 #include <QtGui/QWidget>
45 #include <QtCore/QSet>
46 #include <QtGui/QLineEdit>
47 #include <QtCore/QMap>
48 
49 #if QT_VERSION >= 0x040400
50 QT_BEGIN_NAMESPACE
51 #endif
52 
53 #if defined(Q_WS_WIN)
54 # if !defined(QT_QTPROPERTYBROWSER_EXPORT) && !defined(QT_QTPROPERTYBROWSER_IMPORT)
55 # define QT_QTPROPERTYBROWSER_EXPORT
56 # elif defined(QT_QTPROPERTYBROWSER_IMPORT)
57 # if defined(QT_QTPROPERTYBROWSER_EXPORT)
58 # undef QT_QTPROPERTYBROWSER_EXPORT
59 # endif
60 # define QT_QTPROPERTYBROWSER_EXPORT __declspec(dllimport)
61 # elif defined(QT_QTPROPERTYBROWSER_EXPORT)
62 # undef QT_QTPROPERTYBROWSER_EXPORT
63 # define QT_QTPROPERTYBROWSER_EXPORT __declspec(dllexport)
64 # endif
65 #else
66 # define QT_QTPROPERTYBROWSER_EXPORT
67 #endif
68 
70 
72 class QtPropertyPrivate;
73 
75 {
76 public:
77  virtual ~QtProperty();
78 
79  QList<QtProperty *> subProperties() const;
80 
81  QtAbstractPropertyManager *propertyManager() const;
82 
83  QString toolTip() const;
84  QString statusTip() const;
85  QString whatsThis() const;
86  QString propertyName() const;
87  bool isEnabled() const;
88  bool isModified() const;
89 
90  bool hasValue() const;
91  QIcon valueIcon() const;
92  QString valueText() const;
93  QString displayText() const;
94 
95  void setToolTip(const QString &text);
96  void setStatusTip(const QString &text);
97  void setWhatsThis(const QString &text);
98  void setPropertyName(const QString &text);
99  void setEnabled(bool enable);
100  void setModified(bool modified);
101 
102  void addSubProperty(QtProperty *property);
103  void insertSubProperty(QtProperty *property, QtProperty *afterProperty);
104  void removeSubProperty(QtProperty *property);
105 protected:
106  explicit QtProperty(QtAbstractPropertyManager *manager);
107  void propertyChanged();
108 private:
110  QtPropertyPrivate *d_ptr;
111 };
112 
114 {
115 public:
117  : m_enabled(true),
118  m_modified(false),
119  m_manager(manager) {};
120  QtProperty *q_ptr;
121 
122  QSet<QtProperty *> m_parentItems;
123  QList<QtProperty *> m_subItems;
124 
125  QString m_toolTip;
126  QString m_statusTip;
127  QString m_whatsThis;
128  QString m_name;
129  bool m_enabled;
131 
133 };
134 
135 
137 {
139  Q_DECLARE_PUBLIC(QtAbstractPropertyManager)
140 public:
141  void propertyDestroyed(QtProperty *property);
142  void propertyChanged(QtProperty *property) const;
143  void propertyRemoved(QtProperty *property,
144  QtProperty *parentProperty) const;
145  void propertyInserted(QtProperty *property, QtProperty *parentProperty,
146  QtProperty *afterProperty) const;
147 
148  QSet<QtProperty *> m_properties;
149 };
150 
152 
154 {
155  Q_OBJECT
156 public:
157 
158  explicit QtAbstractPropertyManager(QObject *parent = 0);
160 
161  QSet<QtProperty *> properties() const;
162  void clear() const;
163 
164  QtProperty *addProperty(const QString &name = QString());
165 Q_SIGNALS:
166 
167  void propertyInserted(QtProperty *property,
168  QtProperty *parent, QtProperty *after);
169  void propertyChanged(QtProperty *property);
170  void propertyRemoved(QtProperty *property, QtProperty *parent);
171  void propertyDestroyed(QtProperty *property);
172 protected:
173  virtual bool hasValue(const QtProperty *property) const;
174  virtual QIcon valueIcon(const QtProperty *property) const;
175  virtual QString valueText(const QtProperty *property) const;
176  virtual QString displayText(const QtProperty *property) const;
177  virtual EchoMode echoMode(const QtProperty *) const;
178  virtual void initializeProperty(QtProperty *property) = 0;
179  virtual void uninitializeProperty(QtProperty *property);
180  virtual QtProperty *createProperty();
181 private:
182  friend class QtProperty;
184  Q_DECLARE_PRIVATE(QtAbstractPropertyManager)
185  Q_DISABLE_COPY(QtAbstractPropertyManager)
186 };
187 
189 {
190  Q_OBJECT
191 public:
192  virtual QWidget *createEditor(QtProperty *property, QWidget *parent) = 0;
193 protected:
194  explicit QtAbstractEditorFactoryBase(QObject *parent = 0)
195  : QObject(parent) {}
196 
197  virtual void breakConnection(QtAbstractPropertyManager *manager) = 0;
198 protected Q_SLOTS:
199  virtual void managerDestroyed(QObject *manager) = 0;
200 
202 };
203 
204 template <class PropertyManager>
206 {
207 public:
208  explicit QtAbstractEditorFactory(QObject *parent) : QtAbstractEditorFactoryBase(parent) {}
209  QWidget *createEditor(QtProperty *property, QWidget *parent)
210  {
211  QSetIterator<PropertyManager *> it(m_managers);
212  while (it.hasNext()) {
213  PropertyManager *manager = it.next();
214  if (manager == property->propertyManager()) {
215  return createEditor(manager, property, parent);
216  }
217  }
218  return 0;
219  }
220  void addPropertyManager(PropertyManager *manager)
221  {
222  if (m_managers.contains(manager))
223  return;
224  m_managers.insert(manager);
225  connectPropertyManager(manager);
226  connect(manager, SIGNAL(destroyed(QObject *)),
227  this, SLOT(managerDestroyed(QObject *)));
228  }
229  void removePropertyManager(PropertyManager *manager)
230  {
231  if (!m_managers.contains(manager))
232  return;
233  disconnect(manager, SIGNAL(destroyed(QObject *)),
234  this, SLOT(managerDestroyed(QObject *)));
235  disconnectPropertyManager(manager);
236  m_managers.remove(manager);
237  }
238  QSet<PropertyManager *> propertyManagers() const
239  {
240  return m_managers;
241  }
242  PropertyManager *propertyManager(QtProperty *property) const
243  {
244  QtAbstractPropertyManager *manager = property->propertyManager();
245  QSetIterator<PropertyManager *> itManager(m_managers);
246  while (itManager.hasNext()) {
247  PropertyManager *m = itManager.next();
248  if (m == manager) {
249  return m;
250  }
251  }
252  return 0;
253  }
254 protected:
255  virtual void connectPropertyManager(PropertyManager *manager) = 0;
256  virtual QWidget *createEditor(PropertyManager *manager, QtProperty *property,
257  QWidget *parent) = 0;
258  virtual void disconnectPropertyManager(PropertyManager *manager) = 0;
259  void managerDestroyed(QObject *manager)
260  {
261  QSetIterator<PropertyManager *> it(m_managers);
262  while (it.hasNext()) {
263  PropertyManager *m = it.next();
264  if (m == manager) {
265  m_managers.remove(m);
266  return;
267  }
268  }
269  }
270 private:
271  void breakConnection(QtAbstractPropertyManager *manager)
272  {
273  QSetIterator<PropertyManager *> it(m_managers);
274  while (it.hasNext()) {
275  PropertyManager *m = it.next();
276  if (m == manager) {
277  removePropertyManager(m);
278  return;
279  }
280  }
281  }
282 private:
283  QSet<PropertyManager *> m_managers;
284  friend class QtAbstractPropertyEditor;
285 };
286 
288 class QtBrowserItem;
289 
291 {
292 public:
294  : m_browser(browser), m_property(property), m_parent(parent), q_ptr(0) {};
295 
296  void addChild(QtBrowserItem *index, QtBrowserItem *after);
297  void removeChild(QtBrowserItem *index);
298 
302 
304 
305  QList<QtBrowserItem *> m_children;
306 
307 };
308 
310 {
311 public:
312  QtProperty *property() const;
313  QtBrowserItem *parent() const;
314  QList<QtBrowserItem *> children() const;
315  QtAbstractPropertyBrowser *browser() const;
316 private:
317  explicit QtBrowserItem(QtAbstractPropertyBrowser *browser, QtProperty *property, QtBrowserItem *parent);
318  ~QtBrowserItem();
319  QtBrowserItemPrivate *d_ptr;
321 };
322 
324 {
326  Q_DECLARE_PUBLIC(QtAbstractPropertyBrowser)
327 public:
329 
330  void insertSubTree(QtProperty *property,
331  QtProperty *parentProperty);
332  void removeSubTree(QtProperty *property,
333  QtProperty *parentProperty);
334  void createBrowserIndexes(QtProperty *property, QtProperty *parentProperty, QtProperty *afterProperty);
335  void removeBrowserIndexes(QtProperty *property, QtProperty *parentProperty);
336  QtBrowserItem *createBrowserIndex(QtProperty *property, QtBrowserItem *parentIndex, QtBrowserItem *afterIndex);
337  void removeBrowserIndex(QtBrowserItem *index);
338  void clearIndex(QtBrowserItem *index);
339 
340  void slotPropertyInserted(QtProperty *property,
341  QtProperty *parentProperty, QtProperty *afterProperty);
342  void slotPropertyRemoved(QtProperty *property, QtProperty *parentProperty);
343  void slotPropertyDestroyed(QtProperty *property);
344  void slotPropertyDataChanged(QtProperty *property);
345 
346  QList<QtProperty *> m_subItems;
347  QMap<QtAbstractPropertyManager *, QList<QtProperty *> > m_managerToProperties;
348  QMap<QtProperty *, QList<QtProperty *> > m_propertyToParents;
349 
350  QMap<QtProperty *, QtBrowserItem *> m_topLevelPropertyToIndex;
351  QList<QtBrowserItem *> m_topLevelIndexes;
352  QMap<QtProperty *, QList<QtBrowserItem *> > m_propertyToIndexes;
353 
355 };
356 
358 {
359  Q_OBJECT
360 public:
361 
362  explicit QtAbstractPropertyBrowser(QWidget *parent = 0);
364 
365  QList<QtProperty *> properties() const;
366  QList<QtBrowserItem *> items(QtProperty *property) const;
367  QtBrowserItem *topLevelItem(QtProperty *property) const;
368  QList<QtBrowserItem *> topLevelItems() const;
369  void clear();
370 
371  template <class PropertyManager>
372  void setFactoryForManager(PropertyManager *manager,
374  QtAbstractPropertyManager *abstractManager = manager;
375  QtAbstractEditorFactoryBase *abstractFactory = factory;
376 
377  if (addFactory(abstractManager, abstractFactory))
378  factory->addPropertyManager(manager);
379  }
380 
381  void unsetFactoryForManager(QtAbstractPropertyManager *manager);
382 
383  QtBrowserItem *currentItem() const;
384  void setCurrentItem(QtBrowserItem *);
385 
386 Q_SIGNALS:
387  void currentItemChanged(QtBrowserItem *);
388 
389 public Q_SLOTS:
390 
391  QtBrowserItem *addProperty(QtProperty *property);
392  QtBrowserItem *insertProperty(QtProperty *property, QtProperty *afterProperty);
393  void removeProperty(QtProperty *property);
394 
395 protected:
396 
397  virtual void itemInserted(QtBrowserItem *item, QtBrowserItem *afterItem) = 0;
398  virtual void itemRemoved(QtBrowserItem *item) = 0;
399  // can be tooltip, statustip, whatsthis, name, icon, text.
400  virtual void itemChanged(QtBrowserItem *item) = 0;
401 
402  virtual QWidget *createEditor(QtProperty *property, QWidget *parent);
403 private:
404 
405  bool addFactory(QtAbstractPropertyManager *abstractManager,
406  QtAbstractEditorFactoryBase *abstractFactory);
407 
409  Q_DECLARE_PRIVATE(QtAbstractPropertyBrowser)
410  Q_DISABLE_COPY(QtAbstractPropertyBrowser)
411  Q_PRIVATE_SLOT(d_func(), void slotPropertyInserted(QtProperty *,
412  QtProperty *, QtProperty *))
413  Q_PRIVATE_SLOT(d_func(), void slotPropertyRemoved(QtProperty *,
414  QtProperty *))
415  Q_PRIVATE_SLOT(d_func(), void slotPropertyDestroyed(QtProperty *))
416  Q_PRIVATE_SLOT(d_func(), void slotPropertyDataChanged(QtProperty *))
417 
418 };
419 
420 #if QT_VERSION >= 0x040400
421 QT_END_NAMESPACE
422 #endif
423 
424 #endif // QTPROPERTYBROWSER_H
QtAbstractEditorFactoryBase(QObject *parent=0)
Definition: qtpropertybrowser.h:194
QList< QtProperty * > m_subItems
Definition: qtpropertybrowser.h:346
Definition: qtpropertybrowser.h:113
QtAbstractPropertyBrowser *const m_browser
Definition: qtpropertybrowser.h:299
QtAbstractPropertyBrowser provides a base class for implementing property browsers.
Definition: qtpropertybrowser.h:357
The QtBrowserItem class represents a property in a property browser instance.
Definition: qtpropertybrowser.h:309
Definition: qtpropertybrowser.h:136
QList< QtProperty * > m_subItems
Definition: qtpropertybrowser.h:123
The QtAbstractPropertyManager provides an interface for property managers.
Definition: qtpropertybrowser.h:153
Definition: qtpropertybrowser.h:290
QtBrowserItem * q_ptr
Definition: qtpropertybrowser.h:303
void addPropertyManager(PropertyManager *manager)
Definition: qtpropertybrowser.h:220
QString m_statusTip
Definition: qtpropertybrowser.h:126
QtAbstractPropertyManager * propertyManager() const
Definition: qtpropertybrowser.cpp:170
QtBrowserItem * m_parent
Definition: qtpropertybrowser.h:301
The QtAbstractEditorFactory is the base template class for editor factories.
Definition: qtpropertybrowser.h:205
QtAbstractEditorFactory(QObject *parent)
Definition: qtpropertybrowser.h:208
QMap< QtProperty *, QtBrowserItem * > m_topLevelPropertyToIndex
Definition: qtpropertybrowser.h:350
QSet< QtProperty * > m_parentItems
Definition: qtpropertybrowser.h:122
QMap< QtProperty *, QList< QtProperty * > > m_propertyToParents
Definition: qtpropertybrowser.h:348
bool m_modified
Definition: qtpropertybrowser.h:130
void setFactoryForManager(PropertyManager *manager, QtAbstractEditorFactory< PropertyManager > *factory)
Definition: qtpropertybrowser.h:372
QList< QtBrowserItem * > m_children
Definition: qtpropertybrowser.h:305
void managerDestroyed(QObject *manager)
Definition: qtpropertybrowser.h:259
QtBrowserItemPrivate(QtAbstractPropertyBrowser *browser, QtProperty *property, QtBrowserItem *parent)
Definition: qtpropertybrowser.h:293
QMap< QtAbstractPropertyManager *, QList< QtProperty * > > m_managerToProperties
Definition: qtpropertybrowser.h:347
#define QT_QTPROPERTYBROWSER_EXPORT
Definition: qtpropertybrowser.h:66
QSet< PropertyManager * > propertyManagers() const
Definition: qtpropertybrowser.h:238
QtProperty * m_property
Definition: qtpropertybrowser.h:300
PropertyManager * propertyManager(QtProperty *property) const
Definition: qtpropertybrowser.h:242
QWidget * createEditor(QtProperty *property, QWidget *parent)
Definition: qtpropertybrowser.h:209
The QtProperty class encapsulates an instance of a property.
Definition: qtpropertybrowser.h:74
QString m_toolTip
Definition: qtpropertybrowser.h:125
void removePropertyManager(PropertyManager *manager)
Definition: qtpropertybrowser.h:229
QtBrowserItem * m_currentItem
Definition: qtpropertybrowser.h:354
QString m_whatsThis
Definition: qtpropertybrowser.h:127
QSet< QtProperty * > m_properties
Definition: qtpropertybrowser.h:148
QString m_name
Definition: qtpropertybrowser.h:128
QLineEdit::EchoMode EchoMode
Definition: qtpropertybrowser.h:69
QtProperty * q_ptr
Definition: qtpropertybrowser.h:119
Definition: qtpropertybrowser.h:323
The QtAbstractEditorFactoryBase provides an interface for editor factories.
Definition: qtpropertybrowser.h:188
QMap< QtProperty *, QList< QtBrowserItem * > > m_propertyToIndexes
Definition: qtpropertybrowser.h:352
QtPropertyPrivate(QtAbstractPropertyManager *manager)
Definition: qtpropertybrowser.h:116
bool m_enabled
Definition: qtpropertybrowser.h:129
QtAbstractPropertyManager *const m_manager
Definition: qtpropertybrowser.h:132
QList< QtBrowserItem * > m_topLevelIndexes
Definition: qtpropertybrowser.h:351