Browse Source

Get SimpleListView working again

Donald Carr 6 years ago
parent
commit
5a4d031247
3 changed files with 29 additions and 7 deletions
  1. 11 1
      qml/views/simplelistview/SimpleListView.qml
  2. 15 4
      src/picturemodel.cpp
  3. 3 2
      src/picturemodel.h

+ 11 - 1
qml/views/simplelistview/SimpleListView.qml

@@ -6,19 +6,29 @@ import PictureModel 1.0
 import "../.."
 
 ListView {
+    QtObject {
+        id: d
+        property bool settled: false
+    }
+
     anchors.fill: parent
 
-    delegate: ArtImage {
+    delegate: Image {
         source: path
         height: size.height
         width: size.width
+        Component.onDestruction: {
+            d.settled ? globalUtil.imageModel.retireIndex(index) : undefined
+        }
     }
     model: globalUtil.imageModel
 
     PictureModel {
         id: imageModel
         Component.onCompleted: {
+            imageModel.assumeLinearAccess()
             globalUtil.imageModel = imageModel
+            d.settled = true
         }
     }
 }

+ 15 - 4
src/picturemodel.cpp

@@ -289,6 +289,7 @@ public:
 
     FSNodeTree *fsTree;
     bool useDatabaseBackend;
+    bool assumeLinearAccess = false;
 
     void cacheIndex(int index);
     void retireCachedIndex(int index);
@@ -387,7 +388,7 @@ void PictureModel::PictureModelPrivate::retireCachedIndex(int index)
 {
     int hashIndex = ::offsetHash(index);
     artwork[hashIndex]->refCount--;
-    if (artwork[hashIndex]->refCount < 1) {
+    if (assumeLinearAccess || artwork[hashIndex]->refCount < 1) {
         delete artwork[hashIndex];
         artwork.remove(hashIndex);
     }
@@ -411,6 +412,9 @@ int PictureModel::rowCount(const QModelIndex &parent) const
 
 QVariant PictureModel::data(const QModelIndex &index, int role) const
 {
+    if (d->assumeLinearAccess) {
+        requestIndex(index.row());
+    }
     // What the fuck; Qt queries item 0 before we substantiate it
     // I get to offset my hash by 1 or loss a piece of art
     if (index.row() <= 0 || index.row() >= d->itemCount()) {
@@ -452,9 +456,11 @@ QVariant PictureModel::data(const QModelIndex &index, int role) const
     return QVariant();
 }
 
-int PictureModel::requestIndex()
+int PictureModel::requestIndex(int index) const
 {
-    int index = d->itemCount() == 0 ? 0 : qrand() % d->itemCount();
+    if (index == -1) {
+        index = d->itemCount() == 0 ? 0 : qrand() % d->itemCount();
+    }
 
     if (!d->fsTree) {
         d->cacheIndex(index);
@@ -463,13 +469,18 @@ int PictureModel::requestIndex()
     return index;
 }
 
-void PictureModel::retireIndex(int index)
+void PictureModel::retireIndex(int index) const
 {
     if (!d->fsTree) {
         d->retireCachedIndex(index);
     }
 }
 
+void PictureModel::assumeLinearAccess()
+{
+    d->assumeLinearAccess = true;
+}
+
 QHash<int, QByteArray> PictureModel::roleNames() const
 {
     QHash<int, QByteArray> roles;

+ 3 - 2
src/picturemodel.h

@@ -41,8 +41,9 @@ public:
     int rowCount(const QModelIndex & parent = QModelIndex()) const;
     Q_INVOKABLE QVariant data(const int &row, int role = PathRole) const { return data(index(row, 0), role); }
     QVariant data(const QModelIndex & index, int role = PathRole) const;
-    Q_INVOKABLE int requestIndex();
-    Q_INVOKABLE void retireIndex(int index);
+    Q_INVOKABLE int requestIndex(int index = -1) const;
+    Q_INVOKABLE void retireIndex(int index) const;
+    Q_INVOKABLE void assumeLinearAccess();
 signals:
     void countChanged();