Galaxis Codebase  1.0.0
Team Galaxis software stack for the Carolo Cup 2023 - ROS2 based software
JsonTreeModel Class Reference

#include <jsontreemodel.h>

Inheritance diagram for JsonTreeModel:
Collaboration diagram for JsonTreeModel:

Public Member Functions

 JsonTreeModel (const QStringList &headers, const nlohmann::json &data, QObject *parent=0)
 
 ~JsonTreeModel ()
 
QVariant data (const QModelIndex &index, int role) const
 
QVariant headerData (int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
 
QModelIndex index (int row, int column, const QModelIndex &parent=QModelIndex()) const
 
QModelIndex parent (const QModelIndex &index) const
 
int rowCount (const QModelIndex &parent=QModelIndex()) const
 
int columnCount (const QModelIndex &parent=QModelIndex()) const
 
Qt::ItemFlags flags (const QModelIndex &index) const
 
bool setData (const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
 
bool setHeaderData (int section, Qt::Orientation orientation, const QVariant &value, int role=Qt::EditRole)
 
bool insertColumns (int position, int columns, const QModelIndex &parent=QModelIndex())
 
bool removeColumns (int position, int columns, const QModelIndex &parent=QModelIndex())
 
bool insertRows (int position, int rows, const QModelIndex &parent=QModelIndex())
 
bool removeRows (int position, int rows, const QModelIndex &parent=QModelIndex())
 
JsonTreeItem * getRootItem ()
 

Private Member Functions

void setupModelData (const nlohmann::json &lines, JsonTreeItem *parent)
 
QString getTypeString (value_t vType)
 
JsonTreeItem * getItem (const QModelIndex &index) const
 

Private Attributes

JsonTreeItem * rootItem
 

Detailed Description

Definition at line 13 of file jsontreemodel.h.

Constructor & Destructor Documentation

◆ JsonTreeModel()

JsonTreeModel::JsonTreeModel ( const QStringList &  headers,
const nlohmann::json &  data,
QObject *  parent = 0 
)

Definition at line 8 of file jsontreemodel.cpp.

10  : QAbstractItemModel(parent)
11 {
12  QVector<QVariant> rootData;
13  foreach (QString header, headers)
14  rootData << header;
15 
16  rootItem = new JsonTreeItem(rootData);
18 }

References data(), rootItem, and setupModelData().

Here is the call graph for this function:

◆ ~JsonTreeModel()

JsonTreeModel::~JsonTreeModel ( )

Definition at line 20 of file jsontreemodel.cpp.

21 {
22  delete rootItem;
23 }

References rootItem.

Member Function Documentation

◆ columnCount()

int JsonTreeModel::columnCount ( const QModelIndex &  parent = QModelIndex()) const

Definition at line 25 of file jsontreemodel.cpp.

26 {
27  return rootItem->columnCount();
28 }

References JsonTreeItem::columnCount(), and rootItem.

Referenced by JsonEditorMain::insertTreeChild().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ data()

QVariant JsonTreeModel::data ( const QModelIndex &  index,
int  role 
) const

Definition at line 30 of file jsontreemodel.cpp.

31 {
32  if (!index.isValid())
33  return QVariant();
34 
35  if (role != Qt::DisplayRole && role != Qt::EditRole)
36  return QVariant();
37 
38  JsonTreeItem *item = getItem(index);
39 
40  return item->data(index.column());
41 }

References JsonTreeItem::data(), getItem(), and index().

Referenced by JsonTreeModel(), and setupModelData().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flags()

Qt::ItemFlags JsonTreeModel::flags ( const QModelIndex &  index) const

Definition at line 43 of file jsontreemodel.cpp.

44 {
45  if (!index.isValid())
46  return 0;
47 
48  return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
49 }

References index().

Here is the call graph for this function:

◆ getItem()

JsonTreeItem * JsonTreeModel::getItem ( const QModelIndex &  index) const
private

Definition at line 51 of file jsontreemodel.cpp.

52 {
53  if (index.isValid()) {
54  JsonTreeItem *item = static_cast<JsonTreeItem*>(index.internalPointer());
55  if (item) return item;
56  }
57  return rootItem;
58 }

References index(), and rootItem.

Referenced by data(), index(), insertRows(), parent(), removeRows(), rowCount(), and setData().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getRootItem()

JsonTreeItem * JsonTreeModel::getRootItem ( )

Definition at line 260 of file jsontreemodel.cpp.

261 {
262  return rootItem;
263 }

References rootItem.

Referenced by JsonEditorMain::formatCode().

Here is the caller graph for this function:

◆ getTypeString()

QString JsonTreeModel::getTypeString ( value_t  vType)
private

Definition at line 265 of file jsontreemodel.cpp.

266 {
267  QString typeString;
268  switch (vType)
269  {
270  case value_t::array:
271  typeString = tr("array");
272  break;
273  case value_t::boolean:
274  typeString = tr("boolean");
275  break;
276  case value_t::number_integer:
277  typeString = tr("integer");
278  break;
279  case value_t::null:
280  typeString = tr("null");
281  break;
282  case value_t::object:
283  typeString = tr("object");
284  break;
285  case value_t::number_float:
286  typeString = tr("double");
287  break;
288  case value_t::string:
289  typeString = tr("string");
290  break;
291  case value_t::number_unsigned:
292  typeString = tr("unsigned integer");
293  break;
294  defalut:
295  typeString = tr("type error");
296  }
297  return typeString;
298 }

Referenced by setupModelData().

Here is the caller graph for this function:

◆ headerData()

QVariant JsonTreeModel::headerData ( int  section,
Qt::Orientation  orientation,
int  role = Qt::DisplayRole 
) const

Definition at line 60 of file jsontreemodel.cpp.

62 {
63  if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
64  return rootItem->data(section);
65 
66  return QVariant();
67 }

References JsonTreeItem::data(), and rootItem.

Here is the call graph for this function:

◆ index()

QModelIndex JsonTreeModel::index ( int  row,
int  column,
const QModelIndex &  parent = QModelIndex() 
) const

Definition at line 69 of file jsontreemodel.cpp.

70 {
71  if (parent.isValid() && parent.column() != 0)
72  return QModelIndex();
73 
74  JsonTreeItem *parentItem = getItem(parent);
75 
76  JsonTreeItem *childItem = parentItem->child(row);
77  if (childItem)
78  return createIndex(row, column, childItem);
79  else
80  return QModelIndex();
81 }

References JsonTreeItem::child(), getItem(), and parent().

Referenced by data(), flags(), getItem(), JsonEditorMain::insertTreeChild(), parent(), and setData().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ insertColumns()

bool JsonTreeModel::insertColumns ( int  position,
int  columns,
const QModelIndex &  parent = QModelIndex() 
)

Definition at line 83 of file jsontreemodel.cpp.

84 {
85  bool success;
86 
87  beginInsertColumns(parent, position, position + columns - 1);
88  success = rootItem->insertColumns(position, columns);
89  endInsertColumns();
90 
91  return success;
92 }

References JsonTreeItem::insertColumns(), parent(), and rootItem.

Here is the call graph for this function:

◆ insertRows()

bool JsonTreeModel::insertRows ( int  position,
int  rows,
const QModelIndex &  parent = QModelIndex() 
)

Definition at line 94 of file jsontreemodel.cpp.

95 {
96  JsonTreeItem *parentItem = getItem(parent);
97  bool success;
98 
99  beginInsertRows(parent, position, position + rows - 1);
100  success = parentItem->insertChildren(position, rows, rootItem->columnCount());
101  endInsertRows();
102 
103  return success;
104 }

References JsonTreeItem::columnCount(), getItem(), JsonTreeItem::insertChildren(), parent(), and rootItem.

Here is the call graph for this function:

◆ parent()

QModelIndex JsonTreeModel::parent ( const QModelIndex &  index) const

Definition at line 106 of file jsontreemodel.cpp.

107 {
108  if (!index.isValid())
109  return QModelIndex();
110 
111  JsonTreeItem *childItem = getItem(index);
112  JsonTreeItem *parentItem = childItem->parent();
113 
114  if (parentItem == rootItem)
115  return QModelIndex();
116 
117  return createIndex(parentItem->childNumber(), 0, parentItem);
118 }

References JsonTreeItem::childNumber(), getItem(), index(), JsonTreeItem::parent(), and rootItem.

Referenced by index(), insertColumns(), insertRows(), removeColumns(), removeRows(), rowCount(), and setupModelData().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeColumns()

bool JsonTreeModel::removeColumns ( int  position,
int  columns,
const QModelIndex &  parent = QModelIndex() 
)

Definition at line 120 of file jsontreemodel.cpp.

121 {
122  bool success;
123 
124  beginRemoveColumns(parent, position, position + columns - 1);
125  success = rootItem->removeColumns(position, columns);
126  endRemoveColumns();
127 
128  if (rootItem->columnCount() == 0)
129  removeRows(0, rowCount());
130 
131  return success;
132 }

References JsonTreeItem::columnCount(), parent(), JsonTreeItem::removeColumns(), removeRows(), rootItem, and rowCount().

Here is the call graph for this function:

◆ removeRows()

bool JsonTreeModel::removeRows ( int  position,
int  rows,
const QModelIndex &  parent = QModelIndex() 
)

Definition at line 134 of file jsontreemodel.cpp.

135 {
136  JsonTreeItem *parentItem = getItem(parent);
137  bool success = true;
138 
139  beginRemoveRows(parent, position, position + rows - 1);
140  success = parentItem->removeChildren(position, rows);
141  endRemoveRows();
142 
143  return success;
144 }

References getItem(), parent(), and JsonTreeItem::removeChildren().

Referenced by removeColumns().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rowCount()

int JsonTreeModel::rowCount ( const QModelIndex &  parent = QModelIndex()) const

Definition at line 146 of file jsontreemodel.cpp.

147 {
148  JsonTreeItem *parentItem = getItem(parent);
149 
150  return parentItem->childCount();
151 }

References JsonTreeItem::childCount(), getItem(), and parent().

Referenced by removeColumns().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setData()

bool JsonTreeModel::setData ( const QModelIndex &  index,
const QVariant &  value,
int  role = Qt::EditRole 
)

Definition at line 153 of file jsontreemodel.cpp.

155 {
156  if (role != Qt::EditRole)
157  return false;
158 
159  JsonTreeItem *item = getItem(index);
160  bool result = item->setData(index.column(), value);
161 
162  if (result)
163  emit dataChanged(index, index);
164 
165  return result;
166 }

References getItem(), index(), and JsonTreeItem::setData().

Referenced by JsonEditorMain::insertTreeChild().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setHeaderData()

bool JsonTreeModel::setHeaderData ( int  section,
Qt::Orientation  orientation,
const QVariant &  value,
int  role = Qt::EditRole 
)

Definition at line 168 of file jsontreemodel.cpp.

170 {
171  if (role != Qt::EditRole || orientation != Qt::Horizontal)
172  return false;
173 
174  bool result = rootItem->setData(section, value);
175 
176  if (result)
177  emit headerDataChanged(orientation, section, section);
178 
179  return result;
180 }

References rootItem, and JsonTreeItem::setData().

Here is the call graph for this function:

◆ setupModelData()

void JsonTreeModel::setupModelData ( const nlohmann::json &  lines,
JsonTreeItem *  parent 
)
private

Definition at line 182 of file jsontreemodel.cpp.

183 {
184  nlohmann::json* data = (nlohmann::json*)&lines;
185  //QList<JsonTreeItem*> parents;
186  QVector<QVariant> columnData;
187  //parents << parent;
188 
189  /*
190  JsonTreeItem *parent1 = parents.last();
191  parent1->insertChildren(parent1->childCount(), 1, 2);
192  parent1->child(parent1->childCount() - 1)->setData(0, QVariant(tr("A")));
193  parent1->child(parent1->childCount() - 1)->setData(1, QVariant(tr("fdsfs")));
194 
195  JsonTreeItem *parent2 = parents.last()->child(0);
196  parent2->insertChildren(parent2->childCount(), 1, 2);
197  parent2->child(parent2->childCount() - 1)->setData(0, QVariant(tr("B")));
198  parent2->child(parent2->childCount() - 1)->setData(1, QVariant(tr("fdsfs")));
199  */
200 
201  int len = data->size();
202  nlohmann::json::iterator dataIt = data->begin();
203 
204  for (int i = 0; i < len; i++)
205  {
206  std::string keyStr;
207  auto key = dataIt.key();
208  auto vType = data->type();
209  QVariant keyIndex, keyData, keyType;
210  if (vType == value_t::array )
211  {
212  QString arrayIndex("");
213  arrayIndex.append(QString().sprintf("%i", i));
214  keyIndex = QVariant(arrayIndex);
215  keyData = QVariant(tr(dataIt->dump().c_str()));
216  keyType = QVariant(getTypeString(dataIt.value().type()));
217  }
218  else
219  {
220  // key = data->at(i);
221  keyIndex = QVariant(tr(key.c_str()));
222  if (dataIt.value().type() != value_t::object ) {
223  keyData = QVariant(tr(dataIt->dump().c_str()));
224  }
225  else {
226  keyData = QVariant("");
227  }
228  keyType = QVariant(getTypeString(dataIt.value().type()));
229  }
230 
231  columnData.clear();
232  columnData.push_back(keyIndex);
233  columnData.push_back(keyData);
234  columnData.push_back(keyType);
235 
236  JsonTreeItem *parent1 = parent;
237  parent1->insertChildren(parent1->childCount(), 1, rootItem->columnCount());
238  for (int column = 0; column < columnData.size(); ++column)
239  parent1->child(parent1->childCount() - 1)->setData(column, columnData[column]);
240 
241  if (data->type() == value_t::array)
242  {
243  if (dataIt.value().type() == value_t::array || dataIt.value().type() == value_t::object)
244  {
245  setupModelData(dataIt.value(), parent1->child(parent1->childCount() - 1));
246  }
247  }
248  else
249  {
250  if (dataIt.value().type() == value_t::array || dataIt.value().type() == value_t::object)
251  {
252  setupModelData(dataIt.value(), parent1->child(parent1->childCount() - 1));
253  }
254  }
255 
256  dataIt++;
257  }
258 }

References JsonTreeItem::child(), JsonTreeItem::childCount(), JsonTreeItem::columnCount(), data(), getTypeString(), JsonTreeItem::insertChildren(), parent(), rootItem, and JsonTreeItem::setData().

Referenced by JsonTreeModel().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ rootItem


The documentation for this class was generated from the following files:
JsonTreeModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: jsontreemodel.cpp:146
JsonTreeItem::insertColumns
bool insertColumns(int position, int columns)
Definition: jsontreeitem.cpp:58
JsonTreeItem::removeColumns
bool removeColumns(int position, int columns)
Definition: jsontreeitem.cpp:88
JsonTreeItem::removeChildren
bool removeChildren(int position, int count)
Definition: jsontreeitem.cpp:77
JsonTreeModel::getItem
JsonTreeItem * getItem(const QModelIndex &index) const
Definition: jsontreemodel.cpp:51
json
nlohmann::json json
Definition: AutoPilotNode.hpp:21
JsonTreeItem::childCount
int childCount() const
Definition: jsontreeitem.cpp:21
JsonTreeItem::parent
JsonTreeItem * parent()
Definition: jsontreeitem.cpp:72
JsonTreeItem::columnCount
int columnCount() const
Definition: jsontreeitem.cpp:34
JsonTreeModel::data
QVariant data(const QModelIndex &index, int role) const
Definition: jsontreemodel.cpp:30
JsonTreeItem::child
JsonTreeItem * child(int number)
Definition: jsontreeitem.cpp:16
JsonTreeModel::parent
QModelIndex parent(const QModelIndex &index) const
Definition: jsontreemodel.cpp:106
JsonTreeItem::childNumber
int childNumber() const
Definition: jsontreeitem.cpp:26
JsonTreeItem::setData
bool setData(int column, const QVariant &value)
Definition: jsontreeitem.cpp:102
JsonTreeItem::data
QVariant data(int column) const
Definition: jsontreeitem.cpp:39
JsonTreeModel::setupModelData
void setupModelData(const nlohmann::json &lines, JsonTreeItem *parent)
Definition: jsontreemodel.cpp:182
JsonTreeModel::rootItem
JsonTreeItem * rootItem
Definition: jsontreemodel.h:55
JsonTreeModel::getTypeString
QString getTypeString(value_t vType)
Definition: jsontreemodel.cpp:265
JsonTreeModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Definition: jsontreemodel.cpp:69
JsonTreeItem::insertChildren
bool insertChildren(int position, int count, int columns)
Definition: jsontreeitem.cpp:44
JsonTreeModel::removeRows
bool removeRows(int position, int rows, const QModelIndex &parent=QModelIndex())
Definition: jsontreemodel.cpp:134
JsonTreeItem
Definition: jsontreeitem.h:8