1 /*****************************************************************************
2 * input_manager.cpp : Manage an input and interact with its GUI elements
3 ****************************************************************************
4 * Copyright (C) 2000-2005 the VideoLAN team
5 * $Id: wxwidgets.cpp 15731 2006-05-25 14:43:53Z zorglub $
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 ) )
258 return b_input ? p_cached_item_bi : p_cached_item;
260 if( !b_input && root->i_id == i_id )
265 else if( b_input && root->i_input_id == i_id )
267 ICACHE( i_id, root );
271 QList<PLItem *>::iterator it = root->children.begin();
272 while ( it != root->children.end() )
274 if( !b_input && (*it)->i_id == i_id )
276 CACHE( i_id, (*it) );
277 return p_cached_item;
279 else if( b_input && (*it)->i_input_id == i_id )
281 ICACHE( i_id, (*it) );
283 if( (*it)->children.size() )
285 PLItem *childFound = FindInner( (*it), i_id, b_input );
290 ICACHE( i_id, childFound );
294 CACHE( i_id, childFound );
307 /************************* Updates handling *****************************/
308 void PLModel::customEvent( QEvent *event )
310 PLEvent *ple = dynamic_cast<PLEvent *>(event);
312 if( event->type() == ItemUpdate_Type )
313 ProcessInputItemUpdate( ple->i_id );
314 else if( event->type() == ItemAppend_Type )
315 ProcessItemAppend( ple->p_add );
317 ProcessItemRemoval( ple->i_id );
320 /**** Events processing ****/
321 void PLModel::ProcessInputItemUpdate( int i_input_id )
323 if( i_input_id <= 0 ) return;
324 PLItem *item = FindByInput( rootItem, i_input_id );
325 fprintf( stderr, "Updating %i -> %p \n", i_input_id, item );
326 UpdateTreeItem( item, true );
329 void PLModel::ProcessItemRemoval( int i_id )
331 if( i_id <= 0 ) return;
332 if( i_id == i_cached_id ) i_cached_id = -1;
333 i_cached_input_id = -1;
338 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
340 playlist_item_t *p_item = NULL;
341 PLItem *newItem = NULL;
343 if( b_need_update ) return;
345 PLItem *nodeItem = FindById( rootItem, p_add->i_node );
346 if( !nodeItem ) goto end;
348 p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
349 if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
351 newItem = new PLItem( p_item, nodeItem, this );
352 nodeItem->appendChild( newItem );
353 UpdateTreeItem( p_item, newItem, true );
359 void PLModel::Rebuild()
361 /* Remove callbacks before locking to avoid deadlocks */
365 /* Invalidate cache */
366 i_cached_id = i_cached_input_id = -1;
369 qDeleteAll( rootItem->children );
371 /* Recreate from root */
372 UpdateNodeChildren( rootItem );
374 /* And signal the view */
375 emit layoutChanged();
381 void PLModel::UpdateNodeChildren( PLItem *root )
383 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, root->i_id );
384 UpdateNodeChildren( p_node, root );
387 void PLModel::UpdateNodeChildren( playlist_item_t *p_node, PLItem *root )
389 for( int i = 0; i < p_node->i_children ; i++ )
391 PLItem *newItem = new PLItem( p_node->pp_children[i], root, this );
392 fprintf( stderr, "New %p\n", newItem );
393 root->appendChild( newItem, false );
394 UpdateTreeItem( newItem, false );
395 if( p_node->pp_children[i]->i_children != -1 )
396 UpdateNodeChildren( p_node->pp_children[i], newItem );
400 void PLModel::UpdateTreeItem( PLItem *item, bool signal )
402 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, item->i_id );
403 UpdateTreeItem( p_item, item, signal );
406 void PLModel::UpdateTreeItem( playlist_item_t *p_item, PLItem *item, bool signal )
409 fprintf( stderr, "Updating item %s\n", p_item->p_input->psz_name );
410 item->update( p_item );
416 /**********************************************************************
418 **********************************************************************/
419 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
420 vlc_value_t oval, vlc_value_t nval, void *param )
422 PLModel *p_model = (PLModel *) param;
423 p_model->b_need_update = VLC_TRUE;
427 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
428 vlc_value_t oval, vlc_value_t nval, void *param )
430 PLModel *p_model = (PLModel *) param;
431 PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
432 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
433 event = new PLEvent( ItemUpdate_Type, nval.i_int );
434 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
438 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
439 vlc_value_t oval, vlc_value_t nval, void *param )
441 PLModel *p_model = (PLModel *) param;
442 PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
443 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
447 static int ItemDeleted( 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( ItemDelete_Type, nval.i_int );
452 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
456 static int ItemAppended( 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 playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
461 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
463 if( ++p_model->i_items_to_append >= 50 )
465 p_model->b_need_update = VLC_TRUE;
468 PLEvent *event = new PLEvent( p_add );
469 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );