qtpropertybrowser.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the tools applications of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. #ifndef QTPROPERTYBROWSER_H
  40. #define QTPROPERTYBROWSER_H
  41. #include <QtWidgets/QWidget>
  42. #include <QtCore/QSet>
  43. QT_BEGIN_NAMESPACE
  44. class QtAbstractPropertyManager;
  45. class QtPropertyPrivate;
  46. class QtProperty
  47. {
  48. public:
  49. virtual ~QtProperty();
  50. QList<QtProperty *> subProperties() const;
  51. QtAbstractPropertyManager *propertyManager() const;
  52. QString toolTip() const { return valueToolTip(); } // Compatibility
  53. QString valueToolTip() const;
  54. QString descriptionToolTip() const;
  55. QString statusTip() const;
  56. QString whatsThis() const;
  57. QString propertyName() const;
  58. bool isEnabled() const;
  59. bool isModified() const;
  60. bool hasValue() const;
  61. QIcon valueIcon() const;
  62. QString valueText() const;
  63. void setToolTip(const QString &text) { setValueToolTip(text); } // Compatibility
  64. void setValueToolTip(const QString &text);
  65. void setDescriptionToolTip(const QString &text);
  66. void setStatusTip(const QString &text);
  67. void setWhatsThis(const QString &text);
  68. void setPropertyName(const QString &text);
  69. void setEnabled(bool enable);
  70. void setModified(bool modified);
  71. void addSubProperty(QtProperty *property);
  72. void insertSubProperty(QtProperty *property, QtProperty *afterProperty);
  73. void removeSubProperty(QtProperty *property);
  74. protected:
  75. explicit QtProperty(QtAbstractPropertyManager *manager);
  76. void propertyChanged();
  77. private:
  78. friend class QtAbstractPropertyManager;
  79. QScopedPointer<QtPropertyPrivate> d_ptr;
  80. };
  81. class QtAbstractPropertyManagerPrivate;
  82. class QtAbstractPropertyManager : public QObject
  83. {
  84. Q_OBJECT
  85. public:
  86. explicit QtAbstractPropertyManager(QObject *parent = 0);
  87. ~QtAbstractPropertyManager();
  88. QSet<QtProperty *> properties() const;
  89. void clear() const;
  90. QtProperty *addProperty(const QString &name = QString());
  91. Q_SIGNALS:
  92. void propertyInserted(QtProperty *property,
  93. QtProperty *parent, QtProperty *after);
  94. void propertyChanged(QtProperty *property);
  95. void propertyRemoved(QtProperty *property, QtProperty *parent);
  96. void propertyDestroyed(QtProperty *property);
  97. protected:
  98. virtual bool hasValue(const QtProperty *property) const;
  99. virtual QIcon valueIcon(const QtProperty *property) const;
  100. virtual QString valueText(const QtProperty *property) const;
  101. virtual void initializeProperty(QtProperty *property) = 0;
  102. virtual void uninitializeProperty(QtProperty *property);
  103. virtual QtProperty *createProperty();
  104. private:
  105. friend class QtProperty;
  106. QScopedPointer<QtAbstractPropertyManagerPrivate> d_ptr;
  107. Q_DECLARE_PRIVATE(QtAbstractPropertyManager)
  108. Q_DISABLE_COPY_MOVE(QtAbstractPropertyManager)
  109. };
  110. class QtAbstractEditorFactoryBase : public QObject
  111. {
  112. Q_OBJECT
  113. public:
  114. virtual QWidget *createEditor(QtProperty *property, QWidget *parent) = 0;
  115. protected:
  116. explicit QtAbstractEditorFactoryBase(QObject *parent = 0)
  117. : QObject(parent) {}
  118. virtual void breakConnection(QtAbstractPropertyManager *manager) = 0;
  119. protected Q_SLOTS:
  120. virtual void managerDestroyed(QObject *manager) = 0;
  121. friend class QtAbstractPropertyBrowser;
  122. };
  123. template <class PropertyManager>
  124. class QtAbstractEditorFactory : public QtAbstractEditorFactoryBase
  125. {
  126. public:
  127. explicit QtAbstractEditorFactory(QObject *parent) : QtAbstractEditorFactoryBase(parent) {}
  128. QWidget *createEditor(QtProperty *property, QWidget *parent)
  129. {
  130. for (PropertyManager *manager : qAsConst(m_managers)) {
  131. if (manager == property->propertyManager()) {
  132. return createEditor(manager, property, parent);
  133. }
  134. }
  135. return 0;
  136. }
  137. void addPropertyManager(PropertyManager *manager)
  138. {
  139. if (m_managers.contains(manager))
  140. return;
  141. m_managers.insert(manager);
  142. connectPropertyManager(manager);
  143. connect(manager, SIGNAL(destroyed(QObject *)),
  144. this, SLOT(managerDestroyed(QObject *)));
  145. }
  146. void removePropertyManager(PropertyManager *manager)
  147. {
  148. if (!m_managers.contains(manager))
  149. return;
  150. disconnect(manager, SIGNAL(destroyed(QObject *)),
  151. this, SLOT(managerDestroyed(QObject *)));
  152. disconnectPropertyManager(manager);
  153. m_managers.remove(manager);
  154. }
  155. QSet<PropertyManager *> propertyManagers() const
  156. {
  157. return m_managers;
  158. }
  159. PropertyManager *propertyManager(QtProperty *property) const
  160. {
  161. QtAbstractPropertyManager *manager = property->propertyManager();
  162. for (PropertyManager *m : qAsConst(m_managers)) {
  163. if (m == manager) {
  164. return m;
  165. }
  166. }
  167. return 0;
  168. }
  169. protected:
  170. virtual void connectPropertyManager(PropertyManager *manager) = 0;
  171. virtual QWidget *createEditor(PropertyManager *manager, QtProperty *property,
  172. QWidget *parent) = 0;
  173. virtual void disconnectPropertyManager(PropertyManager *manager) = 0;
  174. void managerDestroyed(QObject *manager)
  175. {
  176. for (PropertyManager *m : qAsConst(m_managers)) {
  177. if (m == manager) {
  178. m_managers.remove(m);
  179. return;
  180. }
  181. }
  182. }
  183. private:
  184. void breakConnection(QtAbstractPropertyManager *manager)
  185. {
  186. for (PropertyManager *m : qAsConst(m_managers)) {
  187. if (m == manager) {
  188. removePropertyManager(m);
  189. return;
  190. }
  191. }
  192. }
  193. private:
  194. QSet<PropertyManager *> m_managers;
  195. friend class QtAbstractPropertyEditor;
  196. };
  197. class QtAbstractPropertyBrowser;
  198. class QtBrowserItemPrivate;
  199. class QtBrowserItem
  200. {
  201. public:
  202. QtProperty *property() const;
  203. QtBrowserItem *parent() const;
  204. QList<QtBrowserItem *> children() const;
  205. QtAbstractPropertyBrowser *browser() const;
  206. private:
  207. explicit QtBrowserItem(QtAbstractPropertyBrowser *browser, QtProperty *property, QtBrowserItem *parent);
  208. ~QtBrowserItem();
  209. QScopedPointer<QtBrowserItemPrivate> d_ptr;
  210. friend class QtAbstractPropertyBrowserPrivate;
  211. };
  212. class QtAbstractPropertyBrowserPrivate;
  213. class QtAbstractPropertyBrowser : public QWidget
  214. {
  215. Q_OBJECT
  216. public:
  217. explicit QtAbstractPropertyBrowser(QWidget *parent = 0);
  218. ~QtAbstractPropertyBrowser();
  219. QList<QtProperty *> properties() const;
  220. QList<QtBrowserItem *> items(QtProperty *property) const;
  221. QtBrowserItem *topLevelItem(QtProperty *property) const;
  222. QList<QtBrowserItem *> topLevelItems() const;
  223. void clear();
  224. template <class PropertyManager>
  225. void setFactoryForManager(PropertyManager *manager,
  226. QtAbstractEditorFactory<PropertyManager> *factory) {
  227. QtAbstractPropertyManager *abstractManager = manager;
  228. QtAbstractEditorFactoryBase *abstractFactory = factory;
  229. if (addFactory(abstractManager, abstractFactory))
  230. factory->addPropertyManager(manager);
  231. }
  232. void unsetFactoryForManager(QtAbstractPropertyManager *manager);
  233. QtBrowserItem *currentItem() const;
  234. void setCurrentItem(QtBrowserItem *);
  235. Q_SIGNALS:
  236. void currentItemChanged(QtBrowserItem *);
  237. public Q_SLOTS:
  238. QtBrowserItem *addProperty(QtProperty *property);
  239. QtBrowserItem *insertProperty(QtProperty *property, QtProperty *afterProperty);
  240. void removeProperty(QtProperty *property);
  241. protected:
  242. virtual void itemInserted(QtBrowserItem *item, QtBrowserItem *afterItem) = 0;
  243. virtual void itemRemoved(QtBrowserItem *item) = 0;
  244. // can be tooltip, statustip, whatsthis, name, icon, text.
  245. virtual void itemChanged(QtBrowserItem *item) = 0;
  246. virtual QWidget *createEditor(QtProperty *property, QWidget *parent);
  247. private:
  248. bool addFactory(QtAbstractPropertyManager *abstractManager,
  249. QtAbstractEditorFactoryBase *abstractFactory);
  250. QScopedPointer<QtAbstractPropertyBrowserPrivate> d_ptr;
  251. Q_DECLARE_PRIVATE(QtAbstractPropertyBrowser)
  252. Q_DISABLE_COPY_MOVE(QtAbstractPropertyBrowser)
  253. Q_PRIVATE_SLOT(d_func(), void slotPropertyInserted(QtProperty *,
  254. QtProperty *, QtProperty *))
  255. Q_PRIVATE_SLOT(d_func(), void slotPropertyRemoved(QtProperty *,
  256. QtProperty *))
  257. Q_PRIVATE_SLOT(d_func(), void slotPropertyDestroyed(QtProperty *))
  258. Q_PRIVATE_SLOT(d_func(), void slotPropertyDataChanged(QtProperty *))
  259. };
  260. QT_END_NAMESPACE
  261. #endif // QTPROPERTYBROWSER_H