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 int type = event->type();
315 if( type != ItemUpdate_Type && type != ItemAppend_Type &&
316 type != ItemDelete_Type )
319 PLEvent *ple = static_cast<PLEvent *>(event);
321 if( type == ItemUpdate_Type )
322 ProcessInputItemUpdate( ple->i_id );
323 else if( type == ItemAppend_Type )
324 ProcessItemAppend( ple->p_add );
326 ProcessItemRemoval( ple->i_id );
329 /**** Events processing ****/
330 void PLModel::ProcessInputItemUpdate( int i_input_id )
332 if( i_input_id <= 0 ) return;
333 PLItem *item = FindByInput( rootItem, i_input_id );
334 fprintf( stderr, "Updating %i -> %p \n", i_input_id, item );
335 UpdateTreeItem( item, true );
338 void PLModel::ProcessItemRemoval( int i_id )
340 if( i_id <= 0 ) return;
341 if( i_id == i_cached_id ) i_cached_id = -1;
342 i_cached_input_id = -1;
347 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
349 playlist_item_t *p_item = NULL;
350 PLItem *newItem = NULL;
352 if( b_need_update ) return;
354 PLItem *nodeItem = FindById( rootItem, p_add->i_node );
355 if( !nodeItem ) goto end;
357 p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
358 if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
360 newItem = new PLItem( p_item, nodeItem, this );
361 nodeItem->appendChild( newItem );
362 UpdateTreeItem( p_item, newItem, true );
368 void PLModel::Rebuild()
370 /* Remove callbacks before locking to avoid deadlocks */
374 /* Invalidate cache */
375 i_cached_id = i_cached_input_id = -1;
378 qDeleteAll( rootItem->children );
380 /* Recreate from root */
381 UpdateNodeChildren( rootItem );
383 /* And signal the view */
384 emit layoutChanged();
390 void PLModel::UpdateNodeChildren( PLItem *root )
392 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
393 UpdateNodeChildren( p_node, root );
396 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
398 for( int i = 0; i < p_node->i_children ; i++ )
400 PLItem *newItem = new PLItem( p_node->pp_children[i], root, this );
401 fprintf( stderr, "New %p\n", newItem );
402 root->appendChild( newItem, false );
403 UpdateTreeItem( newItem, false );
404 if( p_node->pp_children[i]->i_children != -1 )
405 UpdateNodeChildren( p_node->pp_children[i], newItem );
409 void PLModel::UpdateTreeItem( PLItem *item, bool signal )
411 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
412 UpdateTreeItem( p_item, item, signal );
415 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item, bool signal )
418 fprintf( stderr, "Updating item %s\n", p_item->p_input->psz_name );
419 item->update( p_item );
425 /**********************************************************************
427 **********************************************************************/
428 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
429 vlc_value_t oval, vlc_value_t nval, void *param )
431 PLModel *p_model = (PLModel *) param;
432 p_model->b_need_update = VLC_TRUE;
436 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
437 vlc_value_t oval, vlc_value_t nval, void *param )
439 PLModel *p_model = (PLModel *) param;
440 PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
441 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
442 event = new PLEvent( ItemUpdate_Type, nval.i_int );
443 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
447 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
448 vlc_value_t oval, vlc_value_t nval, void *param )
450 PLModel *p_model = (PLModel *) param;
451 PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
452 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
456 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
457 vlc_value_t oval, vlc_value_t nval, void *param )
459 PLModel *p_model = (PLModel *) param;
460 PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
461 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
465 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
466 vlc_value_t oval, vlc_value_t nval, void *param )
468 PLModel *p_model = (PLModel *) param;
469 playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
470 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
472 if( ++p_model->i_items_to_append >= 50 )
474 p_model->b_need_update = VLC_TRUE;
477 PLEvent *event = new PLEvent( p_add );
478 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );