Qt Utilities: models/checklistmodel.h Source File

Qt Utilities 6.19.1 Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models

Loading...
Searching...
No Matches
checklistmodel.h
Go to the documentation of this file.
1#ifndef MODELS_CHECKLISTMODEL_H
2#define MODELS_CHECKLISTMODEL_H
3
4#include "../global.h"
5
6#include <QAbstractListModel>
7#include <QList>
8
9QT_FORWARD_DECLARE_CLASS(QSettings)
10
11namespace QtUtilities {
12
13class ChecklistModel;
14
15 class QT_UTILITIES_EXPORT ChecklistItem {
16 friend class ChecklistModel;
17
18public:
19 ChecklistItem(const QVariant &id = QVariant(), const QString &label = QString(), Qt::CheckState checked = Qt::Unchecked);
20
21 const QVariant &id() const;
22 const QString &label() const;
23 Qt::CheckState checkState() const;
24 bool isChecked() const;
25
26private:
27 QVariant m_id;
28 QString m_label;
29 Qt::CheckState m_checkState;
30};
31
32 inline ChecklistItem::ChecklistItem(const QVariant &id, const QString &label, Qt::CheckState checkState)
33 : m_id(id)
34 , m_label(label)
35 , m_checkState(checkState)
36{
37}
38
42 inline const QVariant &ChecklistItem::id() const
43{
44 return m_id;
45}
46
50 inline const QString &ChecklistItem::label() const
51{
52 return m_label;
53}
54
58 inline Qt::CheckState ChecklistItem::checkState() const
59{
60 return m_checkState;
61}
62
66
67 inline bool ChecklistItem::isChecked() const
68{
69 return m_checkState == Qt::Checked;
70}
71
72 class QT_UTILITIES_EXPORT ChecklistModel : public QAbstractListModel {
73 Q_OBJECT
74public:
75 explicit ChecklistModel(QObject *parent = nullptr);
76
77 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
78 Qt::ItemFlags flags(const QModelIndex &index) const override;
79 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
80 QMap<int, QVariant> itemData(const QModelIndex &index) const override;
81 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole) override;
82 bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles) override;
83 bool setChecked(int row, bool checked);
84 bool setChecked(int row, Qt::CheckState checked);
85 virtual QString labelForId(const QVariant &id) const;
86 Qt::DropActions supportedDropActions() const override;
87 bool insertRows(int row, int count, const QModelIndex &parent) override;
88 bool removeRows(int row, int count, const QModelIndex &parent) override;
89 const QList<ChecklistItem> &items() const;
90 void setItems(const QList<ChecklistItem> &items);
91 void restore(QSettings &settings, const QString &name);
92 void save(QSettings &settings, const QString &name) const;
93 QVariantList toVariantList() const;
94 void applyVariantList(const QVariantList &checkedIds);
95 static constexpr int idRole();
96
97private:
98 QList<ChecklistItem> m_items;
99};
100
104 inline const QList<ChecklistItem> &ChecklistModel::items() const
105{
106 return m_items;
107}
108
112 inline bool ChecklistModel::setChecked(int row, bool checked)
113{
114 return setChecked(row, checked ? Qt::Checked : Qt::Unchecked);
115}
116
120 constexpr int ChecklistModel::idRole()
121{
122 return Qt::UserRole + 1;
123}
124} // namespace QtUtilities
125
126#endif // MODELS_CHECKLISTMODEL_H
QtUtilities::ChecklistItem::checkState
Qt::CheckState checkState() const
Returns the check state.
Definition checklistmodel.h:58
QtUtilities::ChecklistItem::ChecklistItem
ChecklistItem(const QVariant &id=QVariant(), const QString &label=QString(), Qt::CheckState checked=Qt::Unchecked)
Definition checklistmodel.h:32
QtUtilities::ChecklistItem::ChecklistModel
friend class ChecklistModel
Definition checklistmodel.h:16
QtUtilities::ChecklistItem::id
const QVariant & id() const
Returns the ID of the item.
Definition checklistmodel.h:42
QtUtilities::ChecklistItem::isChecked
bool isChecked() const
Returns whether the item is checked.
Definition checklistmodel.h:67
QtUtilities::ChecklistItem::label
const QString & label() const
Returns the label.
Definition checklistmodel.h:50
QtUtilities::ChecklistModel
Definition checklistmodel.h:72
QtUtilities::ChecklistModel::setItems
void setItems(const QList< ChecklistItem > &items)
Sets the items.
Definition checklistmodel.cpp:188
QtUtilities::ChecklistModel::ChecklistModel
ChecklistModel(QObject *parent=nullptr)
Constructs a new checklist model.
Definition checklistmodel.cpp:27
QtUtilities::ChecklistModel::removeRows
bool removeRows(int row, int count, const QModelIndex &parent) override
Definition checklistmodel.cpp:172
QtUtilities::ChecklistModel::setChecked
bool setChecked(int row, bool checked)
Sets the checked state of the specified item.
Definition checklistmodel.h:112
QtUtilities::ChecklistModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition checklistmodel.cpp:48
QtUtilities::ChecklistModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::DisplayRole) override
Definition checklistmodel.cpp:73
QtUtilities::ChecklistModel::save
void save(QSettings &settings, const QString &name) const
Saves the IDs and checkstates to the specified settings object.
Definition checklistmodel.cpp:262
QtUtilities::ChecklistModel::labelForId
virtual QString labelForId(const QVariant &id) const
Returns the label for the specified id.
Definition checklistmodel.cpp:149
QtUtilities::ChecklistModel::applyVariantList
void applyVariantList(const QVariantList &checkedIds)
Checks all items contained by checkedIds and unchecks other items.
Definition checklistmodel.cpp:293
QtUtilities::ChecklistModel::insertRows
bool insertRows(int row, int count, const QModelIndex &parent) override
Definition checklistmodel.cpp:159
QtUtilities::ChecklistModel::supportedDropActions
Qt::DropActions supportedDropActions() const override
Definition checklistmodel.cpp:154
QtUtilities::ChecklistModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition checklistmodel.cpp:32
QtUtilities::ChecklistModel::restore
void restore(QSettings &settings, const QString &name)
Restores the IDs and checkstates read from the specified settings object.
Definition checklistmodel.cpp:210
QtUtilities::ChecklistModel::itemData
QMap< int, QVariant > itemData(const QModelIndex &index) const override
Definition checklistmodel.cpp:64
QtUtilities::ChecklistModel::items
const QList< ChecklistItem > & items() const
Returns the items.
Definition checklistmodel.h:104
QtUtilities::ChecklistModel::toVariantList
QVariantList toVariantList() const
Returns the checked IDs.
Definition checklistmodel.cpp:278
QtUtilities::ChecklistModel::idRole
static constexpr int idRole()
Returns the role used to get or set the item ID.
Definition checklistmodel.h:120
QtUtilities::ChecklistModel::setItemData
bool setItemData(const QModelIndex &index, const QMap< int, QVariant > &roles) override
Definition checklistmodel.cpp:114
QtUtilities::ChecklistModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition checklistmodel.cpp:40
global.h
QT_UTILITIES_EXPORT
#define QT_UTILITIES_EXPORT
Marks the symbol to be exported by the qtutilities library.
Definition global.h:14
QtUtilities
!
Definition trylocker.h:8