| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the tools applications of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 15 | ** | 
|---|
| 16 | ** GNU Lesser General Public License Usage | 
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 20 | ** packaging of this file.  Please review the following information to | 
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 23 | ** | 
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional | 
|---|
| 25 | ** rights.  These rights are described in the Nokia Qt LGPL Exception | 
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #ifndef QTPROPERTYBROWSER_H | 
|---|
| 43 | #define QTPROPERTYBROWSER_H | 
|---|
| 44 |  | 
|---|
| 45 | #include <QtGui/QWidget> | 
|---|
| 46 | #include <QtCore/QSet> | 
|---|
| 47 |  | 
|---|
| 48 | QT_BEGIN_NAMESPACE | 
|---|
| 49 |  | 
|---|
| 50 | class QtAbstractPropertyManager; | 
|---|
| 51 | class QtPropertyPrivate; | 
|---|
| 52 |  | 
|---|
| 53 | class QtProperty | 
|---|
| 54 | { | 
|---|
| 55 | public: | 
|---|
| 56 | virtual ~QtProperty(); | 
|---|
| 57 |  | 
|---|
| 58 | QList<QtProperty *> subProperties() const; | 
|---|
| 59 |  | 
|---|
| 60 | QtAbstractPropertyManager *propertyManager() const; | 
|---|
| 61 |  | 
|---|
| 62 | QString toolTip() const; | 
|---|
| 63 | QString statusTip() const; | 
|---|
| 64 | QString whatsThis() const; | 
|---|
| 65 | QString propertyName() const; | 
|---|
| 66 | bool isEnabled() const; | 
|---|
| 67 | bool isModified() const; | 
|---|
| 68 |  | 
|---|
| 69 | bool hasValue() const; | 
|---|
| 70 | QIcon valueIcon() const; | 
|---|
| 71 | QString valueText() const; | 
|---|
| 72 |  | 
|---|
| 73 | void setToolTip(const QString &text); | 
|---|
| 74 | void setStatusTip(const QString &text); | 
|---|
| 75 | void setWhatsThis(const QString &text); | 
|---|
| 76 | void setPropertyName(const QString &text); | 
|---|
| 77 | void setEnabled(bool enable); | 
|---|
| 78 | void setModified(bool modified); | 
|---|
| 79 |  | 
|---|
| 80 | void addSubProperty(QtProperty *property); | 
|---|
| 81 | void insertSubProperty(QtProperty *property, QtProperty *afterProperty); | 
|---|
| 82 | void removeSubProperty(QtProperty *property); | 
|---|
| 83 | protected: | 
|---|
| 84 | explicit QtProperty(QtAbstractPropertyManager *manager); | 
|---|
| 85 | void propertyChanged(); | 
|---|
| 86 | private: | 
|---|
| 87 | friend class QtAbstractPropertyManager; | 
|---|
| 88 | QScopedPointer<QtPropertyPrivate> d_ptr; | 
|---|
| 89 | }; | 
|---|
| 90 |  | 
|---|
| 91 | class QtAbstractPropertyManagerPrivate; | 
|---|
| 92 |  | 
|---|
| 93 | class QtAbstractPropertyManager : public QObject | 
|---|
| 94 | { | 
|---|
| 95 | Q_OBJECT | 
|---|
| 96 | public: | 
|---|
| 97 |  | 
|---|
| 98 | explicit QtAbstractPropertyManager(QObject *parent = 0); | 
|---|
| 99 | ~QtAbstractPropertyManager(); | 
|---|
| 100 |  | 
|---|
| 101 | QSet<QtProperty *> properties() const; | 
|---|
| 102 | void clear() const; | 
|---|
| 103 |  | 
|---|
| 104 | QtProperty *addProperty(const QString &name = QString()); | 
|---|
| 105 | Q_SIGNALS: | 
|---|
| 106 |  | 
|---|
| 107 | void propertyInserted(QtProperty *property, | 
|---|
| 108 | QtProperty *parent, QtProperty *after); | 
|---|
| 109 | void propertyChanged(QtProperty *property); | 
|---|
| 110 | void propertyRemoved(QtProperty *property, QtProperty *parent); | 
|---|
| 111 | void propertyDestroyed(QtProperty *property); | 
|---|
| 112 | protected: | 
|---|
| 113 | virtual bool hasValue(const QtProperty *property) const; | 
|---|
| 114 | virtual QIcon valueIcon(const QtProperty *property) const; | 
|---|
| 115 | virtual QString valueText(const QtProperty *property) const; | 
|---|
| 116 | virtual void initializeProperty(QtProperty *property) = 0; | 
|---|
| 117 | virtual void uninitializeProperty(QtProperty *property); | 
|---|
| 118 | virtual QtProperty *createProperty(); | 
|---|
| 119 | private: | 
|---|
| 120 | friend class QtProperty; | 
|---|
| 121 | QScopedPointer<QtAbstractPropertyManagerPrivate> d_ptr; | 
|---|
| 122 | Q_DECLARE_PRIVATE(QtAbstractPropertyManager) | 
|---|
| 123 | Q_DISABLE_COPY(QtAbstractPropertyManager) | 
|---|
| 124 | }; | 
|---|
| 125 |  | 
|---|
| 126 | class QtAbstractEditorFactoryBase : public QObject | 
|---|
| 127 | { | 
|---|
| 128 | Q_OBJECT | 
|---|
| 129 | public: | 
|---|
| 130 | virtual QWidget *createEditor(QtProperty *property, QWidget *parent) = 0; | 
|---|
| 131 | protected: | 
|---|
| 132 | explicit QtAbstractEditorFactoryBase(QObject *parent = 0) | 
|---|
| 133 | : QObject(parent) {} | 
|---|
| 134 |  | 
|---|
| 135 | virtual void breakConnection(QtAbstractPropertyManager *manager) = 0; | 
|---|
| 136 | protected Q_SLOTS: | 
|---|
| 137 | virtual void managerDestroyed(QObject *manager) = 0; | 
|---|
| 138 |  | 
|---|
| 139 | friend class QtAbstractPropertyBrowser; | 
|---|
| 140 | }; | 
|---|
| 141 |  | 
|---|
| 142 | template <class PropertyManager> | 
|---|
| 143 | class QtAbstractEditorFactory : public QtAbstractEditorFactoryBase | 
|---|
| 144 | { | 
|---|
| 145 | public: | 
|---|
| 146 | explicit QtAbstractEditorFactory(QObject *parent) : QtAbstractEditorFactoryBase(parent) {} | 
|---|
| 147 | QWidget *createEditor(QtProperty *property, QWidget *parent) | 
|---|
| 148 | { | 
|---|
| 149 | QSetIterator<PropertyManager *> it(m_managers); | 
|---|
| 150 | while (it.hasNext()) { | 
|---|
| 151 | PropertyManager *manager = it.next(); | 
|---|
| 152 | if (manager == property->propertyManager()) { | 
|---|
| 153 | return createEditor(manager, property, parent); | 
|---|
| 154 | } | 
|---|
| 155 | } | 
|---|
| 156 | return 0; | 
|---|
| 157 | } | 
|---|
| 158 | void addPropertyManager(PropertyManager *manager) | 
|---|
| 159 | { | 
|---|
| 160 | if (m_managers.contains(manager)) | 
|---|
| 161 | return; | 
|---|
| 162 | m_managers.insert(manager); | 
|---|
| 163 | connectPropertyManager(manager); | 
|---|
| 164 | connect(manager, SIGNAL(destroyed(QObject *)), | 
|---|
| 165 | this, SLOT(managerDestroyed(QObject *))); | 
|---|
| 166 | } | 
|---|
| 167 | void removePropertyManager(PropertyManager *manager) | 
|---|
| 168 | { | 
|---|
| 169 | if (!m_managers.contains(manager)) | 
|---|
| 170 | return; | 
|---|
| 171 | disconnect(manager, SIGNAL(destroyed(QObject *)), | 
|---|
| 172 | this, SLOT(managerDestroyed(QObject *))); | 
|---|
| 173 | disconnectPropertyManager(manager); | 
|---|
| 174 | m_managers.remove(manager); | 
|---|
| 175 | } | 
|---|
| 176 | QSet<PropertyManager *> propertyManagers() const | 
|---|
| 177 | { | 
|---|
| 178 | return m_managers; | 
|---|
| 179 | } | 
|---|
| 180 | PropertyManager *propertyManager(QtProperty *property) const | 
|---|
| 181 | { | 
|---|
| 182 | QtAbstractPropertyManager *manager = property->propertyManager(); | 
|---|
| 183 | QSetIterator<PropertyManager *> itManager(m_managers); | 
|---|
| 184 | while (itManager.hasNext()) { | 
|---|
| 185 | PropertyManager *m = itManager.next(); | 
|---|
| 186 | if (m == manager) { | 
|---|
| 187 | return m; | 
|---|
| 188 | } | 
|---|
| 189 | } | 
|---|
| 190 | return 0; | 
|---|
| 191 | } | 
|---|
| 192 | protected: | 
|---|
| 193 | virtual void connectPropertyManager(PropertyManager *manager) = 0; | 
|---|
| 194 | virtual QWidget *createEditor(PropertyManager *manager, QtProperty *property, | 
|---|
| 195 | QWidget *parent) = 0; | 
|---|
| 196 | virtual void disconnectPropertyManager(PropertyManager *manager) = 0; | 
|---|
| 197 | void managerDestroyed(QObject *manager) | 
|---|
| 198 | { | 
|---|
| 199 | QSetIterator<PropertyManager *> it(m_managers); | 
|---|
| 200 | while (it.hasNext()) { | 
|---|
| 201 | PropertyManager *m = it.next(); | 
|---|
| 202 | if (m == manager) { | 
|---|
| 203 | m_managers.remove(m); | 
|---|
| 204 | return; | 
|---|
| 205 | } | 
|---|
| 206 | } | 
|---|
| 207 | } | 
|---|
| 208 | private: | 
|---|
| 209 | void breakConnection(QtAbstractPropertyManager *manager) | 
|---|
| 210 | { | 
|---|
| 211 | QSetIterator<PropertyManager *> it(m_managers); | 
|---|
| 212 | while (it.hasNext()) { | 
|---|
| 213 | PropertyManager *m = it.next(); | 
|---|
| 214 | if (m == manager) { | 
|---|
| 215 | removePropertyManager(m); | 
|---|
| 216 | return; | 
|---|
| 217 | } | 
|---|
| 218 | } | 
|---|
| 219 | } | 
|---|
| 220 | private: | 
|---|
| 221 | QSet<PropertyManager *> m_managers; | 
|---|
| 222 | friend class QtAbstractPropertyEditor; | 
|---|
| 223 | }; | 
|---|
| 224 |  | 
|---|
| 225 | class QtAbstractPropertyBrowser; | 
|---|
| 226 | class QtBrowserItemPrivate; | 
|---|
| 227 |  | 
|---|
| 228 | class QtBrowserItem | 
|---|
| 229 | { | 
|---|
| 230 | public: | 
|---|
| 231 | QtProperty *property() const; | 
|---|
| 232 | QtBrowserItem *parent() const; | 
|---|
| 233 | QList<QtBrowserItem *> children() const; | 
|---|
| 234 | QtAbstractPropertyBrowser *browser() const; | 
|---|
| 235 | private: | 
|---|
| 236 | explicit QtBrowserItem(QtAbstractPropertyBrowser *browser, QtProperty *property, QtBrowserItem *parent); | 
|---|
| 237 | ~QtBrowserItem(); | 
|---|
| 238 | QScopedPointer<QtBrowserItemPrivate> d_ptr; | 
|---|
| 239 | friend class QtAbstractPropertyBrowserPrivate; | 
|---|
| 240 | }; | 
|---|
| 241 |  | 
|---|
| 242 | class QtAbstractPropertyBrowserPrivate; | 
|---|
| 243 |  | 
|---|
| 244 | class QtAbstractPropertyBrowser : public QWidget | 
|---|
| 245 | { | 
|---|
| 246 | Q_OBJECT | 
|---|
| 247 | public: | 
|---|
| 248 |  | 
|---|
| 249 | explicit QtAbstractPropertyBrowser(QWidget *parent = 0); | 
|---|
| 250 | ~QtAbstractPropertyBrowser(); | 
|---|
| 251 |  | 
|---|
| 252 | QList<QtProperty *> properties() const; | 
|---|
| 253 | QList<QtBrowserItem *> items(QtProperty *property) const; | 
|---|
| 254 | QtBrowserItem *topLevelItem(QtProperty *property) const; | 
|---|
| 255 | QList<QtBrowserItem *> topLevelItems() const; | 
|---|
| 256 | void clear(); | 
|---|
| 257 |  | 
|---|
| 258 | template <class PropertyManager> | 
|---|
| 259 | void setFactoryForManager(PropertyManager *manager, | 
|---|
| 260 | QtAbstractEditorFactory<PropertyManager> *factory) { | 
|---|
| 261 | QtAbstractPropertyManager *abstractManager = manager; | 
|---|
| 262 | QtAbstractEditorFactoryBase *abstractFactory = factory; | 
|---|
| 263 |  | 
|---|
| 264 | if (addFactory(abstractManager, abstractFactory)) | 
|---|
| 265 | factory->addPropertyManager(manager); | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 | void unsetFactoryForManager(QtAbstractPropertyManager *manager); | 
|---|
| 269 |  | 
|---|
| 270 | QtBrowserItem *currentItem() const; | 
|---|
| 271 | void setCurrentItem(QtBrowserItem *); | 
|---|
| 272 |  | 
|---|
| 273 | Q_SIGNALS: | 
|---|
| 274 | void currentItemChanged(QtBrowserItem *); | 
|---|
| 275 |  | 
|---|
| 276 | public Q_SLOTS: | 
|---|
| 277 |  | 
|---|
| 278 | QtBrowserItem *addProperty(QtProperty *property); | 
|---|
| 279 | QtBrowserItem *insertProperty(QtProperty *property, QtProperty *afterProperty); | 
|---|
| 280 | void removeProperty(QtProperty *property); | 
|---|
| 281 |  | 
|---|
| 282 | protected: | 
|---|
| 283 |  | 
|---|
| 284 | virtual void itemInserted(QtBrowserItem *item, QtBrowserItem *afterItem) = 0; | 
|---|
| 285 | virtual void itemRemoved(QtBrowserItem *item) = 0; | 
|---|
| 286 | // can be tooltip, statustip, whatsthis, name, icon, text. | 
|---|
| 287 | virtual void itemChanged(QtBrowserItem *item) = 0; | 
|---|
| 288 |  | 
|---|
| 289 | virtual QWidget *createEditor(QtProperty *property, QWidget *parent); | 
|---|
| 290 | private: | 
|---|
| 291 |  | 
|---|
| 292 | bool addFactory(QtAbstractPropertyManager *abstractManager, | 
|---|
| 293 | QtAbstractEditorFactoryBase *abstractFactory); | 
|---|
| 294 |  | 
|---|
| 295 | QScopedPointer<QtAbstractPropertyBrowserPrivate> d_ptr; | 
|---|
| 296 | Q_DECLARE_PRIVATE(QtAbstractPropertyBrowser) | 
|---|
| 297 | Q_DISABLE_COPY(QtAbstractPropertyBrowser) | 
|---|
| 298 | Q_PRIVATE_SLOT(d_func(), void slotPropertyInserted(QtProperty *, | 
|---|
| 299 | QtProperty *, QtProperty *)) | 
|---|
| 300 | Q_PRIVATE_SLOT(d_func(), void slotPropertyRemoved(QtProperty *, | 
|---|
| 301 | QtProperty *)) | 
|---|
| 302 | Q_PRIVATE_SLOT(d_func(), void slotPropertyDestroyed(QtProperty *)) | 
|---|
| 303 | Q_PRIVATE_SLOT(d_func(), void slotPropertyDataChanged(QtProperty *)) | 
|---|
| 304 |  | 
|---|
| 305 | }; | 
|---|
| 306 |  | 
|---|
| 307 | QT_END_NAMESPACE | 
|---|
| 308 |  | 
|---|
| 309 | #endif // QTPROPERTYBROWSER_H | 
|---|