-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeDisplay.cpp
236 lines (198 loc) · 7.92 KB
/
TreeDisplay.cpp
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
// Copyright (c) 2017-2017 Ashley Brighthope
// SPDX-License-Identifier: BSL-1.0
#include <Pothos/Framework.hpp>
#include <QBoxLayout>
#include <QLabel>
#include <QTreeView>
#include <QHeaderView>
#include <QStandardItemModel>
#include <Poco/Logger.h>
/***********************************************************************
* |PothosDoc Tree Display
*
* The tree display widget can display object and packets in a tree structure,
* useful for debugging.
* The display value can be set through setValue() slots.
*
* |category /Widgets
* |keywords tree display
*
* |param title The name of the value displayed by this widget
* |default "Object as Tree"
* |widget StringEntry()
*
* |mode graphWidget
* |factory /widgets/tree_display()
* |setter setTitle(title)
**********************************************************************/
static int PothosObjectId = qRegisterMetaType< Pothos::Object >("Pothos::Object"); // Register type for QT slot
class TreeDisplay : public QWidget, public Pothos::Block
{
Q_OBJECT
public:
static Block *make(void)
{
return new TreeDisplay();
}
static Poco::Logger& logger()
{
static Poco::Logger &_logger = Poco::Logger::get("TreeDisplay");
return _logger;
}
TreeDisplay(void):
_layout(new QVBoxLayout(this)),
_label(new QLabel(this)),
_treeView(new QTreeView(this)),
_standardItemModel(new QStandardItemModel())
{
// Setup GUI
_layout->setContentsMargins(QMargins());
_layout->addWidget(_label);
_layout->addWidget(_treeView);
_standardItemModel->setHorizontalHeaderLabels(QStringList({ "Key/Index", "Value" }));
_treeView->setModel(_standardItemModel.get());
// Setup Pothos
this->registerCall(this, POTHOS_FCN_TUPLE(TreeDisplay, widget));
this->registerCall(this, POTHOS_FCN_TUPLE(TreeDisplay, setTitle));
this->registerCall(this, POTHOS_FCN_TUPLE(TreeDisplay, setValue));
}
QWidget *widget(void)
{
return this;
}
void setTitle(const QString &title)
{
QMetaObject::invokeMethod(_label, "setText", Qt::QueuedConnection, Q_ARG(QString, title));
}
QString objectToString(const Pothos::Object &object)
{
return QString("(%1) %2")
.arg(QString::fromStdString(object.getTypeString()))
.arg(QString::fromStdString(object.toString()));
}
QStandardItem* createAndAppendRow(QStandardItem *parent, const QString &key, const Pothos::Object &object, const QString &value)
{
QList< QStandardItem* > standardItemList;
{
auto keyItem = new QStandardItem(key);
// Make item readonly
keyItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
standardItemList.append(keyItem);
}
{
auto dataItem = new QStandardItem(value);
// Make item readonly
dataItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
// Set the tool tip to show the data type for this item
dataItem->setToolTip(QString::fromStdString(object.getTypeString()));
standardItemList.append(dataItem);
}
parent->appendRow(standardItemList);
return standardItemList[0];
}
QStandardItem* createAndAppendRow(QStandardItem *parent, const QString &key, const Pothos::Object &object)
{
return createAndAppendRow(parent, key, object, objectToString(object));
}
void walkObject(QStandardItem *parent, const QString &key, const Pothos::Object &object)
{
if (object.canConvert(typeid(Pothos::ObjectVector)))
{
auto item = createAndAppendRow(parent, key, object, QString::fromStdString(object.getTypeString()));
try {
const auto objectVector = object.convert< Pothos::ObjectVector >();
for (size_t i = 0; i < objectVector.size(); ++i)
{
walkObject(item, QString::number(i), objectVector[i]);
}
}
catch (const std::exception &e) {
poco_error(logger(), "walkObject Pothos::ObjectVector exception: " + std::string(e.what()));
}
return;
}
if (object.canConvert(typeid(Pothos::ObjectMap)))
{
auto item = createAndAppendRow(parent, key, object, QString::fromStdString(object.getTypeString()));
try {
const auto objectMap = object.convert< Pothos::ObjectMap >();
for (const auto &pair : objectMap)
{
walkObject(item, objectToString(pair.first), pair.second);
}
}
catch (const std::exception &e) {
poco_error(logger(), "walkObject Pothos::ObjectMap exception: " + std::string(e.what()));
}
return;
}
if (object.canConvert(typeid(Pothos::Packet)))
{
auto item = createAndAppendRow(parent, key, object, QString::fromStdString(object.getTypeString()));
try {
const auto packet = object.convert< Pothos::Packet >();
Pothos::ObjectMap packetObject;
// Convert Pothos::Packet to a Pothos::Object to simplify displaying in tree
{
Pothos::ObjectKwargs payloadObject;
payloadObject["dtype"] = Pothos::Object(packet.payload.dtype.toMarkup());
payloadObject["length"] = Pothos::Object(packet.payload.length);
payloadObject["elements"] = Pothos::Object(packet.payload.elements());
packetObject[Pothos::Object("payload")] = Pothos::Object::make(payloadObject);
}
packetObject[Pothos::Object("metadata")] = Pothos::Object::make(packet.metadata);
{
Pothos::ObjectVector labelsObject;
for (const auto &label : packet.labels)
{
Pothos::ObjectKwargs labelObject;
labelObject["data"] = label.data;
labelObject["id"] = Pothos::Object(label.id);
labelObject["index"] = Pothos::Object(label.index);
labelObject["width"] = Pothos::Object(label.width);
labelsObject.push_back(Pothos::Object(labelObject));
}
packetObject[Pothos::Object("labels")] = Pothos::Object::make(labelsObject);
}
for (const auto &pair : packetObject)
{
walkObject(item, objectToString(pair.first), pair.second);
}
}
catch (const std::exception &e) {
poco_error(logger(), "walkObject Pothos::Packet exception: " + std::string(e.what()));
}
return;
}
// For all other data types use built in convert
createAndAppendRow(parent, key, object);
return;
}
void setValue(const Pothos::Object &object)
{
QMetaObject::invokeMethod(this, "setTreeValue", Qt::QueuedConnection, Q_ARG(Pothos::Object, object));
}
public slots:
void setTreeValue(Pothos::Object object)
{
_standardItemModel->removeRows(0, _standardItemModel->rowCount(QModelIndex()), QModelIndex());
walkObject(_standardItemModel->invisibleRootItem(), QString(), object);
_treeView->expandAll();
}
QVariant saveState(void) const
{
return _treeView->header()->saveState();
}
void restoreState(const QVariant &state)
{
_treeView->header()->restoreState(state.toByteArray());
}
private:
QVBoxLayout *_layout;
QLabel *_label;
QTreeView *_treeView;
std::unique_ptr< QStandardItemModel > _standardItemModel;
};
static Pothos::BlockRegistry registerTextDisplay(
"/widgets/tree_display", &TreeDisplay::make);
#include "TreeDisplay.moc"