Skip to content

Commit

Permalink
Refs #19743: Refactor model roles to use direct get methods
Browse files Browse the repository at this point in the history
Signed-off-by: JesusPoderoso <[email protected]>
  • Loading branch information
JesusPoderoso committed Nov 13, 2023
1 parent f09da2b commit 70ef10a
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 81 deletions.
7 changes: 0 additions & 7 deletions include/fastdds_monitor/model/tree/StatusTreeItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,6 @@ class StatusTreeItem
//! Destroy the item. It will destroy every child.
~StatusTreeItem();

//! Return the stored data of the node.
const QVariant& data() const;

//! Return the stored data of the node.
const QVariant& data(
int role) const;

const QVariant& entity_id() const;

const QVariant& status_kind() const;
Expand Down
46 changes: 28 additions & 18 deletions include/fastdds_monitor/model/tree/StatusTreeModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ namespace models {
class StatusTreeModel : public QAbstractItemModel
{
Q_OBJECT
//Q_ENUMS(models::StatusTreeItem::ModelItemRoles)

public:
explicit StatusTreeModel(
Expand All @@ -66,21 +65,6 @@ class StatusTreeModel : public QAbstractItemModel
// Overriden method from QAbstractItemModel

public:

//! Role names to allow queries to get some specific information from the Item
enum ModelItemRoles
{
idRole = Qt::UserRole + 1, //! Role for attribute Id
statusRole, //! Role for attribute Status
kindRole, //! Role for attribute Kind
valueRole, //! Role for attribute Value
descriptionRole, //! Role for attribute Description
aliveRole, //! Role for attribute Alive
nameRole //! Role for attribute Name
// The nameRole must always be the last one as it is used in child classes
// as the initial role of the enumeration)
};

int rowCount(
const QModelIndex& index) const override;
int columnCount(
Expand All @@ -98,6 +82,34 @@ class StatusTreeModel : public QAbstractItemModel
const QModelIndex& index,
int role = 0) const override;

Q_INVOKABLE QVariant name(
const QModelIndex& index,
int role = 0) const;

Q_INVOKABLE QVariant id(
const QModelIndex& index,
int role = 0) const;

Q_INVOKABLE QVariant status(
const QModelIndex& index,
int role = 0) const;

Q_INVOKABLE QVariant kind(
const QModelIndex& index,
int role = 0) const;

Q_INVOKABLE QVariant value(
const QModelIndex& index,
int role = 0) const;

Q_INVOKABLE QVariant description(
const QModelIndex& index,
int role = 0) const;

Q_INVOKABLE QVariant alive(
const QModelIndex& index,
int role = 0) const;

bool setData(
const QModelIndex& index,
const QVariant& value,
Expand All @@ -107,8 +119,6 @@ class StatusTreeModel : public QAbstractItemModel
int row,
const QModelIndex &index = QModelIndex());

QHash<int, QByteArray> roleNames() const override;

public:
//! Add an item to the top level.
void addTopLevelItem(
Expand Down
15 changes: 7 additions & 8 deletions qml/StatusTreeViewItem.qml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ Item {
font.pointSize: Theme.font.pointSize
}
property alias font: root.fontMetrics.font
enum Role { Id=257, Status, Kind, Value, Description, Alive, Name }

// private (internal) signals
signal filter_(int entityId)
Expand Down Expand Up @@ -202,12 +201,12 @@ Item {

property var currentIndex: root.model.index(index, 0, parentIndex)
property var currentData: root.model.data(currentIndex)
property var currentId: root.model.data(currentIndex, StatusTreeViewItem.Role.Id)
property var currentStatus: root.model.data(currentIndex, StatusTreeViewItem.Role.Status)
property var currentKind: root.model.data(currentIndex, StatusTreeViewItem.Role.Kind)
property var currentValue: root.model.data(currentIndex, StatusTreeViewItem.Role.Value)
property var currentDescription: root.model.data(currentIndex, StatusTreeViewItem.Role.Description)
property var currentAlive: root.model.data(currentIndex, StatusTreeViewItem.Role.Alive)
property var currentId: root.model.id(currentIndex)
property var currentStatus: root.model.status(currentIndex)
property var currentKind: root.model.kind(currentIndex)
property var currentValue: root.model.value(currentIndex)
property var currentDescription: root.model.description(currentIndex)
property var currentAlive: root.model.alive(currentIndex)
property Item currentItem: repeater.itemAt(index)
property bool expanded: true
property bool selected: false
Expand Down Expand Up @@ -240,7 +239,7 @@ Item {
const parent = root.model.index(index, 0, parentIndex)
_prop.itemChildCount = root.model.rowCount(parent)
// refresh counter
var new_value = root.model.data(_prop.currentIndex, StatusTreeViewItem.Role.Value)
var new_value = root.model.value(_prop.currentIndex)
if (new_value != undefined)
{
_prop.currentValue = new_value
Expand Down
28 changes: 0 additions & 28 deletions src/model/tree/StatusTreeItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,34 +181,6 @@ int StatusTreeItem::childCount() const
return child_items_.count();
}

const QVariant& StatusTreeItem::data() const
{
return this->data(models::StatusTreeModel::ModelItemRoles::nameRole);
}

const QVariant& StatusTreeItem::data(
int role) const
{
switch (role)
{
case models::StatusTreeModel::ModelItemRoles::idRole:
return this->entity_id();
case models::StatusTreeModel::ModelItemRoles::statusRole:
return this->status();
case models::StatusTreeModel::ModelItemRoles::kindRole:
return this->status_kind();
case models::StatusTreeModel::ModelItemRoles::valueRole:
return this->value();
case models::StatusTreeModel::ModelItemRoles::descriptionRole:
return this->description();
case models::StatusTreeModel::ModelItemRoles::aliveRole:
return this->alive();
case models::StatusTreeModel::ModelItemRoles::nameRole:
default:
return this->name();
}
}

const QVariant& StatusTreeItem::entity_id() const
{
return id_variant_;
Expand Down
104 changes: 84 additions & 20 deletions src/model/tree/StatusTreeModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,97 @@ QModelIndex StatusTreeModel::parent(
return createIndex(parentItem->row(), 0, parentItem);
}


QVariant StatusTreeModel::data(
const QModelIndex& index,
const int role) const
const int role) const
{
return name(index, role);
}

QVariant StatusTreeModel::name(
const QModelIndex& index,
const int /*role*/) const
{
if (!index.isValid())
{
return QVariant();
}

return internalPointer(index)->name();
}


QVariant StatusTreeModel::id(
const QModelIndex& index,
const int /*role*/) const
{
if (!index.isValid())
{
return QVariant();
}

return internalPointer(index)->entity_id();
}

QVariant StatusTreeModel::status(
const QModelIndex& index,
const int /*role*/) const
{
if (!index.isValid()/* || role != Qt::DisplayRole*/)
if (!index.isValid())
{
return QVariant();
}

return internalPointer(index)->status();
}

QVariant StatusTreeModel::kind(
const QModelIndex& index,
const int /*role*/) const
{
if (!index.isValid())
{
return QVariant();
}

return internalPointer(index)->data(role);
return internalPointer(index)->status_kind();
}

QVariant StatusTreeModel::value(
const QModelIndex& index,
const int /*role*/) const
{
if (!index.isValid())
{
return QVariant();
}

return internalPointer(index)->value();
}

QVariant StatusTreeModel::description(
const QModelIndex& index,
const int /*role*/) const
{
if (!index.isValid())
{
return QVariant();
}

return internalPointer(index)->description();
}

QVariant StatusTreeModel::alive(
const QModelIndex& index,
const int /*role*/) const
{
if (!index.isValid())
{
return QVariant();
}

return internalPointer(index)->alive();
}

bool StatusTreeModel::setData(
Expand Down Expand Up @@ -444,21 +525,4 @@ StatusTreeItem* StatusTreeModel::getTopLevelItem(
return new_entity_item;
}


QHash<int, QByteArray> StatusTreeModel::roleNames() const
{
// TODO Jesus this roles are not currently used in the QML, find out why
QHash<int, QByteArray> roles;

roles[idRole] = "id";
roles[statusRole] = "status";
roles[kindRole] = "kind";
roles[valueRole] = "value";
roles[descriptionRole] = "description";
roles[aliveRole] = "alive";
roles[nameRole] = "name";

return roles;
}

} // namespace models

0 comments on commit 70ef10a

Please sign in to comment.