JSONTreeModel.cpp
Go to the documentation of this file.
1 #include "JSONTreeModel.h"
2 
4 
5 namespace armarx
6 {
7 
8  void
9  JSONTreeModel::setRoot(nlohmann::json const& rootParam)
10  {
11  root = rootParam;
12  rows.clear();
13  rows[&root] = 0;
14  parents.clear();
15  parents[&root] = nullptr;
16  names.clear();
17  names[&root] = "";
18  }
19 
20  QModelIndex
21  JSONTreeModel::index(int row, int column, const QModelIndex& parentIndex) const
22  {
23  nlohmann::json* parent = refFrom(parentIndex);
24  if (row >= int(parent->size()))
25  {
26  return QModelIndex();
27  }
28 
29  nlohmann::json* child = nullptr;
30  if (parent->is_array())
31  {
32  child = &parent->at(row);
33  names[child] = "[" + std::to_string(row) + "]";
34  }
35  else if (parent->is_object())
36  {
37  auto it = parent->begin();
38  std::advance(it, row);
39  child = &it.value();
40  names[child] = it.key();
41  }
42 
43  if (child)
44  {
45  parents[child] = parent;
46  rows[child] = row;
47 
48  return createIndex(row, column, child);
49  }
50  else
51  {
52  return QModelIndex();
53  }
54  }
55 
56  QModelIndex
57  JSONTreeModel::parent(const QModelIndex& index) const
58  {
59  if (!index.isValid())
60  {
61  return QModelIndex();
62  }
63 
64  nlohmann::json* child = refFrom(index);
65  if (!parents.count(child))
66  {
67  //ARMARX_WARNING << "Map does not contain parent for child: " <<
68  // (child ? child->dump(2) : "(null)");
69  return QModelIndex();
70  }
71  nlohmann::json* parent = parents.at(child);
72  if (parent == nullptr)
73  {
74  return QModelIndex();
75  }
76  int row = rows.at(parent);
77 
78  return createIndex(row, 0, parent);
79  }
80 
81  int
82  JSONTreeModel::rowCount(const QModelIndex& parentIndex) const
83  {
84  if (parentIndex.column() > 1)
85  {
86  return 0;
87  }
88 
89  nlohmann::json* parent = refFrom(parentIndex);
90  if (parent->is_array() || parent->is_object())
91  {
92  return parent->size();
93  }
94  else
95  {
96  return 0;
97  }
98  }
99 
100  int
101  JSONTreeModel::columnCount(const QModelIndex& parent) const
102  {
103  return 2;
104  }
105 
106  QVariant
107  JSONTreeModel::data(const QModelIndex& index, int role) const
108  {
109  if (!index.isValid())
110  {
111  return QVariant();
112  }
113 
114  if (role != Qt::DisplayRole)
115  {
116  return QVariant();
117  }
118 
119  nlohmann::json* ref = refFrom(index);
120 
121  if (index.column() == 0)
122  {
123  if (!names.count(ref))
124  {
125  ARMARX_WARNING << "Map does not contain name for ref: "
126  << (ref ? ref->dump(2) : "(null)");
127  return QVariant();
128  }
129  std::string const& name = names.at(ref);
130  return QVariant(name.c_str());
131  }
132 
133  switch (ref->type())
134  {
135  case nlohmann::json::value_t::null:
136  return QVariant("null");
137  case nlohmann::json::value_t::object:
138  return QVariant("object");
139  case nlohmann::json::value_t::array:
140  return QVariant("array");
141  case nlohmann::json::value_t::string:
142  return QVariant(ref->get<std::string>().c_str());
143  case nlohmann::json::value_t::boolean:
144  return QVariant(ref->get<bool>());
145  case nlohmann::json::value_t::number_integer:
146  return QVariant(ref->get<int>());
147  case nlohmann::json::value_t::number_unsigned:
148  return QVariant(ref->get<unsigned int>());
149  case nlohmann::json::value_t::number_float:
150  return QVariant(ref->get<float>());
151 
152  default:
153  return QVariant("n/a");
154  }
155  }
156 
157  QVariant
158  JSONTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
159  {
160  if (role != Qt::DisplayRole)
161  {
162  return QVariant();
163  }
164 
165  switch (section)
166  {
167  case 0:
168  return QVariant("Name");
169  case 1:
170  return QVariant("Value");
171  default:
172  return QVariant();
173  }
174  }
175 
176  nlohmann::json*
177  JSONTreeModel::refFrom(const QModelIndex& index) const
178  {
179  if (index.isValid())
180  {
181  return static_cast<nlohmann::json*>(index.internalPointer());
182  }
183  else
184  {
185  return &root;
186  }
187  }
188 
189 } // namespace armarx
GfxTL::Orientation
ScalarT Orientation(const VectorXD< 2, ScalarT > &p1, const VectorXD< 2, ScalarT > &p2, const VectorXD< 2, ScalarT > &c)
Definition: Orientation.h:10
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::JSONTreeModel::columnCount
int columnCount(const QModelIndex &parent) const override
Definition: JSONTreeModel.cpp:101
JSONTreeModel.h
armarx::JSONTreeModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
Definition: JSONTreeModel.cpp:158
armarx::JSONTreeModel::index
QModelIndex index(int row, int column, const QModelIndex &parent) const override
Definition: JSONTreeModel.cpp:21
armarx::JSONTreeModel::rowCount
int rowCount(const QModelIndex &parent) const override
Definition: JSONTreeModel.cpp:82
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
armarx::JSONTreeModel::parent
QModelIndex parent(const QModelIndex &index) const override
Definition: JSONTreeModel.cpp:57
armarx::JSONTreeModel::data
QVariant data(const QModelIndex &index, int role) const override
Definition: JSONTreeModel.cpp:107
Logging.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::JSONTreeModel::setRoot
void setRoot(nlohmann::json const &root)
Definition: JSONTreeModel.cpp:9