File: RDTreeWidget.h

package info (click to toggle)
renderdoc 1.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 79,584 kB
  • sloc: cpp: 491,671; ansic: 285,823; python: 12,617; java: 11,345; cs: 7,181; makefile: 6,703; yacc: 5,682; ruby: 4,648; perl: 3,461; php: 2,119; sh: 2,068; lisp: 1,835; tcl: 1,068; ml: 747; xml: 137
file content (340 lines) | stat: -rw-r--r-- 11,276 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/******************************************************************************
 * The MIT License (MIT)
 *
 * Copyright (c) 2016-2018 Baldur Karlsson
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 ******************************************************************************/

#pragma once

#include "RDTreeView.h"

class RDTreeWidget;
class RDTreeWidgetModel;

class RDTreeWidgetItem
{
public:
  RDTreeWidgetItem() = default;
  RDTreeWidgetItem(const QVariantList &values);
  RDTreeWidgetItem(const std::initializer_list<QVariant> &values);
  ~RDTreeWidgetItem();

  QVariant data(int column, int role) const;
  void setData(int column, int role, const QVariant &value);

  void addChild(RDTreeWidgetItem *item);
  void insertChild(int index, RDTreeWidgetItem *child);

  // the data above requires allocating a bunch of vectors since it's stored per-column. Where
  // possible, just use this single per-item tag
  inline QVariant tag() const { return m_tag; }
  inline void setTag(const QVariant &value) { m_tag = value; }
  // inline accessors to the private data
  inline void setIcon(int column, const QIcon &icon)
  {
    if(column >= m_icons.size())
    {
      m_text.resize(column + 1);
      m_icons.resize(m_text.size());
    }

    m_icons[column] = icon;
    dataChanged(column, Qt::DecorationRole);
  }
  inline RDTreeWidgetItem *child(int index) const { return m_children[index]; }
  inline int indexOfChild(RDTreeWidgetItem *child) const { return m_children.indexOf(child); }
  RDTreeWidgetItem *takeChild(int index);
  void removeChild(RDTreeWidgetItem *child);
  void clear();
  inline int dataCount() const { return m_text.count(); }
  inline int childCount() const { return m_children.count(); }
  inline RDTreeWidgetItem *parent() const { return m_parent; }
  inline RDTreeWidget *treeWidget() const { return m_widget; }
  inline void setBold(bool bold)
  {
    m_bold = bold;
    dataChanged(0, Qt::FontRole);
  }
  inline void setItalic(bool italic)
  {
    m_italic = italic;
    dataChanged(0, Qt::FontRole);
  }
  inline void setTreeColor(QColor col, float pixels)
  {
    m_treeCol = col;
    m_treeColWidth = pixels;
  }
  inline void setBackgroundColor(QColor background) { setBackground(QBrush(background)); }
  inline void setForegroundColor(QColor foreground) { setForeground(QBrush(foreground)); }
  inline void setBackground(QBrush background)
  {
    m_back = background;
    dataChanged(0, Qt::BackgroundRole);
  }
  inline void setForeground(QBrush foreground)
  {
    m_fore = foreground;
    dataChanged(0, Qt::ForegroundRole);
  }
  inline QBrush background() { return m_back; }
  inline QBrush foreground() { return m_fore; }
  inline QString text(int column) const { return m_text[column].toString(); }
  inline void setText(int column, const QVariant &value)
  {
    if(column >= m_text.size())
    {
      m_text.resize(column + 1);
      m_icons.resize(m_text.size());
    }

    m_text[column] = value;
    checkForResourceId(column);
    dataChanged(column, Qt::DisplayRole);
  }
  inline void setToolTip(const QString &value)
  {
    m_tooltip = value;
    dataChanged(0, Qt::ToolTipRole);
  }

  inline Qt::CheckState checkState(int column) const
  {
    return static_cast<Qt::CheckState>(data(column, Qt::CheckStateRole).toInt());
  }
  inline void setCheckState(int column, Qt::CheckState state)
  {
    setData(column, Qt::CheckStateRole, static_cast<int>(state));
    dataChanged(column, Qt::CheckStateRole);
  }

private:
  void checkForResourceId(int column);

  void sort(int column, Qt::SortOrder order);

  friend class RDTreeWidget;
  friend class RDTreeWidgetModel;
  friend class RDTreeWidgetDelegate;

  void setWidget(RDTreeWidget *widget);
  RDTreeWidget *m_widget = NULL;

  RDTreeWidgetItem *m_parent = NULL;
  QVector<RDTreeWidgetItem *> m_children;

  struct RoleData
  {
    RoleData() : role(0), data() {}
    RoleData(int r, const QVariant &d) : role(r), data(d) {}
    int role;
    QVariant data;
  };

  void dataChanged(int column, int role);

  // per-column properties
  QVector<QVariant> m_text;
  QVector<QIcon> m_icons;
  // each element, per-column, is a list of other data values
  // we allocate this lazily only if it's really needed
  QVector<QVector<RoleData>> *m_data = NULL;

  // per-item properties
  QString m_tooltip;
  bool m_bold = false;
  bool m_italic = false;
  QColor m_treeCol;
  float m_treeColWidth = 0.0f;
  QBrush m_back;
  QBrush m_fore;
  QVariant m_tag;
};

class RDTreeWidgetItemIterator
{
public:
  RDTreeWidgetItemIterator(RDTreeWidget *widget);

  RDTreeWidgetItemIterator &operator++();
  inline const RDTreeWidgetItemIterator operator++(int)
  {
    RDTreeWidgetItemIterator it = *this;
    ++(*this);
    return it;
  }

  // TODO implement operator-- if we need it.
  /*
  RDTreeWidgetItemIterator &operator--();
  inline const RDTreeWidgetItemIterator operator--(int)
  {
    RDTreeWidgetItemIterator it = *this;
    --(*this);
    return it;
  }
  */

  inline RDTreeWidgetItem *operator*() const { return m_Current; }
private:
  RDTreeWidgetItem *m_Current;
};

class RDTreeWidgetDelegate : public RDTreeViewDelegate
{
  Q_OBJECT
public:
  explicit RDTreeWidgetDelegate(RDTreeWidget *parent);
  ~RDTreeWidgetDelegate();

  void paint(QPainter *painter, const QStyleOptionViewItem &option,
             const QModelIndex &index) const override;
  QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;

  bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
                   const QModelIndex &index) override;

  bool linkHover(QMouseEvent *e, const QModelIndex &index);

private:
  RDTreeWidget *m_widget;
};

class RDTreeWidget : public RDTreeView
{
  Q_OBJECT

  Q_PROPERTY(bool instantTooltips READ instantTooltips WRITE setInstantTooltips)
  Q_PROPERTY(bool customCopyPasteHandler READ customCopyPasteHandler WRITE setCustomCopyPasteHandler)
public:
  explicit RDTreeWidget(QWidget *parent = 0);
  ~RDTreeWidget();

  void setHoverIconColumn(int column, QIcon normal, QIcon hover)
  {
    m_hoverColumn = column;
    m_normalHoverIcon = normal;
    m_activeHoverIcon = hover;
    m_hoverHandCursor = true;
    m_activateOnClick = true;
  }
  void setHoverHandCursor(bool hand) { m_hoverHandCursor = hand; }
  void setHoverClickActivate(bool click) { m_activateOnClick = click; }
  void setClearSelectionOnFocusLoss(bool clear) { m_clearSelectionOnFocusLoss = clear; }
  bool instantTooltips() { return m_instantTooltips; }
  void setInstantTooltips(bool instant) { m_instantTooltips = instant; }
  bool customCopyPasteHandler() { return m_customCopyPaste; }
  void setCustomCopyPasteHandler(bool custom) { m_customCopyPaste = custom; }
  RDTreeWidgetItem *invisibleRootItem() { return m_root; }
  void addTopLevelItem(RDTreeWidgetItem *item) { m_root->addChild(item); }
  RDTreeWidgetItem *topLevelItem(int index) const { return m_root->child(index); }
  int indexOfTopLevelItem(RDTreeWidgetItem *item) const { return m_root->indexOfChild(item); }
  RDTreeWidgetItem *takeTopLevelItem(int index) { return m_root->takeChild(index); }
  int topLevelItemCount() const { return m_root->childCount(); }
  void beginUpdate();
  void endUpdate();
  void setColumnAlignment(int column, Qt::Alignment align);

  void setItemDelegate(QAbstractItemDelegate *delegate);
  QAbstractItemDelegate *itemDelegate() const;

  void setColumns(const QStringList &columns);
  QString headerText(int column) const { return m_headers[column]; }
  void setHeaderText(int column, const QString &text);
  RDTreeWidgetItem *selectedItem() const;
  RDTreeWidgetItem *currentItem() const;
  void setSelectedItem(RDTreeWidgetItem *node);
  void setCurrentItem(RDTreeWidgetItem *node);

  RDTreeWidgetItem *itemAt(const QPoint &p) const;
  RDTreeWidgetItem *itemAt(int x, int y) const { return itemAt(QPoint(x, y)); }
  void expandItem(RDTreeWidgetItem *item);
  void expandAllItems(RDTreeWidgetItem *item);
  void collapseItem(RDTreeWidgetItem *item);
  void collapseAllItems(RDTreeWidgetItem *item);
  void scrollToItem(RDTreeWidgetItem *node);

  void copySelection();

  void clear();

signals:
  void mouseMove(QMouseEvent *e);
  void itemClicked(RDTreeWidgetItem *item, int column);
  void itemChanged(RDTreeWidgetItem *item, int column);
  void itemDoubleClicked(RDTreeWidgetItem *item, int column);
  void itemActivated(RDTreeWidgetItem *item, int column);
  void currentItemChanged(RDTreeWidgetItem *current, RDTreeWidgetItem *previous);
  void itemSelectionChanged();

public slots:

private:
  void mouseMoveEvent(QMouseEvent *e) override;
  void mouseReleaseEvent(QMouseEvent *e) override;
  void leaveEvent(QEvent *e) override;
  void focusOutEvent(QFocusEvent *event) override;
  void keyPressEvent(QKeyEvent *e) override;
  void drawBranches(QPainter *painter, const QRect &rect, const QModelIndex &index) const override;

  void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) override;
  void currentChanged(const QModelIndex &current, const QModelIndex &previous) override;

  void setModel(QAbstractItemModel *model) override {}
  void itemDataChanged(RDTreeWidgetItem *item, int column, int role);
  void beginInsertChild(RDTreeWidgetItem *item, int index);
  void endInsertChild(RDTreeWidgetItem *item, int index);

  friend class RDTreeWidgetModel;
  friend class RDTreeWidgetItem;
  friend class RDTreeWidgetDelegate;

  // invisible root item, used to simplify recursion by even top-level items having a parent
  RDTreeWidgetItem *m_root;

  RDTreeWidgetModel *m_model;

  QAbstractItemDelegate *m_userDelegate = NULL;
  RDTreeWidgetDelegate *m_delegate;

  bool m_clearing = false;

  QStringList m_headers;

  bool m_queueUpdates = false;

  RDTreeWidgetItem *m_queuedItem;
  QPair<int, int> m_lowestIndex;
  QPair<int, int> m_highestIndex;
  uint64_t m_queuedRoles = 0;
  bool m_queuedChildren = false;

  QVector<Qt::Alignment> m_alignments;

  bool m_instantTooltips = false;
  bool m_customCopyPaste = false;
  int m_hoverColumn = -1;
  QIcon m_normalHoverIcon;
  QIcon m_activeHoverIcon;
  bool m_hoverHandCursor = false;
  bool m_clearSelectionOnFocusLoss = false;
  bool m_activateOnClick = false;
};