DolphinQt: avoid scrolling while game list is being modified

This commit is contained in:
Tillmann Karras 2024-06-23 00:48:16 +01:00
parent 401d6e70f6
commit 3a6cf7202f
6 changed files with 70 additions and 0 deletions

View File

@ -325,6 +325,8 @@ add_executable(dolphin-emu
QtUtils/SignalBlocking.h
QtUtils/UTF8CodePointCountValidator.cpp
QtUtils/UTF8CodePointCountValidator.h
QtUtils/ViewScrollLock.cpp
QtUtils/ViewScrollLock.h
QtUtils/WindowActivationEventFilter.cpp
QtUtils/WindowActivationEventFilter.h
QtUtils/WrapInScrollArea.cpp

View File

@ -199,6 +199,7 @@
<ClCompile Include="QtUtils\PartiallyClosableTabWidget.cpp" />
<ClCompile Include="QtUtils\QtUtils.cpp" />
<ClCompile Include="QtUtils\SetWindowDecorations.cpp" />
<ClCompile Include="QtUtils\ViewScrollLock.cpp" />
<ClCompile Include="QtUtils\UTF8CodePointCountValidator.cpp" />
<ClCompile Include="QtUtils\WindowActivationEventFilter.cpp" />
<ClCompile Include="QtUtils\WrapInScrollArea.cpp" />
@ -259,6 +260,7 @@
<ClInclude Include="QtUtils\QueueOnObject.h" />
<ClInclude Include="QtUtils\RunOnObject.h" />
<ClInclude Include="QtUtils\SignalBlocking.h" />
<ClInclude Include="QtUtils\ViewScrollLock.h" />
<ClInclude Include="QtUtils\WrapInScrollArea.h" />
<ClInclude Include="ResourcePackManager.h" />
<ClInclude Include="Resources.h" />

View File

@ -67,6 +67,7 @@
#include "DolphinQt/QtUtils/ModalMessageBox.h"
#include "DolphinQt/QtUtils/ParallelProgressDialog.h"
#include "DolphinQt/QtUtils/SetWindowDecorations.h"
#include "DolphinQt/QtUtils/ViewScrollLock.h"
#include "DolphinQt/Resources.h"
#include "DolphinQt/Settings.h"
#include "DolphinQt/WiiUpdate.h"
@ -116,6 +117,8 @@ GameList::GameList(QWidget* parent) : QStackedWidget(parent), m_model(this)
connect(&m_model, &QAbstractItemModel::rowsInserted, this, &GameList::ConsiderViewChange);
connect(&m_model, &QAbstractItemModel::rowsRemoved, this, &GameList::ConsiderViewChange);
m_viewScrollLock = new ViewScrollLock(this, m_list_proxy, m_list);
addWidget(m_list);
addWidget(m_grid);
addWidget(m_empty);

View File

@ -20,6 +20,8 @@ namespace UICommon
class GameFile;
}
class ViewScrollLock;
class GameList final : public QStackedWidget
{
Q_OBJECT
@ -100,6 +102,7 @@ private:
void UpdateFont();
GameListModel m_model;
ViewScrollLock* m_viewScrollLock;
QSortFilterProxyModel* m_list_proxy;
QSortFilterProxyModel* m_grid_proxy;

View File

@ -0,0 +1,35 @@
// Copyright 2024 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <QAbstractItemModel>
#include <QAbstractItemView>
#include "DolphinQt/QtUtils/ViewScrollLock.h"
ViewScrollLock::ViewScrollLock(QObject* parent, QAbstractItemModel* model, QAbstractItemView* view)
: QObject(parent), m_view(view)
{
connect(model, &QAbstractItemModel::rowsAboutToBeInserted, this,
&ViewScrollLock::AboutToBeModified);
connect(model, &QAbstractItemModel::rowsAboutToBeRemoved, this,
&ViewScrollLock::AboutToBeModified);
connect(model, &QAbstractItemModel::rowsInserted, this, &ViewScrollLock::Modified);
connect(model, &QAbstractItemModel::rowsRemoved, this, &ViewScrollLock::Modified);
}
void ViewScrollLock::AboutToBeModified()
{
QSize size = m_view->size();
m_first = m_view->indexAt(QPoint(0, 0));
m_last = m_view->indexAt(QPoint(size.height(), size.width()));
}
void ViewScrollLock::Modified()
{
// Try to keep the first row at the top.
// If that fails, try to keep the last row at the bottom.
if (m_first.isValid())
m_view->scrollTo(m_first, QAbstractItemView::PositionAtTop);
else if (m_last.isValid())
m_view->scrollTo(m_last, QAbstractItemView::PositionAtBottom);
}

View File

@ -0,0 +1,25 @@
// Copyright 2024 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <QObject>
#include <QPersistentModelIndex>
class QAbstractItemModel;
class QAbstractItemView;
// Try to keep visible items in view while items are added/removed.
class ViewScrollLock : public QObject
{
Q_OBJECT
public:
ViewScrollLock(QObject* parent, QAbstractItemModel* model, QAbstractItemView* view);
void AboutToBeModified();
void Modified();
private:
QAbstractItemView* m_view;
QPersistentModelIndex m_first, m_last;
};