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