-
Notifications
You must be signed in to change notification settings - Fork 5
/
TreeUtils.h
191 lines (154 loc) · 4.81 KB
/
TreeUtils.h
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
#ifndef TREEUTILS_H
#define TREEUTILS_H
#pragma once
#include <QObject>
#include <QDebug>
#include <QTreeWidget>
#include <QMenu>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include "GenericStruct.h"
class TreeUtils : public QObject
{
Q_OBJECT
public:
/**
* @brief constructor
*/
TreeUtils();
/**
* @brief inject treewidget which class will work
* @param tree treewidget pointer
*/
void setTreeWidget(QTreeWidget* tree);
/**
* @brief get treewidgetitem variant data (in this case a generic struct)
* @param item treewidgetitem
* @param col treewidgetitem column
* @return custom type inside generic struct
*/
template <class T>
static T* getTreeItemData(QTreeWidgetItem* item, int col)
{
return item->data(col, Qt::UserRole).value<GenericStruct<T>>().getObject();
};
/**
* @brief creates the treewidget based on json file
* @param jsonDoc json document
*/
void parseJsonToTree(QJsonDocument jsonDoc);
private:
QTreeWidget* mTreeWidget = nullptr; ///< treewidget class member
/**
* @brief recursively creates the tree based on json array members
* @param item parent item
* @param jsonArray array of children
*/
void createJsonTreeItems(QTreeWidgetItem* item, QJsonArray jsonArray);
/**
* @brief creates a treewidgetitem with a custom struct inside its variant data
* @param parent parent treewidgetitem
* @return created treewidgetitem
*/
template <class T>
QTreeWidgetItem* createTreeItem(QTreeWidgetItem* parent = nullptr)
{
auto item = new QTreeWidgetItem(parent);
// verifying if treeitem is a toplevel/child node
auto itemText = parent == nullptr ? QString("Top Level Item %1").arg(this->mTreeWidget->topLevelItemCount() + 1) :
parent->text(0).append(" Child %1").arg(parent->childCount());
item->setText(0, itemText); // column 0
item->setFlags(item->flags() | Qt::ItemIsEditable | Qt::ItemIsUserCheckable);
item->setCheckState(0, Qt::Unchecked); // column 0
item->setData(0, Qt::UserRole, QVariant::fromValue(GenericStruct<T>())); // column 0
return item;
};
/**
* @brief verify if treewidget has a selected treewidgetitem
* @return bool
*/
bool hasSelectedItem();
/**
* @brief get children of given tree node
* @param item treewidgetitem
* @return children list
*/
QList<QTreeWidgetItem *> getChildren(QTreeWidgetItem* item);
/**
* @brief gets json array based on desired treewidgetitem recursively
* @param item treewidgetitem
* @param jsonArray base item json array
* @return json array
*/
QJsonArray getItemJson(QTreeWidgetItem* item, QJsonArray& jsonArray);
/**
* @brief updates json text on mainwindow
*/
void updateTreeJson();
public slots:
/**
* @brief add node in tree, with item text based on current selected item
*/
void slotAddItemInTree();
/**
* @brief remove the selected treewidgetitem and all its children from tree
*/
void slotRemoveTreeItem();
/**
* @brief clear tree items
*/
void slotClearTree();
/**
* @brief update selected treewidgetitem custom struct text value
* @param newText text in line edit
*/
void slotUpdateItemText(QString newText);
/**
* @brief update selected treewidgetitem custom struct type value
* @param newIndex index of type combobox
*/
void slotUpdateItemType(int newIndex);
/**
* @brief expand all treewidgetitems
*/
void slotExpandAll();
/**
* @brief collapse all trewidgetitems
*/
void slotCollapseAll();
/**
* @brief clear treewidget selection
*/
void slotClearSelection();
/**
* @brief show only matched treewidget items at tree
* @param text text filter
*/
void slotApplyFilter(QString text);
private slots:
/**
* @brief handle context menu over treewidgetitem
* @param point screen clicked location
*/
void slotHandleContextMenu(const QPoint& point);
signals:
/**
* @brief signal to mainwindow update custom struct form with clicked tree item data
* @param newText updated lineedit text
* @param newIndex updated combobox index
*/
void signalUpdateForm(QString newText, int newIndex);
/**
* @brief signal to mainwindow show message in the status bar
* @param message text
* @param timeout milliseconds to show message
*/
void signalShowStatusbarMessage(QString message, int timeout);
/**
* @brief signal to mainwindow update json textedit
* @param text json array
*/
void signalUpdateJsonText(QString text);
};
#endif