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));
78 if (index.isValid() && index.row() < m_items.size()) {
81 m_items[index.row()].m_label = value.toString();
84 case Qt::CheckStateRole:
85 if (value.canConvert(QMetaType::Int)) {
86 m_items[index.row()].m_checkState =
static_cast<Qt::CheckState
>(value.toInt());
91 m_items[index.row()].m_id = value;
94 if (!label.isEmpty()) {
95 m_items[index.row()].m_label = label;
96 roles << Qt::DisplayRole;
104 dataChanged(index, index, roles);
111 for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it) {
112 setData(index, it.value(), it.key());
134 return Qt::MoveAction;
139 if (count < 1 || row < 0 || row >
rowCount() || parent.isValid()) {
142 beginInsertRows(QModelIndex(), row, row + count - 1);
143 for (
int index = row, end = row + count; index < end; ++index) {
152 if (count < 1 || row < 0 || (row + count) >
rowCount() || parent.isValid()) {
155 beginRemoveRows(QModelIndex(), row, row + count - 1);
156 for (
int index = row, end = row + count; index < end; ++index) {
157 m_items.removeAt(index);
186 auto currentItems = m_items;
187 QList<QVariant> restoredIds;
189 int rows = settings.beginReadArray(name);
190 m_items.reserve(rows);
191 for (
int i = 0; i < rows; ++i) {
192 settings.setArrayIndex(i);
193 QVariant
id = settings.value(QStringLiteral(
"id"));
194 QVariant selected = settings.value(QStringLiteral(
"selected"));
195 if (!
id.isNull() && !selected.isNull() && selected.canConvert(QMetaType::Bool) && !restoredIds.contains(
id)) {
202 if (!restoredIds.contains(item.id())) {
218 settings.beginWriteArray(name, m_items.size());
221 settings.setArrayIndex(index);
222 settings.setValue(QStringLiteral(
"id"), item.id());
223 settings.setValue(QStringLiteral(
"selected"), item.isChecked());
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.
virtual QString labelForId(const QVariant &id) const
Returns the label for the specified id.
void setItems(const QList< ChecklistItem > &items)
Sets the items.
bool setItemData(const QModelIndex &index, const QMap< int, QVariant > &roles)
bool removeRows(int row, int count, const QModelIndex &parent)
int rowCount(const QModelIndex &parent=QModelIndex()) const
Qt::ItemFlags flags(const QModelIndex &index) const
static constexpr int idRole()
Returns the role used to get or set the item ID.
QMap< int, QVariant > itemData(const QModelIndex &index) const
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::DisplayRole)
The ChecklistItem class provides an item for use with the ChecklistModel class.
void restore(QSettings &settings, const QString &name)
Restores the IDs and checkstates read from the specified settings object.
Qt::DropActions supportedDropActions() const
const QList< ChecklistItem > & items() const
Returns the items.
bool insertRows(int row, int count, const QModelIndex &parent)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const