From 17e31cb30705e79068e91063b6075ec813ae6495 Mon Sep 17 00:00:00 2001 From: Daniel Poelzleithner Date: Sat, 2 Jan 2021 20:35:37 +0100 Subject: [PATCH] Use QCache for model states --- src/widget/wlibrarytableview.cpp | 48 ++++++-------------------------- src/widget/wlibrarytableview.h | 9 ++---- src/widget/wtracktableview.cpp | 4 +-- src/widget/wtracktableview.h | 1 - 4 files changed, 12 insertions(+), 50 deletions(-) diff --git a/src/widget/wlibrarytableview.cpp b/src/widget/wlibrarytableview.cpp index 6a7425869db7..d8497626f698 100644 --- a/src/widget/wlibrarytableview.cpp +++ b/src/widget/wlibrarytableview.cpp @@ -14,19 +14,15 @@ #include "widget/wwidget.h" namespace { -// number of items to stop purging -constexpr int kClearModelStatesLowWatermark = 1000; -// number of items to start purging -constexpr int kClearModelStatesHighWatermark = 1100; +// number of entries in the model cache +constexpr int kModelCacheSize = 1000; } // namespace WLibraryTableView::WLibraryTableView(QWidget* parent, - UserSettingsPointer pConfig, - const ConfigKey& vScrollBarPosKey) + UserSettingsPointer pConfig) : QTableView(parent), m_pConfig(pConfig), - m_vScrollBarPosKey(vScrollBarPosKey) { - + m_modelStateCache(kModelCacheSize) { // Setup properties for table // Editing starts when clicking on an already selected item. @@ -60,8 +56,7 @@ WLibraryTableView::WLibraryTableView(QWidget* parent, } WLibraryTableView::~WLibraryTableView() { - qDeleteAll(m_vModelState); - m_vModelState.clear(); + m_modelStateCache.clear(); } @@ -126,12 +121,10 @@ void WLibraryTableView::saveTrackModelState( VERIFY_OR_DEBUG_ASSERT(!key.isEmpty()) { return; } - ModelState* state = m_vModelState[key]; + ModelState* state = m_modelStateCache[key]; if (!state) { state = new ModelState(); } - - state->lastChange = QDateTime::currentSecsSinceEpoch(); // qDebug() << "save: saveTrackModelState:" << key << model << verticalScrollBar()->value() << " | "; state->verticalScrollPosition = verticalScrollBar()->value(); state->horizontalScrollPosition = horizontalScrollBar()->value(); @@ -142,11 +135,7 @@ void WLibraryTableView::saveTrackModelState( state->selectionIndex = QModelIndexList(); } state->currentIndex = selectionModel()->currentIndex(); - m_vModelState[key] = state; - - if (m_vModelState.size() > kClearModelStatesHighWatermark) { - clearStateCache(); - } + m_modelStateCache.insert(key, state, 1); } void WLibraryTableView::restoreTrackModelState( @@ -157,7 +146,7 @@ void WLibraryTableView::restoreTrackModelState( return; } - ModelState* state = m_vModelState[key]; + ModelState* state = m_modelStateCache[key]; if (!state) { return; } @@ -225,27 +214,6 @@ void WLibraryTableView::restoreCurrentViewState() { restoreTrackModelState(currentModel, key); } -void WLibraryTableView::clearStateCache() { - auto lru = QMultiMap(); - - auto i = m_vModelState.constBegin(); - while (i != m_vModelState.constEnd()) { - lru.insert(i.value()->lastChange, i.key()); - i++; - } - QList sortKeys = lru.keys(); - std::sort(sortKeys.begin(), - sortKeys.end(), - [](const qint64 a, const qint64 b) { return a < b; }); - - while (m_vModelState.size() > kClearModelStatesLowWatermark) { - const QStringList keys = lru.values(sortKeys.takeFirst()); - for (const auto& key : keys) { - m_vModelState.take(key); - } - } -} - void WLibraryTableView::focusInEvent(QFocusEvent* event) { QTableView::focusInEvent(event); diff --git a/src/widget/wlibrarytableview.h b/src/widget/wlibrarytableview.h index b71861ac22a4..662b11ccabbc 100644 --- a/src/widget/wlibrarytableview.h +++ b/src/widget/wlibrarytableview.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -20,13 +21,11 @@ class WLibraryTableView : public QTableView, public virtual LibraryView { int verticalScrollPosition; QModelIndexList selectionIndex; QModelIndex currentIndex; - qint64 lastChange; }; public: WLibraryTableView(QWidget* parent, - UserSettingsPointer pConfig, - const ConfigKey& vScrollBarPosKey); + UserSettingsPointer pConfig); ~WLibraryTableView() override; void moveSelection(int delta) override; @@ -41,7 +40,6 @@ class WLibraryTableView : public QTableView, public virtual LibraryView { /// @param key unique for trackmodel void restoreTrackModelState(const QAbstractItemModel* model, const QString& key); /// @brief clears the state cache until it's size is = kClearModelStatesLowWatermark - void clearStateCache(); void saveCurrentViewState() override; void restoreCurrentViewState() override; @@ -63,8 +61,7 @@ class WLibraryTableView : public QTableView, public virtual LibraryView { private: const UserSettingsPointer m_pConfig; - const ConfigKey m_vScrollBarPosKey; // saves scrollposition/selection/etc - QMap m_vModelState; + QCache m_modelStateCache; }; diff --git a/src/widget/wtracktableview.cpp b/src/widget/wtracktableview.cpp index feb5589d96a8..cc49e5c988a7 100644 --- a/src/widget/wtracktableview.cpp +++ b/src/widget/wtracktableview.cpp @@ -38,9 +38,7 @@ WTrackTableView::WTrackTableView(QWidget* parent, double backgroundColorOpacity, bool sorting) : WLibraryTableView(parent, - pConfig, - ConfigKey(LIBRARY_CONFIGVALUE, - WTRACKTABLEVIEW_VSCROLLBARPOS_KEY)), + pConfig), m_pConfig(pConfig), m_pLibrary(pLibrary), m_backgroundColorOpacity(backgroundColorOpacity), diff --git a/src/widget/wtracktableview.h b/src/widget/wtracktableview.h index 1b9e4edf2841..a9a7d5e06bbe 100644 --- a/src/widget/wtracktableview.h +++ b/src/widget/wtracktableview.h @@ -18,7 +18,6 @@ class ExternalTrackCollection; class Library; class WTrackMenu; -const QString WTRACKTABLEVIEW_VSCROLLBARPOS_KEY = "VScrollBarPos"; /** ConfigValue key for QTable vertical scrollbar position */ const QString LIBRARY_CONFIGVALUE = "[Library]"; /** ConfigValue "value" (wtf) for library stuff */ class WTrackTableView : public WLibraryTableView {