]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Qt: move animators code
[vlc] / modules / gui / qt4 / components / playlist / standardpanel.cpp
1 /*****************************************************************************
2  * standardpanel.cpp : The "standard" playlist panel : just a treeview
3  ****************************************************************************
4  * Copyright © 2000-2010 VideoLAN
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "components/playlist/standardpanel.hpp"
30
31 #include "components/playlist/vlc_model.hpp"      /* VLCModel */
32 #include "components/playlist/playlist_model.hpp" /* PLModel */
33 #include "components/playlist/ml_model.hpp"       /* MLModel */
34 #include "components/playlist/views.hpp"          /* 3 views */
35 #include "components/playlist/selector.hpp"       /* PLSelector */
36 #include "util/animators.hpp"                     /* PixmapAnimator */
37 #include "menus.hpp"                              /* Popup */
38 #include "input_manager.hpp"                      /* THEMIM */
39 #include "dialogs_provider.hpp"                   /* THEDP */
40 #include "recents.hpp"                            /* RecentMRL */
41 #include "dialogs/playlist.hpp"                   /* Playlist Dialog */
42 #include "dialogs/mediainfo.hpp"                  /* MediaInfoDialog */
43 #include "util/qt_dirs.hpp"
44
45 #include <vlc_services_discovery.h>               /* SD_CMD_SEARCH */
46 #include <vlc_intf_strings.h>                     /* POP_ */
47
48 #define I_NEW_DIR \
49     I_DIR_OR_FOLDER( N_("Create Directory"), N_( "Create Folder" ) )
50 #define I_NEW_DIR_NAME \
51     I_DIR_OR_FOLDER( N_( "Enter name for new directory:" ), \
52                      N_( "Enter name for new folder:" ) )
53
54 #define I_RENAME_DIR \
55     I_DIR_OR_FOLDER( N_("Rename Directory"), N_( "Rename Folder" ) )
56 #define I_RENAME_DIR_NAME \
57     I_DIR_OR_FOLDER( N_( "Enter a new name for the directory:" ), \
58                      N_( "Enter a new name for the folder:" ) )
59
60 #include <QHeaderView>
61 #include <QMenu>
62 #include <QKeyEvent>
63 #include <QWheelEvent>
64 #include <QStackedLayout>
65 #include <QSignalMapper>
66 #include <QSettings>
67 #include <QStylePainter>
68 #include <QInputDialog>
69 #include <QDesktopServices>
70 #include <QUrl>
71 #include <QFont>
72
73 #include <assert.h>
74
75 /* local helper */
76 inline QModelIndex popupIndex( QAbstractItemView *view );
77
78 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
79                                   intf_thread_t *_p_intf,
80                                   playlist_item_t *p_root,
81                                   PLSelector *_p_selector,
82                                   VLCProxyModel *_p_model )
83                 : QWidget( _parent ),
84                   model( _p_model ),
85                   p_intf( _p_intf ),
86                   p_selector( _p_selector )
87 {
88     viewStack = new QStackedLayout( this );
89     viewStack->setSpacing( 0 ); viewStack->setMargin( 0 );
90     setMinimumWidth( 300 );
91
92     iconView    = NULL;
93     treeView    = NULL;
94     listView    = NULL;
95     picFlowView = NULL;
96
97     currentRootIndexPLId  = -1;
98     lastActivatedPLItemId     = -1;
99
100     QList<QString> frames;
101     frames << ":/util/wait1";
102     frames << ":/util/wait2";
103     frames << ":/util/wait3";
104     frames << ":/util/wait4";
105     spinnerAnimation = new PixmapAnimator( this, frames );
106     CONNECT( spinnerAnimation, pixmapReady( const QPixmap & ), this, updateViewport() );
107
108     /* Saved Settings */
109     int i_savedViewMode = getSettings()->value( "Playlist/view-mode", TREE_VIEW ).toInt();
110     i_zoom = getSettings()->value( "Playlist/zoom", 0 ).toInt();
111
112     showView( i_savedViewMode );
113
114     DCONNECT( THEMIM, leafBecameParent( int ),
115               this, browseInto( int ) );
116
117     CONNECT( model->sigs, currentIndexChanged( const QModelIndex& ),
118              this, handleExpansion( const QModelIndex& ) );
119     CONNECT( model->sigs, rootIndexChanged(), this, browseInto() );
120
121     setRootItem( p_root, false );
122 }
123
124 StandardPLPanel::~StandardPLPanel()
125 {
126     getSettings()->beginGroup("Playlist");
127     if( treeView )
128         getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
129     getSettings()->setValue( "view-mode", currentViewIndex() );
130     getSettings()->setValue( "zoom", i_zoom );
131     getSettings()->endGroup();
132 }
133
134 /* Unused anymore, but might be useful, like in right-click menu */
135 void StandardPLPanel::gotoPlayingItem()
136 {
137     currentView->scrollTo( model->currentIndex() );
138 }
139
140 void StandardPLPanel::handleExpansion( const QModelIndex& index )
141 {
142     assert( currentView );
143     if( currentRootIndexPLId != -1 && currentRootIndexPLId != model->itemId( index.parent(), PLAYLIST_ID ) )
144         browseInto( index.parent() );
145     currentView->scrollTo( index );
146 }
147
148 void StandardPLPanel::popupPlView( const QPoint &point )
149 {
150     QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
151     QModelIndex index = currentView->indexAt( point );
152     if ( !index.isValid() )
153     {
154         currentView->clearSelection();
155     }
156     else if ( ! currentView->selectionModel()->selectedIndexes().contains( index ) )
157     {
158         currentView->selectionModel()->select( index, QItemSelectionModel::Select );
159     }
160
161     if( !popup( globalPoint ) ) VLCMenuBar::PopupMenu( p_intf, true );
162 }
163
164 /*********** Popup *********/
165 bool StandardPLPanel::popup( const QPoint &point )
166 {
167     QModelIndex index = popupIndex( currentView ); /* index for menu logic only. Do not store.*/
168     VLCProxyModel *model = qobject_cast<VLCProxyModel *>(currentView->model());
169
170 #define ADD_MENU_ENTRY( icon, title, act ) \
171     if ( model->isSupportedAction( act, index ) )\
172     {\
173     action = menu.addAction( icon, title ); \
174     container.action = act; \
175     action->setData( QVariant::fromValue( container ) );\
176     }
177
178     /* */
179     QMenu menu;
180     QAction *action;
181     VLCModelSubInterface::actionsContainerType container;
182
183     /* Play/Stream/Info static actions */
184
185     ADD_MENU_ENTRY( QIcon( ":/menu/play" ), qtr(I_POP_PLAY),
186                     VLCModelSubInterface::ACTION_PLAY )
187
188     ADD_MENU_ENTRY( QIcon( ":/menu/stream" ), qtr(I_POP_STREAM),
189                     VLCModelSubInterface::ACTION_STREAM )
190
191     ADD_MENU_ENTRY( QIcon(), qtr(I_POP_SAVE),
192                     VLCModelSubInterface::ACTION_SAVE );
193
194     ADD_MENU_ENTRY( QIcon( ":/menu/info" ), qtr(I_POP_INFO),
195                     VLCModelSubInterface::ACTION_INFO );
196
197     menu.addSeparator();
198
199     ADD_MENU_ENTRY( QIcon( ":/type/folder-grey" ), qtr(I_POP_EXPLORE),
200                     VLCModelSubInterface::ACTION_EXPLORE );
201
202     QIcon addIcon( ":/buttons/playlist/playlist_add" );
203
204     ADD_MENU_ENTRY( addIcon, qtr(I_POP_NEWFOLDER),
205                     VLCModelSubInterface::ACTION_CREATENODE )
206
207     ADD_MENU_ENTRY( QIcon(), qtr(I_POP_RENAMEFOLDER),
208                     VLCModelSubInterface::ACTION_RENAMENODE )
209
210     menu.addSeparator();
211     /* In PL or ML, allow to add a file/folder */
212     ADD_MENU_ENTRY( addIcon, qtr(I_PL_ADDF),
213                     VLCModelSubInterface::ACTION_ENQUEUEFILE )
214
215     ADD_MENU_ENTRY( addIcon, qtr(I_PL_ADDDIR),
216                     VLCModelSubInterface::ACTION_ENQUEUEDIR )
217
218     ADD_MENU_ENTRY( addIcon, qtr(I_OP_ADVOP),
219                     VLCModelSubInterface::ACTION_ENQUEUEGENERIC )
220
221     ADD_MENU_ENTRY( QIcon(), qtr(I_PL_ADDPL),
222                     VLCModelSubInterface::ACTION_ADDTOPLAYLIST );
223
224     menu.addSeparator();
225     ADD_MENU_ENTRY( QIcon(), qtr( I_PL_SAVE ),
226                     VLCModelSubInterface::ACTION_SAVETOPLAYLIST );
227
228     menu.addSeparator();
229
230     /* Item removal */
231
232     ADD_MENU_ENTRY( QIcon( ":/buttons/playlist/playlist_remove" ), qtr(I_POP_DEL),
233                     VLCModelSubInterface::ACTION_REMOVE );
234
235     ADD_MENU_ENTRY( QIcon( ":/toolbar/clear" ), qtr("Clear the playlist"),
236                     VLCModelSubInterface::ACTION_CLEAR );
237
238     menu.addSeparator();
239
240     /* Playlist sorting */
241     if ( model->isSupportedAction( VLCModelSubInterface::ACTION_SORT, index ) )
242     {
243         QMenu *sortingMenu = new QMenu( qtr( "Sort by" ) );
244         /* Choose what columns to show in sorting menu, not sure if this should be configurable*/
245         QList<int> sortingColumns;
246         sortingColumns << COLUMN_TITLE << COLUMN_ARTIST << COLUMN_ALBUM << COLUMN_TRACK_NUMBER << COLUMN_URI;
247         container.action = VLCModelSubInterface::ACTION_SORT;
248         foreach( int Column, sortingColumns )
249         {
250             action = sortingMenu->addAction( qfu( psz_column_title( Column ) ) + " " + qtr("Ascending") );
251             container.column = model->columnFromMeta(Column) + 1;
252             action->setData( QVariant::fromValue( container ) );
253
254             action = sortingMenu->addAction( qfu( psz_column_title( Column ) ) + " " + qtr("Descending") );
255             container.column = -1 * (model->columnFromMeta(Column)+1);
256             action->setData( QVariant::fromValue( container ) );
257         }
258         menu.addMenu( sortingMenu );
259     }
260
261     /* Zoom */
262     QMenu *zoomMenu = new QMenu( qtr( "Display size" ) );
263     zoomMenu->addAction( qtr( "Increase" ), this, SLOT( increaseZoom() ) );
264     zoomMenu->addAction( qtr( "Decrease" ), this, SLOT( decreaseZoom() ) );
265     menu.addMenu( zoomMenu );
266
267     CONNECT( &menu, triggered( QAction * ), this, popupAction( QAction * ) );
268
269     menu.addMenu( StandardPLPanel::viewSelectionMenu( this ) );
270
271     /* Display and forward the result */
272     if( !menu.isEmpty() )
273     {
274         menu.exec( point ); return true;
275     }
276     else return false;
277
278 #undef ADD_MENU_ENTRY
279 }
280
281 void StandardPLPanel::popupAction( QAction *action )
282 {
283     VLCProxyModel *model = qobject_cast<VLCProxyModel *>(currentView->model());
284     VLCModelSubInterface::actionsContainerType a =
285             action->data().value<VLCModelSubInterface::actionsContainerType>();
286     QModelIndexList list = currentView->selectionModel()->selectedRows();
287     QModelIndex index = popupIndex( currentView );
288     char *path = NULL;
289     OpenDialog *dialog;
290     QString temp;
291     QStringList uris;
292     bool ok;
293
294     /* first try to complete actions requiring missing parameters thru UI dialogs */
295     switch( a.action )
296     {
297     case VLCModelSubInterface::ACTION_INFO:
298         /* locally handled only */
299         if( index.isValid() )
300         {
301             input_item_t* p_input = model->getInputItem( index );
302             MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_input );
303             mid->setParent( PlaylistDialog::getInstance( p_intf ),
304                             Qt::Dialog );
305             mid->show();
306         }
307         break;
308
309     case VLCModelSubInterface::ACTION_EXPLORE:
310         /* locally handled only */
311         temp = model->getURI( index );
312         if( ! temp.isEmpty() ) path = make_path( temp.toLatin1().constData() );
313         if( path == NULL ) return;
314         QDesktopServices::openUrl(
315                     QUrl::fromLocalFile( QFileInfo( qfu( path ) ).absolutePath() ) );
316         free( path );
317         break;
318
319     case VLCModelSubInterface::ACTION_STREAM:
320         /* locally handled only */
321         temp = model->getURI( index );
322         if ( ! temp.isEmpty() )
323             THEDP->streamingDialog( NULL, temp, false );
324         break;
325
326     case VLCModelSubInterface::ACTION_SAVE:
327         /* locally handled only */
328         temp = model->getURI( index );
329         if ( ! temp.isEmpty() )
330             THEDP->streamingDialog( NULL, temp );
331         break;
332
333     case VLCModelSubInterface::ACTION_CREATENODE:
334         temp = QInputDialog::getText( PlaylistDialog::getInstance( p_intf ),
335             qtr( I_NEW_DIR ), qtr( I_NEW_DIR_NAME ),
336             QLineEdit::Normal, QString(), &ok);
337         if ( !ok ) return;
338         model->createNode( index, temp );
339         break;
340
341     case VLCModelSubInterface::ACTION_RENAMENODE:
342         temp = QInputDialog::getText( PlaylistDialog::getInstance( p_intf ),
343             qtr( I_RENAME_DIR ), qtr( I_RENAME_DIR_NAME ),
344             QLineEdit::Normal, model->getTitle( index ), &ok);
345         if ( !ok ) return;
346         model->renameNode( index, temp );
347         break;
348
349     case VLCModelSubInterface::ACTION_ENQUEUEFILE:
350         uris = THEDP->showSimpleOpen();
351         if ( uris.isEmpty() ) return;
352         uris.sort();
353         foreach( const QString &file, uris )
354             a.uris << qtu( toURI( toNativeSeparators( file ) ) );
355         action->setData( QVariant::fromValue( a ) );
356         if ( model->action( action, list ) )
357             foreach( const QString &file, a.uris )
358                 RecentsMRL::getInstance( p_intf )->addRecent( file );
359         break;
360
361     case VLCModelSubInterface::ACTION_ENQUEUEDIR:
362         temp = THEDP->getDirectoryDialog();
363         if ( temp.isEmpty() ) return;
364         a.uris << temp;
365         action->setData( QVariant::fromValue( a ) );
366         model->action( action, list );
367         break;
368
369     case VLCModelSubInterface::ACTION_ENQUEUEGENERIC:
370         dialog = OpenDialog::getInstance( this, p_intf, false, SELECT, true, true );
371         dialog->showTab( OPEN_FILE_TAB );
372         dialog->exec(); /* make it modal */
373         a.uris = dialog->getMRLs( false );
374         a.options = dialog->getOptions();
375         if ( a.uris.isEmpty() ) return;
376         action->setData( QVariant::fromValue( a ) );
377         if ( model->action( action, list ) )
378             foreach( const QString &file, a.uris )
379                 RecentsMRL::getInstance( p_intf )->addRecent( file );
380         break;
381
382     case VLCModelSubInterface::ACTION_SAVETOPLAYLIST:
383         THEDP->savePlayingToPlaylist();
384         break;
385     default:
386         model->action( action, list );
387     }
388 }
389
390 QMenu* StandardPLPanel::viewSelectionMenu( StandardPLPanel *panel )
391 {
392     QMenu *viewMenu = new QMenu( qtr( "Playlist View Mode" ) );
393     QSignalMapper *viewSelectionMapper = new QSignalMapper( viewMenu );
394     CONNECT( viewSelectionMapper, mapped( int ), panel, showView( int ) );
395
396     QActionGroup *viewGroup = new QActionGroup( viewMenu );
397 # define MAX_VIEW StandardPLPanel::VIEW_COUNT
398     for( int i = 0; i < MAX_VIEW; i++ )
399     {
400         QAction *action = viewMenu->addAction( viewNames[i] );
401         action->setCheckable( true );
402         viewGroup->addAction( action );
403         viewSelectionMapper->setMapping( action, i );
404         CONNECT( action, triggered(), viewSelectionMapper, map() );
405         if( panel->currentViewIndex() == i )
406             action->setChecked( true );
407     }
408     return viewMenu;
409 }
410
411 inline QModelIndex popupIndex( QAbstractItemView *view )
412 {
413     QModelIndexList list = view->selectionModel()->selectedIndexes();
414     if ( list.isEmpty() )
415         return QModelIndex();
416     else
417         return list.first();
418 }
419
420 void StandardPLPanel::popupSelectColumn( QPoint )
421 {
422     QMenu menu;
423     assert( treeView );
424
425     /* We do not offer the option to hide index 0 column, or
426      * QTreeView will behave weird */
427     for( int i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
428     {
429         QAction* option = menu.addAction( qfu( psz_column_title( i ) ) );
430         option->setCheckable( true );
431         option->setChecked( !treeView->isColumnHidden( j ) );
432         selectColumnsSigMapper->setMapping( option, j );
433         CONNECT( option, triggered(), selectColumnsSigMapper, map() );
434     }
435     menu.exec( QCursor::pos() );
436 }
437
438 void StandardPLPanel::toggleColumnShown( int i )
439 {
440     treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
441 }
442
443 /* Search in the playlist */
444 void StandardPLPanel::search( const QString& searchText )
445 {
446     int type;
447     QString name;
448     bool can_search;
449     p_selector->getCurrentItemInfos( &type, &can_search, &name );
450
451     if( type != SD_TYPE || !can_search )
452     {
453         bool flat = ( currentView == iconView ||
454                       currentView == listView ||
455                       currentView == picFlowView );
456         model->filter( searchText,
457                        flat ? currentView->rootIndex() : QModelIndex(),
458                        !flat );
459     }
460 }
461
462 void StandardPLPanel::searchDelayed( const QString& searchText )
463 {
464     int type;
465     QString name;
466     bool can_search;
467     p_selector->getCurrentItemInfos( &type, &can_search, &name );
468
469     if( type == SD_TYPE && can_search )
470     {
471         if( !name.isEmpty() && !searchText.isEmpty() )
472             playlist_ServicesDiscoveryControl( THEPL, qtu( name ), SD_CMD_SEARCH,
473                                               qtu( searchText ) );
474     }
475 }
476
477 /* Set the root of the new Playlist */
478 /* This activated by the selector selection */
479 void StandardPLPanel::setRootItem( playlist_item_t *p_item, bool b )
480 {
481 #ifdef SQL_MEDIA_LIBRARY
482     if( b )
483     {
484         msg_Dbg( p_intf, "Setting the SQL ML" );
485         if ( model->switchToModel( VLCProxyModel::SQLML_MODEL ) )
486             currentView->setModel( model );
487     }
488     else
489 #else
490     Q_UNUSED( b );
491 #endif
492     {
493         if ( model->switchToModel( VLCProxyModel::PL_MODEL ) )
494             model->rebuild( p_item );
495     }
496 }
497
498 void StandardPLPanel::browseInto( const QModelIndex &index )
499 {
500     if( currentView == iconView || currentView == listView || currentView == picFlowView )
501     {
502
503         currentView->setRootIndex( index );
504
505         /* When going toward root in LocationBar, scroll to the item
506            that was previously as root */
507         QModelIndex newIndex = model->indexByPLID(currentRootIndexPLId,0);
508         while( newIndex.isValid() && (newIndex.parent() != index) )
509             newIndex = newIndex.parent();
510         if( newIndex.isValid() )
511             currentView->scrollTo( newIndex );
512
513         /* Store new rootindexid*/
514         currentRootIndexPLId = model->itemId( index, PLAYLIST_ID );
515
516         model->ensureArtRequested( index );
517     }
518
519     emit viewChanged( index );
520 }
521
522 void StandardPLPanel::browseInto()
523 {
524     browseInto( (currentRootIndexPLId != -1 && currentView != treeView) ?
525                  model->indexByPLID( currentRootIndexPLId, 0 ) :
526                  QModelIndex() );
527 }
528
529 void StandardPLPanel::wheelEvent( QWheelEvent *e )
530 {
531     if( e->modifiers() & Qt::ControlModifier ) {
532         int numSteps = e->delta() / 8 / 15;
533         if( numSteps > 0)
534             increaseZoom();
535         else if( numSteps < 0)
536             decreaseZoom();
537     }
538     // Accept this event in order to prevent unwanted volume up/down changes
539     e->accept();
540 }
541
542 bool StandardPLPanel::eventFilter ( QObject *obj, QEvent * event )
543 {
544     if (event->type() == QEvent::KeyPress)
545     {
546         QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
547         if( keyEvent->key() == Qt::Key_Delete ||
548             keyEvent->key() == Qt::Key_Backspace )
549         {
550             deleteSelection();
551             return true;
552         }
553     }
554     else if ( event->type() == QEvent::Paint )
555     {/* Warn! Don't filter events from anything else than views ! */
556         if ( model->rowCount() == 0 && p_selector->getCurrentItemCategory() == PL_ITEM_TYPE )
557         {
558             QWidget *viewport = qobject_cast<QWidget *>( obj );
559             QStylePainter painter( viewport );
560             QPixmap dropzone(":/dropzone");
561             QRect rect = viewport->geometry();
562             QSize size = rect.size() / 2 - dropzone.size() / 2;
563             rect.adjust( 0, size.height(), 0 , 0 );
564             painter.drawItemPixmap( rect, Qt::AlignHCenter, dropzone );
565             /* now select the zone just below the drop zone and let Qt center
566                the text by itself */
567             rect.adjust( 0, dropzone.size().height() + 10, 0, 0 );
568             rect.setRight( viewport->geometry().width() );
569             rect.setLeft( 0 );
570             painter.drawItemText( rect,
571                                   Qt::AlignHCenter,
572                                   palette(),
573                                   true,
574                                   qtr("Playlist is currently empty.\n"
575                                       "Drop a file here or select a "
576                                       "media source from the left."),
577                                   QPalette::Text );
578         }
579         else if ( spinnerAnimation->state() == PixmapAnimator::Running )
580         {
581             if ( currentView->model()->rowCount() )
582                 spinnerAnimation->stop(); /* Trick until SD emits events */
583             else
584             {
585                 QWidget *viewport = qobject_cast<QWidget *>( obj );
586                 QStylePainter painter( viewport );
587                 QPixmap *spinner = spinnerAnimation->getPixmap();
588                 QPoint point = viewport->geometry().center();
589                 point -= QPoint( spinner->size().width() / 2, spinner->size().height() / 2 );
590                 painter.drawPixmap( point, *spinner );
591             }
592         }
593     }
594     return false;
595 }
596
597 void StandardPLPanel::deleteSelection()
598 {
599     QModelIndexList list = currentView->selectionModel()->selectedIndexes();
600     model->doDelete( list );
601 }
602
603 void StandardPLPanel::createIconView()
604 {
605     iconView = new PlIconView( model, this );
606     iconView->setContextMenuPolicy( Qt::CustomContextMenu );
607     CONNECT( iconView, customContextMenuRequested( const QPoint & ),
608              this, popupPlView( const QPoint & ) );
609     CONNECT( iconView, activated( const QModelIndex & ),
610              this, activate( const QModelIndex & ) );
611     iconView->installEventFilter( this );
612     iconView->viewport()->installEventFilter( this );
613     viewStack->addWidget( iconView );
614 }
615
616 void StandardPLPanel::createListView()
617 {
618     listView = new PlListView( model, this );
619     listView->setContextMenuPolicy( Qt::CustomContextMenu );
620     CONNECT( listView, customContextMenuRequested( const QPoint & ),
621              this, popupPlView( const QPoint & ) );
622     CONNECT( listView, activated( const QModelIndex & ),
623              this, activate( const QModelIndex & ) );
624     listView->installEventFilter( this );
625     listView->viewport()->installEventFilter( this );
626     viewStack->addWidget( listView );
627 }
628
629 void StandardPLPanel::createCoverView()
630 {
631     picFlowView = new PicFlowView( model, this );
632     picFlowView->setContextMenuPolicy( Qt::CustomContextMenu );
633     CONNECT( picFlowView, customContextMenuRequested( const QPoint & ),
634              this, popupPlView( const QPoint & ) );
635     CONNECT( picFlowView, activated( const QModelIndex & ),
636              this, activate( const QModelIndex & ) );
637     viewStack->addWidget( picFlowView );
638     picFlowView->installEventFilter( this );
639 }
640
641 void StandardPLPanel::createTreeView()
642 {
643     /* Create and configure the QTreeView */
644     treeView = new PlTreeView( model, this );
645
646     /* setModel after setSortingEnabled(true), or the model will sort immediately! */
647
648     /* Connections for the TreeView */
649     CONNECT( treeView, activated( const QModelIndex& ),
650              this, activate( const QModelIndex& ) );
651     CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
652              this, popupSelectColumn( QPoint ) );
653     CONNECT( treeView, customContextMenuRequested( const QPoint & ),
654              this, popupPlView( const QPoint & ) );
655     treeView->installEventFilter( this );
656     treeView->viewport()->installEventFilter( this );
657
658     /* SignalMapper for columns */
659     selectColumnsSigMapper = new QSignalMapper( this );
660     CONNECT( selectColumnsSigMapper, mapped( int ),
661              this, toggleColumnShown( int ) );
662
663     viewStack->addWidget( treeView );
664 }
665
666 void StandardPLPanel::updateZoom( int i )
667 {
668     if ( i < 5 - QApplication::font().pointSize() ) return;
669     if ( i > 3 + QApplication::font().pointSize() ) return;
670     i_zoom = i;
671 #define A_ZOOM( view ) \
672     if ( view ) \
673     qobject_cast<AbstractPlViewItemDelegate*>( view->itemDelegate() )->setZoom( i_zoom )
674     /* Can't iterate as picflow & tree aren't using custom delegate */
675     A_ZOOM( iconView );
676     A_ZOOM( listView );
677 #undef A_ZOOM
678 }
679
680 void StandardPLPanel::showView( int i_view )
681 {
682     bool b_treeViewCreated = false;
683
684     switch( i_view )
685     {
686     case ICON_VIEW:
687     {
688         if( iconView == NULL )
689             createIconView();
690         currentView = iconView;
691         break;
692     }
693     case LIST_VIEW:
694     {
695         if( listView == NULL )
696             createListView();
697         currentView = listView;
698         break;
699     }
700     case PICTUREFLOW_VIEW:
701     {
702         if( picFlowView == NULL )
703             createCoverView();
704         currentView = picFlowView;
705         break;
706     }
707     default:
708     case TREE_VIEW:
709     {
710         if( treeView == NULL )
711         {
712             createTreeView();
713             b_treeViewCreated = true;
714         }
715         currentView = treeView;
716         break;
717     }
718     }
719
720     currentView->setModel( model );
721
722     /* Restoring the header Columns must come after changeModel */
723     if( b_treeViewCreated )
724     {
725         assert( treeView );
726         if( getSettings()->contains( "Playlist/headerStateV2" ) )
727         {
728             treeView->header()->restoreState(getSettings()
729                     ->value( "Playlist/headerStateV2" ).toByteArray() );
730             /* if there is allready stuff in playlist, we don't sort it and we reset
731                sorting */
732             if( model->rowCount() )
733             {
734                 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
735             }
736         }
737         else
738         {
739             for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
740             {
741                 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
742                 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
743                 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
744             }
745         }
746     }
747
748     updateZoom( i_zoom );
749     viewStack->setCurrentWidget( currentView );
750     browseInto();
751     gotoPlayingItem();
752 }
753
754 void StandardPLPanel::setWaiting( bool b )
755 {
756     if ( b )
757     {
758         spinnerAnimation->setLoopCount( 20 ); /* Trick until SD emits an event */
759         spinnerAnimation->start();
760     }
761     else
762         spinnerAnimation->stop();
763 }
764
765 void StandardPLPanel::updateViewport()
766 {
767     /* A single update on parent widget won't work */
768     currentView->viewport()->repaint();
769 }
770
771 int StandardPLPanel::currentViewIndex() const
772 {
773     if( currentView == treeView )
774         return TREE_VIEW;
775     else if( currentView == iconView )
776         return ICON_VIEW;
777     else if( currentView == listView )
778         return LIST_VIEW;
779     else
780         return PICTUREFLOW_VIEW;
781 }
782
783 void StandardPLPanel::cycleViews()
784 {
785     if( currentView == iconView )
786         showView( TREE_VIEW );
787     else if( currentView == treeView )
788         showView( LIST_VIEW );
789     else if( currentView == listView )
790 #ifndef NDEBUG
791         showView( PICTUREFLOW_VIEW  );
792     else if( currentView == picFlowView )
793 #endif
794         showView( ICON_VIEW );
795     else
796         assert( 0 );
797 }
798
799 void StandardPLPanel::activate( const QModelIndex &index )
800 {
801     if( currentView->model() == model )
802     {
803         /* If we are not a leaf node */
804         if( !index.data( VLCModelSubInterface::IsLeafNodeRole ).toBool() )
805         {
806             if( currentView != treeView )
807                 browseInto( index );
808         }
809         else
810         {
811             playlist_Lock( THEPL );
812             playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index, PLAYLIST_ID ) );
813             if ( p_item )
814             {
815                 p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
816                 lastActivatedPLItemId = p_item->i_id;
817             }
818             playlist_Unlock( THEPL );
819             if ( p_item && index.isValid() )
820                 model->activateItem( index );
821         }
822     }
823 }
824
825 void StandardPLPanel::browseInto( int i_pl_item_id )
826 {
827     if( i_pl_item_id != lastActivatedPLItemId ) return;
828
829     QModelIndex index = model->indexByPLID( i_pl_item_id, 0 );
830
831     if( currentView == treeView )
832         treeView->setExpanded( index, true );
833     else
834         browseInto( index );
835
836     lastActivatedPLItemId = -1;
837 }