diff options
| author | Mikaël Capelle <capelle.mikael@gmail.com> | 2020-12-28 13:50:43 +0100 |
|---|---|---|
| committer | Mikaël Capelle <capelle.mikael@gmail.com> | 2021-01-02 15:38:15 +0100 |
| commit | 4636d7bd5d092ff47146248232cd73c8d0254d7a (patch) | |
| tree | 2b24e8f0847a0fd18c98646ea1aea4e9e994bb91 | |
| parent | addb38645b41507ae8e0536bb83d3b99d365f664 (diff) | |
Small refactoring to avoid duplicated code.
| -rw-r--r-- | src/modlist.cpp | 8 | ||||
| -rw-r--r-- | src/modlist.h | 4 | ||||
| -rw-r--r-- | src/modlistbypriorityproxy.cpp | 33 |
3 files changed, 20 insertions, 25 deletions
diff --git a/src/modlist.cpp b/src/modlist.cpp index b79f0b0e..a058e71c 100644 --- a/src/modlist.cpp +++ b/src/modlist.cpp @@ -1108,7 +1108,7 @@ int ModList::dropPriority(int row, const QModelIndex& parent) const return newPriority; } -std::vector<int> ModList::sourceRows(const QMimeData* mimeData) const +std::vector<int> ModList::sourceRows(const QMimeData* mimeData) { QByteArray encoded = mimeData->data("application/x-qabstractitemmodeldatalist"); QDataStream stream(&encoded, QIODevice::ReadOnly); @@ -1125,7 +1125,7 @@ std::vector<int> ModList::sourceRows(const QMimeData* mimeData) const return sourceRows; } -std::optional<std::pair<QString, QString>> ModList::relativeUrl(const QUrl& url) const +std::optional<std::pair<QString, QString>> ModList::relativeUrl(const QUrl& url) { if (!url.isLocalFile()) { return {}; @@ -1211,7 +1211,7 @@ bool ModList::dropMod(const QMimeData *mimeData, int row, const QModelIndex &par } try { - std::vector<int> sourceRows = this->sourceRows(mimeData); + std::vector<int> sourceRows = ModList::sourceRows(mimeData); changeModPriority(sourceRows, newPriority); } catch (const std::exception &e) { @@ -1229,7 +1229,7 @@ bool ModList::dropArchive(const QMimeData* mimeData, int row, const QModelIndex& } try { - std::vector<int> sourceRows = this->sourceRows(mimeData); + std::vector<int> sourceRows = ModList::sourceRows(mimeData); if (sourceRows.size() == 1) { emit downloadArchiveDropped(sourceRows[0], priority); } diff --git a/src/modlist.h b/src/modlist.h index b2eb6be6..eebb1105 100644 --- a/src/modlist.h +++ b/src/modlist.h @@ -372,12 +372,12 @@ private: // retrieve the relative path of file and its origin given a URL from Mime data
// returns an empty optional if the URL is not a valid file for dropping
//
- std::optional<std::pair<QString, QString>> relativeUrl(const QUrl&) const;
+ static std::optional<std::pair<QString, QString>> relativeUrl(const QUrl&);
// return the source rows from the given mime data for drag&drop of mods or
// installation archives
//
- std::vector<int> sourceRows(const QMimeData* mimeData) const;
+ static std::vector<int> sourceRows(const QMimeData* mimeData);
bool dropURLs(const QMimeData* mimeData, int row, const QModelIndex& parent);
bool dropMod(const QMimeData* mimeData, int row, const QModelIndex& parent);
diff --git a/src/modlistbypriorityproxy.cpp b/src/modlistbypriorityproxy.cpp index dc51617e..eb931aa9 100644 --- a/src/modlistbypriorityproxy.cpp +++ b/src/modlistbypriorityproxy.cpp @@ -160,35 +160,30 @@ bool ModListByPriorityProxy::setData(const QModelIndex& index, const QVariant& v bool ModListByPriorityProxy::canDropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) const { - bool firstRowSeparator = false; - try { - QByteArray encoded = data->data("application/x-qabstractitemmodeldatalist"); - QDataStream stream(&encoded, QIODevice::ReadOnly); + if (data->hasText()) { + bool firstRowSeparator = false; - int firstRowPriority = INT_MAX; - unsigned int firstRowIndex = -1; + try { + int firstRowPriority = INT_MAX; + unsigned int firstRowIndex = -1; - while (!stream.atEnd()) { - int sourceRow, col; - QMap<int, QVariant> roleDataMap; - stream >> sourceRow >> col >> roleDataMap; - if (col == 0) { + for (auto sourceRow : ModList::sourceRows(data)) { if (m_Profile->getModPriority(sourceRow) < firstRowPriority) { firstRowIndex = sourceRow; firstRowPriority = m_Profile->getModPriority(sourceRow); } } - } - firstRowSeparator = firstRowIndex != -1 && ModInfo::getByIndex(firstRowIndex)->isSeparator(); - } - catch (std::exception const&) { + firstRowSeparator = firstRowIndex != -1 && ModInfo::getByIndex(firstRowIndex)->isSeparator(); + } + catch (std::exception const&) { - } + } - // first row is a separator, we can drop it anywhere - if (firstRowSeparator) { - return QAbstractProxyModel::canDropMimeData(data, action, row, column, parent); + // first row is a separator, we can drop it anywhere + if (firstRowSeparator) { + return QAbstractProxyModel::canDropMimeData(data, action, row, column, parent); + } } if (!parent.isValid() && row >= 0) { |
