26 QAbstractListModel(parent)
31 if(!parent.isValid()) {
32 return m_items.size();
39 if(!index.isValid() || index.row() >= m_items.count() || index.model() !=
this) {
40 return Qt::ItemIsDropEnabled;
42 return QAbstractListModel::flags(index) | Qt::ItemIsUserCheckable | Qt::ItemIsDragEnabled;
47 if(index.isValid() && index.row() < m_items.size()) {
50 return m_items.at(index.row()).label();
51 case Qt::CheckStateRole:
52 return m_items.at(index.row()).checkState();
54 return m_items.at(index.row()).
id();
64 QMap<int, QVariant> roles;
65 roles.insert(Qt::DisplayRole,
data(index, Qt::DisplayRole));
66 roles.insert(Qt::CheckStateRole,
data(index, Qt::CheckStateRole));
76 if(index.isValid() && index.row() < m_items.size()) {
79 m_items[index.row()].m_label = value.toString();
82 case Qt::CheckStateRole:
83 if(value.canConvert(QMetaType::Int)) {
84 m_items[index.row()].m_checkState =
static_cast<Qt::CheckState
>(value.toInt());
89 m_items[index.row()].m_id = value;
92 if(!label.isEmpty()) {
93 m_items[index.row()].m_label = label;
94 roles << Qt::DisplayRole;
102 dataChanged(index, index, roles);
109 for(QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it) {
110 setData(index, it.value(), it.key());
132 return Qt::MoveAction;
137 if (count < 1 || row < 0 || row >
rowCount() || parent.isValid()) {
140 beginInsertRows(QModelIndex(), row, row + count - 1);
141 for(
int index = row, end = row + count; index < end; ++index) {
150 if (count < 1 || row < 0 || (row + count) >
rowCount() || parent.isValid()) {
153 beginRemoveRows(QModelIndex(), row, row + count - 1);
154 for(
int index = row, end = row + count; index < end; ++index) {
155 m_items.removeAt(index);
183 auto currentItems = m_items;
184 QList<QVariant> restoredIds;
186 int rows = settings.beginReadArray(name);
187 m_items.reserve(rows);
188 for(
int i = 0; i < rows; ++i) {
189 settings.setArrayIndex(i);
190 QVariant
id = settings.value(QStringLiteral(
"id"));
191 QVariant selected = settings.value(QStringLiteral(
"selected"));
192 if(!
id.isNull() && !selected.isNull() && selected.canConvert(QMetaType::Bool) && !restoredIds.contains(
id)) {
199 if(!restoredIds.contains(item.id())) {
215 settings.beginWriteArray(name, m_items.size());
218 settings.setArrayIndex(index);
219 settings.setValue(QStringLiteral(
"id"), item.id());
220 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