1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QTPROPERTYBROWSER_H |
5 | #define QTPROPERTYBROWSER_H |
6 | |
7 | #include <QtWidgets/QWidget> |
8 | #include <QtCore/QSet> |
9 | |
10 | QT_BEGIN_NAMESPACE |
11 | |
12 | class QtAbstractPropertyManager; |
13 | class QtPropertyPrivate; |
14 | |
15 | class QtProperty |
16 | { |
17 | public: |
18 | virtual ~QtProperty(); |
19 | |
20 | QList<QtProperty *> subProperties() const; |
21 | |
22 | QtAbstractPropertyManager *propertyManager() const; |
23 | |
24 | QString toolTip() const { return valueToolTip(); } // Compatibility |
25 | QString valueToolTip() const; |
26 | QString descriptionToolTip() const; |
27 | QString statusTip() const; |
28 | QString whatsThis() const; |
29 | QString propertyName() const; |
30 | bool isEnabled() const; |
31 | bool isModified() const; |
32 | |
33 | bool hasValue() const; |
34 | QIcon valueIcon() const; |
35 | QString valueText() const; |
36 | |
37 | void setToolTip(const QString &text) { setValueToolTip(text); } // Compatibility |
38 | void setValueToolTip(const QString &text); |
39 | void setDescriptionToolTip(const QString &text); |
40 | void setStatusTip(const QString &text); |
41 | void setWhatsThis(const QString &text); |
42 | void setPropertyName(const QString &text); |
43 | void setEnabled(bool enable); |
44 | void setModified(bool modified); |
45 | |
46 | void addSubProperty(QtProperty *property); |
47 | void insertSubProperty(QtProperty *property, QtProperty *afterProperty); |
48 | void removeSubProperty(QtProperty *property); |
49 | protected: |
50 | explicit QtProperty(QtAbstractPropertyManager *manager); |
51 | void propertyChanged(); |
52 | private: |
53 | friend class QtAbstractPropertyManager; |
54 | QScopedPointer<QtPropertyPrivate> d_ptr; |
55 | }; |
56 | |
57 | class QtAbstractPropertyManagerPrivate; |
58 | |
59 | class QtAbstractPropertyManager : public QObject |
60 | { |
61 | Q_OBJECT |
62 | public: |
63 | |
64 | explicit QtAbstractPropertyManager(QObject *parent = 0); |
65 | ~QtAbstractPropertyManager(); |
66 | |
67 | QSet<QtProperty *> properties() const; |
68 | void clear() const; |
69 | |
70 | QtProperty *addProperty(const QString &name = QString()); |
71 | Q_SIGNALS: |
72 | void propertyInserted(QtProperty *property, QtProperty *parent, QtProperty *after); |
73 | void propertyChanged(QtProperty *property); |
74 | void propertyRemoved(QtProperty *property, QtProperty *parent); |
75 | void propertyDestroyed(QtProperty *property); |
76 | protected: |
77 | virtual bool hasValue(const QtProperty *property) const; |
78 | virtual QIcon valueIcon(const QtProperty *property) const; |
79 | virtual QString valueText(const QtProperty *property) const; |
80 | virtual void initializeProperty(QtProperty *property) = 0; |
81 | virtual void uninitializeProperty(QtProperty *property); |
82 | virtual QtProperty *createProperty(); |
83 | private: |
84 | friend class QtProperty; |
85 | QScopedPointer<QtAbstractPropertyManagerPrivate> d_ptr; |
86 | Q_DECLARE_PRIVATE(QtAbstractPropertyManager) |
87 | Q_DISABLE_COPY_MOVE(QtAbstractPropertyManager) |
88 | }; |
89 | |
90 | class QtAbstractEditorFactoryBase : public QObject |
91 | { |
92 | Q_OBJECT |
93 | public: |
94 | virtual QWidget *createEditor(QtProperty *property, QWidget *parent) = 0; |
95 | protected: |
96 | explicit QtAbstractEditorFactoryBase(QObject *parent = 0) |
97 | : QObject(parent) {} |
98 | |
99 | virtual void breakConnection(QtAbstractPropertyManager *manager) = 0; |
100 | protected Q_SLOTS: |
101 | virtual void managerDestroyed(QObject *manager) = 0; |
102 | |
103 | friend class QtAbstractPropertyBrowser; |
104 | }; |
105 | |
106 | template <class PropertyManager> |
107 | class QtAbstractEditorFactory : public QtAbstractEditorFactoryBase |
108 | { |
109 | public: |
110 | explicit QtAbstractEditorFactory(QObject *parent) : QtAbstractEditorFactoryBase(parent) {} |
111 | QWidget *createEditor(QtProperty *property, QWidget *parent) override |
112 | { |
113 | for (PropertyManager *manager : std::as_const(m_managers)) { |
114 | if (manager == property->propertyManager()) { |
115 | return createEditor(manager, property, parent); |
116 | } |
117 | } |
118 | return 0; |
119 | } |
120 | void addPropertyManager(PropertyManager *manager) |
121 | { |
122 | if (m_managers.contains(manager)) |
123 | return; |
124 | m_managers.insert(manager); |
125 | connectPropertyManager(manager); |
126 | connect(manager, &QObject::destroyed, |
127 | this, &QtAbstractEditorFactory<PropertyManager>::managerDestroyed); |
128 | } |
129 | void removePropertyManager(PropertyManager *manager) |
130 | { |
131 | if (!m_managers.contains(manager)) |
132 | return; |
133 | disconnect(manager, &QObject::destroyed, |
134 | this, &QtAbstractEditorFactory<PropertyManager>::managerDestroyed); |
135 | disconnectPropertyManager(manager); |
136 | m_managers.remove(manager); |
137 | } |
138 | QSet<PropertyManager *> propertyManagers() const |
139 | { |
140 | return m_managers; |
141 | } |
142 | PropertyManager *propertyManager(QtProperty *property) const |
143 | { |
144 | QtAbstractPropertyManager *manager = property->propertyManager(); |
145 | for (PropertyManager *m : std::as_const(m_managers)) { |
146 | if (m == manager) { |
147 | return m; |
148 | } |
149 | } |
150 | return 0; |
151 | } |
152 | protected: |
153 | virtual void connectPropertyManager(PropertyManager *manager) = 0; |
154 | virtual QWidget *createEditor(PropertyManager *manager, QtProperty *property, |
155 | QWidget *parent) = 0; |
156 | virtual void disconnectPropertyManager(PropertyManager *manager) = 0; |
157 | void managerDestroyed(QObject *manager) override |
158 | { |
159 | for (PropertyManager *m : std::as_const(m_managers)) { |
160 | if (m == manager) { |
161 | m_managers.remove(m); |
162 | return; |
163 | } |
164 | } |
165 | } |
166 | private: |
167 | void breakConnection(QtAbstractPropertyManager *manager) override |
168 | { |
169 | for (PropertyManager *m : std::as_const(m_managers)) { |
170 | if (m == manager) { |
171 | removePropertyManager(manager: m); |
172 | return; |
173 | } |
174 | } |
175 | } |
176 | private: |
177 | QSet<PropertyManager *> m_managers; |
178 | friend class QtAbstractPropertyEditor; |
179 | }; |
180 | |
181 | class QtAbstractPropertyBrowser; |
182 | class QtBrowserItemPrivate; |
183 | |
184 | class QtBrowserItem |
185 | { |
186 | public: |
187 | QtProperty *property() const; |
188 | QtBrowserItem *parent() const; |
189 | QList<QtBrowserItem *> children() const; |
190 | QtAbstractPropertyBrowser *browser() const; |
191 | private: |
192 | explicit QtBrowserItem(QtAbstractPropertyBrowser *browser, QtProperty *property, QtBrowserItem *parent); |
193 | ~QtBrowserItem(); |
194 | QScopedPointer<QtBrowserItemPrivate> d_ptr; |
195 | friend class QtAbstractPropertyBrowserPrivate; |
196 | }; |
197 | |
198 | class QtAbstractPropertyBrowserPrivate; |
199 | |
200 | class QtAbstractPropertyBrowser : public QWidget |
201 | { |
202 | Q_OBJECT |
203 | public: |
204 | |
205 | explicit QtAbstractPropertyBrowser(QWidget *parent = 0); |
206 | ~QtAbstractPropertyBrowser(); |
207 | |
208 | QList<QtProperty *> properties() const; |
209 | QList<QtBrowserItem *> items(QtProperty *property) const; |
210 | QtBrowserItem *topLevelItem(QtProperty *property) const; |
211 | QList<QtBrowserItem *> topLevelItems() const; |
212 | void clear(); |
213 | |
214 | template <class PropertyManager> |
215 | void setFactoryForManager(PropertyManager *manager, |
216 | QtAbstractEditorFactory<PropertyManager> *factory) { |
217 | QtAbstractPropertyManager *abstractManager = manager; |
218 | QtAbstractEditorFactoryBase *abstractFactory = factory; |
219 | |
220 | if (addFactory(abstractManager, abstractFactory)) |
221 | factory->addPropertyManager(manager); |
222 | } |
223 | |
224 | void unsetFactoryForManager(QtAbstractPropertyManager *manager); |
225 | |
226 | QtBrowserItem *currentItem() const; |
227 | void setCurrentItem(QtBrowserItem *); |
228 | |
229 | Q_SIGNALS: |
230 | void currentItemChanged(QtBrowserItem *); |
231 | |
232 | public Q_SLOTS: |
233 | |
234 | QtBrowserItem *addProperty(QtProperty *property); |
235 | QtBrowserItem *insertProperty(QtProperty *property, QtProperty *afterProperty); |
236 | void removeProperty(QtProperty *property); |
237 | |
238 | protected: |
239 | |
240 | virtual void itemInserted(QtBrowserItem *item, QtBrowserItem *afterItem) = 0; |
241 | virtual void itemRemoved(QtBrowserItem *item) = 0; |
242 | // can be tooltip, statustip, whatsthis, name, icon, text. |
243 | virtual void itemChanged(QtBrowserItem *item) = 0; |
244 | |
245 | virtual QWidget *createEditor(QtProperty *property, QWidget *parent); |
246 | private: |
247 | |
248 | bool addFactory(QtAbstractPropertyManager *abstractManager, |
249 | QtAbstractEditorFactoryBase *abstractFactory); |
250 | |
251 | QScopedPointer<QtAbstractPropertyBrowserPrivate> d_ptr; |
252 | Q_DECLARE_PRIVATE(QtAbstractPropertyBrowser) |
253 | Q_DISABLE_COPY_MOVE(QtAbstractPropertyBrowser) |
254 | }; |
255 | |
256 | QT_END_NAMESPACE |
257 | |
258 | #endif // QTPROPERTYBROWSER_H |
259 | |