From 36f3c71a66ac081f7c113d1cad1355a14a4a1375 Mon Sep 17 00:00:00 2001 From: Marius Kittler Date: Wed, 10 Oct 2018 17:47:57 +0200 Subject: [PATCH] Make use of ChecklistModel more convenient --- CMakeLists.txt | 2 +- models/checklistmodel.cpp | 35 ++++++++++++++++++++++++++++------- models/checklistmodel.h | 10 ++++++++++ 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fee7755..aa0c2f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ set(META_APP_AUTHOR "Martchus") set(META_APP_URL "https://github.com/${META_APP_AUTHOR}/${META_PROJECT_NAME}") set(META_APP_DESCRIPTION "Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models") set(META_VERSION_MAJOR 5) -set(META_VERSION_MINOR 11) +set(META_VERSION_MINOR 12) set(META_VERSION_PATCH 0) set(META_APP_VERSION ${META_VERSION_MAJOR}.${META_VERSION_MINOR}.${META_VERSION_PATCH}) diff --git a/models/checklistmodel.cpp b/models/checklistmodel.cpp index 1969e63..c427ba3 100644 --- a/models/checklistmodel.cpp +++ b/models/checklistmodel.cpp @@ -73,8 +73,7 @@ QMap ChecklistModel::itemData(const QModelIndex &index) const bool ChecklistModel::setData(const QModelIndex &index, const QVariant &value, int role) { bool success = false; - QVector roles; - roles << role; + QVector roles{ role }; if (index.isValid() && index.row() < m_items.size()) { switch (role) { case Qt::DisplayRole: @@ -90,9 +89,9 @@ bool ChecklistModel::setData(const QModelIndex &index, const QVariant &value, in case idRole(): { m_items[index.row()].m_id = value; success = true; - QString label = labelForId(value); + auto label = labelForId(value); if (!label.isEmpty()) { - m_items[index.row()].m_label = label; + m_items[index.row()].m_label = std::move(label); roles << Qt::DisplayRole; } break; @@ -114,15 +113,32 @@ bool ChecklistModel::setItemData(const QModelIndex &index, const QMap= m_items.size()) { + return false; + } + m_items[row].m_checkState = checked ? Qt::Checked : Qt::Unchecked; + const auto index(this->index(row)); + dataChanged(index, index, QVector{ Qt::CheckStateRole }); + return true; +} + /*! * \brief Returns the label for the specified \a id. * * This method might be reimplemented when subclassing to provide labels * for the item IDs. * - * If an item's ID is set (using setData() and idRole()) this method is called - * to update the item's label as well. If this method returns an empty string - * (default behaviour) the item's label will not be updated. + * If an item's ID is set (using setData() with idRole() or setItems()) this method + * is called to update or initialize the item's label as well. If this method returns + * an empty string (default behaviour) the item's label will not be updated. + * + * This is useful when items are moved by the view (eg. for Drag & Drop) and to + * initialize the ChecklistItem labels more conveniently. */ QString ChecklistModel::labelForId(const QVariant &) const { @@ -167,6 +183,11 @@ void ChecklistModel::setItems(const QList &items) { beginResetModel(); m_items = items; + for (auto &item : m_items) { + if (item.m_label.isEmpty()) { + item.m_label = labelForId(item.id()); + } + } endResetModel(); } diff --git a/models/checklistmodel.h b/models/checklistmodel.h index bcca282..d06b18f 100644 --- a/models/checklistmodel.h +++ b/models/checklistmodel.h @@ -80,6 +80,8 @@ public: QMap itemData(const QModelIndex &index) const; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole); bool setItemData(const QModelIndex &index, const QMap &roles); + bool setChecked(int row, bool checked); + bool setChecked(int row, Qt::CheckState checked); virtual QString labelForId(const QVariant &id) const; Qt::DropActions supportedDropActions() const; bool insertRows(int row, int count, const QModelIndex &parent); @@ -102,6 +104,14 @@ inline const QList &ChecklistModel::items() const return m_items; } +/*! + * \brief Sets the checked state of the specified item. + */ +inline bool ChecklistModel::setChecked(int row, bool checked) +{ + return setChecked(row, checked ? Qt::Checked : Qt::Unchecked); +} + /*! * \brief Returns the role used to get or set the item ID. */