From 71c154bcaa4ccc807c90569f3ffb05bc8b6a8b83 Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Thu, 19 Dec 2024 15:02:10 -0800 Subject: [PATCH] Remember previous selected piece when switching categories. --- common/lc_partselectionwidget.cpp | 27 ++++++++++++++++++++++++++- common/lc_partselectionwidget.h | 1 + 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/common/lc_partselectionwidget.cpp b/common/lc_partselectionwidget.cpp index f5b1b3f5..a019fbe0 100644 --- a/common/lc_partselectionwidget.cpp +++ b/common/lc_partselectionwidget.cpp @@ -540,6 +540,7 @@ lcPartSelectionListView::lcPartSelectionListView(QWidget* Parent, lcPartSelectio setWordWrap(false); setDragEnabled(true); setContextMenuPolicy(Qt::CustomContextMenu); + setAutoScroll(false); mListModel = new lcPartSelectionListModel(this); setModel(mListModel); @@ -585,6 +586,11 @@ void lcPartSelectionListView::CustomContextMenuRequested(QPoint Pos) void lcPartSelectionListView::SetCategory(lcPartCategoryType Type, int Index) { + QModelIndex CurrentIndex = currentIndex(); + + if (CurrentIndex.isValid()) + mLastCategoryRow[{mCategoryType, mCategoryIndex}] = CurrentIndex.row(); + mCategoryType = Type; mCategoryIndex = Index; @@ -610,7 +616,26 @@ void lcPartSelectionListView::SetCategory(lcPartCategoryType Type, int Index) break; } - setCurrentIndex(mListModel->index(0, 0)); + auto CurrentIt = mLastCategoryRow.find({mCategoryType, mCategoryIndex}); + bool ScrollToTop = true; + + if (CurrentIt != mLastCategoryRow.end()) + { + CurrentIndex = mListModel->index(CurrentIt->second, 0); + + if (!isRowHidden(CurrentIndex.row())) + { + scrollTo(CurrentIndex); + setCurrentIndex(CurrentIndex); + ScrollToTop = false; + } + } + + if (ScrollToTop) + { + scrollToTop(); + setCurrentIndex(indexAt(QPoint(0, 0))); + } } void lcPartSelectionListView::SetCustomParts(const std::vector& Parts) diff --git a/common/lc_partselectionwidget.h b/common/lc_partselectionwidget.h index 540b72b2..d18ef91c 100644 --- a/common/lc_partselectionwidget.h +++ b/common/lc_partselectionwidget.h @@ -247,6 +247,7 @@ protected: PieceInfo* mContextInfo = nullptr; lcPartCategoryType mCategoryType = lcPartCategoryType::AllParts; int mCategoryIndex = 0; + std::map, int> mLastCategoryRow; }; class lcPartSelectionWidget : public QWidget