From 16bf9716a36b5ada56f8ae105c39d50c0f716906 Mon Sep 17 00:00:00 2001 From: cc9cii Date: Sat, 30 May 2015 18:25:43 +1000 Subject: [PATCH] Scroll the selected row to the middle of the viewport. --- apps/opencs/view/world/table.cpp | 17 +++++++++++++++++ apps/opencs/view/world/table.hpp | 2 ++ 2 files changed, 19 insertions(+) diff --git a/apps/opencs/view/world/table.cpp b/apps/opencs/view/world/table.cpp index 25162a67f7..692f54b8e9 100644 --- a/apps/opencs/view/world/table.cpp +++ b/apps/opencs/view/world/table.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "../../model/doc/document.hpp" @@ -744,6 +745,10 @@ std::vector< CSMWorld::UniversalId > CSVWorld::Table::getDraggedRecords() const return idToDrag; } +// parent, start and end depend on the model sending the signal, in this case mProxyModel +// +// If, for example, mModel was used instead, then scrolTo() should use the index +// mProxyModel->mapFromSource(mModel->index(end, 0)) void CSVWorld::Table::rowsInsertedEvent(const QModelIndex& parent, int start, int end) { tableSizeUpdate(); @@ -752,17 +757,29 @@ void CSVWorld::Table::rowsInsertedEvent(const QModelIndex& parent, int start, in { selectRow(end); + // without this delay the scroll works but goes to top for add/clone + QMetaObject::invokeMethod(this, "queuedScrollTo", Qt::QueuedConnection, Q_ARG(int, end)); + if(mUnselectAfterJump) clearSelection(); } } +void CSVWorld::Table::queuedScrollTo(int row) +{ + scrollTo(mProxyModel->index(row, 0), QAbstractItemView::PositionAtCenter); +} + void CSVWorld::Table::dataChangedEvent(const QModelIndex &topLeft, const QModelIndex &bottomRight) { tableSizeUpdate(); if (mAutoJump) + { selectRow(bottomRight.row()); + scrollTo(mProxyModel->index(bottomRight.row(), 0), QAbstractItemView::PositionAtCenter); + //scrollTo(bottomRight, QAbstractItemView::PositionAtCenter); // alternative + } } void CSVWorld::Table::jumpAfterModChanged(int state) diff --git a/apps/opencs/view/world/table.hpp b/apps/opencs/view/world/table.hpp index ea6a026f98..a3d726a7f7 100644 --- a/apps/opencs/view/world/table.hpp +++ b/apps/opencs/view/world/table.hpp @@ -148,6 +148,8 @@ namespace CSVWorld void dataChangedEvent(const QModelIndex &topLeft, const QModelIndex &bottomRight); void jumpAfterModChanged(int state); + + void queuedScrollTo(int state); }; }