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 8e7d11d1a1
commit 7eab58d0cd
5 changed files with 67 additions and 0 deletions

View File

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

View File

@ -67,6 +67,7 @@
#include "DolphinQt/QtUtils/ModalMessageBox.h"
#include "DolphinQt/QtUtils/ParallelProgressDialog.h"
#include "DolphinQt/QtUtils/SetWindowDecorations.h"
#include "DolphinQt/QtUtils/ViewportLock.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_viewportLock = new ViewportLock(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 ViewportLock;
class GameList final : public QStackedWidget
{
Q_OBJECT
@ -100,6 +102,7 @@ private:
void UpdateFont();
GameListModel m_model;
ViewportLock* m_viewportLock;
QSortFilterProxyModel* m_list_proxy;
QSortFilterProxyModel* m_grid_proxy;

View File

@ -0,0 +1,34 @@
// Copyright 2024 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <QAbstractItemModel>
#include <QAbstractItemView>
#include "DolphinQt/QtUtils/ViewportLock.h"
ViewportLock::ViewportLock(QObject* parent, QAbstractItemModel* model, QAbstractItemView* view)
: QObject(parent), m_view(view)
{
connect(model, &QAbstractItemModel::rowsAboutToBeInserted, this,
&ViewportLock::AboutToBeModified);
connect(model, &QAbstractItemModel::rowsAboutToBeRemoved, this, &ViewportLock::AboutToBeModified);
connect(model, &QAbstractItemModel::rowsInserted, this, &ViewportLock::Modified);
connect(model, &QAbstractItemModel::rowsRemoved, this, &ViewportLock::Modified);
}
void ViewportLock::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 ViewportLock::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 ViewportLock : public QObject
{
Q_OBJECT
public:
ViewportLock(QObject* parent, QAbstractItemModel* model, QAbstractItemView* view);
void AboutToBeModified();
void Modified();
private:
QAbstractItemView* m_view;
QPersistentModelIndex m_first, m_last;
};