1515
1616#include < QDateTime>
1717#include " ../src/staticViewData.hpp"
18+ #include " ../src/assert.hpp"
1819
1920
2021LevelViewList::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
3940void 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
6663void 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
145126QVector<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+
166153void 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
176161void 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
188171bool 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
192175quint64 LevelListProxy::getLid (const QModelIndex &i) const {
@@ -253,6 +236,7 @@ void LevelListProxy::setInstalledFilter(bool on) {
253236}
254237
255238void LevelListProxy::setSortMode (SortMode mode) {
239+ qDebug () << " LevelListProxy::setSortMode" ;
256240 Qt::SortOrder order;
257241 if (m_sortMode == mode){
258242 order = Qt::AscendingOrder;
0 commit comments