Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
src
tree_completer.h
Go to the documentation of this file.
1
#ifndef TREE_COMPLETER_H
2
#define TREE_COMPLETER_H
3
4
#include <QStandardItem>
5
#include <QStandardItemModel>
6
#include <QString>
7
#include <QVariant>
8
#include <QFont>
9
#include <QFontDatabase>
10
#include <map>
11
#include "
PlotJuggler/alphanum.hpp
"
12
/*/
13
class TreeItem {
14
public:
15
explicit TreeItem(QStandardItem* name_item, QStandardItem* value_item)
16
: _name_item(name_item), _value_item(value_item) {}
17
18
TreeItem* appendChild(const QString& name)
19
{
20
auto child_name = new QStandardItem(name);
21
auto child_value = new QStandardItem("-");
22
23
child_value->setSelectable(false);
24
child_value->setTextAlignment(Qt::AlignRight);
25
child_value->setFlags( Qt::NoItemFlags | Qt::ItemIsEnabled );
26
auto font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
27
child_value->setFont( font );
28
child_value->setFlags(Qt::NoItemFlags);
29
30
QList<QStandardItem*> columns;
31
columns << child_name << child_value;
32
_name_item->appendRow(columns);
33
34
auto res =
35
_child_items_map.insert(std::make_pair(name, TreeItem(child_name, child_value)));
36
return &(res.first->second);
37
}
38
39
TreeItem* findChild(const QString& name) {
40
auto it = _child_items_map.find(name);
41
if (it == _child_items_map.end()) {
42
return nullptr;
43
}
44
return &(it->second);
45
}
46
47
QStandardItem* nameItem() { return _name_item; }
48
QStandardItem* valueItem() { return _value_item; }
49
50
private:
51
std::map<QString, TreeItem> _child_items_map;
52
QStandardItem* _name_item;
53
QStandardItem* _value_item;
54
};
55
56
class TreeModel : public QAbstractItemModel {
57
public:
58
TreeModel(QStandardItemModel* parent_model)
59
: QStandardItemModel(0, 2, parent_model),
60
_root_tree_item(invisibleRootItem(), nullptr),
61
_parent_model(parent_model) {}
62
63
void clear() {
64
QStandardItemModel::clear();
65
_root_tree_item = TreeItem(invisibleRootItem(), nullptr);
66
}
67
68
void addToTree(const QString& name, int reference_row) {
69
auto parts = name.split('/', QString::SplitBehavior::SkipEmptyParts);
70
if (parts.size() == 0) {
71
return;
72
}
73
74
TreeItem* tree_parent = &_root_tree_item;
75
76
for (int i = 0; i < parts.size(); i++) {
77
bool is_leaf = (i == parts.size() - 1);
78
const auto& part = parts[i];
79
80
TreeItem* matching_child = tree_parent->findChild(part);
81
if (matching_child) {
82
tree_parent = matching_child;
83
} else {
84
85
tree_parent = tree_parent->appendChild(part);
86
tree_parent->nameItem()->setSelectable(is_leaf);
87
}
88
}
89
}
90
91
private:
92
TreeItem _root_tree_item;
93
QStandardItemModel* _parent_model;
94
};*/
95
96
#endif // TREE_COMPLETER_H
alphanum.hpp
plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 04:02:48