From 6e803a35226980a135d8837898345b38675e3188 Mon Sep 17 00:00:00 2001 From: Mikaƫl Capelle Date: Tue, 29 Dec 2020 20:29:00 +0100 Subject: Fix prev/next button in mod info dialog. --- src/mainwindow.cpp | 22 +++++++++++----------- src/modlistview.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++--------- src/modlistview.h | 5 +++++ 3 files changed, 59 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 99c5293d..c6c4e562 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -2764,20 +2764,20 @@ void MainWindow::setWindowEnabled(bool enabled) ModInfo::Ptr MainWindow::nextModInList() { - int index = ui->modList->nextMod(m_ContextRow); - if (index == -1) { + m_ContextRow = ui->modList->nextMod(m_ContextRow); + if (m_ContextRow == -1) { return {}; } - return ModInfo::getByIndex(index); + return ModInfo::getByIndex(m_ContextRow); } ModInfo::Ptr MainWindow::previousModInList() { - int index = ui->modList->prevMod(m_ContextRow); - if (index == -1) { + m_ContextRow = ui->modList->prevMod(m_ContextRow); + if (m_ContextRow == -1) { return {}; } - return ModInfo::getByIndex(index); + return ModInfo::getByIndex(m_ContextRow); } void MainWindow::displayModInformation(const QString &modName, ModInfoTabIDs tabID) @@ -3479,13 +3479,13 @@ void MainWindow::on_modList_doubleClicked(const QModelIndex &index) } bool indexOk = false; - int modIndex = index.data(ModList::IndexRole).toInt(&indexOk); + m_ContextRow = index.data(ModList::IndexRole).toInt(&indexOk); - if (!indexOk || modIndex < 0 || modIndex >= ModInfo::getNumMods()) { + if (!indexOk || m_ContextRow < 0 || m_ContextRow >= ModInfo::getNumMods()) { return; } - ModInfo::Ptr modInfo = ModInfo::getByIndex(modIndex); + ModInfo::Ptr modInfo = ModInfo::getByIndex(m_ContextRow); Qt::KeyboardModifiers modifiers = QApplication::queryKeyboardModifiers(); if (modifiers.testFlag(Qt::ControlModifier)) { @@ -3502,7 +3502,7 @@ void MainWindow::on_modList_doubleClicked(const QModelIndex &index) } else if (modifiers.testFlag(Qt::ShiftModifier)) { try { - QModelIndex idx = m_OrganizerCore.modList()->index(modIndex, 0); + QModelIndex idx = m_OrganizerCore.modList()->index(m_ContextRow, 0); visitNexusOrWebPage(idx); ui->modList->closePersistentEditor(index); } @@ -3526,7 +3526,7 @@ void MainWindow::on_modList_doubleClicked(const QModelIndex &index) case ModList::COL_CONFLICTFLAGS: tab = ModInfoTabIDs::Conflicts; break; } - displayModInformation(modIndex, tab); + displayModInformation(m_ContextRow, tab); // workaround to cancel the editor that might have opened because of // selection-click ui->modList->closePersistentEditor(index); diff --git a/src/modlistview.cpp b/src/modlistview.cpp index bccacb24..643b1971 100644 --- a/src/modlistview.cpp +++ b/src/modlistview.cpp @@ -107,14 +107,15 @@ int ModListView::nextMod(int modIndex) const auto index = start; for (;;) { - index = model()->index((index.row() + 1) % model()->rowCount(), 0); - modIndex = indexViewToModel(index).data(ModList::IndexRole).toInt(); + index = nextIndex(index); if (index == start || !index.isValid()) { // wrapped around, give up break; } + modIndex = index.data(ModList::IndexRole).toInt(); + ModInfo::Ptr mod = ModInfo::getByIndex(modIndex); // skip overwrite and backups and separators @@ -137,19 +138,15 @@ int ModListView::prevMod(int modIndex) const auto index = start; for (;;) { - int row = index.row() - 1; - if (row == -1) { - row = model()->rowCount() - 1; - } - - index = model()->index(row, 0); - modIndex = indexViewToModel(index).data(ModList::IndexRole).toInt(); + index = prevIndex(index); if (index == start || !index.isValid()) { // wrapped around, give up break; } + modIndex = index.data(ModList::IndexRole).toInt(); + // skip overwrite and backups and separators ModInfo::Ptr mod = ModInfo::getByIndex(modIndex); @@ -286,6 +283,43 @@ QModelIndex ModListView::indexViewToModel(const QModelIndex& index) const } } +QModelIndex ModListView::nextIndex(const QModelIndex& index) const +{ + auto* model = index.model(); + + if (model->rowCount(index) > 0) { + return model->index(0, index.column(), index); + } + + if (index.parent().isValid()) { + if (index.row() + 1 < model->rowCount(index.parent())) { + return index.model()->index(index.row() + 1, index.column(), index.parent()); + } + else { + return index.model()->index((index.parent().row() + 1) % model->rowCount(index.parent().parent()), index.column(), index.parent().parent());; + } + } + else { + return index.model()->index((index.row() + 1) % model->rowCount(index.parent()), index.column(), index.parent()); + } +} + +QModelIndex ModListView::prevIndex(const QModelIndex& index) const +{ + if (index.row() == 0 && index.parent().isValid()) { + return index.parent(); + } + + auto* model = index.model(); + auto prev = model->index((index.row() - 1) % model->rowCount(index.parent()), index.column(), index.parent()); + + if (model->rowCount(prev) > 0) { + return model->index(model->rowCount(prev) - 1, index.column(), prev); + } + + return prev; +} + std::vector ModListView::allIndex( const QAbstractItemModel* model, int column, const QModelIndex& parent) const { diff --git a/src/modlistview.h b/src/modlistview.h index 1e6c5ceb..7e6b29bb 100644 --- a/src/modlistview.h +++ b/src/modlistview.h @@ -105,6 +105,11 @@ protected: QModelIndex indexModelToView(const QModelIndex& index) const; QModelIndex indexViewToModel(const QModelIndex& index) const; + // returns the next/previous index of the given index + // + QModelIndex nextIndex(const QModelIndex& index) const; + QModelIndex prevIndex(const QModelIndex& index) const; + // all index for the given model under the given index, recursively // std::vector allIndex( -- cgit v1.3.1