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