1 /*****************************************************************************
2 * input_manager.cpp : Manage an input and interact with its GUI elements
3 ****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
24 #include <QApplication>
25 #include "playlist_model.hpp"
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 );
41 /*************************************************************************
42 * Playlist item implementation
43 *************************************************************************/
52 void PLItem::init( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
55 i_id = _i_id; i_input_id = _i_input_id;
62 PLItem::PLItem( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
64 init( _i_id, _i_input_id, parent, m );
67 PLItem::PLItem( playlist_item_t * p_item, PLItem *parent, PLModel *m )
69 init( p_item->i_id, p_item->p_input->i_id, parent, m );
77 void PLItem::insertChild( PLItem *item, int i_pos, bool signal )
80 fprintf( stderr, "Inserting child \n" );
82 model->beginInsertRows( model->index( this , 0 ), i_pos, i_pos );
83 children.append( item );
85 model->endInsertRows();
88 int PLItem::row() const
91 return parentItem->children.indexOf(const_cast<PLItem*>(this));
95 void PLItem::update( playlist_item_t *p_item )
97 assert( p_item->p_input->i_id == i_input_id );
98 strings[0] = QString::fromUtf8( p_item->p_input->psz_name );
101 /*************************************************************************
102 * Playlist model implementation
103 *************************************************************************/
105 PLModel::PLModel( playlist_t *_p_playlist,
106 playlist_item_t * p_root, int i_depth, QObject *parent)
107 : QAbstractItemModel(parent)
110 rootItem = new PLItem( p_root, NULL, this );
111 fprintf( stderr, "%i -> %i, %i -> %i", p_root->i_id, rootItem->i_id, p_root->p_input->i_id, rootItem->i_input_id );
112 p_playlist= _p_playlist;
113 i_items_to_append = 0;
114 b_need_update = false;
116 i_cached_input_id = -1;
127 void PLModel::addCallbacks()
129 /* Some global changes happened -> Rebuild all */
130 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
131 /* We went to the next item */
132 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
133 /* One item has been updated */
134 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
135 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
136 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
139 void PLModel::delCallbacks()
141 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
142 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
143 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
144 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
145 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
148 /****************** Base model mandatory implementations *****************/
150 QVariant PLModel::data(const QModelIndex &index, int role) const
152 if (!index.isValid())
154 if (role != Qt::DisplayRole)
157 PLItem *item = static_cast<PLItem*>(index.internalPointer());
158 return QVariant( item->columnString( index.column() ) );
161 Qt::ItemFlags PLModel::flags(const QModelIndex &index) const
163 if (!index.isValid())
164 return Qt::ItemIsEnabled;
166 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
169 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
172 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
173 return QVariant( rootItem->columnString( section ) );
177 QModelIndex PLModel::index(int row, int column, const QModelIndex &parent)
181 if (!parent.isValid())
182 parentItem = rootItem;
184 parentItem = static_cast<PLItem*>(parent.internalPointer());
186 PLItem *childItem = parentItem->child(row);
188 return createIndex(row, column, childItem);
190 return QModelIndex();
193 /* Return the index of a given item */
194 QModelIndex PLModel::index( PLItem *item, int column ) const
196 if( !item ) return QModelIndex();
197 const PLItem *parent = item->parent();
199 return createIndex( parent->children.lastIndexOf( item ), column, item );
200 return QModelIndex();
203 QModelIndex PLModel::parent(const QModelIndex &index) const
205 if (!index.isValid())
206 return QModelIndex();
208 PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
209 PLItem *parentItem = childItem->parent();
211 if (parentItem == rootItem)
212 return QModelIndex();
214 return createIndex(parentItem->row(), 0, parentItem);
217 int PLModel::columnCount( const QModelIndex &i) const
222 int PLModel::childrenCount(const QModelIndex &parent) const
224 return rowCount( parent );
227 int PLModel::rowCount(const QModelIndex &parent) const
231 if (!parent.isValid())
232 parentItem = rootItem;
234 parentItem = static_cast<PLItem*>(parent.internalPointer());
236 return parentItem->childCount();
239 /************************* Lookups *****************************/
241 PLItem *PLModel::FindById( PLItem *root, int i_id )
243 return FindInner( root, i_id, false );
246 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
248 return FindInner( root, i_id, true );
251 #define CACHE( i, p ) i_cached_id = i; p_cached_item = p;
252 #define ICACHE( i, p ) i_cached_input_id = i; p_cached_item_bi = p;
254 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
256 if( ( !b_input && i_cached_id == i_id) ||
257 ( b_input && i_cached_input_id ==i_id ) )
259 return b_input ? p_cached_item_bi : p_cached_item;
262 if( !b_input && root->i_id == i_id )
267 else if( b_input && root->i_input_id == i_id )
269 ICACHE( i_id, root );
273 QList<PLItem *>::iterator it = root->children.begin();
274 while ( it != root->children.end() )
276 if( !b_input && (*it)->i_id == i_id )
278 CACHE( i_id, (*it) );
279 return p_cached_item;
281 else if( b_input && (*it)->i_input_id == i_id )
283 ICACHE( i_id, (*it) );
284 return p_cached_item_bi;
286 if( (*it)->children.size() )
288 PLItem *childFound = FindInner( (*it), i_id, b_input );
293 ICACHE( i_id, childFound );
297 CACHE( i_id, childFound );
304 fprintf( stderr, "Never found" );
311 /************************* Updates handling *****************************/
312 void PLModel::customEvent( QEvent *event )
314 PLEvent *ple = dynamic_cast<PLEvent *>(event);
316 if( event->type() == ItemUpdate_Type )
317 ProcessInputItemUpdate( ple->i_id );
318 else if( event->type() == ItemAppend_Type )
319 ProcessItemAppend( ple->p_add );
321 ProcessItemRemoval( ple->i_id );
324 /**** Events processing ****/
325 void PLModel::ProcessInputItemUpdate( int i_input_id )
327 if( i_input_id <= 0 ) return;
328 PLItem *item = FindByInput( rootItem, i_input_id );
329 fprintf( stderr, "Updating %i -> %p \n", i_input_id, item );
330 UpdateTreeItem( item, true );
333 void PLModel::ProcessItemRemoval( int i_id )
335 if( i_id <= 0 ) return;
336 if( i_id == i_cached_id ) i_cached_id = -1;
337 i_cached_input_id = -1;
342 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
344 playlist_item_t *p_item = NULL;
345 PLItem *newItem = NULL;
347 if( b_need_update ) return;
349 PLItem *nodeItem = FindById( rootItem, p_add->i_node );
350 if( !nodeItem ) goto end;
352 p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
353 if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
355 newItem = new PLItem( p_item, nodeItem, this );
356 nodeItem->appendChild( newItem );
357 UpdateTreeItem( p_item, newItem, true );
363 void PLModel::Rebuild()
365 /* Remove callbacks before locking to avoid deadlocks */
369 /* Invalidate cache */
370 i_cached_id = i_cached_input_id = -1;
373 qDeleteAll( rootItem->children );
375 /* Recreate from root */
376 UpdateNodeChildren( rootItem );
378 /* And signal the view */
379 emit layoutChanged();
385 void PLModel::UpdateNodeChildren( PLItem *root )
387 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
388 UpdateNodeChildren( p_node, root );
391 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
393 for( int i = 0; i < p_node->i_children ; i++ )
395 PLItem *newItem = new PLItem( p_node->pp_children[i], root, this );
396 fprintf( stderr, "New %p\n", newItem );
397 root->appendChild( newItem, false );
398 UpdateTreeItem( newItem, false );
399 if( p_node->pp_children[i]->i_children != -1 )
400 UpdateNodeChildren( p_node->pp_children[i], newItem );
404 void PLModel::UpdateTreeItem( PLItem *item, bool signal )
406 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
407 UpdateTreeItem( p_item, item, signal );
410 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item, bool signal )
413 fprintf( stderr, "Updating item %s\n", p_item->p_input->psz_name );
414 item->update( p_item );
420 /**********************************************************************
422 **********************************************************************/
423 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
424 vlc_value_t oval, vlc_value_t nval, void *param )
426 PLModel *p_model = (PLModel *) param;
427 p_model->b_need_update = VLC_TRUE;
431 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
432 vlc_value_t oval, vlc_value_t nval, void *param )
434 PLModel *p_model = (PLModel *) param;
435 PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
436 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
437 event = new PLEvent( ItemUpdate_Type, nval.i_int );
438 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
442 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
443 vlc_value_t oval, vlc_value_t nval, void *param )
445 PLModel *p_model = (PLModel *) param;
446 PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
447 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
451 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
452 vlc_value_t oval, vlc_value_t nval, void *param )
454 PLModel *p_model = (PLModel *) param;
455 PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
456 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
460 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
461 vlc_value_t oval, vlc_value_t nval, void *param )
463 PLModel *p_model = (PLModel *) param;
464 playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
465 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
467 if( ++p_model->i_items_to_append >= 50 )
469 p_model->b_need_update = VLC_TRUE;
472 PLEvent *event = new PLEvent( p_add );
473 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );