From d9d20790f13fe4e98b1ccb52fbd06aec7fbf6dc1 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Wed, 3 Apr 2024 13:17:13 +0300 Subject: [PATCH] UI: Refactor TreeItemDelegate --- src/ui/form/controls/treeitemdelegate.cpp | 21 ++++++++++----------- src/ui/form/controls/treeitemdelegate.h | 7 +------ src/ui/form/controls/treeview.cpp | 10 ---------- src/ui/form/controls/treeview.h | 3 --- src/ui/model/rulelistmodel.cpp | 18 ++++++++++-------- src/ui/model/rulelistmodel.h | 5 ++--- src/ui/model/zonelistmodel.h | 4 ++-- src/ui/task/tasklistmodel.h | 4 ++-- src/ui/util/model/tableitemmodel.cpp | 5 ----- src/ui/util/model/tableitemmodel.h | 12 ++++++++---- 10 files changed, 35 insertions(+), 54 deletions(-) diff --git a/src/ui/form/controls/treeitemdelegate.cpp b/src/ui/form/controls/treeitemdelegate.cpp index 1adea5ef..7eb830f8 100644 --- a/src/ui/form/controls/treeitemdelegate.cpp +++ b/src/ui/form/controls/treeitemdelegate.cpp @@ -4,22 +4,13 @@ TreeItemDelegate::TreeItemDelegate(QObject *parent) : QStyledItemDelegate(parent) { } -void TreeItemDelegate::setModel(TableItemModel *model) -{ - m_model = model; - Q_ASSERT(m_model); -} - void TreeItemDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItem opt(option); - if (index.parent().isValid() && opt.rect.left() > 0) { - opt.rect.setLeft(16); - } - if (model()->flagIsEnabled(index) != Qt::ItemIsEnabled) { - opt.state &= ~QStyle::State_Enabled; + if (index.parent().isValid()) { + setOptionEnabled(opt, index); } QStyledItemDelegate::paint(painter, opt, index); @@ -30,3 +21,11 @@ QSize TreeItemDelegate::sizeHint( { return QSize(100, 24); } + +void TreeItemDelegate::setOptionEnabled(QStyleOptionViewItem &opt, const QModelIndex &index) const +{ + const bool isIndexEnabled = index.data(TableItemModel::EnabledRole).toBool(); + if (!isIndexEnabled) { + opt.state &= ~QStyle::State_Enabled; + } +} diff --git a/src/ui/form/controls/treeitemdelegate.h b/src/ui/form/controls/treeitemdelegate.h index 05477bd4..5c2ab3df 100644 --- a/src/ui/form/controls/treeitemdelegate.h +++ b/src/ui/form/controls/treeitemdelegate.h @@ -3,8 +3,6 @@ #include -class TableItemModel; - class TreeItemDelegate : public QStyledItemDelegate { Q_OBJECT @@ -12,15 +10,12 @@ class TreeItemDelegate : public QStyledItemDelegate public: explicit TreeItemDelegate(QObject *parent = nullptr); - TableItemModel *model() const { return m_model; } - void setModel(TableItemModel *model); - void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override; private: - TableItemModel *m_model = nullptr; + void setOptionEnabled(QStyleOptionViewItem &opt, const QModelIndex &index) const; }; #endif // TREEITEMDELEGATE_H diff --git a/src/ui/form/controls/treeview.cpp b/src/ui/form/controls/treeview.cpp index a7114e58..dfc46071 100644 --- a/src/ui/form/controls/treeview.cpp +++ b/src/ui/form/controls/treeview.cpp @@ -20,8 +20,6 @@ void TreeView::setModel(QAbstractItemModel *model) connect(model, &QAbstractItemModel::modelReset, this, [&] { emit currentIndexChanged(currentIndex()); }); - - setupItemDelegateModel(); } void TreeView::setupItemDelegate() @@ -31,14 +29,6 @@ void TreeView::setupItemDelegate() setItemDelegateForColumn(0, tid); } -void TreeView::setupItemDelegateModel() -{ - auto tid = qobject_cast(itemDelegateForColumn(0)); - Q_ASSERT(tid); - - tid->setModel(qobject_cast(model())); -} - void TreeView::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) { QTreeView::selectionChanged(selected, deselected); diff --git a/src/ui/form/controls/treeview.h b/src/ui/form/controls/treeview.h index ea5ce817..71e333a6 100644 --- a/src/ui/form/controls/treeview.h +++ b/src/ui/form/controls/treeview.h @@ -27,9 +27,6 @@ protected: void contextMenuEvent(QContextMenuEvent *event) override; -private: - void setupItemDelegateModel(); - private: QMenu *m_menu = nullptr; }; diff --git a/src/ui/model/rulelistmodel.cpp b/src/ui/model/rulelistmodel.cpp index 301be9db..2de64766 100644 --- a/src/ui/model/rulelistmodel.cpp +++ b/src/ui/model/rulelistmodel.cpp @@ -175,6 +175,10 @@ QVariant RuleListModel::data(const QModelIndex &index, int role) const // Icon case Qt::DecorationRole: return dataDecoration(index); + + // Enabled + case EnabledRole: + return dataEnabled(index); } return QVariant(); @@ -238,6 +242,12 @@ QVariant RuleListModel::dataDecoration(const QModelIndex &index) const return QVariant(); } +QVariant RuleListModel::dataEnabled(const QModelIndex &index) const +{ + const auto &ruleRow = ruleRowAt(index); + return ruleRow.enabled; +} + void RuleListModel::fillQueryVars(QVariantHash &vars) const { FtsTableSqlModel::fillQueryVars(vars); @@ -245,14 +255,6 @@ void RuleListModel::fillQueryVars(QVariantHash &vars) const vars.insert(":type", sqlRuleType()); } -Qt::ItemFlags RuleListModel::flagIsEnabled(const QModelIndex &index) const -{ - setSqlRuleType(index); - - const auto &ruleRow = ruleRowAt(index); - return ruleRow.enabled ? Qt::ItemIsEnabled : Qt::NoItemFlags; -} - Qt::ItemFlags RuleListModel::flagHasChildren(const QModelIndex &index) const { return isIndexRule(index) ? Qt::ItemNeverHasChildren : Qt::NoItemFlags; diff --git a/src/ui/model/rulelistmodel.h b/src/ui/model/rulelistmodel.h index 5b106b2f..74947385 100644 --- a/src/ui/model/rulelistmodel.h +++ b/src/ui/model/rulelistmodel.h @@ -49,15 +49,14 @@ public: int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - Qt::ItemFlags flagIsEnabled(const QModelIndex &index) const override; - Qt::ItemFlags flagHasChildren(const QModelIndex &index) const override; - const RuleRow &ruleRowAt(const QModelIndex &index) const; RuleRow ruleRowById(int ruleId, Rule::RuleType ruleType) const; static QStringList ruleTypeNames(); protected: + Qt::ItemFlags flagHasChildren(const QModelIndex &index) const override; + void fillQueryVars(QVariantHash &vars) const override; bool updateTableRow(const QVariantHash &vars, int row) const override; diff --git a/src/ui/model/zonelistmodel.h b/src/ui/model/zonelistmodel.h index 8027b9cb..999b5d88 100644 --- a/src/ui/model/zonelistmodel.h +++ b/src/ui/model/zonelistmodel.h @@ -37,8 +37,6 @@ public: QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; - Qt::ItemFlags flagIsUserCheckable(const QModelIndex &index) const override; - const ZoneRow &zoneRowAt(int row) const; QVariant zoneTypeByCode(const QString &typeCode) const; @@ -47,6 +45,8 @@ public: const QVariantList &zoneSources() const { return m_zoneSources; } protected: + Qt::ItemFlags flagIsUserCheckable(const QModelIndex &index) const override; + bool updateTableRow(const QVariantHash &vars, int row) const override; TableRow &tableRow() const override { return m_zoneRow; } diff --git a/src/ui/task/tasklistmodel.h b/src/ui/task/tasklistmodel.h index 2a4f226a..5fb6d87e 100644 --- a/src/ui/task/tasklistmodel.h +++ b/src/ui/task/tasklistmodel.h @@ -38,8 +38,6 @@ public: QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; - Qt::ItemFlags flagIsUserCheckable(const QModelIndex &index) const override; - void setupTaskRows(); QVariant toVariant() const; @@ -48,6 +46,8 @@ signals: void dataEdited(); protected: + Qt::ItemFlags flagIsUserCheckable(const QModelIndex &index) const override; + bool updateTableRow(const QVariantHash & /*vars*/, int /*row*/) const override { return true; } TableRow &tableRow() const override { return m_taskRow; } diff --git a/src/ui/util/model/tableitemmodel.cpp b/src/ui/util/model/tableitemmodel.cpp index 6e19dfd2..667a485f 100644 --- a/src/ui/util/model/tableitemmodel.cpp +++ b/src/ui/util/model/tableitemmodel.cpp @@ -39,11 +39,6 @@ void TableItemModel::resetLater() m_resetTimer->startTrigger(); } -Qt::ItemFlags TableItemModel::flagIsEnabled(const QModelIndex &index) const -{ - return Qt::ItemIsEnabled; -} - Qt::ItemFlags TableItemModel::flagHasChildren(const QModelIndex & /*index*/) const { return Qt::ItemNeverHasChildren; diff --git a/src/ui/util/model/tableitemmodel.h b/src/ui/util/model/tableitemmodel.h index 2d08a7dd..819223c2 100644 --- a/src/ui/util/model/tableitemmodel.h +++ b/src/ui/util/model/tableitemmodel.h @@ -19,6 +19,11 @@ class TableItemModel : public QAbstractItemModel Q_OBJECT public: + enum TableItemRole { + EnabledRole = Qt::UserRole, + EndRole, + }; + explicit TableItemModel(QObject *parent = nullptr); QModelIndex index( @@ -29,16 +34,15 @@ public: Qt::ItemFlags flags(const QModelIndex &index) const override; - virtual Qt::ItemFlags flagIsEnabled(const QModelIndex &index) const; - virtual Qt::ItemFlags flagHasChildren(const QModelIndex &index) const; - virtual Qt::ItemFlags flagIsUserCheckable(const QModelIndex &index) const; - public slots: void resetLater(); void reset(); void refresh(); protected: + virtual Qt::ItemFlags flagHasChildren(const QModelIndex &index) const; + virtual Qt::ItemFlags flagIsUserCheckable(const QModelIndex &index) const; + virtual void invalidateRowCache() const; void updateRowCache(int row) const;