]> git.sesse.net Git - vlc/blob - modules/gui/qt4/playlist_model.cpp
Improve Qt interaction stuff
[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 "pixmaps/codec.xpm"
25 #include <QIcon>
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 int PLItem::row() const
102 {
103     if (parentItem)
104         return parentItem->children.indexOf(const_cast<PLItem*>(this));
105     return 0;
106 }
107
108 void PLItem::update( playlist_item_t *p_item )
109 {
110     assert( p_item->p_input->i_id == i_input_id );
111     strings[0] = QString::fromUtf8( p_item->p_input->psz_name );
112     if( p_item->p_input->p_meta )
113     {
114         strings[1] = QString::fromUtf8( p_item->p_input->p_meta->psz_artist );
115     }
116     type = p_item->p_input->i_type;
117 }
118
119 /*************************************************************************
120  * Playlist model implementation
121  *************************************************************************/
122
123 PLModel::PLModel( playlist_t *_p_playlist,
124                   playlist_item_t * p_root, int _i_depth, QObject *parent)
125                                     : QAbstractItemModel(parent)
126 {
127     i_depth = _i_depth;
128     assert( i_depth == 1 || i_depth == -1 );
129     p_playlist= _p_playlist;
130     i_items_to_append = 0;
131     b_need_update     = false;
132     i_cached_id       = -1;
133     i_cached_input_id = -1;
134
135 #define ADD_ICON(type, x) icons[ITEM_TYPE_##type] = QIcon( QPixmap( type_##x##_xpm ) );
136     ADD_ICON( UNKNOWN , unknown );
137     ADD_ICON( AFILE,afile );
138     ADD_ICON( VFILE, vfile );
139     ADD_ICON( DIRECTORY, directory );
140     ADD_ICON( DISC, disc );
141     ADD_ICON( CDDA, cdda );
142     ADD_ICON( CARD, card );
143     ADD_ICON( NET, net );
144     ADD_ICON( PLAYLIST, playlist );
145     ADD_ICON( NODE, node );
146
147     rootItem = NULL;
148     rebuildRoot( p_root );
149     addCallbacks();
150
151 }
152
153 void PLModel::rebuildRoot( playlist_item_t *p_root )
154 {
155     if( rootItem ) delete rootItem;
156     rootItem = new PLItem( p_root, NULL, this );
157     rootItem->strings[0] = qtr("Name");
158     rootItem->strings[1] = qtr("Artist");
159 }
160
161 PLModel::~PLModel()
162 {
163     delCallbacks();
164     delete rootItem;
165 }
166
167 void PLModel::addCallbacks()
168 {
169     /* Some global changes happened -> Rebuild all */
170     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
171     /* We went to the next item */
172     var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
173     /* One item has been updated */
174     var_AddCallback( p_playlist, "item-change", ItemChanged, this );
175     var_AddCallback( p_playlist, "item-append", ItemAppended, this );
176     var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
177 }
178
179 void PLModel::delCallbacks()
180 {
181     var_DelCallback( p_playlist, "item-change", ItemChanged, this );
182     var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
183     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
184     var_DelCallback( p_playlist, "item-append", ItemAppended, this );
185     var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
186 }
187
188 void PLModel::activateItem( const QModelIndex &index )
189 {
190     assert( index.isValid() );
191     PLItem *item = static_cast<PLItem*>(index.internalPointer());
192     assert( item );
193     PL_LOCK;
194     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
195     playlist_item_t *p_parent = p_item;
196     while( p_parent )
197     {
198         if( p_parent->i_id == rootItem->i_id ) break;
199         p_parent = p_parent->p_parent;
200     }
201     if( p_parent )
202     {
203         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, p_parent, p_item );
204     }
205     PL_UNLOCK;
206 }
207
208 /****************** Base model mandatory implementations *****************/
209 QVariant PLModel::data(const QModelIndex &index, int role) const
210 {
211     assert( index.isValid() );
212     PLItem *item = static_cast<PLItem*>(index.internalPointer());
213     if( role == Qt::DisplayRole )
214     {
215         return QVariant( item->columnString( index.column() ) );
216     }
217     else if( role == Qt::DecorationRole && index.column() == 0  )
218     {
219         if( item->type >= 0 )
220             return QVariant( PLModel::icons[item->type] );
221     }
222     return QVariant();
223 }
224
225 int PLModel::itemId( const QModelIndex &index ) const
226 {
227     assert( index.isValid() );
228     return static_cast<PLItem*>(index.internalPointer())->i_id;
229 }
230
231 Qt::ItemFlags PLModel::flags(const QModelIndex &index) const
232 {
233     if( !index.isValid() ) return Qt::ItemIsEnabled;
234     return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
235 }
236
237 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
238                               int role) const
239 {
240     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
241             return QVariant( rootItem->columnString( section ) );
242     return QVariant();
243 }
244
245 QModelIndex PLModel::index(int row, int column, const QModelIndex &parent)
246                   const
247 {
248     PLItem *parentItem;
249     if (!parent.isValid())
250         parentItem = rootItem;
251     else
252         parentItem = static_cast<PLItem*>(parent.internalPointer());
253
254     PLItem *childItem = parentItem->child(row);
255     if (childItem)
256         return createIndex(row, column, childItem);
257     else
258         return QModelIndex();
259 }
260
261 /* Return the index of a given item */
262 QModelIndex PLModel::index( PLItem *item, int column ) const
263 {
264     if( !item ) return QModelIndex();
265     const PLItem *parent = item->parent();
266     if( parent )
267         return createIndex( parent->children.lastIndexOf( item ), column, item );
268     return QModelIndex();
269 }
270
271 QModelIndex PLModel::parent(const QModelIndex &index) const
272 {
273     if (!index.isValid())  return QModelIndex();
274
275     PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
276     PLItem *parentItem = childItem->parent();
277
278     if (parentItem == rootItem) return QModelIndex();
279
280     return createIndex(parentItem->row(), 0, parentItem);
281 }
282
283 int PLModel::columnCount( const QModelIndex &i) const
284 {
285     if( i_depth == 1 ) return 1;
286     return 2;
287 }
288
289 int PLModel::childrenCount(const QModelIndex &parent) const
290 {
291     return rowCount( parent );
292 }
293
294 int PLModel::rowCount(const QModelIndex &parent) const
295 {
296     PLItem *parentItem;
297
298     if (!parent.isValid())
299         parentItem = rootItem;
300     else
301         parentItem = static_cast<PLItem*>(parent.internalPointer());
302
303     return parentItem->childCount();
304 }
305
306 /************************* Lookups *****************************/
307
308 PLItem *PLModel::FindById( PLItem *root, int i_id )
309 {
310     return FindInner( root, i_id, false );
311 }
312
313 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
314 {
315     return FindInner( root, i_id, true );
316 }
317
318 #define CACHE( i, p ) { i_cached_id = i; p_cached_item = p; }
319 #define ICACHE( i, p ) { i_cached_input_id = i; p_cached_item_bi = p; }
320
321 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
322 {
323     if( ( !b_input && i_cached_id == i_id) ||
324         ( b_input && i_cached_input_id ==i_id ) )
325     {
326         return b_input ? p_cached_item_bi : p_cached_item;
327     }
328
329     if( !b_input && root->i_id == i_id )
330     {
331         CACHE( i_id, root );
332         return root;
333     }
334     else if( b_input && root->i_input_id == i_id )
335     {
336         ICACHE( i_id, root );
337         return root;
338     }
339
340     QList<PLItem *>::iterator it = root->children.begin();
341     while ( it != root->children.end() )
342     {
343         if( !b_input && (*it)->i_id == i_id )
344         {
345             CACHE( i_id, (*it) );
346             return p_cached_item;
347         }
348         else if( b_input && (*it)->i_input_id == i_id )
349         {
350             ICACHE( i_id, (*it) );
351             return p_cached_item_bi;
352         }
353         if( (*it)->children.size() )
354         {
355             PLItem *childFound = FindInner( (*it), i_id, b_input );
356             if( childFound )
357             {
358                 if( b_input )
359                     ICACHE( i_id, childFound )
360                 else
361                     CACHE( i_id, childFound )
362                 return childFound;
363             }
364         }
365         it++;
366     }
367     return NULL;
368 }
369 #undef CACHE
370 #undef ICACHE
371
372
373 /************************* Updates handling *****************************/
374 void PLModel::customEvent( QEvent *event )
375 {
376     int type = event->type();
377     if( type != ItemUpdate_Type && type != ItemAppend_Type &&
378         type != ItemDelete_Type )
379         return;
380
381     PLEvent *ple = static_cast<PLEvent *>(event);
382
383     if( type == ItemUpdate_Type )
384         ProcessInputItemUpdate( ple->i_id );
385     else if( type == ItemAppend_Type )
386         ProcessItemAppend( ple->p_add );
387     else
388         ProcessItemRemoval( ple->i_id );
389 }
390
391 /**** Events processing ****/
392 void PLModel::ProcessInputItemUpdate( int i_input_id )
393 {
394     if( i_input_id <= 0 ) return;
395     PLItem *item = FindByInput( rootItem, i_input_id );
396     if( item )
397         UpdateTreeItem( item, true );
398 }
399
400 void PLModel::ProcessItemRemoval( int i_id )
401 {
402     if( i_id <= 0 ) return;
403     if( i_id == i_cached_id ) i_cached_id = -1;
404     i_cached_input_id = -1;
405
406     /// \todo
407 }
408
409 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
410 {
411     playlist_item_t *p_item = NULL;
412     PLItem *newItem = NULL;
413     i_items_to_append--;
414     if( b_need_update ) return;
415
416     PLItem *nodeItem = FindById( rootItem, p_add->i_node );
417     if( !nodeItem ) goto end;
418
419     p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
420     if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
421     if( i_depth == 1 && p_item->p_parent &&
422                         p_item->p_parent->i_id != rootItem->i_id )
423         goto end;
424
425     newItem = new PLItem( p_item, nodeItem, this );
426     nodeItem->appendChild( newItem );
427     UpdateTreeItem( p_item, newItem, true );
428 end:
429     return;
430 }
431
432 void PLModel::Rebuild()
433 {
434     /* Remove callbacks before locking to avoid deadlocks */
435     delCallbacks();
436     PL_LOCK;
437
438     /* Invalidate cache */
439     i_cached_id = i_cached_input_id = -1;
440
441     /* Clear the tree */
442     qDeleteAll( rootItem->children );
443
444     /* Recreate from root */
445     UpdateNodeChildren( rootItem );
446
447     /* And signal the view */
448     emit layoutChanged();
449
450     addCallbacks();
451     PL_UNLOCK;
452 }
453
454 void PLModel::UpdateNodeChildren( PLItem *root )
455 {
456     playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
457     UpdateNodeChildren( p_node, root );
458 }
459
460 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
461 {
462     for( int i = 0; i < p_node->i_children ; i++ )
463     {
464         PLItem *newItem =  new PLItem( p_node->pp_children[i], root, this );
465         root->appendChild( newItem, false );
466         UpdateTreeItem( newItem, false, true );
467         if( i_depth != 1 && p_node->pp_children[i]->i_children != -1 )
468             UpdateNodeChildren( p_node->pp_children[i], newItem );
469     }
470 }
471
472 void PLModel::UpdateTreeItem( PLItem *item, bool signal, bool force )
473 {
474     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
475     UpdateTreeItem( p_item, item, signal, force );
476 }
477
478 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item,
479                               bool signal, bool force )
480 {
481     if( !force && i_depth == 1 && p_item->p_parent &&
482                                  p_item->p_parent->i_id != rootItem->i_id )
483         return;
484     item->update( p_item );
485     if( signal )
486     {    /// \todo emit
487     }
488 }
489
490 /**********************************************************************
491  * Playlist callbacks
492  **********************************************************************/
493 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
494                             vlc_value_t oval, vlc_value_t nval, void *param )
495 {
496     PLModel *p_model = (PLModel *) param;
497     p_model->b_need_update = VLC_TRUE;
498     return VLC_SUCCESS;
499 }
500
501 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
502                          vlc_value_t oval, vlc_value_t nval, void *param )
503 {
504     PLModel *p_model = (PLModel *) param;
505     PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
506     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
507     event = new PLEvent( ItemUpdate_Type, nval.i_int );
508     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
509     return VLC_SUCCESS;
510 }
511
512 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
513                         vlc_value_t oval, vlc_value_t nval, void *param )
514 {
515     PLModel *p_model = (PLModel *) param;
516     PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
517     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
518     return VLC_SUCCESS;
519 }
520
521 static int ItemDeleted( 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     PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
526     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
527     return VLC_SUCCESS;
528 }
529
530 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
531                          vlc_value_t oval, vlc_value_t nval, void *param )
532 {
533     PLModel *p_model = (PLModel *) param;
534     playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
535     memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
536
537     if( ++p_model->i_items_to_append >= 50 )
538     {
539         p_model->b_need_update = VLC_TRUE;
540         return VLC_SUCCESS;
541     }
542     PLEvent *event = new PLEvent(  p_add );
543     QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
544     return VLC_SUCCESS;
545 }