2#include "dbc_tree_model.h"
9 QVariantList ToVariantList(
const ValueDescriptions &values) {
10 QVariantList descriptions;
11 for (
const ValueDescriptions::value_type &entry : values) {
13 if (
const std::int64_t *number = std::get_if<std::int64_t>(&entry.first))
14 pair.insert(
"value", QVariant::fromValue(
static_cast<qlonglong
>(*number)));
16 pair.insert(
"value", QVariant::fromValue(
17 static_cast<qulonglong
>(std::get<std::uint64_t>(entry.first))));
18 pair.insert(
"description", QString::fromStdString(entry.second));
19 descriptions.append(pair);
25DbcTreeModel::DbcTreeModel(QObject *parent) : QAbstractItemModel(parent) {}
27void DbcTreeModel::SetTree(std::unique_ptr<TreeNode> root) {
28 if (root && (root->GetType() != NodeType::Root || root->GetParent()))
29 throw std::invalid_argument(
"Model requires a detached root node");
31 m_root = std::move(root);
35const TreeNode *DbcTreeModel::Node(
const QModelIndex &index)
const {
36 if (!index.isValid() || index.model() !=
this || index.column() < 0 ||
37 index.column() >= ColumnCount || index.row() < 0)
39 return static_cast<const TreeNode *
>(index.internalPointer());
42QModelIndex DbcTreeModel::index(
int row,
int column,
const QModelIndex &parentIndex)
const {
43 if (row < 0 || column < 0 || column >= ColumnCount ||
44 (parentIndex.isValid() && parentIndex.column() != 0))
46 const TreeNode *owner = parentIndex.isValid() ? Node(parentIndex) : m_root.get();
50 return child ? createIndex(row, column,
const_cast<TreeNode *
>(child)) : QModelIndex();
53QModelIndex DbcTreeModel::parent(
const QModelIndex &childIndex)
const {
54 const TreeNode *child = Node(childIndex);
56 if (!owner || owner == m_root.get())
58 return createIndex(
static_cast<int>(owner->
GetRow()), 0,
const_cast<TreeNode *
>(owner));
61int DbcTreeModel::rowCount(
const QModelIndex &parentIndex)
const {
62 if (parentIndex.isValid() && parentIndex.column() != 0)
64 const TreeNode *owner = parentIndex.isValid() ? Node(parentIndex) : m_root.get();
67 return static_cast<int>(qMin(owner->
GetChildCount(),
static_cast<std::size_t
>(std::numeric_limits<int>::max())));
70int DbcTreeModel::columnCount(
const QModelIndex &parentIndex)
const {
71 return parentIndex.isValid() && !Node(parentIndex) ? 0 : ColumnCount;
74QVariant DbcTreeModel::data(
const QModelIndex &item,
int role)
const {
82 comment = QString::fromStdString(frame->
comment);
83 else if (signal !=
nullptr)
84 comment = QString::fromStdString(signal->
comment);
85 if (role == Qt::ToolTipRole || role == CommentRole)
87 if (role == NodeTypeRole)
88 return static_cast<int>(node->
GetType());
89 if (role == ValueDescriptionsRole && signal) {
93 if (role == CanIdRole)
95 if (role == PayloadLengthRole)
96 return static_cast<int>(frame->
dlc);
97 if (role == ExtendedRole)
101 if (role == StartBitRole)
103 if (role == BitLengthRole)
105 if (role == LittleEndianRole)
107 if (role == SignedRole)
109 if (role == UnitRole)
110 return QString::fromStdString(signal->
unit);
112 if (role != Qt::DisplayRole)
114 switch (item.column()) {
116 return QString::fromStdString(node->
GetName());
118 return frame ? QVariant(QString(
"0x%1").arg(frame->
canId, 0, 16).toUpper()) : QVariant();
120 return frame ? QVariant(
static_cast<int>(frame->
dlc)) : QVariant();
122 return signal ? QVariant(signal->
startBit) : QVariant();
124 return signal ? QVariant(signal->
length) : QVariant();
126 return signal ? QVariant(signal->
isLittleEndian ?
"Intel" :
"Motorola") : QVariant();
128 return signal ? QVariant(signal->
isSigned ?
"Signed" :
"Unsigned") : QVariant();
130 return signal ? QVariant(QString::fromStdString(signal->
unit)) : QVariant();
138QVariant DbcTreeModel::headerData(
int section, Qt::Orientation orientation,
int role)
const {
139 static const char *
const headers[] = {
"Name",
"CAN ID",
"Payload (bytes)",
"Start bit",
"Length (bits)",
"Byte order",
"Signedness",
"Unit",
"Comment"};
140 if (orientation != Qt::Horizontal || role != Qt::DisplayRole || section < 0 || section >= ColumnCount)
142 return QString::fromLatin1(headers[section]);
145Qt::ItemFlags DbcTreeModel::flags(
const QModelIndex &item)
const {
146 return Node(item) ? Qt::ItemIsEnabled | Qt::ItemIsSelectable : Qt::NoItemFlags;
Tree node for later use in model/view or other hierarchy consumers.
const SignalInfo * GetSignal() const
Get signal payload if node is signal.
const TreeNode * GetChild(std::size_t index) const
Get child by index.
const TreeNode * GetParent() const
const std::string & GetName() const
Get display name.
std::size_t GetChildCount() const
Get child count.
std::size_t GetRow() const
NodeType GetType() const
Get node type.
const FrameInfo * GetFrame() const
Get frame payload if node is frame.
ByteOrder
Signal byte order used for runtime decoding.
Describes one CAN frame from a DBC file.
Describes one signal inside a DBC frame.
ValueDescriptions valueDescriptions