Skip to content

Commit d10308a

Browse files
committed
fix updating covers when scrolling, using flags will update covers first on the list view
1 parent b3f50c6 commit d10308a

File tree

4 files changed

+136
-131
lines changed

4 files changed

+136
-131
lines changed

src/LevelViewList.cpp

Lines changed: 52 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -15,86 +15,61 @@
1515

1616
#include <QDateTime>
1717
#include "../src/staticViewData.hpp"
18+
#include "../src/assert.hpp"
1819

1920

2021
LevelViewList::LevelViewList(QWidget *parent)
2122
: QListView(parent)
2223
{
24+
m_coversLoaded = false;
25+
m_proxyCoversFirst = false;
2326
}
2427

25-
void LevelViewList::scrollContentsBy(int dx, int dy)
26-
{
27-
// qDebug() << "LevelViewList::scrollContentsBy: " << "";
28-
QListView::scrollContentsBy(dx, dy);
29-
// updateVisibleItems();
28+
void LevelViewList::setProxyCoversFirst() {
29+
m_proxyCoversFirst = true;
3030
}
3131

32-
void LevelViewList::resizeEvent(QResizeEvent *event)
32+
void LevelViewList::scrollContentsBy(int dx, int dy)
3333
{
34-
// qDebug() << "LevelViewList::resizeEvent: " << "";
35-
QListView::resizeEvent(event);
36-
// updateVisibleItems();
34+
QListView::scrollContentsBy(dx, dy);
35+
if (!m_coversLoaded) {
36+
updateVisibleItems();
37+
}
3738
}
3839

3940
void LevelViewList::paintEvent(QPaintEvent *event)
4041
{
41-
// qDebug() << "LevelViewList::paintEvent: " << "";
4242
QListView::paintEvent(event);
43-
// updateVisibleItems();
43+
if (m_proxyCoversFirst == true) {
44+
updateVisibleItems();
45+
}
4446
}
4547

46-
QModelIndexList LevelViewList::visibleIndexes() const
48+
QModelIndexList LevelViewList::visibleIndexes(QAbstractProxyModel* proxy) const
4749
{
4850
QModelIndexList result;
49-
auto r = viewport()->rect();
50-
auto topLeft = indexAt(r.topLeft());
51-
auto bottomRight = indexAt(r.bottomRight());
52-
53-
if (!topLeft.isValid() || !bottomRight.isValid())
54-
return result;
55-
56-
int top = topLeft.row();
57-
int bottom = bottomRight.row();
58-
for (int row = top; row <= bottom; ++row) {
59-
auto idx = indexAt(QPoint(0, visualRect(model()->index(row, 0)).center().y()));
60-
if (idx.isValid())
61-
result << idx;
51+
QRect visRect = viewport()->rect();
52+
53+
for (int row = 0; row < model()->rowCount(); ++row) {
54+
QModelIndex idx = model()->index(row, 0);
55+
QRect itemRect = visualRect(idx);
56+
if (itemRect.isValid() && visRect.intersects(itemRect)) {
57+
result << proxy->mapToSource(idx);
58+
}
6259
}
6360
return result;
6461
}
6562

6663
void LevelViewList::updateVisibleItems()
6764
{
68-
QModelIndexList vis = visibleIndexes();
69-
for (auto &idx : vis) {
70-
if (!idx.isValid())
71-
continue;
72-
// Map proxy index to source index if model is a proxy
73-
QModelIndex sourceIdx = idx;
74-
// Try cast to QAbstractProxyModel to map
75-
if (auto proxy = qobject_cast<QAbstractProxyModel *>(model())) {
76-
sourceIdx = proxy->mapToSource(idx);
77-
}
78-
79-
// Now fetch LevelPtr from the source model, not the proxy (if available)
80-
QVariant varLvl;
81-
/*
82-
if (sourceIdx.isValid()) {
83-
// If we have proxy, get sourceModel, else fallback to model()
84-
if (auto proxy = qobject_cast<QAbstractProxyModel *>(model())) {
85-
varLvl = proxy->sourceModel()->data(sourceIdx, LevelModel::RoleLevel);
86-
} else {
87-
varLvl = model()->data(sourceIdx, LevelModel::RoleLevel);
88-
}
89-
} else {
90-
varLvl = model()->data(idx, LevelModel::RoleLevel);
91-
}
92-
auto lvl = varLvl.value<LevelPtr>();
93-
if (lvl) {
94-
// do update, e.g. preload something
95-
lvl->requestThumb();
96-
}
97-
*/
65+
QAbstractProxyModel* proxy = qobject_cast<QAbstractProxyModel *>(model());
66+
Q_ASSERT_WITH_TRACE(proxy != nullptr);
67+
LevelListModel* model = qobject_cast<LevelListModel*>(proxy->sourceModel());
68+
Q_ASSERT_WITH_TRACE( model != nullptr);
69+
if (model->stop()) {
70+
m_coversLoaded = true;
71+
} else {
72+
model->setScrollChanged(visibleIndexes(proxy));
9873
}
9974
}
10075

@@ -133,13 +108,19 @@ inline quint64 LevelListModel::indexInBounds(const quint64 index) const {
133108
return qMin(index, quint64(m_levels.size()));
134109
}
135110

136-
void LevelListModel::setScrollChange() {
111+
void LevelListModel::setScrollChanged(QModelIndexList list) {
137112
if (!m_levels.empty()) {
138-
m_scrollCursorChanged = true;
113+
m_viewItems << list;
139114
}
140115
}
141116

142-
void LevelListModel::addScrollItem(const quint64 index) {
117+
QVector<QSharedPointer<ListItemData>>
118+
LevelListModel::getChunk(QModelIndexList list) {
119+
QVector<QSharedPointer<ListItemData>> result;
120+
for (QModelIndex& item : list) {
121+
result << m_levels.at(item.row());
122+
}
123+
return result;
143124
}
144125

145126
QVector<QSharedPointer<ListItemData>>
@@ -152,8 +133,8 @@ QVector<QSharedPointer<ListItemData>>
152133
LevelListModel::getDataBuffer(quint64 items) {
153134
QVector<QSharedPointer<ListItemData>> chunk;
154135
if (!m_levels.isEmpty()) {
155-
if (m_scrollCursorChanged == true) {
156-
// chunk = m_scrollBuffer;
136+
if (!m_viewItems.isEmpty()) {
137+
chunk = getChunk(m_viewItems);
157138
} else {
158139
m_cursor_b = indexInBounds(m_cursor_a + items);
159140
chunk = getChunk(m_cursor_a, items);
@@ -163,30 +144,32 @@ QVector<QSharedPointer<ListItemData>>
163144
return chunk;
164145
}
165146

147+
void LevelListModel::updateCovers(QModelIndexList list) {
148+
for (QModelIndex& item : list) {
149+
emit dataChanged(item, item);
150+
}
151+
}
152+
166153
void LevelListModel::updateCovers(quint64 a, quint64 b) {
167154
QModelIndex index_a(index(a, 0));
168155
QModelIndex index_b(index(b, 0));
169156
if (index_a.isValid() && index_b.isValid()) {
170-
qDebug() << "LevelListModel updated covers "
171-
<< index_a.row() << " to " << index_b.row();
172157
emit dataChanged(index_a, index_b);
173158
}
174159
}
175160

176161
void LevelListModel::reset() {
177-
if (m_scrollCursorChanged == true) {
178-
QModelIndex index_b(index(0, 0));
179-
// updateCovers(m_scrollBuffer);
180-
// m_scrollBuffer.clear();
181-
m_scrollCursorChanged = false;
162+
if (!m_viewItems.isEmpty()) {
163+
updateCovers(m_viewItems);
164+
m_viewItems.clear();
182165
} else {
183166
updateCovers(m_cursor_a, m_cursor_b);
184167
m_cursor_a = m_cursor_b;
185168
}
186169
}
187170

188171
bool LevelListModel::stop() const {
189-
return !m_scrollCursorChanged && (m_cursor_b >= m_levels.size());
172+
return m_viewItems.isEmpty() && (m_cursor_b >= m_levels.size());
190173
}
191174

192175
quint64 LevelListProxy::getLid(const QModelIndex &i) const {
@@ -253,6 +236,7 @@ void LevelListProxy::setInstalledFilter(bool on) {
253236
}
254237

255238
void LevelListProxy::setSortMode(SortMode mode) {
239+
qDebug() << "LevelListProxy::setSortMode";
256240
Qt::SortOrder order;
257241
if (m_sortMode == mode){
258242
order = Qt::AscendingOrder;

src/LevelViewList.hpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@ class LevelViewList : public QListView {
2727

2828
public:
2929
explicit LevelViewList(QWidget *parent = nullptr);
30-
31-
QModelIndexList visibleIndexes() const;
30+
void setProxyCoversFirst();
3231

3332
protected:
3433
void scrollContentsBy(int dx, int dy) override;
35-
void resizeEvent(QResizeEvent *event) override;
3634
void paintEvent(QPaintEvent *event) override;
3735

3836
private:
3937
void updateVisibleItems();
38+
QModelIndexList visibleIndexes(QAbstractProxyModel* proxy) const;
39+
bool m_coversLoaded;
40+
bool m_proxyCoversFirst;
4041
};
4142

4243

@@ -46,7 +47,6 @@ class LevelListModel : public QAbstractListModel {
4647
public:
4748
explicit LevelListModel(QObject *parent = nullptr)
4849
: QAbstractListModel(parent),
49-
m_scrollCursorChanged(false),
5050
m_cursor_a(0),
5151
m_cursor_b(0),
5252
m_roleTable({
@@ -65,24 +65,27 @@ class LevelListModel : public QAbstractListModel {
6565
})
6666
{}
6767

68+
QVector<QSharedPointer<ListItemData>> getChunk(QModelIndexList list);
6869
QVector<QSharedPointer<ListItemData>> getChunk(const quint64 cursor,
6970
const quint64 items);
7071
QVector<QSharedPointer<ListItemData>> getDataBuffer(const quint64 items);
72+
7173
void setLevels(const QVector<QSharedPointer<ListItemData>>& levels);
72-
void setScrollChange();
73-
void addScrollItem(const quint64 index);
74+
void setScrollChanged(QModelIndexList list);
7475
void setInstalled(const QModelIndex &index);
76+
7577
quint64 indexInBounds(quint64 index) const;
7678
bool stop() const;
7779
void updateCovers(quint64 a, quint64 b);
80+
void updateCovers(QModelIndexList list);
7881
void reset();
7982
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
8083
QVariant data(const QModelIndex &index, int role) const override;
8184

8285
private:
8386
const QHash<int, std::function<QVariant(const ListItemData&)>> m_roleTable;
8487
QVector<QSharedPointer<ListItemData>> m_levels;
85-
bool m_scrollCursorChanged;
88+
QModelIndexList m_viewItems;
8689
quint64 m_cursor_a;
8790
quint64 m_cursor_b;
8891
};

0 commit comments

Comments
 (0)