113QMap<
int, QVariant> QStringListModel::itemData(
const QModelIndex &index)
const
115 if (!checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid))
116 return QMap<
int, QVariant>{};
117 const QVariant displayData = lst.at(index.row());
118 return QMap<
int, QVariant>{{
119 std::make_pair<
int>(Qt::DisplayRole, displayData),
120 std::make_pair<
int>(Qt::EditRole, displayData)
132bool QStringListModel::setItemData(
const QModelIndex &index,
const QMap<
int, QVariant> &roles)
136 if (std::any_of(roles.keyBegin(), roles.keyEnd(), [](
int role) ->
bool {
137 return role != Qt::DisplayRole && role != Qt::EditRole;
141 auto roleIter = roles.constFind(Qt::EditRole);
142 if (roleIter == roles.constEnd())
143 roleIter = roles.constFind(Qt::DisplayRole);
144 Q_ASSERT(roleIter != roles.constEnd());
145 return setData(index, roleIter.value(), roleIter.key());
200bool QStringListModel::setData(
const QModelIndex &index,
const QVariant &value,
int role)
202 if (index.row() >= 0 && index.row() < lst.size()
203 && (role == Qt::EditRole || role == Qt::DisplayRole)) {
204 const QString valueString = value.toString();
205 if (lst.at(index.row()) == valueString)
207 lst.replace(index.row(), valueString);
208 emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
236bool QStringListModel::insertRows(
int row,
int count,
const QModelIndex &parent)
238 if (count < 1 || row < 0 || row > rowCount(parent))
241 beginInsertRows(QModelIndex(), row, row + count - 1);
243 for (
int r = 0; r < count; ++r)
244 lst.insert(row, QString());
264bool QStringListModel::removeRows(
int row,
int count,
const QModelIndex &parent)
266 if (count <= 0 || row < 0 || (row + count) > rowCount(parent))
269 beginRemoveRows(QModelIndex(), row, row + count - 1);
271 const auto it = lst.begin() + row;
272 lst.erase(it, it + count);
283bool QStringListModel::moveRows(
const QModelIndex &sourceParent,
int sourceRow,
int count,
const QModelIndex &destinationParent,
int destinationChild)
286 || destinationChild < 0
288 || sourceRow == destinationChild
289 || sourceRow == destinationChild - 1
290 || sourceParent.isValid()
291 || destinationParent.isValid()) {
295 if (
const auto rc = rowCount(); sourceRow + count - 1 >= rc || destinationChild > rc)
298 if (!beginMoveRows(QModelIndex(), sourceRow, sourceRow + count - 1, QModelIndex(), destinationChild))
302 if (sourceRow < destinationChild) {
303 auto beg = lst.begin() + sourceRow;
304 auto end = beg + count;
305 auto to = lst.begin() + destinationChild;
306 std::rotate(beg, end, to);
308 auto to = lst.begin() + destinationChild;
309 auto beg = lst.begin() + sourceRow;
310 auto end = beg + count;
311 std::rotate(to, beg, end);
330void QStringListModel::sort(
int, Qt::SortOrder order)
332 emit layoutAboutToBeChanged(QList<QPersistentModelIndex>(), VerticalSortHint);
334 QList<std::pair<QString,
int>> list;
335 const int lstCount = lst.size();
336 list.reserve(lstCount);
337 for (
int i = 0; i < lstCount; ++i)
338 list.emplace_back(lst.at(i), i);
340 if (order == Qt::AscendingOrder)
341 std::sort(list.begin(), list.end(), ascendingLessThan);
343 std::sort(list.begin(), list.end(), decendingLessThan);
346 QList<
int> forwarding(lstCount);
347 for (
int i = 0; i < lstCount; ++i) {
348 lst.append(list.at(i).first);
349 forwarding[list.at(i).second] = i;
352 QModelIndexList oldList = persistentIndexList();
353 QModelIndexList newList;
354 const int numOldIndexes = oldList.size();
355 newList.reserve(numOldIndexes);
356 for (
int i = 0; i < numOldIndexes; ++i)
357 newList.append(index(forwarding.at(oldList.at(i).row()), 0));
358 changePersistentIndexList(oldList, newList);
360 emit layoutChanged(QList<QPersistentModelIndex>(), VerticalSortHint);