diff options
| author | Mikaël Capelle <capelle.mikael@gmail.com> | 2020-12-27 17:10:43 +0100 |
|---|---|---|
| committer | Mikaël Capelle <capelle.mikael@gmail.com> | 2021-01-02 15:38:14 +0100 |
| commit | d4e05894704544a37414a4cfafbb2613a6f570c1 (patch) | |
| tree | 2c61481a61cf7ad7bd8ffac832f3af86fd6a8b06 | |
| parent | 44103ef737090dd74666f98fc6953d40d36adbd4 (diff) | |
Fix drag and drop of regular mods.
| -rw-r--r-- | src/modlistbypriorityproxy.cpp | 62 | ||||
| -rw-r--r-- | src/modlistbypriorityproxy.h | 2 | ||||
| -rw-r--r-- | src/modlistsortproxy.cpp | 5 |
3 files changed, 62 insertions, 7 deletions
diff --git a/src/modlistbypriorityproxy.cpp b/src/modlistbypriorityproxy.cpp index 77b7262a..d69950db 100644 --- a/src/modlistbypriorityproxy.cpp +++ b/src/modlistbypriorityproxy.cpp @@ -38,6 +38,7 @@ void ModListByPriorityProxy::buildTree() m_IndexToItem.clear(); TreeItem* root = &m_Root; + std::unique_ptr<TreeItem> overwrite; for (auto& [priority, index] : m_Profile->getAllIndexesByPriority()) { ModInfo::Ptr modInfo = ModInfo::getByIndex(index); @@ -49,17 +50,20 @@ void ModListByPriorityProxy::buildTree() root = item; } else if (modInfo->isOverwrite()) { - m_Root.children.push_back(std::make_unique<TreeItem>(modInfo, index, &m_Root)); - item = m_Root.children.back().get(); + // do not push here, because the overwrite is usually not at the right position + overwrite = std::make_unique<TreeItem>(modInfo, index, &m_Root); + item = overwrite.get(); } else { root->children.push_back(std::make_unique<TreeItem>(modInfo, index, root)); item = root->children.back().get(); } - m_IndexToItem[index] = item; + m_IndexToItem[index] = item; } + m_Root.children.push_back(std::move(overwrite)); + endResetModel(); // restore expand-state @@ -154,6 +158,58 @@ bool ModListByPriorityProxy::setData(const QModelIndex& index, const QVariant& v return QAbstractProxyModel::setData(index, value, role); } +bool ModListByPriorityProxy::canDropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) const +{ + if (!parent.isValid()) { + // the row may be outside of the children list if we insert at the end + if (row >= m_Root.children.size()) { + return false; + } + + if (row >= 0 && (m_Root.children[row]->mod->isSeparator() || m_Root.children[row]->mod->isOverwrite())) { + return false; + } + } + + return QAbstractProxyModel::canDropMimeData(data, action, row, column, parent); +} + +bool ModListByPriorityProxy::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) +{ + // we need to fix the source row + int sourceRow = -1; + + if (row >= 0) { + if (!parent.isValid()) { + if (row < m_Root.children.size()) { + sourceRow = m_Root.children[row]->index; + } + else { + sourceRow = ModInfo::getNumMods(); + } + } + else { + auto* item = static_cast<TreeItem*>(parent.internalPointer()); + QStringList what; + for (auto& child : item->children) { + what.append(QString::number(child->index)); + } + + if (row < item->children.size()) { + sourceRow = item->children[row]->index; + } + else if (parent.row() + 1 < m_Root.children.size()) { + sourceRow = m_Root.children[parent.row() + 1]->index; + } + } + } + else if (parent.isValid()) { + // this is a drop in a separator + sourceRow = m_Root.children[parent.row() + 1]->index; + } + + return sourceModel()->dropMimeData(data, action, sourceRow, column, QModelIndex()); +} Qt::ItemFlags ModListByPriorityProxy::flags(const QModelIndex& idx) const { diff --git a/src/modlistbypriorityproxy.h b/src/modlistbypriorityproxy.h index d5f59f4c..fdf59182 100644 --- a/src/modlistbypriorityproxy.h +++ b/src/modlistbypriorityproxy.h @@ -37,6 +37,8 @@ public: bool hasChildren(const QModelIndex& parent) const override; bool setData(const QModelIndex& index, const QVariant& value, int role) override; + bool canDropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) const override; + bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) override; QModelIndex mapFromSource(const QModelIndex& sourceIndex) const override; QModelIndex mapToSource(const QModelIndex& proxyIndex) const override; diff --git a/src/modlistsortproxy.cpp b/src/modlistsortproxy.cpp index f54f3b7a..e86c6f34 100644 --- a/src/modlistsortproxy.cpp +++ b/src/modlistsortproxy.cpp @@ -636,10 +636,7 @@ bool ModListSortProxy::dropMimeData(const QMimeData *data, Qt::DropAction action --row;
}
- QModelIndex proxyIndex = index(row, column, parent);
- QModelIndex sourceIndex = mapToSource(proxyIndex);
- return this->sourceModel()->dropMimeData(data, action, sourceIndex.row(), sourceIndex.column(),
- sourceIndex.parent());
+ return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
}
void ModListSortProxy::setSourceModel(QAbstractItemModel *sourceModel)
|
