Debian Codegraph @

src/playlist/navigators/NonlinearTrackNavigator.cpp

raw
1 /****************************************************************************************
2 * Copyright (c) 2008 Seb Ruiz <[email protected]> *
3 * Copyright (c) 2008 Soren Harward <[email protected]> *
4 * Copyright (c) 2008 Nikolaj Hald Nielsen <[email protected]> *
5 * Copyright (c) 2009 Téo Mrnjavac <[email protected]> *
6 * Copyright (c) 2010 Nanno Langstraat <[email protected]> *
7 * *
8 * This program is free software; you can redistribute it and/or modify it under *
9 * the terms of the GNU General Public License as published by the Free Software *
10 * Foundation; either version 2 of the License, or (at your option) version 3 or *
11 * any later version accepted by the membership of KDE e.V. (or its successor approved *
12 * by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of *
13 * version 3 of the license. *
14 * *
15 * This program is distributed in the hope that it will be useful, but WITHOUT ANY *
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
17 * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU General Public License along with *
20 * this program. If not, see <http://www.gnu.org/licenses/>. *
21 ****************************************************************************************/
22
23 #define DEBUG_PREFIX "Playlist::NonlinearTrackNavigator"
24
25 #include "NonlinearTrackNavigator.h"
26
27 #include "core/meta/Meta.h"
28 #include "core/support/Debug.h"
29 #include "playlist/PlaylistModel.h"
30 #include "playlist/PlaylistModelStack.h"
31
32
33 Playlist::NonlinearTrackNavigator::NonlinearTrackNavigator()
34 : m_currentItem( 0 )
35 {
36 // Connect to the QAbstractItemModel signals of the source model.
37 // Ignore SIGNAL dataChanged: changes in metadata etc. don't affect the random play order.
38 // Ignore SIGNAL layoutChanged: rows moving around doesn't affect the random play order.
39 connect( m_model->qaim(), &QAbstractItemModel::modelReset, this, &NonlinearTrackNavigator::slotModelReset );
40 connect( m_model->qaim(), &QAbstractItemModel::rowsInserted, this, &NonlinearTrackNavigator::slotRowsInserted );
41 connect( m_model->qaim(), &QAbstractItemModel::rowsAboutToBeRemoved, this, &NonlinearTrackNavigator::slotRowsAboutToBeRemoved );
42
43 // Connect to the Playlist::AbstractModel signals of the source model.
44 connect( Playlist::ModelStack::instance()->bottom(), &Playlist::Model::activeTrackChanged,
45 this, &NonlinearTrackNavigator::slotActiveTrackChanged );
46 }
47
48
49 //!***** Keeping in-sync with the source model
50
51 void
52 Playlist::NonlinearTrackNavigator::loadFromSourceModel()
53 {
54 DEBUG_BLOCK
55 slotModelReset();
56 }
57
58 void
59 Playlist::NonlinearTrackNavigator::slotModelReset()
60 {
61 DEBUG_BLOCK
62
63 m_insertedItems.clear();
64 m_removedItems += allItemsSet();
65
66 int lastRowInModel = m_model->qaim()->rowCount() - 1;
67 if ( lastRowInModel >= 0 )
68 slotRowsInserted( QModelIndex(), 0, lastRowInModel );
69
70 doItemListsMaintenance();
71
72 if ( !currentItem() )
73 setCurrentItem( m_model->activeId() );
74 }
75
76 // This function can get called thousands of times during a single FilterProxy change.
77 // Be very efficient here! (e.g. no DEBUG_BLOCK)
78 void
79 Playlist::NonlinearTrackNavigator::slotRowsInserted( const QModelIndex& parent, int startRow, int endRow )
80 {
81 Q_UNUSED( parent );
82
83 for ( int row = startRow; row <= endRow; row++ )
84 {
85 quint64 itemId = m_model->idAt( row );
86
87 m_insertedItems.insert( itemId );
88 m_removedItems.remove( itemId );
89 }
90 }
91
92 // This function can get called thousands of times during a single FilterProxy change.
93 // Be very efficient here! (e.g. no DEBUG_BLOCK)
94 void
95 Playlist::NonlinearTrackNavigator::slotRowsAboutToBeRemoved( const QModelIndex& parent, int startRow, int endRow )
96 {
97 Q_UNUSED( parent );
98
99 for ( int row = startRow; row <= endRow; row++ )
100 {
101 quint64 itemId = m_model->idAt( row );
102
103 m_insertedItems.remove( itemId );
104 m_removedItems.insert( itemId );
105 }
106 }
107
108 // A general note on this function: thousands of rows can be inserted/removed by a single
109 // FilterProxy change. However, this function gets to process them in a big batch.
110 //
111 // So: O(n * log n) performance is good enough, but O(n^2) is not.
112 // (that's also why we need the 'listRemove()' helper function)
113 void
114 Playlist::NonlinearTrackNavigator::doItemListsMaintenance()
115 {
116 DEBUG_BLOCK
117
118 // Move batch instructions to local storage immediately, because we may get called recursively.
119 QSet<quint64> tmpInsertedItems = m_insertedItems;
120 m_insertedItems.clear();
121
122 QSet<quint64> tmpRemovedItems = m_removedItems;
123 m_removedItems.clear();
124
125 // Handle the removed items
126 if ( !tmpRemovedItems.isEmpty() )
127 {
128 QSet<quint64> knownRemovedItems = tmpRemovedItems & allItemsSet(); // Filter out items inserted+removed between calls to us.
129
130 Item::listRemove( m_allItemsList, tmpRemovedItems );
131 Item::listRemove( m_historyItems, tmpRemovedItems );
132 Item::listRemove( m_replayedItems, tmpRemovedItems );
133 Item::listRemove( m_plannedItems, tmpRemovedItems );
134
135 notifyItemsRemoved( knownRemovedItems );
136
137 if ( tmpRemovedItems.contains( currentItem() ) ) // After 'notifyItemsRemoved()', so that they get a chance to choose a new one.
138 setCurrentItem( 0 );
139 }
140
141 // Handle the newly inserted items
142 if ( !tmpInsertedItems.isEmpty() )
143 {
144 QSet<quint64> unknownInsertedItems = tmpInsertedItems - allItemsSet(); // Filter out items removed+reinserted between calls to us.
145
146 m_allItemsList.append( unknownInsertedItems.values() );
147 m_plannedItems.clear(); // Could do this more subtly in each child class, but this is good enough.
148
149 notifyItemsInserted( unknownInsertedItems );
150 }
151
152 // Prune history size
153 while ( m_historyItems.size() > MAX_HISTORY_SIZE )
154 m_historyItems.removeFirst();
155 }
156
157
158 //!***** Current playlist item
159
160 quint64
161 Playlist::NonlinearTrackNavigator::currentItem()
162 {
163 doItemListsMaintenance();
164 return m_currentItem;
165 }
166
167 void
168 Playlist::NonlinearTrackNavigator::setCurrentItem( const quint64 newItem, bool goingBackward )
169 {
170 DEBUG_BLOCK
171
172 doItemListsMaintenance();
173
174 // Remember that we've played the old item.
175 if ( m_currentItem )
176 {
177 if ( goingBackward )
178 m_replayedItems.prepend( m_currentItem );
179 else
180 m_historyItems.append( m_currentItem );
181 }
182
183 m_currentItem = newItem;
184
185 // If the new current item happens to also be the next planned item, consider that
186 // plan "done". Can happen e.g. when the user manually plays our next planned item.
187 if ( m_currentItem )
188 if ( !m_plannedItems.isEmpty() && m_plannedItems.first() == m_currentItem )
189 m_plannedItems.removeFirst();
190 }
191
192 // In the normal case this signal slot is redundant, because 'requestNext|LastTrack()'
193 // already called 'setCurrentItem()' long before this function gets called.
194 //
195 // This signal slot takes care of some special cases, like the user clicking on
196 // an arbitrary item in the playlist.
197 void
198 Playlist::NonlinearTrackNavigator::slotActiveTrackChanged( const quint64 id )
199 {
200 DEBUG_BLOCK
201
202 doItemListsMaintenance();
203
204 if ( currentItem() != id ) // If the new item is not what we expected:
205 {
206 // Heuristic: if this new "current item" does not look like we're going back/fwd in
207 // history, then cancel "visit history" mode.
208 // Not important, just a small nicety. It's what the user probably wants.
209 if ( ( m_historyItems.isEmpty() || m_historyItems.last() != id ) &&
210 ( !m_replayedItems.contains( id ) ) )
211 {
212 m_historyItems.append( m_replayedItems );
213 m_replayedItems.clear();
214 }
215
216 // Ditch the plan. The unexpected "current item" might change what we want to do next.
217 m_plannedItems.clear();
218
219 // The main thing we need to do.
220 setCurrentItem( id );
221 }
222 }
223
224
225 //!***** Choosing next playlist item
226
227 Playlist::ItemList*
228 Playlist::NonlinearTrackNavigator::nextItemChooseDonorList()
229 {
230 DEBUG_BLOCK
231
232 if ( !m_queue.isEmpty() ) // User-specified queue has highest priority.
233 return &m_queue;
234 else if ( !m_replayedItems.isEmpty() ) // If the user pressed "previous" once or more, first re-play those items again when we go forward again.
235 return &m_replayedItems;
236 else
237 {
238 if ( m_plannedItems.isEmpty() )
239 planOne();
240 if ( !m_plannedItems.isEmpty() ) // The normal case.
241 return &m_plannedItems;
242 else
243 debug() << "planOne() didn't plan a next item.";
244 }
245
246 return nullptr;
247 }
248
249 quint64
250 Playlist::NonlinearTrackNavigator::likelyNextTrack()
251 {
252 // Reminder: if that function is modified, it's important to research if changes must also
253 // be applied to similar code in `Playlist::StandardTrackNavigator::requestNextTrack()`
254
255 doItemListsMaintenance();
256
257 quint64 nextItem = 0;
258 bool trackHasBeenSet = false;
259 ItemList *donor;
260 while (!trackHasBeenSet)
261 {
262 donor = nextItemChooseDonorList();
263 if (!donor || donor->isEmpty()) {
264 nextItem = 0;
265 break;
266 }
267 while (!donor->isEmpty()) {
268 nextItem = donor->first();
269 if (m_model->trackForId(nextItem)->isPlayable()) {
270 trackHasBeenSet = true;
271 break;
272 } else {
273 donor->removeFirst();
274 }
275 }
276 }
277
278 return nextItem;
279 }
280
281 // We could just call 'likelyNextTrack()' and assume that we'll get a 'slotActiveTrackChanged'
282 // callback later. But let's follow our API strictly: update the donor list immediately.
283 quint64
284 Playlist::NonlinearTrackNavigator::requestNextTrack()
285 {
286 // Reminder: if that function is modified, it's important to research if changes must also
287 // be applied to similar code in `Playlist::StandardTrackNavigator::likelyNextTrack()`
288
289 doItemListsMaintenance();
290
291 quint64 nextItem = 0;
292 bool trackHasBeenSet = false;
293 ItemList *donor;
294 while (!trackHasBeenSet)
295 {
296 donor = nextItemChooseDonorList();
297 if (!donor || donor->isEmpty()) {
298 nextItem = 0;
299 break;
300 }
301 while (!donor->isEmpty()) {
302 nextItem = donor->takeFirst();
303 if (m_model->trackForId(nextItem)->isPlayable()) {
304 trackHasBeenSet = true;
305 break;
306 }
307 }
308 }
309
310 setCurrentItem( nextItem );
311 return m_currentItem;
312 }
313
314
315 //!***** Choosing previous playlist item
316
317 quint64
318 Playlist::NonlinearTrackNavigator::likelyLastTrack()
319 {
320 doItemListsMaintenance();
321
322 return m_historyItems.isEmpty() ? 0 : m_historyItems.last();
323 }
324
325 quint64
326 Playlist::NonlinearTrackNavigator::requestLastTrack()
327 {
328 doItemListsMaintenance();
329
330 quint64 lastItem = 0;
331 while (!m_historyItems.isEmpty())
332 {
333 quint64 possibleLastItem = m_historyItems.takeLast();
334 if (m_model->trackForId(possibleLastItem)->isPlayable()) {
335 lastItem = possibleLastItem;
336 break;
337 }
338 }
339
340 setCurrentItem( lastItem, true );
341 return m_currentItem;
342 }