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 <QWidget>
45 #include <QSet>
46 #include <QLineEdit>
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 protected:
103  explicit QtProperty(QtAbstractPropertyManager* manager);
104  void propertyChanged();
105 private:
107  QtPropertyPrivate* d_ptr;
108 };
109 
111 
113 {
114  Q_OBJECT
115 public:
116 
117  explicit QtAbstractPropertyManager(QObject* parent = 0);
118  ~QtAbstractPropertyManager() override;
119 
120  QSet<QtProperty*> properties() const;
121  void clear() const;
122 
123  QtProperty* addProperty(const QString& name = QString());
124 Q_SIGNALS:
125 
126  void propertyInserted(QtProperty* property,
127  QtProperty* parent, QtProperty* after);
128  void propertyChanged(QtProperty* property);
129  void propertyRemoved(QtProperty* property, QtProperty* parent);
130  void propertyDestroyed(QtProperty* property);
131 protected:
132  virtual bool hasValue(const QtProperty* property) const;
133  virtual QIcon valueIcon(const QtProperty* property) const;
134  virtual QString valueText(const QtProperty* property) const;
135  virtual QString displayText(const QtProperty* property) const;
136  virtual EchoMode echoMode(const QtProperty*) const;
137  virtual void initializeProperty(QtProperty* property) = 0;
138  virtual void uninitializeProperty(QtProperty* property);
139  virtual QtProperty* createProperty();
140 private:
141  friend class QtProperty;
143  Q_DECLARE_PRIVATE(QtAbstractPropertyManager)
144  Q_DISABLE_COPY(QtAbstractPropertyManager)
145 };
146 
148 {
149  Q_OBJECT
150 public:
151  virtual QWidget* createEditor(QtProperty* property, QWidget* parent) = 0;
152 protected:
153  explicit QtAbstractEditorFactoryBase(QObject* parent = 0)
154  : QObject(parent) {}
155 
156  virtual void breakConnection(QtAbstractPropertyManager* manager) = 0;
157 protected Q_SLOTS:
158  virtual void managerDestroyed(QObject* manager) = 0;
159 
161 };
162 
163 template <class PropertyManager>
165 {
166 public:
167  explicit QtAbstractEditorFactory(QObject* parent) : QtAbstractEditorFactoryBase(parent) {}
168  QWidget* createEditor(QtProperty* property, QWidget* parent) override
169  {
170  QSetIterator<PropertyManager*> it(m_managers);
171 
172  while (it.hasNext())
173  {
174  PropertyManager* manager = it.next();
175 
176  if (manager == property->propertyManager())
177  {
178  return createEditor(manager, property, parent);
179  }
180  }
181 
182  return 0;
183  }
184  void addPropertyManager(PropertyManager* manager)
185  {
186  if (m_managers.contains(manager))
187  {
188  return;
189  }
190 
191  m_managers.insert(manager);
192  connectPropertyManager(manager);
193  connect(manager, SIGNAL(destroyed(QObject*)),
194  this, SLOT(managerDestroyed(QObject*)));
195  }
196  void removePropertyManager(PropertyManager* manager)
197  {
198  if (!m_managers.contains(manager))
199  {
200  return;
201  }
202 
203  disconnect(manager, SIGNAL(destroyed(QObject*)),
204  this, SLOT(managerDestroyed(QObject*)));
205  disconnectPropertyManager(manager);
206  m_managers.remove(manager);
207  }
208  QSet<PropertyManager*> propertyManagers() const
209  {
210  return m_managers;
211  }
212  PropertyManager* propertyManager(QtProperty* property) const
213  {
214  QtAbstractPropertyManager* manager = property->propertyManager();
215  QSetIterator<PropertyManager*> itManager(m_managers);
216 
217  while (itManager.hasNext())
218  {
219  PropertyManager* m = itManager.next();
220 
221  if (m == manager)
222  {
223  return m;
224  }
225  }
226 
227  return 0;
228  }
229 protected:
230  virtual void connectPropertyManager(PropertyManager* manager) = 0;
231  virtual QWidget* createEditor(PropertyManager* manager, QtProperty* property,
232  QWidget* parent) = 0;
233  virtual void disconnectPropertyManager(PropertyManager* manager) = 0;
234  void managerDestroyed(QObject* manager) override
235  {
236  QSetIterator<PropertyManager*> it(m_managers);
237 
238  while (it.hasNext())
239  {
240  PropertyManager* m = it.next();
241 
242  if (m == manager)
243  {
244  m_managers.remove(m);
245  return;
246  }
247  }
248  }
249 private:
250  void breakConnection(QtAbstractPropertyManager* manager) override
251  {
252  QSetIterator<PropertyManager*> it(m_managers);
253 
254  while (it.hasNext())
255  {
256  PropertyManager* m = it.next();
257 
258  if (m == manager)
259  {
261  return;
262  }
263  }
264  }
265 private:
266  QSet<PropertyManager*> m_managers;
268 };
269 
272 
274 {
275 public:
276  QtProperty* property() const;
277  QtBrowserItem* parent() const;
278  QList<QtBrowserItem*> children() const;
279  QtAbstractPropertyBrowser* browser() const;
280 private:
281  explicit QtBrowserItem(QtAbstractPropertyBrowser* browser, QtProperty* property, QtBrowserItem* parent);
282  ~QtBrowserItem();
283  QtBrowserItemPrivate* d_ptr;
285 };
286 
288 
290 {
291  Q_OBJECT
292 public:
293 
294  explicit QtAbstractPropertyBrowser(QWidget* parent = 0);
295  ~QtAbstractPropertyBrowser() override;
296 
297  QList<QtProperty*> properties() const;
298  QList<QtBrowserItem*> items(QtProperty* property) const;
299  QtBrowserItem* topLevelItem(QtProperty* property) const;
300  QList<QtBrowserItem*> topLevelItems() const;
301  void clear();
302 
303  template <class PropertyManager>
304  void setFactoryForManager(PropertyManager* manager,
306  {
307  QtAbstractPropertyManager* abstractManager = manager;
308  QtAbstractEditorFactoryBase* abstractFactory = factory;
309 
310  if (addFactory(abstractManager, abstractFactory))
311  {
312  factory->addPropertyManager(manager);
313  }
314  }
315 
316  void unsetFactoryForManager(QtAbstractPropertyManager* manager);
317 
318  QtBrowserItem* currentItem() const;
319  void setCurrentItem(QtBrowserItem*);
320 
321 Q_SIGNALS:
322  void currentItemChanged(QtBrowserItem*);
323 
324 public Q_SLOTS:
325 
326  QtBrowserItem* addProperty(QtProperty* property);
327  QtBrowserItem* insertProperty(QtProperty* property, QtProperty* afterProperty);
328  void removeProperty(QtProperty* property);
329 
330 protected:
331 
332  virtual void itemInserted(QtBrowserItem* item, QtBrowserItem* afterItem) = 0;
333  virtual void itemRemoved(QtBrowserItem* item) = 0;
334  // can be tooltip, statustip, whatsthis, name, icon, text.
335  virtual void itemChanged(QtBrowserItem* item) = 0;
336 
337  virtual QWidget* createEditor(QtProperty* property, QWidget* parent);
338 private:
339 
340  bool addFactory(QtAbstractPropertyManager* abstractManager,
341  QtAbstractEditorFactoryBase* abstractFactory);
342 
344  Q_DECLARE_PRIVATE(QtAbstractPropertyBrowser)
345  Q_DISABLE_COPY(QtAbstractPropertyBrowser)
346  Q_PRIVATE_SLOT(d_func(), void slotPropertyInserted(QtProperty*,
348  Q_PRIVATE_SLOT(d_func(), void slotPropertyRemoved(QtProperty*,
349  QtProperty*))
350  Q_PRIVATE_SLOT(d_func(), void slotPropertyDestroyed(QtProperty*))
351  Q_PRIVATE_SLOT(d_func(), void slotPropertyDataChanged(QtProperty*))
352 
353 };
354 
355 QT_END_NAMESPACE
QtAbstractEditorFactory::disconnectPropertyManager
virtual void disconnectPropertyManager(PropertyManager *manager)=0
QtAbstractEditorFactory::propertyManagers
QSet< PropertyManager * > propertyManagers() const
Definition: qtpropertybrowser.h:208
QtAbstractEditorFactory::createEditor
QWidget * createEditor(QtProperty *property, QWidget *parent) override
Definition: qtpropertybrowser.h:168
QtAbstractEditorFactory::QtAbstractEditorFactory
QtAbstractEditorFactory(QObject *parent)
Definition: qtpropertybrowser.h:167
QtAbstractPropertyBrowser
QtAbstractPropertyBrowser provides a base class for implementing property browsers.
Definition: qtpropertybrowser.h:289
QT_QTPROPERTYBROWSER_EXPORT
#define QT_QTPROPERTYBROWSER_EXPORT
Definition: qtpropertybrowser.h:63
QtAbstractEditorFactory::managerDestroyed
void managerDestroyed(QObject *manager) override
Definition: qtpropertybrowser.h:234
QtBrowserItem
The QtBrowserItem class represents a property in a property browser instance.
Definition: qtpropertybrowser.h:273
QtAbstractEditorFactoryBase::QtAbstractEditorFactoryBase
QtAbstractEditorFactoryBase(QObject *parent=0)
Definition: qtpropertybrowser.h:153
QtProperty
The QtProperty class encapsulates an instance of a property.
Definition: qtpropertybrowser.h:71
QtAbstractEditorFactory::QtAbstractPropertyEditor
friend class QtAbstractPropertyEditor
Definition: qtpropertybrowser.h:267
QtAbstractEditorFactoryBase
The QtAbstractEditorFactoryBase provides an interface for editor factories.
Definition: qtpropertybrowser.h:147
QtAbstractEditorFactory::connectPropertyManager
virtual void connectPropertyManager(PropertyManager *manager)=0
QtAbstractPropertyBrowserPrivate
Definition: qtpropertybrowser.cpp:1288
QtProperty::propertyManager
QtAbstractPropertyManager * propertyManager() const
Definition: qtpropertybrowser.cpp:207
QtAbstractEditorFactory
The QtAbstractEditorFactory is the base template class for editor factories.
Definition: qtpropertybrowser.h:164
QtBrowserItemPrivate
Definition: qtpropertybrowser.cpp:1157
QtAbstractEditorFactory::propertyManager
PropertyManager * propertyManager(QtProperty *property) const
Definition: qtpropertybrowser.h:212
QtAbstractEditorFactory::removePropertyManager
void removePropertyManager(PropertyManager *manager)
Definition: qtpropertybrowser.h:196
QtAbstractPropertyManager
The QtAbstractPropertyManager provides an interface for property managers.
Definition: qtpropertybrowser.h:112
QtAbstractPropertyManagerPrivate
Definition: qtpropertybrowser.cpp:76
QtAbstractEditorFactory::addPropertyManager
void addPropertyManager(PropertyManager *manager)
Definition: qtpropertybrowser.h:184
EchoMode
QLineEdit::EchoMode EchoMode
Definition: qtpropertybrowser.h:66
QtPropertyPrivate
Definition: qtpropertybrowser.cpp:54
QtAbstractPropertyBrowser::setFactoryForManager
void setFactoryForManager(PropertyManager *manager, QtAbstractEditorFactory< PropertyManager > *factory)
Definition: qtpropertybrowser.h:304