Skip to content

Commit

Permalink
Use QCache for model states
Browse files Browse the repository at this point in the history
  • Loading branch information
poelzi authored and ronso0 committed Aug 6, 2021
1 parent f2f72d1 commit 17e31cb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 50 deletions.
48 changes: 8 additions & 40 deletions src/widget/wlibrarytableview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -60,8 +56,7 @@ WLibraryTableView::WLibraryTableView(QWidget* parent,
}

WLibraryTableView::~WLibraryTableView() {
qDeleteAll(m_vModelState);
m_vModelState.clear();
m_modelStateCache.clear();
}


Expand Down Expand Up @@ -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();
Expand All @@ -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(
Expand All @@ -157,7 +146,7 @@ void WLibraryTableView::restoreTrackModelState(
return;
}

ModelState* state = m_vModelState[key];
ModelState* state = m_modelStateCache[key];
if (!state) {
return;
}
Expand Down Expand Up @@ -225,27 +214,6 @@ void WLibraryTableView::restoreCurrentViewState() {
restoreTrackModelState(currentModel, key);
}

void WLibraryTableView::clearStateCache() {
auto lru = QMultiMap<qint64, QString>();

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);

Expand Down
9 changes: 3 additions & 6 deletions src/widget/wlibrarytableview.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <QCache>
#include <QDateTime>
#include <QFont>
#include <QItemSelectionModel>
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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<QString, ModelState*> m_vModelState;
QCache<QString, ModelState> m_modelStateCache;
};
4 changes: 1 addition & 3 deletions src/widget/wtracktableview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 0 additions & 1 deletion src/widget/wtracktableview.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 17e31cb

Please sign in to comment.