forked from facontidavide/PlotJuggler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_completer.h
102 lines (84 loc) · 2.76 KB
/
tree_completer.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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#ifndef TREE_COMPLETER_H
#define TREE_COMPLETER_H
#include <QStandardItem>
#include <QStandardItemModel>
#include <QString>
#include <QVariant>
#include <QFont>
#include <QFontDatabase>
#include <map>
#include "PlotJuggler/alphanum.hpp"
/*/
class TreeItem {
public:
explicit TreeItem(QStandardItem* name_item, QStandardItem* value_item)
: _name_item(name_item), _value_item(value_item) {}
TreeItem* appendChild(const QString& name)
{
auto child_name = new QStandardItem(name);
auto child_value = new QStandardItem("-");
child_value->setSelectable(false);
child_value->setTextAlignment(Qt::AlignRight);
child_value->setFlags( Qt::NoItemFlags | Qt::ItemIsEnabled );
auto font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
child_value->setFont( font );
child_value->setFlags(Qt::NoItemFlags);
QList<QStandardItem*> columns;
columns << child_name << child_value;
_name_item->appendRow(columns);
auto res =
_child_items_map.insert(std::make_pair(name, TreeItem(child_name, child_value)));
return &(res.first->second);
}
TreeItem* findChild(const QString& name) {
auto it = _child_items_map.find(name);
if (it == _child_items_map.end()) {
return nullptr;
}
return &(it->second);
}
QStandardItem* nameItem() { return _name_item; }
QStandardItem* valueItem() { return _value_item; }
private:
std::map<QString, TreeItem> _child_items_map;
QStandardItem* _name_item;
QStandardItem* _value_item;
};
class TreeModel : public QAbstractItemModel {
public:
TreeModel(QStandardItemModel* parent_model)
: QStandardItemModel(0, 2, parent_model),
_root_tree_item(invisibleRootItem(), nullptr),
_parent_model(parent_model) {}
void clear() {
QStandardItemModel::clear();
_root_tree_item = TreeItem(invisibleRootItem(), nullptr);
}
void addToTree(const QString& name, int reference_row) {
auto parts = name.split('/', QString::SplitBehavior::SkipEmptyParts);
if (parts.size() == 0) {
return;
}
TreeItem* tree_parent = &_root_tree_item;
for (int i = 0; i < parts.size(); i++) {
bool is_leaf = (i == parts.size() - 1);
const auto& part = parts[i];
TreeItem* matching_child = tree_parent->findChild(part);
if (matching_child) {
tree_parent = matching_child;
} else {
tree_parent = tree_parent->appendChild(part);
tree_parent->nameItem()->setSelectable(is_leaf);
}
}
}
private:
TreeItem _root_tree_item;
QStandardItemModel* _parent_model;
};*/
#endif // TREE_COMPLETER_H