Qt Utilities  5.7.1
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
checklistmodel.cpp
Go to the documentation of this file.
1 #include "./checklistmodel.h"
2 
3 #include <QSettings>
4 
10 namespace Models {
11 
28  : QAbstractListModel(parent)
29 {
30 }
31 
32 int ChecklistModel::rowCount(const QModelIndex &parent) const
33 {
34  if (!parent.isValid()) {
35  return m_items.size();
36  }
37  return 0;
38 }
39 
40 Qt::ItemFlags ChecklistModel::flags(const QModelIndex &index) const
41 {
42  if (!index.isValid() || index.row() >= m_items.count() || index.model() != this) {
43  return Qt::ItemIsDropEnabled; // allows drops outside the items
44  }
45  return QAbstractListModel::flags(index) | Qt::ItemIsUserCheckable | Qt::ItemIsDragEnabled;
46 }
47 
48 QVariant ChecklistModel::data(const QModelIndex &index, int role) const
49 {
50  if (index.isValid() && index.row() < m_items.size()) {
51  switch (role) {
52  case Qt::DisplayRole:
53  return m_items.at(index.row()).label();
54  case Qt::CheckStateRole:
55  return m_items.at(index.row()).checkState();
56  case idRole():
57  return m_items.at(index.row()).id();
58  default:;
59  }
60  }
61  return QVariant();
62 }
63 
64 QMap<int, QVariant> ChecklistModel::itemData(const QModelIndex &index) const
65 {
66  QMap<int, QVariant> roles;
67  roles.insert(Qt::DisplayRole, data(index, Qt::DisplayRole));
68  roles.insert(Qt::CheckStateRole, data(index, Qt::CheckStateRole));
69  roles.insert(idRole(), data(index, idRole()));
70  return roles;
71 }
72 
73 bool ChecklistModel::setData(const QModelIndex &index, const QVariant &value, int role)
74 {
75  bool success = false;
76  QVector<int> roles;
77  roles << role;
78  if (index.isValid() && index.row() < m_items.size()) {
79  switch (role) {
80  case Qt::DisplayRole:
81  m_items[index.row()].m_label = value.toString();
82  success = true;
83  break;
84  case Qt::CheckStateRole:
85  if (value.canConvert(QMetaType::Int)) {
86  m_items[index.row()].m_checkState = static_cast<Qt::CheckState>(value.toInt());
87  success = true;
88  }
89  break;
90  case idRole(): {
91  m_items[index.row()].m_id = value;
92  success = true;
93  QString label = labelForId(value);
94  if (!label.isEmpty()) {
95  m_items[index.row()].m_label = label;
96  roles << Qt::DisplayRole;
97  }
98  break;
99  }
100  default:;
101  }
102  }
103  if (success) {
104  dataChanged(index, index, roles);
105  }
106  return success;
107 }
108 
109 bool ChecklistModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
110 {
111  for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it) {
112  setData(index, it.value(), it.key());
113  }
114  return true;
115 }
116 
127 QString ChecklistModel::labelForId(const QVariant &) const
128 {
129  return QString();
130 }
131 
133 {
134  return Qt::MoveAction;
135 }
136 
137 bool ChecklistModel::insertRows(int row, int count, const QModelIndex &parent)
138 {
139  if (count < 1 || row < 0 || row > rowCount() || parent.isValid()) {
140  return false;
141  }
142  beginInsertRows(QModelIndex(), row, row + count - 1);
143  for (int index = row, end = row + count; index < end; ++index) {
144  m_items.insert(index, ChecklistItem());
145  }
146  endInsertRows();
147  return true;
148 }
149 
150 bool ChecklistModel::removeRows(int row, int count, const QModelIndex &parent)
151 {
152  if (count < 1 || row < 0 || (row + count) > rowCount() || parent.isValid()) {
153  return false;
154  }
155  beginRemoveRows(QModelIndex(), row, row + count - 1);
156  for (int index = row, end = row + count; index < end; ++index) {
157  m_items.removeAt(index);
158  }
159  endRemoveRows();
160  return true;
161 }
162 
166 void ChecklistModel::setItems(const QList<ChecklistItem> &items)
167 {
168  beginResetModel();
169  m_items = items;
170  endResetModel();
171 }
172 
183 void ChecklistModel::restore(QSettings &settings, const QString &name)
184 {
185  beginResetModel();
186  auto currentItems = m_items;
187  QList<QVariant> restoredIds;
188  m_items.clear();
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)) {
196  m_items << ChecklistItem(id, labelForId(id), selected.toBool() ? Qt::Checked : Qt::Unchecked);
197  restoredIds << id;
198  }
199  }
200  settings.endArray();
201  for (const ChecklistItem &item : currentItems) {
202  if (!restoredIds.contains(item.id())) {
203  m_items << item;
204  }
205  }
206  endResetModel();
207 }
208 
216 void ChecklistModel::save(QSettings &settings, const QString &name) const
217 {
218  settings.beginWriteArray(name, m_items.size());
219  int index = 0;
220  for (const ChecklistItem &item : m_items) {
221  settings.setArrayIndex(index);
222  settings.setValue(QStringLiteral("id"), item.id());
223  settings.setValue(QStringLiteral("selected"), item.isChecked());
224  ++index;
225  }
226  settings.endArray();
227 }
228 }
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
Provides common models.
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