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