From 8aa10754e2be850c2d90db21bc09eac58e092c85 Mon Sep 17 00:00:00 2001 From: Gres Date: Thu, 2 Apr 2020 20:52:29 +0300 Subject: [PATCH] Add methods to convert between component/index --- src/service/updates.cpp | 30 ++++++++++++++++++++++-------- src/service/updates.h | 2 ++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/service/updates.cpp b/src/service/updates.cpp index afcf959..eb6c64e 100644 --- a/src/service/updates.cpp +++ b/src/service/updates.cpp @@ -407,22 +407,36 @@ void Model::emitColumnsChanged(const QModelIndex &parent) for (auto i = 0; i < count; ++i) emitColumnsChanged(index(0, 0, parent)); } +Model::Component *Model::toComponent(const QModelIndex &index) const +{ + return static_cast(index.internalPointer()); +} + +QModelIndex Model::toIndex(const Model::Component &component, int column) const +{ + return createIndex(component.index, column, + const_cast(&component)); +} + QModelIndex Model::index(int row, int column, const QModelIndex &parent) const { if (!root_) return {}; - if (auto ptr = static_cast(parent.internalPointer())) { + + if (auto ptr = toComponent(parent)) { SOFT_ASSERT(row >= 0 && row < int(ptr->children.size()), return {}); - return createIndex(row, column, ptr->children[row].get()); + return toIndex(*ptr->children[row], column); } + if (row < 0 && row >= int(root_->children.size())) return {}; - return createIndex(row, column, root_->children[row].get()); + + return toIndex(*root_->children[row], column); } QModelIndex Model::parent(const QModelIndex &child) const { - auto ptr = static_cast(child.internalPointer()); + auto ptr = toComponent(child); if (auto parent = ptr->parent) return createIndex(parent->index, 0, parent); return {}; @@ -430,7 +444,7 @@ QModelIndex Model::parent(const QModelIndex &child) const int Model::rowCount(const QModelIndex &parent) const { - if (auto ptr = static_cast(parent.internalPointer())) { + if (auto ptr = toComponent(parent)) { return ptr->children.size(); } return root_ ? root_->children.size() : 0; @@ -463,7 +477,7 @@ QVariant Model::data(const QModelIndex &index, int role) const if ((role != Qt::DisplayRole && role != Qt::EditRole) || !index.isValid()) return {}; - auto ptr = static_cast(index.internalPointer()); + auto ptr = toComponent(index); SOFT_ASSERT(ptr, return {}); switch (index.column()) { @@ -487,7 +501,7 @@ bool Model::setData(const QModelIndex &index, const QVariant &value, int role) if (!index.isValid() || role != Qt::EditRole) return false; - auto ptr = static_cast(index.internalPointer()); + auto ptr = toComponent(index); SOFT_ASSERT(ptr, return false); if (index.column() != int(Column::Action)) @@ -506,7 +520,7 @@ bool Model::setData(const QModelIndex &index, const QVariant &value, int role) Qt::ItemFlags Model::flags(const QModelIndex &index) const { - auto ptr = static_cast(index.internalPointer()); + auto ptr = toComponent(index); SOFT_ASSERT(ptr, return {}); auto result = Qt::NoItemFlags | Qt::ItemIsSelectable; diff --git a/src/service/updates.h b/src/service/updates.h index 0d75bdd..7091626 100644 --- a/src/service/updates.h +++ b/src/service/updates.h @@ -81,6 +81,8 @@ private: State currentState(const File& file) const; QString expanded(const QString& source) const; void emitColumnsChanged(const QModelIndex& parent); + Component* toComponent(const QModelIndex& index) const; + QModelIndex toIndex(const Component& component, int column) const; std::unique_ptr root_; std::map expansions_;