Make use of ChecklistModel more convenient

This commit is contained in:
Marius Kittler 2018-10-10 17:47:57 +02:00
parent 390716a330
commit 36f3c71a66
3 changed files with 39 additions and 8 deletions

View File

@ -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})

View File

@ -73,8 +73,7 @@ QMap<int, QVariant> ChecklistModel::itemData(const QModelIndex &index) const
bool ChecklistModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
bool success = false;
QVector<int> roles;
roles << role;
QVector<int> 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<int, QVari
return true;
}
/*!
* \brief Sets the checked state of the specified item.
*/
bool ChecklistModel::setChecked(int row, Qt::CheckState checked)
{
if (row < 0 || row >= 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<int>{ 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<ChecklistItem> &items)
{
beginResetModel();
m_items = items;
for (auto &item : m_items) {
if (item.m_label.isEmpty()) {
item.m_label = labelForId(item.id());
}
}
endResetModel();
}

View File

@ -80,6 +80,8 @@ public:
QMap<int, QVariant> itemData(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole);
bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &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<ChecklistItem> &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.
*/