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 *************************************************************************/
45 PLItem::PLItem( int _i_id, int _i_input_id, PLItem *parent, PLModel *m)
48 i_id = _i_id; i_input_id = _i_input_id;
52 PLItem::PLItem( playlist_item_t * p_item, PLItem *parent, PLModel *m )
55 i_input_id = p_item->p_input->i_id;
65 void PLItem::insertChild( PLItem *item, int i_pos )
68 model->beginInsertRows( model->index( this , 0 ), i_pos, i_pos );
69 children.append( item );
70 model->endInsertRows();
73 int PLItem::row() const
76 return parentItem->children.indexOf(const_cast<PLItem*>(this));
81 /*************************************************************************
82 * Playlist model implementation
83 *************************************************************************/
85 PLModel::PLModel( playlist_item_t * p_root, int i_depth, QObject *parent)
86 : QAbstractItemModel(parent)
88 rootItem = new PLItem( p_root, NULL, this );
90 i_items_to_append = 0;
91 b_need_update = false;
93 i_cached_input_id = -1;
95 /* Some global changes happened -> Rebuild all */
96 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
97 /* We went to the next item */
98 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
99 /* One item has been updated */
100 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
101 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
102 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
107 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
108 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
109 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
110 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
111 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
115 /****************** Base model mandatory implementations *****************/
116 QVariant PLModel::data(const QModelIndex &index, int role) const
118 if (!index.isValid())
120 if (role != Qt::DisplayRole)
123 PLItem *item = static_cast<PLItem*>(index.internalPointer());
124 return QVariant( item->columnString( index.column() ) );
127 Qt::ItemFlags PLModel::flags(const QModelIndex &index) const
129 if (!index.isValid())
130 return Qt::ItemIsEnabled;
132 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
135 QVariant PLModel::headerData( int section, Qt::Orientation orientation,
138 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
139 return QVariant( rootItem->columnString( section ) );
143 QModelIndex PLModel::index(int row, int column, const QModelIndex &parent)
147 if (!parent.isValid())
148 parentItem = rootItem;
150 parentItem = static_cast<PLItem*>(parent.internalPointer());
152 PLItem *childItem = parentItem->child(row);
154 return createIndex(row, column, childItem);
156 return QModelIndex();
159 /* Return the index of a given item */
160 QModelIndex PLModel::index( PLItem *item, int column ) const
162 if( !item ) return QModelIndex();
163 const PLItem *parent = item->parent();
165 return createIndex( parent->children.lastIndexOf( item ), column, item );
166 return QModelIndex();
169 QModelIndex PLModel::parent(const QModelIndex &index) const
171 if (!index.isValid())
172 return QModelIndex();
174 PLItem *childItem = static_cast<PLItem*>(index.internalPointer());
175 PLItem *parentItem = childItem->parent();
177 if (parentItem == rootItem)
178 return QModelIndex();
180 return createIndex(parentItem->row(), 0, parentItem);
183 int PLModel::childrenCount(const QModelIndex &parent) const
187 if (!parent.isValid())
188 parentItem = rootItem;
190 parentItem = static_cast<PLItem*>(parent.internalPointer());
192 return parentItem->childCount();
195 /************************* Lookups *****************************/
197 PLItem *PLModel::FindById( PLItem *root, int i_id )
199 return FindInner( root, i_id, false );
202 PLItem *PLModel::FindByInput( PLItem *root, int i_id )
204 return FindInner( root, i_id, true );
207 #define CACHE( i, p ) i_cached_id = i; p_cached_item = p;
208 #define ICACHE( i, p ) i_cached_input_id = i; p_cached_item_bi = p;
210 PLItem * PLModel::FindInner( PLItem *root, int i_id, bool b_input )
212 if( ( !b_input && i_cached_id == i_id) ||
213 ( b_input && i_cached_input_id ==i_id ) )
214 return b_input ? p_cached_item_bi : p_cached_item;
216 if( !b_input && root->i_id == i_id )
221 else if( b_input && root->i_input_id == i_id )
223 ICACHE( i_id, root );
227 QList<PLItem *>::iterator it = root->children.begin();
228 while ( it != root->children.end() )
230 if( !b_input && (*it)->i_id == i_id )
232 CACHE( i_id, (*it) );
233 return p_cached_item;
235 else if( b_input && (*it)->i_input_id == i_id )
237 ICACHE( i_id, (*it) );
239 if( (*it)->children.size() )
241 PLItem *childFound = FindInner( (*it), i_id, b_input );
246 ICACHE( i_id, childFound );
250 CACHE( i_id, childFound );
262 /************************* Updates handling *****************************/
263 void PLModel::customEvent( QEvent *event )
265 PLEvent *ple = dynamic_cast<PLEvent *>(event);
267 if( event->type() == ItemUpdate_Type )
268 ProcessInputItemUpdate( ple->i_id );
269 else if( event->type() == ItemAppend_Type )
270 ProcessItemAppend( ple->p_add );
272 ProcessItemRemoval( ple->i_id );
275 void PLModel::ProcessInputItemUpdate( int i_input_id )
277 assert( i_input_id >= 0 );
281 void PLModel::ProcessItemRemoval( int i_id )
284 if( i_id == i_cached_id ) i_cached_id = -1;
285 i_cached_input_id = -1;
290 void PLModel::ProcessItemAppend( playlist_add_t *p_add )
292 playlist_item_t *p_item = NULL;
294 if( b_need_update ) return;
296 PLItem *nodeItem = FindById( rootItem, p_add->i_node );
297 if( !nodeItem ) goto end;
299 p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
300 if( !p_item || p_item->i_flags & PLAYLIST_DBL_FLAG ) goto end;
302 nodeItem->appendChild( new PLItem( p_item, nodeItem, this ) );
309 /**********************************************************************
311 **********************************************************************/
312 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
313 vlc_value_t oval, vlc_value_t nval, void *param )
315 PLModel *p_model = (PLModel *) param;
316 p_model->b_need_update = VLC_TRUE;
320 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
321 vlc_value_t oval, vlc_value_t nval, void *param )
323 PLModel *p_model = (PLModel *) param;
324 PLEvent *event = new PLEvent( ItemUpdate_Type, oval.i_int );
325 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
326 event = new PLEvent( ItemUpdate_Type, nval.i_int );
327 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
331 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
332 vlc_value_t oval, vlc_value_t nval, void *param )
334 PLModel *p_model = (PLModel *) param;
335 PLEvent *event = new PLEvent( ItemUpdate_Type, nval.i_int );
336 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
340 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
341 vlc_value_t oval, vlc_value_t nval, void *param )
343 PLModel *p_model = (PLModel *) param;
344 PLEvent *event = new PLEvent( ItemDelete_Type, nval.i_int );
345 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );
349 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
350 vlc_value_t oval, vlc_value_t nval, void *param )
352 PLModel *p_model = (PLModel *) param;
353 playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t));
354 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
356 if( ++p_model->i_items_to_append >= 50 )
358 p_model->b_need_update = VLC_TRUE;
361 PLEvent *event = new PLEvent( p_add );
362 QApplication::postEvent( p_model, static_cast<QEvent*>(event) );