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);
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 {
37 index.column() >= ColumnCount ||
index.row() < 0)
39 return static_cast<const TreeNode *
>(
index.internalPointer());
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();
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));
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())));
71 return parentIndex.isValid() && !Node(parentIndex) ? 0 : ColumnCount;
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();
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]);
146 return Node(item) ? Qt::ItemIsEnabled | Qt::ItemIsSelectable : Qt::NoItemFlags;
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Return the child count, capped at the Qt integer limit.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Return the fixed metadata column count for this model.
DbcTreeModel(QObject *parent=nullptr)
Create an empty read-only model.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Return a child index; only column zero can have children.
void SetTree(std::unique_ptr< TreeNode > root)
Replace the tree and invalidate indexes; reject roots with a parent or non-root type.
Qt::ItemFlags flags(const QModelIndex &index) const override
Allow selecting and enabling valid indexes without editing.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Return horizontal display column titles.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Return display text or typed metadata for an index.
QModelIndex parent(const QModelIndex &child) const override
Return the parent in column zero, or an invalid index for frames.
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