]> git.sesse.net Git - vlc/blob - modules/gui/qt4/playlist_model.cpp
Qt4:
[vlc] / modules / gui / qt4 / playlist_model.cpp
1 /*****************************************************************************
2  * playlist_model.cpp : Manage playlist model
3  ****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <QIcon>
25 #include <QFont>
26 #include "qt4.hpp"
27 #include <QApplication>
28 #include "playlist_model.hpp"
29 #include <assert.h>
30
31 #include "pixmaps/type_unknown.xpm"
32 #include "pixmaps/type_afile.xpm"
33 #include "pixmaps/type_vfile.xpm"
34 #include "pixmaps/type_net.xpm"
35 #include "pixmaps/type_card.xpm"
36 #include "pixmaps/type_disc.xpm"
37 #include "pixmaps/type_cdda.xpm"
38 #include "pixmaps/type_directory.xpm"
39 #include "pixmaps/type_playlist.xpm"
40 #include "pixmaps/type_node.xpm"
41
42 QIcon PLModel::icons[ITEM_TYPE_NUMBER];
43
44 static int PlaylistChanged( vlc_object_t *, const char *,
45                             vlc_value_t, vlc_value_t, void * );
46 static int PlaylistNext( vlc_object_t *, const char *,
47                          vlc_value_t, vlc_value_t, void * );
48 static int ItemChanged( vlc_object_t *, const char *,
49                         vlc_value_t, vlc_value_t, void * );
50 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
51                          vlc_value_t oval, vlc_value_t nval, void *param );
52 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
53                         vlc_value_t oval, vlc_value_t nval, void *param );
54
55 /*************************************************************************
56  * Playlist item implementation
57  *************************************************************************/
58
59 /**
60  * Column strings
61  *      Title
62  *      Artist
63  *      Duration
64  */
65
66 void PLItem::init( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
67 {
68     parentItem = parent;
69     i_id = _i_id; i_input_id = _i_input_id;
70     model = m;
71     strings.append( "" );
72     strings.append( "" );
73     strings.append( "" );
74 }
75
76 PLItem::PLItem( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
77 {
78     init( _i_id, _i_input_id, parent, m );
79 }
80
81 PLItem::PLItem( playlist_item_t * p_item, PLItem *parent, PLModel *m )
82 {
83     init( p_item->i_id, p_item->p_input->i_id, parent, m );
84 }
85
86 PLItem::~PLItem()
87 {
88     qDeleteAll(children);
89 }
90
91 void PLItem::insertChild( PLItem *item, int i_pos, bool signal )
92 {
93     assert( model );
94     if( signal )
95         model->beginInsertRows( model->index( this , 0 ), i_pos, i_pos );
96     children.append( item );
97     if( signal )
98         model->endInsertRows();
99 }
100
101 void PLItem::remove( PLItem *removed )
102 {
103     assert( model && parentItem );
104     int i_index = parentItem->children.indexOf( removed );
105     model->beginRemoveRows( model->index( parentItem, 0 ), i_index, i_index );
106     parentItem->children.removeAt( i_index );
107     model->endRemoveRows();
108 }
109
110 int PLItem::row() const
111 {
112     if (parentItem)
113         return parentItem->children.indexOf(const_cast<PLItem*>(this));
114     return 0;
115 }
116
117 void PLItem::update( playlist_item_t *p_item, bool iscurrent )
118 {
119     assert( p_item->p_input->i_id == i_input_id );
120     strings[0] = QString::fromUtf8( p_item->p_input->psz_name );
121     if( p_item->p_input->p_meta )
122     {
123         strings[1] = QString::fromUtf8( p_item->p_input->p_meta->psz_artist );
124     }
125     type = p_item->p_input->i_type;
126     current = iscurrent;
127 }
128
129 /*************************************************************************
130  * Playlist model implementation
131  *************************************************************************/
132
133 PLModel::PLModel( playlist_t *_p_playlist,
134                   playlist_item_t * p_root, int _i_depth, QObject *parent)
135                                     : QAbstractItemModel(parent)
136 {
137     i_depth = _i_depth;
138     assert( i_depth == 1 || i_depth == -1 );
139     p_playlist= _p_playlist;
140     i_items_to_append = 0;
141     b_need_update     = false;
142     i_cached_id       = -1;
143     i_cached_input_id = -1;
144
145 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( QPixmap( type_##x##_xpm ) );
146     ADD_ICON( UNKNOWN , unknown );
147     ADD_ICON( AFILE,afile );
148     ADD_ICON( VFILE, vfile );
149     ADD_ICON( DIRECTORY, directory );
150     ADD_ICON( DISC, disc );
151     ADD_ICON( CDDA, cdda );
152     ADD_ICON( CARD, card );
153     ADD_ICON( NET, net );
154     ADD_ICON( PLAYLIST, playlist );
155     ADD_ICON( NODE, node );
156
157     rootItem = NULL;
158     rebuildRoot( p_root );
159     addCallbacks();
160
161 }
162
163 void PLModel::rebuildRoot( playlist_item_t *p_root )
164 {
165     if( rootItem ) delete rootItem;
166     rootItem = new PLItem( p_root, NULL, this );
167     rootItem->strings[0] = qtr("Name");
168     rootItem->strings[1] = qtr("Artist");
169 }
170
171 PLModel::~PLModel()
172 {
173     delCallbacks();
174     delete rootItem;
175 }
176
177 void PLModel::addCallbacks()
178 {
179     /* Some global changes happened -> Rebuild all */
180     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
181     /* We went to the next item */
182     var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
183     /* One item has been updated */
184     var_AddCallback( p_playlist, "item-change", ItemChanged, this );
185     var_AddCallback( p_playlist, "item-append", ItemAppended, this );
186     var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
187 }
188
189 void PLModel::delCallbacks()
190 {
191     var_DelCallback( p_playlist, "item-change", ItemChanged, this );
192     var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
193     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
194     var_DelCallback( p_playlist, "item-append", ItemAppended, this );
195     var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
196 }
197
198 void PLModel::activateItem( const QModelIndex &index )
199 {
200     assert( index.isValid() );
201     PLItem *item = static_cast<PLItem*>(index.internalPointer());
202     assert( item );
203     PL_LOCK;
204     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
205     playlist_item_t *p_parent = p_item;
206     while( p_parent )
207     {
208         if( p_parent->i_id == rootItem->i_id ) break;
209         p_parent = p_parent->p_parent;
210     }
211     if( p_parent )
212     {
213         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, p_parent, p_item );
214     }
215     PL_UNLOCK;
216 }
217
218 /****************** Base model mandatory implementations *****************/
219 QVariant PLModel::data(const QModelIndex &index, int role) const
220 {
221     assert( index.isValid() );
222     PLItem *item = static_cast<PLItem*>(index.internalPointer());
223     if( role == Qt::DisplayRole )
224     {
225         return QVariant( item->columnString( index.column() ) );
226     }
227     else if( role == Qt::DecorationRole && index.column() == 0  )
228     {
229         if( item->type >= 0 )
230             return QVariant( PLModel::icons[item->type] );
231     }
232     else if( role == Qt::FontRole )
233     {
234         if( item->current == true )
235         {
236             QFont f; f.setBold( true ); return QVariant( f );
237         }
238     }
239     return QVariant();
240 }
241
242 bool PLModel::isCurrent( const QModelIndex &index )
243 {
244     assert( index.isValid() );
245     return static_cast<PLItem*>(index.internalPointer())->current;
246 }
247
248 int PLModel::itemId( const QModelIndex &index ) const
249 {
250     assert( index.isValid() );
251     return static_cast<PLItem*>(index.internalPointer())->i_id;
252 }
253
254 Qt::ItemFlags PLModel::flags(const QModelIndex &index) const
255 {
256     if( !index.isValid() ) return Qt::ItemIsEnabled;
257     return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
258 }
259
260 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
261                               int role) const
262 {
263     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
264             return QVariant( rootItem->columnString( section ) );
265     return QVariant();
266 }
267
268 QModelIndex PLModel::index(int row, int column, const QModelIndex &parent)
269                   const
270 {
271     PLItem *parentItem;
272     if (!parent.isValid())
273         parentItem = rootItem;
274     else
275         parentItem = static_cast<PLItem*>(parent.internalPointer());
276
277     PLItem *childItem = parentItem->child(row);
278     if (childItem)
279         return createIndex(row, column, childItem);
280     else
281         return QModelIndex();
282 }
283
284 /* Return the index of a given item */
285 QModelIndex PLModel::index( PLItem *item, int column ) const
286 {
287     if( !item ) return QModelIndex();
288     const PLItem *parent = item->parent();
289     if( parent )
290         return createIndex( parent->children.lastIndexOf( item ), column, item );
291     return QModelIndex();
292 }
293
294 QModelIndex PLModel::parent(const QModelIndex &index) const
295 {
296     if( !index.isValid() ) return QModelIndex();
297
298     PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
299     PLItem *parentItem = childItem->parent();
300
301     if (parentItem == rootItem) return QModelIndex();
302     return createIndex(parentItem->row(), 0, parentItem);
303 }
304
305 int PLModel::columnCount( const QModelIndex &i) const
306 {
307     if( i_depth == 1 ) return 1;
308     return 2;
309 }
310
311 int PLModel::childrenCount(const QModelIndex &parent) const
312 {
313     return rowCount( parent );
314 }
315
316 int PLModel::rowCount(const QModelIndex &parent) const
317 {
318     PLItem *parentItem;
319
320     if (!parent.isValid())
321         parentItem = rootItem;
322     else
323         parentItem = static_cast<PLItem*>(parent.internalPointer());
324
325     return parentItem->childCount();
326 }
327
328 /************************* Lookups *****************************/
329
330 PLItem *PLModel::FindById( PLItem *root, int i_id )
331 {
332     return FindInner( root, i_id, false );
333 }
334
335 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
336 {
337     return FindInner( root, i_id, true );
338 }
339
340 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
341 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
342
343 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
344 {
345     if( ( !b_input && i_cached_id == i_id) ||
346         ( b_input && i_cached_input_id ==i_id ) )
347     {
348         return b_input ? p_cached_item_bi : p_cached_item;
349     }
350
351     if( !b_input && root->i_id == i_id )
352     {
353         CACHE( i_id, root );
354         return root;
355     }
356     else if( b_input && root->i_input_id == i_id )
357     {
358         ICACHE( i_id, root );
359         return root;
360     }
361
362     QList<PLItem *>::iterator it = root->children.begin();
363     while ( it != root->children.end() )
364     {
365         if( !b_input && (*it)->i_id == i_id )
366         {
367             CACHE( i_id, (*it) );
368             return p_cached_item;
369         }
370         else if( b_input && (*it)->i_input_id == i_id )
371         {
372             ICACHE( i_id, (*it) );
373             return p_cached_item_bi;
374         }
375         if( (*it)->children.size() )
376         {
377             PLItem *childFound = FindInner( (*it), i_id, b_input );
378             if( childFound )
379             {
380                 if( b_input )
381                     ICACHE( i_id, childFound )
382                 else
383                     CACHE( i_id, childFound )
384                 return childFound;
385             }
386         }
387         it++;
388     }
389     return NULL;
390 }
391 #undef CACHE
392 #undef ICACHE
393
394
395 /************************* Updates handling *****************************/
396 void PLModel::customEvent( QEvent *event )
397 {
398     int type = event->type();
399     if( type != ItemUpdate_Type && type != ItemAppend_Type &&
400         type != ItemDelete_Type )
401         return;
402
403     PLEvent *ple = static_cast<PLEvent *>(event);
404
405     if( type == ItemUpdate_Type )
406         ProcessInputItemUpdate( ple->i_id );
407     else if( type == ItemAppend_Type )
408         ProcessItemAppend( ple->p_add );
409     else
410         ProcessItemRemoval( ple->i_id );
411 }
412
413 /**** Events processing ****/
414 void PLModel::ProcessInputItemUpdate( int i_input_id )
415 {
416     if( i_input_id <= 0 ) return;
417     PLItem *item = FindByInput( rootItem, i_input_id );
418     if( item )
419         UpdateTreeItem( item, true );
420 }
421
422 void PLModel::ProcessItemRemoval( int i_id )
423 {
424     if( i_id <= 0 ) return;
425     if( i_id == i_cached_id ) i_cached_id = -1;
426     i_cached_input_id = -1;
427
428     PLItem *item = FindById( rootItem, i_id );
429     if( item )
430         item->remove( item );
431 }
432
433 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
434 {
435     playlist_item_t *p_item = NULL;
436     PLItem *newItem = NULL;
437     i_items_to_append--;
438     if( b_need_update ) return;
439
440     PLItem *nodeItem = FindById( rootItem, p_add->i_node );
441     if( !nodeItem ) goto end;
442
443     PL_LOCK;
444     p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
445     if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
446     if( i_depth == 1 && p_item->p_parent &&
447                         p_item->p_parent->i_id != rootItem->i_id )
448         goto end;
449
450     newItem = new PLItem( p_item, nodeItem, this );
451     nodeItem->appendChild( newItem );
452     UpdateTreeItem( p_item, newItem, true );
453 end:
454     PL_UNLOCK;
455     return;
456 }
457
458 void PLModel::Rebuild()
459 {
460     /* Remove callbacks before locking to avoid deadlocks */
461     delCallbacks();
462     /* Invalidate cache */
463     i_cached_id = i_cached_input_id = -1;
464
465     PL_LOCK;
466     /* Clear the tree */
467     qDeleteAll( rootItem->children );
468     /* Recreate from root */
469     UpdateNodeChildren( rootItem );
470     PL_UNLOCK;
471
472     /* And signal the view */
473     emit layoutChanged();
474     /// \todo  Force current item to be updated
475
476     addCallbacks();
477 }
478
479 /* This function must be entered WITH the playlist lock */
480 void PLModel::UpdateNodeChildren( PLItem *root )
481 {
482     playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
483     UpdateNodeChildren( p_node, root );
484 }
485
486 /* This function must be entered WITH the playlist lock */
487 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
488 {
489     for( int i = 0; i < p_node->i_children ; i++ )
490     {
491         PLItem *newItem =  new PLItem( p_node->pp_children[i], root, this );
492         root->appendChild( newItem, false );
493         UpdateTreeItem( newItem, false, true );
494         if( i_depth != 1 && p_node->pp_children[i]->i_children != -1 )
495             UpdateNodeChildren( p_node->pp_children[i], newItem );
496     }
497 }
498
499 /* This function must be entered WITH the playlist lock */
500 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
501 {
502     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
503     UpdateTreeItem( p_item, item, signal, force );
504 }
505
506 /* This function must be entered WITH the playlist lock */
507 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
508                               bool signal, bool force )
509 {
510     if( !force && i_depth == 1 && p_item->p_parent &&
511                                  p_item->p_parent->i_id != rootItem->i_id )
512         return;
513     item->update( p_item, p_item == p_playlist->status.p_item );
514     if( signal )
515         emit dataChanged( index( item, 0 ) , index( item, 1 ) );
516 }
517
518 /**********************************************************************
519  * Playlist callbacks
520  **********************************************************************/
521 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
522                             vlc_value_t oval, vlc_value_t nval, void *param )
523 {
524     PLModel *p_model = (PLModel *) param;
525     p_model->b_need_update = VLC_TRUE;
526     return VLC_SUCCESS;
527 }
528
529 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
530                          vlc_value_t oval, vlc_value_t nval, void *param )
531 {
532     PLModel *p_model = (PLModel *) param;
533     PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
534     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
535     event = new PLEvent( ItemUpdate_Type, nval.i_int );
536     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
537     return VLC_SUCCESS;
538 }
539
540 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
541                         vlc_value_t oval, vlc_value_t nval, void *param )
542 {
543     PLModel *p_model = (PLModel *) param;
544     PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
545     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
546     return VLC_SUCCESS;
547 }
548
549 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
550                         vlc_value_t oval, vlc_value_t nval, void *param )
551 {
552     PLModel *p_model = (PLModel *) param;
553     PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
554     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
555     return VLC_SUCCESS;
556 }
557
558 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
559                          vlc_value_t oval, vlc_value_t nval, void *param )
560 {
561     PLModel *p_model = (PLModel *) param;
562     playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
563     memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
564
565     if( ++p_model->i_items_to_append >= 50 )
566     {
567         p_model->b_need_update = VLC_TRUE;
568         return VLC_SUCCESS;
569     }
570     PLEvent *event = new PLEvent(  p_add );
571     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
572     return VLC_SUCCESS;
573 }