Skip to content
Snippets Groups Projects
Commit b2513dfd authored by Fabian Paus's avatar Fabian Paus
Browse files

Add JSONTreeModel

- Model for QTreeView
- Displays JSON objects in a tree view
parent bd6a121f
No related branches found
No related tags found
No related merge requests found
......@@ -10,8 +10,14 @@ armarx_build_if(jsoncpp_FOUND "jsoncpp not available")
set(LIB_NAME RobotAPIWidgets)
set(LIBS RobotAPICore)
set(LIB_FILES DebugLayerControlWidget.cpp)
set(LIB_HEADERS DebugLayerControlWidget.h)
set(LIB_FILES
DebugLayerControlWidget.cpp
JSONTreeModel.cpp
)
set(LIB_HEADERS
DebugLayerControlWidget.h
JSONTreeModel.h
)
set(GUI_UIS DebugLayerControlWidget.ui)
list(APPEND LIB_HEADERS ${GUI_UIS})
......
#include "JSONTreeModel.h"
namespace armarx
{
void JSONTreeModel::setRoot(ValuePtr root)
{
this->root = root;
rows.clear();
rows[root] = 0;
parents.clear();
parents[root] = nullptr;
names.clear();
names[root] = "";
}
QModelIndex JSONTreeModel::index(int row, int column, const QModelIndex& parentIndex) const
{
ValuePtr parent = refFrom(parentIndex);
if (row >= (int)parent->size())
{
return QModelIndex();
}
ValuePtr child = nullptr;
if (parent->is_array())
{
child = &parent->at(row);
names[child] = "[" + std::to_string(row) + "]";
}
else if (parent->is_object())
{
auto it = parent->begin();
std::advance(it, row);
child = &it.value();
names[child] = it.key();
}
if (child)
{
parents[child] = parent;
rows[child] = row;
return createIndex(row, column, child);
}
else
{
return QModelIndex();
}
}
QModelIndex JSONTreeModel::parent(const QModelIndex& index) const
{
if (!index.isValid())
{
return QModelIndex();
}
ValuePtr child = refFrom(index);
ValuePtr parent = parents.at(child);
if (parent == nullptr)
{
return QModelIndex();
}
int row = rows.at(parent);
return createIndex(row, 0, parent);
}
int JSONTreeModel::rowCount(const QModelIndex& parentIndex) const
{
if (parentIndex.column() > 1)
{
return 0;
}
ValuePtr parent = refFrom(parentIndex);
if (parent->is_array() || parent->is_object())
{
return parent->size();
}
else
{
return 0;
}
}
int JSONTreeModel::columnCount(const QModelIndex& parent) const
{
return 2;
}
QVariant JSONTreeModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
{
return QVariant();
}
if (role != Qt::DisplayRole)
{
return QVariant();
}
nlohmann::json* ref = refFrom(index);
if (index.column() == 0)
{
std::string const& name = names.at(ref);
return QVariant(name.c_str());
}
switch (ref->type())
{
case nlohmann::json::value_t::null:
return QVariant("null");
case nlohmann::json::value_t::object:
return QVariant("object");
case nlohmann::json::value_t::array:
return QVariant("array");
case nlohmann::json::value_t::string:
return QVariant(ref->get<std::string>().c_str());
case nlohmann::json::value_t::boolean:
return QVariant(ref->get<bool>());
case nlohmann::json::value_t::number_integer:
return QVariant(ref->get<int>());
case nlohmann::json::value_t::number_unsigned:
return QVariant(ref->get<unsigned int>());
case nlohmann::json::value_t::number_float:
return QVariant(ref->get<float>());
default:
return QVariant("n/a");
}
}
QVariant JSONTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
{
return QVariant();
}
switch (section)
{
case 0:
return QVariant("Name");
case 1:
return QVariant("Value");
default:
return QVariant();
}
}
auto JSONTreeModel::refFrom(const QModelIndex& index) const -> ValuePtr
{
if (index.isValid())
{
return static_cast<ValuePtr>(index.internalPointer());
}
else
{
return root;
}
}
}
#pragma once
#include <VirtualRobot/Util/json/json.hpp>
#include <QAbstractItemModel>
namespace armarx
{
class JSONTreeModel : public QAbstractItemModel
{
public:
using ValuePtr = nlohmann::json*;
void setRoot(ValuePtr root);
// QAbstractItemModel interface
QModelIndex index(int row, int column, const QModelIndex& parent) const override;
QModelIndex parent(const QModelIndex& index) const override;
int rowCount(const QModelIndex& parent) const override;
int columnCount(const QModelIndex& parent) const override;
QVariant data(const QModelIndex& index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
private:
ValuePtr refFrom(QModelIndex const& index) const;
private:
mutable std::map<ValuePtr, int> rows;
mutable std::map<ValuePtr, ValuePtr> parents;
mutable std::map<ValuePtr, std::string> names;
ValuePtr root;
};
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment