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