Make use of ChecklistModel more convenient
This commit is contained in:
parent
390716a330
commit
36f3c71a66
|
@ -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_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_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_MAJOR 5)
|
||||||
set(META_VERSION_MINOR 11)
|
set(META_VERSION_MINOR 12)
|
||||||
set(META_VERSION_PATCH 0)
|
set(META_VERSION_PATCH 0)
|
||||||
set(META_APP_VERSION ${META_VERSION_MAJOR}.${META_VERSION_MINOR}.${META_VERSION_PATCH})
|
set(META_APP_VERSION ${META_VERSION_MAJOR}.${META_VERSION_MINOR}.${META_VERSION_PATCH})
|
||||||
|
|
||||||
|
|
|
@ -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 ChecklistModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||||
{
|
{
|
||||||
bool success = false;
|
bool success = false;
|
||||||
QVector<int> roles;
|
QVector<int> roles{ role };
|
||||||
roles << role;
|
|
||||||
if (index.isValid() && index.row() < m_items.size()) {
|
if (index.isValid() && index.row() < m_items.size()) {
|
||||||
switch (role) {
|
switch (role) {
|
||||||
case Qt::DisplayRole:
|
case Qt::DisplayRole:
|
||||||
|
@ -90,9 +89,9 @@ bool ChecklistModel::setData(const QModelIndex &index, const QVariant &value, in
|
||||||
case idRole(): {
|
case idRole(): {
|
||||||
m_items[index.row()].m_id = value;
|
m_items[index.row()].m_id = value;
|
||||||
success = true;
|
success = true;
|
||||||
QString label = labelForId(value);
|
auto label = labelForId(value);
|
||||||
if (!label.isEmpty()) {
|
if (!label.isEmpty()) {
|
||||||
m_items[index.row()].m_label = label;
|
m_items[index.row()].m_label = std::move(label);
|
||||||
roles << Qt::DisplayRole;
|
roles << Qt::DisplayRole;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -114,15 +113,32 @@ bool ChecklistModel::setItemData(const QModelIndex &index, const QMap<int, QVari
|
||||||
return true;
|
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.
|
* \brief Returns the label for the specified \a id.
|
||||||
*
|
*
|
||||||
* This method might be reimplemented when subclassing to provide labels
|
* This method might be reimplemented when subclassing to provide labels
|
||||||
* for the item IDs.
|
* for the item IDs.
|
||||||
*
|
*
|
||||||
* If an item's ID is set (using setData() and idRole()) this method is called
|
* If an item's ID is set (using setData() with idRole() or setItems()) this method
|
||||||
* to update the item's label as well. If this method returns an empty string
|
* is called to update or initialize the item's label as well. If this method returns
|
||||||
* (default behaviour) the item's label will not be updated.
|
* 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
|
QString ChecklistModel::labelForId(const QVariant &) const
|
||||||
{
|
{
|
||||||
|
@ -167,6 +183,11 @@ void ChecklistModel::setItems(const QList<ChecklistItem> &items)
|
||||||
{
|
{
|
||||||
beginResetModel();
|
beginResetModel();
|
||||||
m_items = items;
|
m_items = items;
|
||||||
|
for (auto &item : m_items) {
|
||||||
|
if (item.m_label.isEmpty()) {
|
||||||
|
item.m_label = labelForId(item.id());
|
||||||
|
}
|
||||||
|
}
|
||||||
endResetModel();
|
endResetModel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -80,6 +80,8 @@ public:
|
||||||
QMap<int, QVariant> itemData(const QModelIndex &index) const;
|
QMap<int, QVariant> itemData(const QModelIndex &index) const;
|
||||||
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole);
|
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole);
|
||||||
bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles);
|
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;
|
virtual QString labelForId(const QVariant &id) const;
|
||||||
Qt::DropActions supportedDropActions() const;
|
Qt::DropActions supportedDropActions() const;
|
||||||
bool insertRows(int row, int count, const QModelIndex &parent);
|
bool insertRows(int row, int count, const QModelIndex &parent);
|
||||||
|
@ -102,6 +104,14 @@ inline const QList<ChecklistItem> &ChecklistModel::items() const
|
||||||
return m_items;
|
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.
|
* \brief Returns the role used to get or set the item ID.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue