qttreepropertybrowser.h
Go to the documentation of this file.
1 /****************************************************************************
2  **
3  ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4  ** All rights reserved.
5  **
6  ** Contact: Nokia Corporation (qt-info@nokia.com)
7  **
8  ** This file is part of a Qt Solutions component.
9  **
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 Nokia Corporation and its Subsidiary(-ies) nor
22  ** the names of its contributors may be used to endorse or promote
23  ** products derived from this software without specific prior written
24  ** permission.
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  ****************************************************************************/
39 
40 #pragma GCC system_header
41 #pragma GCC diagnostic ignored "-Wswitch-default"
42 #pragma GCC diagnostic ignored "-Wfloat-equal"
43 #pragma GCC diagnostic ignored "-Wshadow"
44 
45 #ifndef QTTREEPROPERTYBROWSER_H
46 #define QTTREEPROPERTYBROWSER_H
47 
48 #include <QtCore/QModelIndex>
49 #include <QtGui/QTreeWidget>
50 #include <QtGui/QItemDelegate>
51 #include "qtpropertybrowser.h"
52 
53 #if QT_VERSION >= 0x040400
54 QT_BEGIN_NAMESPACE
55 #endif
56 
57 class QTreeWidgetItem;
59 
60 class QtPropertyEditorDelegate : public QItemDelegate
61 {
62  Q_OBJECT
63  public:
64  explicit QtPropertyEditorDelegate(QObject *parent = 0)
65  : QItemDelegate(parent), m_editorPrivate(0), m_editedItem(0),
66  m_editedWidget(0), m_disablePainting(false)
67  {}
68 
70  { m_editorPrivate = editorPrivate; }
71 
72  QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
73  const QModelIndex &index) const;
74 
75  void updateEditorGeometry(QWidget *editor,
76  const QStyleOptionViewItem &option,
77  const QModelIndex &index) const;
78 
79  void paint(QPainter *painter, const QStyleOptionViewItem &option,
80  const QModelIndex &index) const;
81 
82  QSize sizeHint(const QStyleOptionViewItem &option,
83  const QModelIndex &index) const;
84 
85  void setModelData(QWidget *, QAbstractItemModel *,
86  const QModelIndex &) const {}
87 
88  void setEditorData(QWidget *, const QModelIndex &) const {}
89 
90  bool eventFilter(QObject *object, QEvent *event);
91  void closeEditor(QtProperty *property);
92 
93  QTreeWidgetItem *editedItem() const { return m_editedItem; }
94 
95  protected:
96 
97  void drawDecoration(QPainter *painter, const QStyleOptionViewItem &option,
98  const QRect &rect, const QPixmap &pixmap) const;
99  void drawDisplay(QPainter *painter, const QStyleOptionViewItem &option,
100  const QRect &rect, const QString &text) const;
101 
102  private slots:
103  void slotEditorDestroyed(QObject *object);
104 
105  private:
106  int indentation(const QModelIndex &index) const;
107 
108  typedef QMap<QWidget *, QtProperty *> EditorToPropertyMap;
109  mutable EditorToPropertyMap m_editorToProperty;
110 
111  typedef QMap<QtProperty *, QWidget *> PropertyToEditorMap;
112  mutable PropertyToEditorMap m_propertyToEditor;
113  QtTreePropertyBrowserPrivate *m_editorPrivate;
114  mutable QTreeWidgetItem *m_editedItem;
115  mutable QWidget *m_editedWidget;
116  mutable bool m_disablePainting;
117 };
118 
119 class QtPropertyEditorView : public QTreeWidget
120 {
121  Q_OBJECT
122  public:
123  explicit QtPropertyEditorView(QWidget *parent = 0);
124 
126  { m_editorPrivate = editorPrivate; }
127 
128  QTreeWidgetItem *indexToItem(const QModelIndex &index) const
129  { return itemFromIndex(index); }
130 
131  protected:
132  void keyPressEvent(QKeyEvent *event);
133  void mousePressEvent(QMouseEvent *event);
134  void drawRow(QPainter *painter, const QStyleOptionViewItem &option,
135  const QModelIndex &index) const;
136 
137  private:
138  QtTreePropertyBrowserPrivate *m_editorPrivate;
139 };
140 
141 
144 {
145  Q_OBJECT
146  Q_ENUMS(ResizeMode)
147  Q_PROPERTY(int indentation READ indentation WRITE setIndentation)
148  Q_PROPERTY(
149  bool rootIsDecorated READ rootIsDecorated WRITE setRootIsDecorated)
150  Q_PROPERTY(
151  bool alternatingRowColors READ
152  alternatingRowColors WRITE setAlternatingRowColors)
153  Q_PROPERTY(bool headerVisible READ isHeaderVisible WRITE setHeaderVisible)
154  Q_PROPERTY(ResizeMode resizeMode READ resizeMode WRITE setResizeMode)
155  Q_PROPERTY(
156  int splitterPosition READ splitterPosition WRITE setSplitterPosition)
157  Q_PROPERTY(
158  bool propertiesWithoutValueMarked READ
159  propertiesWithoutValueMarked WRITE setPropertiesWithoutValueMarked)
160  public:
161 
162  enum ResizeMode
163  {
164  Interactive,
165  Stretch,
166  Fixed,
167  ResizeToContents
168  };
169 
170  explicit QtTreePropertyBrowser(QWidget *parent = 0);
172 
173  int indentation() const;
174  void setIndentation(int i);
175 
176  bool rootIsDecorated() const;
177  void setRootIsDecorated(bool show);
178 
179  bool alternatingRowColors() const;
180  void setAlternatingRowColors(bool enable);
181 
182  bool isHeaderVisible() const;
183  void setHeaderVisible(bool visible);
184 
185  ResizeMode resizeMode() const;
186  void setResizeMode(ResizeMode mode);
187 
188  int splitterPosition() const;
189  void setSplitterPosition(int position);
190 
191  void setExpanded(QtBrowserItem *item, bool expanded);
192  bool isExpanded(QtBrowserItem *item) const;
193 
194  bool isItemVisible(QtBrowserItem *item) const;
195  void setItemVisible(QtBrowserItem *item, bool visible);
196 
197  void setBackgroundColor(QtBrowserItem *item, const QColor &color);
198  QColor backgroundColor(QtBrowserItem *item) const;
199  QColor calculatedBackgroundColor(QtBrowserItem *item) const;
200 
201  void setPropertiesWithoutValueMarked(bool mark);
202  bool propertiesWithoutValueMarked() const;
203 
204  void editItem(QtBrowserItem *item);
205 
206  Q_SIGNALS:
207 
208  void collapsed(QtBrowserItem *item);
209  void expanded(QtBrowserItem *item);
210 
211  protected:
212  virtual void itemInserted(QtBrowserItem *item, QtBrowserItem *afterItem);
213  virtual void itemRemoved(QtBrowserItem *item);
214  virtual void itemChanged(QtBrowserItem *item);
215 
216  private:
217 
219  Q_DECLARE_PRIVATE(QtTreePropertyBrowser)
220  Q_DISABLE_COPY(QtTreePropertyBrowser)
221 
222  Q_PRIVATE_SLOT(d_func(), void slotCollapsed(const QModelIndex &t))
223  Q_PRIVATE_SLOT(d_func(), void slotExpanded(const QModelIndex &t))
224  Q_PRIVATE_SLOT(d_func(),
225  void slotCurrentBrowserItemChanged(QtBrowserItem *t))
226  Q_PRIVATE_SLOT(d_func(),
227  void slotCurrentTreeItemChanged(
228  QTreeWidgetItem *t, QTreeWidgetItem *t2))
229 };
230 
232 {
233  QtTreePropertyBrowser *q_ptr;
234  Q_DECLARE_PUBLIC(QtTreePropertyBrowser)
235 
236  public:
238  void init(QWidget *parent);
239 
240  void propertyInserted(QtBrowserItem *index, QtBrowserItem *afterIndex);
241  void propertyRemoved(QtBrowserItem *index);
242  void propertyChanged(QtBrowserItem *index);
243  QWidget *createEditor(QtProperty *property, QWidget *parent) const
244  { return q_ptr->createEditor(property, parent); }
245  QtProperty *indexToProperty(const QModelIndex &index) const;
246  QTreeWidgetItem *indexToItem(const QModelIndex &index) const;
247  QtBrowserItem *indexToBrowserItem(const QModelIndex &index) const;
248  bool lastColumn(int column) const;
249  void disableItem(QTreeWidgetItem *item) const;
250  void enableItem(QTreeWidgetItem *item) const;
251  bool hasValue(QTreeWidgetItem *item) const;
252 
253  void slotCollapsed(const QModelIndex &index);
254  void slotExpanded(const QModelIndex &index);
255 
256  QColor calculatedBackgroundColor(QtBrowserItem *item) const;
257 
258  QtPropertyEditorView *treeWidget() const { return m_treeWidget; }
259  bool markPropertiesWithoutValue(
260  ) const { return m_markPropertiesWithoutValue; }
261 
262  QtBrowserItem *currentItem() const;
263  void setCurrentItem(QtBrowserItem *browserItem, bool block);
264  void editItem(QtBrowserItem *browserItem);
265 
266  void slotCurrentBrowserItemChanged(QtBrowserItem *item);
267  void slotCurrentTreeItemChanged(QTreeWidgetItem *newItem,
268  QTreeWidgetItem *);
269 
270  QTreeWidgetItem *editedItem() const;
271 
272  private:
273  void updateItem(QTreeWidgetItem *item);
274 
275  QMap<QtBrowserItem *, QTreeWidgetItem *> m_indexToItem;
276  QMap<QTreeWidgetItem *, QtBrowserItem *> m_itemToIndex;
277 
278  QMap<QtBrowserItem *, QColor> m_indexToBackgroundColor;
279 
280  QtPropertyEditorView *m_treeWidget;
281 
282  bool m_headerVisible;
283  QtTreePropertyBrowser::ResizeMode m_resizeMode;
284  class QtPropertyEditorDelegate *m_delegate;
285  bool m_markPropertiesWithoutValue;
286  bool m_browserChangedBlocked;
287  QIcon m_expandIcon;
288 };
289 #if QT_VERSION >= 0x040400
290 QT_END_NAMESPACE
291 #endif
292 
293 #endif
Definition: qtpropertybrowser.h:384
virtual void itemChanged(QtBrowserItem *item)=0
void setEditorData(QWidget *, const QModelIndex &) const
Definition: qttreepropertybrowser.h:88
void setEditorPrivate(QtTreePropertyBrowserPrivate *editorPrivate)
Definition: qttreepropertybrowser.h:125
Definition: qtpropertybrowser.h:311
Definition: qttreepropertybrowser.h:142
void drawRow(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
void setEditorPrivate(QtTreePropertyBrowserPrivate *editorPrivate)
Definition: qttreepropertybrowser.h:69
virtual void itemRemoved(QtBrowserItem *item)=0
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: qttreepropertybrowser.h:231
void keyPressEvent(QKeyEvent *event)
void drawDecoration(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QPixmap &pixmap) const
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
void mousePressEvent(QMouseEvent *event)
GAZEBO_VISIBLE void enable()
Enable sensors.
Definition: qttreepropertybrowser.h:119
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
void closeEditor(QtProperty *property)
bool eventFilter(QObject *object, QEvent *event)
QtPropertyEditorDelegate(QObject *parent=0)
Definition: qttreepropertybrowser.h:64
QTreeWidgetItem * indexToItem(const QModelIndex &index) const
Definition: qttreepropertybrowser.h:128
QtPropertyEditorView(QWidget *parent=0)
virtual QWidget * createEditor(QtProperty *property, QWidget *parent)
#define QT_QTPROPERTYBROWSER_EXPORT
Definition: qtpropertybrowser.h:68
Definition: qttreepropertybrowser.h:60
void drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const
virtual void itemInserted(QtBrowserItem *item, QtBrowserItem *afterItem)=0
Definition: qtpropertybrowser.h:77
QTreeWidgetItem * editedItem() const
Definition: qttreepropertybrowser.h:93
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
void setModelData(QWidget *, QAbstractItemModel *, const QModelIndex &) const
Definition: qttreepropertybrowser.h:85