@@ -45,60 +45,65 @@ QVariant LevelListModel::data(const QModelIndex &index, int role) const {
4545 return result;
4646}
4747
48+ inline quint64 LevelListModel::indexInBounds (const quint64 index) const {
49+ return qMin (index, quint64 (m_levels.size ()));
50+ }
51+
4852void LevelListModel::setScrollChange (const quint64 index) {
4953 if (!m_levels.empty ()) {
5054 m_scrollCursorChanged = true ;
51- m_seq_cursor_a = m_cursor_a.row ();
52-
53- quint64 size = m_levels.size ();
54- quint64 i = qMin (index, size);
55- m_cursor_a = this ->index (i, 0 );
55+ m_scroll_cursor_a = indexInBounds (index);;
5656 }
5757}
5858
59- QVector<QSharedPointer<ListItemData>> LevelListModel::getDataBuffer (quint64 items) {
60- QVector<QSharedPointer<ListItemData>> buffer;
59+ QVector<QSharedPointer<ListItemData>>
60+ LevelListModel::getChunk (const quint64 cursor, const quint64 items) {
61+ const quint64 len = indexInBounds (cursor + items) - cursor;
62+ return m_levels.mid (cursor, len);
63+ }
6164
65+ QVector<QSharedPointer<ListItemData>>
66+ LevelListModel::getDataBuffer (quint64 items) {
67+ QVector<QSharedPointer<ListItemData>> chunk;
6268 if ((items > 0 ) && !m_levels.isEmpty ()) {
63- quint64 size = m_levels.size ();
64- // m_cursor_a starts on -1
65- // there is no way to inialize it
66- quint64 a = m_cursor_a.row () + 1 ;
67- quint64 b = qMin (a + items, size);
68-
69- m_cursor_b = this ->index (b, 0 );
70-
71- buffer.reserve (b - a);
72- for (quint64 i = a; i < b; ++i) {
73- buffer.append (m_levels[i]);
69+ if (m_scrollCursorChanged == true ) {
70+ m_scroll_cursor_b = indexInBounds (m_scroll_cursor_a + items);
71+ chunk = getChunk (m_scroll_cursor_a, items);
72+ } else {
73+ m_cursor_b = indexInBounds (m_cursor_a + items);
74+ chunk = getChunk (m_cursor_a, items);
7475 }
7576 }
7677
77- return buffer ;
78+ return chunk ;
7879}
7980
80- void LevelListModel::reset () {
81- if (m_cursor_a.isValid () && m_cursor_b.isValid ()) {
82- emit dataChanged (m_cursor_a, m_cursor_b);
81+ void LevelListModel::updateCovers (quint64 a, quint64 b) {
82+ QModelIndex index_a (index (a, 0 ));
83+ QModelIndex index_b (index (b, 0 ));
84+ if (index_a.isValid () && index_b.isValid ()) {
85+ qDebug () << " LevelListModel updated covers "
86+ << index_a.row () << " to " << index_b.row ();
87+ emit dataChanged (index_a, index_b);
8388 }
89+ }
8490
91+ void LevelListModel::reset () {
8592 if (m_scrollCursorChanged == true ) {
86- m_cursor_a = this ->index (m_seq_cursor_a, 0 );
93+ updateCovers (m_scroll_cursor_a, m_scroll_cursor_b);
94+ m_scroll_cursor_a = m_scroll_cursor_b;
8795 m_scrollCursorChanged = false ;
8896 } else {
89- m_cursor_a = this ->index (m_cursor_b.row (), 0 );
97+ updateCovers (m_cursor_a, m_cursor_b);
98+ m_cursor_a = m_cursor_b;
9099 }
91100}
92101
93- bool LevelListModel::stop () {
94- bool status = false ;
95- if (m_scrollCursorChanged == false ) {
96- status = (m_cursor_b.row () >= m_levels.size ());
97- }
98- return status;
102+ bool LevelListModel::stop () const {
103+ return !m_scrollCursorChanged && (m_cursor_b >= m_levels.size ());
99104}
100105
101- quint64 LevelListProxy::getLid (const QModelIndex &i) {
106+ quint64 LevelListProxy::getLid (const QModelIndex &i) const {
102107 quint64 id = sourceModel ()->data (i, Qt::UserRole+1 ).toInt ();
103108 if (id == 0 ) {
104109 id = sourceModel ()->data (i, Qt::UserRole+10 ).toInt ();
@@ -107,11 +112,11 @@ quint64 LevelListProxy::getLid(const QModelIndex &i) {
107112 return id;
108113}
109114
110- bool LevelListProxy::getItemType (const QModelIndex &i) {
115+ bool LevelListProxy::getItemType (const QModelIndex &i) const {
111116 return sourceModel ()->data (i, Qt::UserRole+1 ).toBool ();
112117}
113118
114- bool LevelListProxy::getInstalled (const QModelIndex &i) {
119+ bool LevelListProxy::getInstalled (const QModelIndex &i) const {
115120 return sourceModel ()->data (i, Qt::UserRole+11 ).toBool ();
116121}
117122
0 commit comments