tree_completer.h
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5  */
6 
7 #ifndef TREE_COMPLETER_H
8 #define TREE_COMPLETER_H
9 
10 #include <QStandardItem>
11 #include <QStandardItemModel>
12 #include <QString>
13 #include <QVariant>
14 #include <QFont>
15 #include <QFontDatabase>
16 #include <map>
17 #include "PlotJuggler/alphanum.hpp"
18 /*/
19 class TreeItem {
20  public:
21  explicit TreeItem(QStandardItem* name_item, QStandardItem* value_item)
22  : _name_item(name_item), _value_item(value_item) {}
23 
24  TreeItem* appendChild(const QString& name)
25  {
26  auto child_name = new QStandardItem(name);
27  auto child_value = new QStandardItem("-");
28 
29  child_value->setSelectable(false);
30  child_value->setTextAlignment(Qt::AlignRight);
31  child_value->setFlags( Qt::NoItemFlags | Qt::ItemIsEnabled );
32  auto font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
33  child_value->setFont( font );
34  child_value->setFlags(Qt::NoItemFlags);
35 
36  QList<QStandardItem*> columns;
37  columns << child_name << child_value;
38  _name_item->appendRow(columns);
39 
40  auto res =
41  _child_items_map.insert(std::make_pair(name, TreeItem(child_name, child_value)));
42  return &(res.first->second);
43  }
44 
45  TreeItem* findChild(const QString& name) {
46  auto it = _child_items_map.find(name);
47  if (it == _child_items_map.end()) {
48  return nullptr;
49  }
50  return &(it->second);
51  }
52 
53  QStandardItem* nameItem() { return _name_item; }
54  QStandardItem* valueItem() { return _value_item; }
55 
56  private:
57  std::map<QString, TreeItem> _child_items_map;
58  QStandardItem* _name_item;
59  QStandardItem* _value_item;
60 };
61 
62 class TreeModel : public QAbstractItemModel {
63  public:
64  TreeModel(QStandardItemModel* parent_model)
65  : QStandardItemModel(0, 2, parent_model),
66  _root_tree_item(invisibleRootItem(), nullptr),
67  _parent_model(parent_model) {}
68 
69  void clear() {
70  QStandardItemModel::clear();
71  _root_tree_item = TreeItem(invisibleRootItem(), nullptr);
72  }
73 
74  void addToTree(const QString& name, int reference_row) {
75  auto parts = name.split('/', PJ::SkipEmptyParts);
76  if (parts.size() == 0) {
77  return;
78  }
79 
80  TreeItem* tree_parent = &_root_tree_item;
81 
82  for (int i = 0; i < parts.size(); i++) {
83  bool is_leaf = (i == parts.size() - 1);
84  const auto& part = parts[i];
85 
86  TreeItem* matching_child = tree_parent->findChild(part);
87  if (matching_child) {
88  tree_parent = matching_child;
89  } else {
90 
91  tree_parent = tree_parent->appendChild(part);
92  tree_parent->nameItem()->setSelectable(is_leaf);
93  }
94  }
95  }
96 
97  private:
98  TreeItem _root_tree_item;
99  QStandardItemModel* _parent_model;
100 };*/
101 
102 #endif // TREE_COMPLETER_H
alphanum.hpp


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Aug 11 2024 02:24:26