qtpropertybrowser.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. 
  2. /****************************************************************************
  3. **
  4. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  5. ** Contact: http://www.qt-project.org/legal
  6. **
  7. ** This file is part of the Qt Solutions component.
  8. **
  9. ** $QT_BEGIN_LICENSE:BSD$
  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 Digia Plc and its Subsidiary(-ies) nor the names
  22. ** of its contributors may be used to endorse or promote products derived
  23. ** from this software without specific prior written permission.
  24. **
  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. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #ifndef QTPROPERTYBROWSER_H
  42. #define QTPROPERTYBROWSER_H
  43. #include <QWidget>
  44. #include <QSet>
  45. #include <QLineEdit>
  46. #if QT_VERSION >= 0x040400
  47. QT_BEGIN_NAMESPACE
  48. #endif
  49. #if defined(Q_OS_WIN)
  50. # if !defined(QT_QTPROPERTYBROWSER_EXPORT) && !defined(QT_QTPROPERTYBROWSER_IMPORT)
  51. # define QT_QTPROPERTYBROWSER_EXPORT
  52. # elif defined(QT_QTPROPERTYBROWSER_IMPORT)
  53. # if defined(QT_QTPROPERTYBROWSER_EXPORT)
  54. # undef QT_QTPROPERTYBROWSER_EXPORT
  55. # endif
  56. # define QT_QTPROPERTYBROWSER_EXPORT __declspec(dllimport)
  57. # elif defined(QT_QTPROPERTYBROWSER_EXPORT)
  58. # undef QT_QTPROPERTYBROWSER_EXPORT
  59. # define QT_QTPROPERTYBROWSER_EXPORT __declspec(dllexport)
  60. # endif
  61. #else
  62. # define QT_QTPROPERTYBROWSER_EXPORT
  63. #endif
  64. typedef QLineEdit::EchoMode EchoMode;
  65. class QtAbstractPropertyManager;
  66. class QtPropertyPrivate;
  67. class QT_QTPROPERTYBROWSER_EXPORT QtProperty
  68. {
  69. public:
  70. virtual ~QtProperty();
  71. QList<QtProperty *> subProperties() const;
  72. QtAbstractPropertyManager *propertyManager() const;
  73. QString toolTip() const;
  74. QString statusTip() const;
  75. QString whatsThis() const;
  76. QString propertyName() const;
  77. bool isEnabled() const;
  78. bool isModified() const;
  79. bool hasValue() const;
  80. QIcon valueIcon() const;
  81. QString valueText() const;
  82. QString displayText() const;
  83. void setToolTip(const QString &text);
  84. void setStatusTip(const QString &text);
  85. void setWhatsThis(const QString &text);
  86. void setPropertyName(const QString &text);
  87. void setEnabled(bool enable);
  88. void setModified(bool modified);
  89. void addSubProperty(QtProperty *property);
  90. void insertSubProperty(QtProperty *property, QtProperty *afterProperty);
  91. void removeSubProperty(QtProperty *property);
  92. protected:
  93. explicit QtProperty(QtAbstractPropertyManager *manager);
  94. void propertyChanged();
  95. private:
  96. friend class QtAbstractPropertyManager;
  97. QtPropertyPrivate *d_ptr;
  98. };
  99. class QtAbstractPropertyManagerPrivate;
  100. class QT_QTPROPERTYBROWSER_EXPORT QtAbstractPropertyManager : public QObject
  101. {
  102. Q_OBJECT
  103. public:
  104. explicit QtAbstractPropertyManager(QObject *parent = 0);
  105. ~QtAbstractPropertyManager();
  106. QSet<QtProperty *> properties() const;
  107. void clear() const;
  108. QtProperty *addProperty(const QString &name = QString());
  109. Q_SIGNALS:
  110. void propertyInserted(QtProperty *property,
  111. QtProperty *parent, QtProperty *after);
  112. void propertyChanged(QtProperty *property);
  113. void propertyRemoved(QtProperty *property, QtProperty *parent);
  114. void propertyDestroyed(QtProperty *property);
  115. protected:
  116. virtual bool hasValue(const QtProperty *property) const;
  117. virtual QIcon valueIcon(const QtProperty *property) const;
  118. virtual QString valueText(const QtProperty *property) const;
  119. virtual QString displayText(const QtProperty *property) const;
  120. virtual EchoMode echoMode(const QtProperty *) const;
  121. virtual void initializeProperty(QtProperty *property) = 0;
  122. virtual void uninitializeProperty(QtProperty *property);
  123. virtual QtProperty *createProperty();
  124. private:
  125. friend class QtProperty;
  126. QtAbstractPropertyManagerPrivate *d_ptr;
  127. Q_DECLARE_PRIVATE(QtAbstractPropertyManager)
  128. Q_DISABLE_COPY(QtAbstractPropertyManager)
  129. };
  130. class QT_QTPROPERTYBROWSER_EXPORT QtAbstractEditorFactoryBase : public QObject
  131. {
  132. Q_OBJECT
  133. public:
  134. virtual QWidget *createEditor(QtProperty *property, QWidget *parent) = 0;
  135. protected:
  136. explicit QtAbstractEditorFactoryBase(QObject *parent = 0)
  137. : QObject(parent) {}
  138. virtual void breakConnection(QtAbstractPropertyManager *manager) = 0;
  139. protected Q_SLOTS:
  140. virtual void managerDestroyed(QObject *manager) = 0;
  141. friend class QtAbstractPropertyBrowser;
  142. };
  143. template <class PropertyManager>
  144. class QtAbstractEditorFactory : public QtAbstractEditorFactoryBase
  145. {
  146. public:
  147. explicit QtAbstractEditorFactory(QObject *parent) : QtAbstractEditorFactoryBase(parent) {}
  148. QWidget *createEditor(QtProperty *property, QWidget *parent)
  149. {
  150. QSetIterator<PropertyManager *> it(m_managers);
  151. while (it.hasNext()) {
  152. PropertyManager *manager = it.next();
  153. if (manager == property->propertyManager()) {
  154. return createEditor(manager, property, parent);
  155. }
  156. }
  157. return 0;
  158. }
  159. void addPropertyManager(PropertyManager *manager)
  160. {
  161. if (m_managers.contains(manager))
  162. return;
  163. m_managers.insert(manager);
  164. connectPropertyManager(manager);
  165. connect(manager, SIGNAL(destroyed(QObject *)),
  166. this, SLOT(managerDestroyed(QObject *)));
  167. }
  168. void removePropertyManager(PropertyManager *manager)
  169. {
  170. if (!m_managers.contains(manager))
  171. return;
  172. disconnect(manager, SIGNAL(destroyed(QObject *)),
  173. this, SLOT(managerDestroyed(QObject *)));
  174. disconnectPropertyManager(manager);
  175. m_managers.remove(manager);
  176. }
  177. QSet<PropertyManager *> propertyManagers() const
  178. {
  179. return m_managers;
  180. }
  181. PropertyManager *propertyManager(QtProperty *property) const
  182. {
  183. QtAbstractPropertyManager *manager = property->propertyManager();
  184. QSetIterator<PropertyManager *> itManager(m_managers);
  185. while (itManager.hasNext()) {
  186. PropertyManager *m = itManager.next();
  187. if (m == manager) {
  188. return m;
  189. }
  190. }
  191. return 0;
  192. }
  193. protected:
  194. virtual void connectPropertyManager(PropertyManager *manager) = 0;
  195. virtual QWidget *createEditor(PropertyManager *manager, QtProperty *property,
  196. QWidget *parent) = 0;
  197. virtual void disconnectPropertyManager(PropertyManager *manager) = 0;
  198. void managerDestroyed(QObject *manager)
  199. {
  200. QSetIterator<PropertyManager *> it(m_managers);
  201. while (it.hasNext()) {
  202. PropertyManager *m = it.next();
  203. if (m == manager) {
  204. m_managers.remove(m);
  205. return;
  206. }
  207. }
  208. }
  209. private:
  210. void breakConnection(QtAbstractPropertyManager *manager)
  211. {
  212. QSetIterator<PropertyManager *> it(m_managers);
  213. while (it.hasNext()) {
  214. PropertyManager *m = it.next();
  215. if (m == manager) {
  216. removePropertyManager(m);
  217. return;
  218. }
  219. }
  220. }
  221. private:
  222. QSet<PropertyManager *> m_managers;
  223. friend class QtAbstractPropertyEditor;
  224. };
  225. class QtAbstractPropertyBrowser;
  226. class QtBrowserItemPrivate;
  227. class QT_QTPROPERTYBROWSER_EXPORT QtBrowserItem
  228. {
  229. public:
  230. QtProperty *property() const;
  231. QtBrowserItem *parent() const;
  232. QList<QtBrowserItem *> children() const;
  233. QtAbstractPropertyBrowser *browser() const;
  234. private:
  235. explicit QtBrowserItem(QtAbstractPropertyBrowser *browser, QtProperty *property, QtBrowserItem *parent);
  236. ~QtBrowserItem();
  237. QtBrowserItemPrivate *d_ptr;
  238. friend class QtAbstractPropertyBrowserPrivate;
  239. };
  240. class QtAbstractPropertyBrowserPrivate;
  241. class QT_QTPROPERTYBROWSER_EXPORT QtAbstractPropertyBrowser : public QWidget
  242. {
  243. Q_OBJECT
  244. public:
  245. explicit QtAbstractPropertyBrowser(QWidget *parent = 0);
  246. ~QtAbstractPropertyBrowser();
  247. QList<QtProperty *> properties() const;
  248. QList<QtBrowserItem *> items(QtProperty *property) const;
  249. QtBrowserItem *topLevelItem(QtProperty *property) const;
  250. QList<QtBrowserItem *> topLevelItems() const;
  251. void clear();
  252. template <class PropertyManager>
  253. void setFactoryForManager(PropertyManager *manager,
  254. QtAbstractEditorFactory<PropertyManager> *factory) {
  255. QtAbstractPropertyManager *abstractManager = manager;
  256. QtAbstractEditorFactoryBase *abstractFactory = factory;
  257. if (addFactory(abstractManager, abstractFactory))
  258. factory->addPropertyManager(manager);
  259. }
  260. void unsetFactoryForManager(QtAbstractPropertyManager *manager);
  261. QtBrowserItem *currentItem() const;
  262. void setCurrentItem(QtBrowserItem *);
  263. Q_SIGNALS:
  264. void currentItemChanged(QtBrowserItem *);
  265. public Q_SLOTS:
  266. QtBrowserItem *addProperty(QtProperty *property);
  267. QtBrowserItem *insertProperty(QtProperty *property, QtProperty *afterProperty);
  268. void removeProperty(QtProperty *property);
  269. //// 2021-11-28 增加删除扩展动态属性
  270. //void removePropertyEx(QtProperty* property);
  271. protected:
  272. virtual void itemInserted(QtBrowserItem *item, QtBrowserItem *afterItem) = 0;
  273. virtual void itemRemoved(QtBrowserItem *item) = 0;
  274. // can be tooltip, statustip, whatsthis, name, icon, text.
  275. virtual void itemChanged(QtBrowserItem *item) = 0;
  276. virtual QWidget *createEditor(QtProperty *property, QWidget *parent);
  277. private:
  278. bool addFactory(QtAbstractPropertyManager *abstractManager,
  279. QtAbstractEditorFactoryBase *abstractFactory);
  280. QtAbstractPropertyBrowserPrivate *d_ptr;
  281. Q_DECLARE_PRIVATE(QtAbstractPropertyBrowser)
  282. Q_DISABLE_COPY(QtAbstractPropertyBrowser)
  283. Q_PRIVATE_SLOT(d_func(), void slotPropertyInserted(QtProperty *,
  284. QtProperty *, QtProperty *))
  285. Q_PRIVATE_SLOT(d_func(), void slotPropertyRemoved(QtProperty *,
  286. QtProperty *))
  287. Q_PRIVATE_SLOT(d_func(), void slotPropertyDestroyed(QtProperty *))
  288. Q_PRIVATE_SLOT(d_func(), void slotPropertyDataChanged(QtProperty *))
  289. };
  290. #if QT_VERSION >= 0x040400
  291. QT_END_NAMESPACE
  292. #endif
  293. #endif // QTPROPERTYBROWSER_H