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