Files
dbc/qt/dbc_tree_model.h
T
2026-10-20 13:38:21 -04:00

62 lines
2.6 KiB
C++

// SPDX-License-Identifier: MIT
#ifndef DBC_TREE_MODEL_H
#define DBC_TREE_MODEL_H
#include "tree_node.h"
#include <QAbstractItemModel>
/** Read-only model. Owns an immutable tree; all indexes expire on SetTree().
* Use QPersistentModelIndex to observe reset invalidation. Call on the model thread.
*/
class DbcTreeModel : public QAbstractItemModel {
public:
/** @brief Display columns for frame and signal metadata. */
enum Column { Name,
CanId,
PayloadLength,
StartBit,
BitLength,
ByteOrder,
Signed,
Unit,
Comment,
ColumnCount };
// Metadata roles apply on every column; inapplicable roles return invalid QVariant.
/** @brief Typed metadata roles in addition to Qt display roles. */
enum Role { NodeTypeRole = Qt::UserRole + 1,
CanIdRole,
PayloadLengthRole,
StartBitRole,
BitLengthRole,
LittleEndianRole,
SignedRole,
UnitRole,
CommentRole,
ValueDescriptionsRole,
ExtendedRole };
/** @brief Create an empty read-only model. */
explicit DbcTreeModel(QObject *parent = nullptr);
/** @brief Replace the tree and invalidate indexes; reject roots with a parent or non-root type. */
void SetTree(std::unique_ptr<TreeNode> root);
/** @brief Return a child index; only column zero can have children. */
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
/** @brief Return the parent in column zero, or an invalid index for frames. */
QModelIndex parent(const QModelIndex &child) const override;
/** @brief Return the child count, capped at the Qt integer limit. */
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
/** @brief Return the fixed metadata column count for this model. */
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
/** @brief Return display text or typed metadata for an index. */
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
/** @brief Return horizontal display column titles. */
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
/** @brief Allow selecting and enabling valid indexes without editing. */
Qt::ItemFlags flags(const QModelIndex &index) const override;
private:
const TreeNode *Node(const QModelIndex &index) const;
std::unique_ptr<TreeNode> m_root;
};
#endif