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