108QMap<
int, QVariant> QStringListModel::itemData(
const QModelIndex &index)
const
110 if (!checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid))
111 return QMap<
int, QVariant>{};
112 const QVariant displayData = lst.at(index.row());
113 return QMap<
int, QVariant>{{
114 std::make_pair<
int>(Qt::DisplayRole, displayData),
115 std::make_pair<
int>(Qt::EditRole, displayData)
124bool QStringListModel::setItemData(
const QModelIndex &index,
const QMap<
int, QVariant> &roles)
128 if (std::any_of(roles.keyBegin(), roles.keyEnd(), [](
int role) ->
bool {
129 return role != Qt::DisplayRole && role != Qt::EditRole;
133 auto roleIter = roles.constFind(Qt::EditRole);
134 if (roleIter == roles.constEnd())
135 roleIter = roles.constFind(Qt::DisplayRole);
136 Q_ASSERT(roleIter != roles.constEnd());
137 return setData(index, roleIter.value(), roleIter.key());
186bool QStringListModel::setData(
const QModelIndex &index,
const QVariant &value,
int role)
188 if (index.row() >= 0 && index.row() < lst.size()
189 && (role == Qt::EditRole || role == Qt::DisplayRole)) {
190 const QString valueString = value.toString();
191 if (lst.at(index.row()) == valueString)
193 lst.replace(index.row(), valueString);
194 emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
222bool QStringListModel::insertRows(
int row,
int count,
const QModelIndex &parent)
224 if (count < 1 || row < 0 || row > rowCount(parent))
227 beginInsertRows(QModelIndex(), row, row + count - 1);
229 for (
int r = 0; r < count; ++r)
230 lst.insert(row, QString());
250bool QStringListModel::removeRows(
int row,
int count,
const QModelIndex &parent)
252 if (count <= 0 || row < 0 || (row + count) > rowCount(parent))
255 beginRemoveRows(QModelIndex(), row, row + count - 1);
257 const auto it = lst.begin() + row;
258 lst.erase(it, it + count);
269bool QStringListModel::moveRows(
const QModelIndex &sourceParent,
int sourceRow,
int count,
const QModelIndex &destinationParent,
int destinationChild)
272 || destinationChild < 0
274 || sourceRow == destinationChild
275 || sourceRow == destinationChild - 1
276 || sourceParent.isValid()
277 || destinationParent.isValid()) {
281 if (
const auto rc = rowCount(); sourceRow + count - 1 >= rc || destinationChild > rc)
284 if (!beginMoveRows(QModelIndex(), sourceRow, sourceRow + count - 1, QModelIndex(), destinationChild))
288 if (sourceRow < destinationChild) {
289 auto beg = lst.begin() + sourceRow;
290 auto end = beg + count;
291 auto to = lst.begin() + destinationChild;
292 std::rotate(beg, end, to);
294 auto to = lst.begin() + destinationChild;
295 auto beg = lst.begin() + sourceRow;
296 auto end = beg + count;
297 std::rotate(to, beg, end);
316void QStringListModel::sort(
int, Qt::SortOrder order)
318 emit layoutAboutToBeChanged(QList<QPersistentModelIndex>(), VerticalSortHint);
320 QList<std::pair<QString,
int>> list;
321 const int lstCount = lst.size();
322 list.reserve(lstCount);
323 for (
int i = 0; i < lstCount; ++i)
324 list.emplace_back(lst.at(i), i);
326 if (order == Qt::AscendingOrder)
327 std::sort(list.begin(), list.end(), ascendingLessThan);
329 std::sort(list.begin(), list.end(), decendingLessThan);
332 QList<
int> forwarding(lstCount);
333 for (
int i = 0; i < lstCount; ++i) {
334 lst.append(list.at(i).first);
335 forwarding[list.at(i).second] = i;
338 QModelIndexList oldList = persistentIndexList();
339 QModelIndexList newList;
340 const int numOldIndexes = oldList.size();
341 newList.reserve(numOldIndexes);
342 for (
int i = 0; i < numOldIndexes; ++i)
343 newList.append(index(forwarding.at(oldList.at(i).row()), 0));
344 changePersistentIndexList(oldList, newList);
346 emit layoutChanged(QList<QPersistentModelIndex>(), VerticalSortHint);