28 : QAbstractListModel(parent)
34 if (!parent.isValid()) {
35 return m_items.size();
42 if (!index.isValid() || index.row() >= m_items.count() || index.model() !=
this) {
43 return Qt::ItemIsDropEnabled;
45 return QAbstractListModel::flags(index) | Qt::ItemIsUserCheckable | Qt::ItemIsDragEnabled;
50 if (index.isValid() && index.row() < m_items.size()) {
53 return m_items.at(index.row()).label();
54 case Qt::CheckStateRole:
55 return m_items.at(index.row()).checkState();
57 return m_items.at(index.row()).
id();
66 QMap<int, QVariant> roles;
67 roles.insert(Qt::DisplayRole,
data(index, Qt::DisplayRole));
68 roles.insert(Qt::CheckStateRole,
data(index, Qt::CheckStateRole));
76 QVector<int> roles{ role };
77 if (index.isValid() && index.row() < m_items.size()) {
80 m_items[index.row()].m_label = value.toString();
83 case Qt::CheckStateRole:
84 if (value.canConvert(QMetaType::Int)) {
85 m_items[index.row()].m_checkState = static_cast<Qt::CheckState>(value.toInt());
90 m_items[index.row()].m_id = value;
93 if (!label.isEmpty()) {
94 m_items[index.row()].m_label = std::move(label);
95 roles << Qt::DisplayRole;
103 dataChanged(index, index, roles);
110 for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it) {
111 setData(index, it.value(), it.key());
121 if (row < 0 || row >= m_items.size()) {
124 m_items[row].m_checkState = checked ? Qt::Checked : Qt::Unchecked;
125 const auto index(this->index(row));
126 dataChanged(index, index, QVector<int>{ Qt::CheckStateRole });
150 return Qt::MoveAction;
155 if (count < 1 || row < 0 || row >
rowCount() || parent.isValid()) {
158 beginInsertRows(QModelIndex(), row, row + count - 1);
159 for (
int index = row, end = row + count; index < end; ++index) {
168 if (count < 1 || row < 0 || (row + count) >
rowCount() || parent.isValid()) {
171 beginRemoveRows(QModelIndex(), row, row + count - 1);
172 for (
int index = row, end = row + count; index < end; ++index) {
173 m_items.removeAt(index);
186 for (
auto &item : m_items) {
187 if (item.m_label.isEmpty()) {
207 auto currentItems = m_items;
208 QList<QVariant> restoredIds;
210 int rows = settings.beginReadArray(name);
211 m_items.reserve(rows);
212 for (
int i = 0; i < rows; ++i) {
213 settings.setArrayIndex(i);
214 QVariant
id = settings.value(QStringLiteral(
"id"));
215 QVariant selected = settings.value(QStringLiteral(
"selected"));
216 if (!
id.isNull() && !selected.isNull() && selected.canConvert(QMetaType::Bool) && !restoredIds.contains(
id)) {
223 if (!restoredIds.contains(item.id())) {
239 settings.beginWriteArray(name, m_items.size());
242 settings.setArrayIndex(index);
243 settings.setValue(QStringLiteral(
"id"), item.id());
244 settings.setValue(QStringLiteral(
"selected"), item.isChecked());
255 QVariantList checkedIds;
256 checkedIds.reserve(m_items.size());
257 for (
const auto &item : m_items) {
258 if (item.isChecked()) {
259 checkedIds << item.id();
270 for (
auto &item : m_items) {
271 item.m_checkState = checkedIds.contains(item.id()) ? Qt::Checked : Qt::Unchecked;
273 emit dataChanged(index(0), index(m_items.size()), { Qt::CheckStateRole });
ChecklistModel(QObject *parent=nullptr)
Constructs a new checklist model.
void save(QSettings &settings, const QString &name) const
Saves the IDs and checkstates to the specified settings object.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::DisplayRole) override
virtual QString labelForId(const QVariant &id) const
Returns the label for the specified id.
void setItems(const QList< ChecklistItem > &items)
Sets the items.
bool insertRows(int row, int count, const QModelIndex &parent) override
bool setItemData(const QModelIndex &index, const QMap< int, QVariant > &roles) override
bool removeRows(int row, int count, const QModelIndex &parent) override
Qt::ItemFlags flags(const QModelIndex &index) const override
static constexpr int idRole()
Returns the role used to get or set the item ID.
The ChecklistItem class provides an item for use with the ChecklistModel class.
QVariantList toVariantList() const
Returns the checked IDs.
QMap< int, QVariant > itemData(const QModelIndex &index) const override
void applyVariantList(const QVariantList &checkedIds)
Checks all items contained by checkedIds and unchecks other items.
void restore(QSettings &settings, const QString &name)
Restores the IDs and checkstates read from the specified settings object.
Qt::DropActions supportedDropActions() const override
const QList< ChecklistItem > & items() const
Returns the items.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
bool setChecked(int row, bool checked)
Sets the checked state of the specified item.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override