|
| 1 | +#include "scriptmodel.hpp" |
| 2 | +#include <algorithm> |
| 3 | +#include <iterator> |
| 4 | + |
| 5 | +#include <qabstractitemmodel.h> |
| 6 | +#include <qcontainerfwd.h> |
| 7 | +#include <qlist.h> |
| 8 | +#include <qnamespace.h> |
| 9 | +#include <qtmetamacros.h> |
| 10 | +#include <qtypes.h> |
| 11 | +#include <qvariant.h> |
| 12 | + |
| 13 | +void ScriptModel::updateValuesUnique(const QVariantList& newValues) { |
| 14 | + this->mValues.reserve(newValues.size()); |
| 15 | + |
| 16 | + auto iter = this->mValues.begin(); |
| 17 | + auto newIter = newValues.begin(); |
| 18 | + |
| 19 | + while (true) { |
| 20 | + if (newIter == newValues.end()) { |
| 21 | + if (iter == this->mValues.end()) break; |
| 22 | + |
| 23 | + auto startIndex = static_cast<qint32>(newValues.length()); |
| 24 | + auto endIndex = static_cast<qint32>(this->mValues.length() - 1); |
| 25 | + |
| 26 | + this->beginRemoveRows(QModelIndex(), startIndex, endIndex); |
| 27 | + this->mValues.erase(iter, this->mValues.end()); |
| 28 | + this->endRemoveRows(); |
| 29 | + |
| 30 | + break; |
| 31 | + } else if (iter == this->mValues.end()) { |
| 32 | + // Prior branch ensures length is at least 1. |
| 33 | + auto startIndex = static_cast<qint32>(this->mValues.length()); |
| 34 | + auto endIndex = static_cast<qint32>(newValues.length() - 1); |
| 35 | + |
| 36 | + this->beginInsertRows(QModelIndex(), startIndex, endIndex); |
| 37 | + this->mValues.append(newValues.sliced(startIndex)); |
| 38 | + this->endInsertRows(); |
| 39 | + |
| 40 | + break; |
| 41 | + } else if (*newIter != *iter) { |
| 42 | + auto oldIter = std::find(iter, this->mValues.end(), *newIter); |
| 43 | + |
| 44 | + if (oldIter != this->mValues.end()) { |
| 45 | + if (std::find(newIter, newValues.end(), *iter) == newValues.end()) { |
| 46 | + // Remove any entries we would otherwise move around that aren't in the new list. |
| 47 | + auto startIter = iter; |
| 48 | + |
| 49 | + do { |
| 50 | + ++iter; |
| 51 | + } while (iter != this->mValues.end() |
| 52 | + && std::find(newIter, newValues.end(), *iter) == newValues.end()); |
| 53 | + |
| 54 | + auto index = static_cast<qint32>(std::distance(this->mValues.begin(), iter)); |
| 55 | + auto startIndex = static_cast<qint32>(std::distance(this->mValues.begin(), startIter)); |
| 56 | + |
| 57 | + this->beginRemoveRows(QModelIndex(), startIndex, index - 1); |
| 58 | + iter = this->mValues.erase(startIter, iter); |
| 59 | + this->endRemoveRows(); |
| 60 | + } else { |
| 61 | + // Advance iters to capture a whole move sequence as a single operation if possible. |
| 62 | + auto oldStartIter = oldIter; |
| 63 | + do { |
| 64 | + ++oldIter; |
| 65 | + ++newIter; |
| 66 | + } while (oldIter != this->mValues.end() && newIter != newValues.end() |
| 67 | + && *oldIter == *newIter); |
| 68 | + |
| 69 | + auto index = static_cast<qint32>(std::distance(this->mValues.begin(), iter)); |
| 70 | + auto oldStartIndex = |
| 71 | + static_cast<qint32>(std::distance(this->mValues.begin(), oldStartIter)); |
| 72 | + auto oldIndex = static_cast<qint32>(std::distance(this->mValues.begin(), oldIter)); |
| 73 | + auto len = oldIndex - oldStartIndex; |
| 74 | + |
| 75 | + this->beginMoveRows(QModelIndex(), oldStartIndex, oldIndex - 1, QModelIndex(), index); |
| 76 | + |
| 77 | + // While it is possible to optimize this further, it is currently not worth the time. |
| 78 | + for (auto i = 0; i != len; i++) { |
| 79 | + this->mValues.move(oldStartIndex + i, index + i); |
| 80 | + } |
| 81 | + |
| 82 | + iter = this->mValues.begin() + (index + len); |
| 83 | + this->endMoveRows(); |
| 84 | + } |
| 85 | + } else { |
| 86 | + auto startNewIter = newIter; |
| 87 | + |
| 88 | + do { |
| 89 | + newIter++; |
| 90 | + } while (newIter != newValues.end() |
| 91 | + && std::find(iter, this->mValues.end(), *newIter) == this->mValues.end()); |
| 92 | + |
| 93 | + auto index = static_cast<qint32>(std::distance(this->mValues.begin(), iter)); |
| 94 | + auto newIndex = static_cast<qint32>(std::distance(newValues.begin(), newIter)); |
| 95 | + auto startNewIndex = static_cast<qint32>(std::distance(newValues.begin(), startNewIter)); |
| 96 | + auto len = newIndex - startNewIndex; |
| 97 | + |
| 98 | + this->beginInsertRows(QModelIndex(), index, index + len - 1); |
| 99 | + this->mValues.resizeForOverwrite(this->mValues.length() + len); |
| 100 | + iter = this->mValues.begin() + index; // invalidated |
| 101 | + std::move_backward(iter, this->mValues.end() - len, this->mValues.end()); |
| 102 | + iter = std::copy(startNewIter, newIter, iter); |
| 103 | + this->endInsertRows(); |
| 104 | + } |
| 105 | + } else { |
| 106 | + ++iter; |
| 107 | + ++newIter; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +void ScriptModel::setValues(const QVariantList& newValues) { |
| 113 | + if (newValues == this->mValues) return; |
| 114 | + this->updateValuesUnique(newValues); |
| 115 | + emit this->valuesChanged(); |
| 116 | +} |
| 117 | + |
| 118 | +qint32 ScriptModel::rowCount(const QModelIndex& parent) const { |
| 119 | + if (parent != QModelIndex()) return 0; |
| 120 | + return static_cast<qint32>(this->mValues.length()); |
| 121 | +} |
| 122 | + |
| 123 | +QVariant ScriptModel::data(const QModelIndex& index, qint32 role) const { |
| 124 | + if (role != Qt::UserRole) return QVariant(); |
| 125 | + return this->mValues.at(index.row()); |
| 126 | +} |
| 127 | + |
| 128 | +QHash<int, QByteArray> ScriptModel::roleNames() const { return {{Qt::UserRole, "modelData"}}; } |
0 commit comments