]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Qt: add rename directory option
[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 "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
226     /* Item removal */
227
228     ADD_MENU_ENTRY( QIcon( ":/buttons/playlist/playlist_remove" ), qtr(I_POP_DEL),
229                     VLCModelSubInterface::ACTION_REMOVE );
230
231     ADD_MENU_ENTRY( QIcon( ":/toolbar/clear" ), qtr("Clear the playlist"),
232                     VLCModelSubInterface::ACTION_CLEAR );
233
234     menu.addSeparator();
235
236     /* Playlist sorting */
237     if ( model->isSupportedAction( VLCModelSubInterface::ACTION_SORT, index ) )
238     {
239         QMenu *sortingMenu = new QMenu( qtr( "Sort by" ) );
240         /* Choose what columns to show in sorting menu, not sure if this should be configurable*/
241         QList<int> sortingColumns;
242         sortingColumns << COLUMN_TITLE << COLUMN_ARTIST << COLUMN_ALBUM << COLUMN_TRACK_NUMBER << COLUMN_URI;
243         container.action = VLCModelSubInterface::ACTION_SORT;
244         foreach( int Column, sortingColumns )
245         {
246             action = sortingMenu->addAction( qfu( psz_column_title( Column ) ) + " " + qtr("Ascending") );
247             container.column = model->columnFromMeta(Column) + 1;
248             action->setData( QVariant::fromValue( container ) );
249
250             action = sortingMenu->addAction( qfu( psz_column_title( Column ) ) + " " + qtr("Descending") );
251             container.column = -1 * (model->columnFromMeta(Column)+1);
252             action->setData( QVariant::fromValue( container ) );
253         }
254         menu.addMenu( sortingMenu );
255     }
256
257     /* Zoom */
258     QMenu *zoomMenu = new QMenu( qtr( "Display size" ) );
259     zoomMenu->addAction( qtr( "Increase" ), this, SLOT( increaseZoom() ) );
260     zoomMenu->addAction( qtr( "Decrease" ), this, SLOT( decreaseZoom() ) );
261     menu.addMenu( zoomMenu );
262
263     CONNECT( &menu, triggered( QAction * ), this, popupAction( QAction * ) );
264
265     menu.addMenu( StandardPLPanel::viewSelectionMenu( this ) );
266
267     /* Display and forward the result */
268     if( !menu.isEmpty() )
269     {
270         menu.exec( point ); return true;
271     }
272     else return false;
273
274 #undef ADD_MENU_ENTRY
275 }
276
277 void StandardPLPanel::popupAction( QAction *action )
278 {
279     VLCProxyModel *model = qobject_cast<VLCProxyModel *>(currentView->model());
280     VLCModelSubInterface::actionsContainerType a =
281             action->data().value<VLCModelSubInterface::actionsContainerType>();
282     QModelIndexList list = currentView->selectionModel()->selectedRows();
283     QModelIndex index = popupIndex( currentView );
284     char *path = NULL;
285     OpenDialog *dialog;
286     QString temp;
287     QStringList uris;
288     bool ok;
289
290     /* first try to complete actions requiring missing parameters thru UI dialogs */
291     switch( a.action )
292     {
293     case VLCModelSubInterface::ACTION_INFO:
294         /* locally handled only */
295         if( index.isValid() )
296         {
297             input_item_t* p_input = model->getInputItem( index );
298             MediaInfoDialog *mid = new MediaInfoDialog( p_intf, p_input );
299             mid->setParent( PlaylistDialog::getInstance( p_intf ),
300                             Qt::Dialog );
301             mid->show();
302         }
303         break;
304
305     case VLCModelSubInterface::ACTION_EXPLORE:
306         /* locally handled only */
307         temp = model->getURI( index );
308         if( ! temp.isEmpty() ) path = make_path( temp.toLatin1().constData() );
309         if( path == NULL ) return;
310         QDesktopServices::openUrl(
311                     QUrl::fromLocalFile( QFileInfo( qfu( path ) ).absolutePath() ) );
312         free( path );
313         break;
314
315     case VLCModelSubInterface::ACTION_STREAM:
316         /* locally handled only */
317         temp = model->getURI( index );
318         if ( ! temp.isEmpty() )
319             THEDP->streamingDialog( NULL, temp, false );
320         break;
321
322     case VLCModelSubInterface::ACTION_SAVE:
323         /* locally handled only */
324         temp = model->getURI( index );
325         if ( ! temp.isEmpty() )
326             THEDP->streamingDialog( NULL, temp );
327         break;
328
329     case VLCModelSubInterface::ACTION_CREATENODE:
330         temp = QInputDialog::getText( PlaylistDialog::getInstance( p_intf ),
331             qtr( I_NEW_DIR ), qtr( I_NEW_DIR_NAME ),
332             QLineEdit::Normal, QString(), &ok);
333         if ( !ok ) return;
334         model->createNode( index, temp );
335         break;
336
337     case VLCModelSubInterface::ACTION_RENAMENODE:
338         temp = QInputDialog::getText( PlaylistDialog::getInstance( p_intf ),
339             qtr( I_RENAME_DIR ), qtr( I_RENAME_DIR_NAME ),
340             QLineEdit::Normal, model->getTitle( index ), &ok);
341         if ( !ok ) return;
342         model->renameNode( index, temp );
343         break;
344
345     case VLCModelSubInterface::ACTION_ENQUEUEFILE:
346         uris = THEDP->showSimpleOpen();
347         if ( uris.isEmpty() ) return;
348         uris.sort();
349         foreach( const QString &file, uris )
350             a.uris << qtu( toURI( toNativeSeparators( file ) ) );
351         action->setData( QVariant::fromValue( a ) );
352         if ( model->action( action, list ) )
353             foreach( const QString &file, a.uris )
354                 RecentsMRL::getInstance( p_intf )->addRecent( file );
355         break;
356
357     case VLCModelSubInterface::ACTION_ENQUEUEDIR:
358         temp = THEDP->getDirectoryDialog();
359         if ( temp.isEmpty() ) return;
360         a.uris << temp;
361         action->setData( QVariant::fromValue( a ) );
362         model->action( action, list );
363         break;
364
365     case VLCModelSubInterface::ACTION_ENQUEUEGENERIC:
366         dialog = OpenDialog::getInstance( this, p_intf, false, SELECT, true, true );
367         dialog->showTab( OPEN_FILE_TAB );
368         dialog->exec(); /* make it modal */
369         a.uris = dialog->getMRLs( false );
370         a.options = dialog->getOptions();
371         if ( a.uris.isEmpty() ) return;
372         action->setData( QVariant::fromValue( a ) );
373         if ( model->action( action, list ) )
374             foreach( const QString &file, a.uris )
375                 RecentsMRL::getInstance( p_intf )->addRecent( file );
376         break;
377
378     default:
379         model->action( action, list );
380     }
381 }
382
383 QMenu* StandardPLPanel::viewSelectionMenu( StandardPLPanel *panel )
384 {
385     QMenu *viewMenu = new QMenu( qtr( "Playlist View Mode" ) );
386     QSignalMapper *viewSelectionMapper = new QSignalMapper( viewMenu );
387     CONNECT( viewSelectionMapper, mapped( int ), panel, showView( int ) );
388
389     QActionGroup *viewGroup = new QActionGroup( viewMenu );
390 # define MAX_VIEW StandardPLPanel::VIEW_COUNT
391     for( int i = 0; i < MAX_VIEW; i++ )
392     {
393         QAction *action = viewMenu->addAction( viewNames[i] );
394         action->setCheckable( true );
395         viewGroup->addAction( action );
396         viewSelectionMapper->setMapping( action, i );
397         CONNECT( action, triggered(), viewSelectionMapper, map() );
398         if( panel->currentViewIndex() == i )
399             action->setChecked( true );
400     }
401     return viewMenu;
402 }
403
404 inline QModelIndex popupIndex( QAbstractItemView *view )
405 {
406     QModelIndexList list = view->selectionModel()->selectedIndexes();
407     if ( list.isEmpty() )
408         return QModelIndex();
409     else
410         return list.first();
411 }
412
413 void StandardPLPanel::popupSelectColumn( QPoint )
414 {
415     QMenu menu;
416     assert( treeView );
417
418     /* We do not offer the option to hide index 0 column, or
419      * QTreeView will behave weird */
420     for( int i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
421     {
422         QAction* option = menu.addAction( qfu( psz_column_title( i ) ) );
423         option->setCheckable( true );
424         option->setChecked( !treeView->isColumnHidden( j ) );
425         selectColumnsSigMapper->setMapping( option, j );
426         CONNECT( option, triggered(), selectColumnsSigMapper, map() );
427     }
428     menu.exec( QCursor::pos() );
429 }
430
431 void StandardPLPanel::toggleColumnShown( int i )
432 {
433     treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
434 }
435
436 /* Search in the playlist */
437 void StandardPLPanel::search( const QString& searchText )
438 {
439     int type;
440     QString name;
441     bool can_search;
442     p_selector->getCurrentItemInfos( &type, &can_search, &name );
443
444     if( type != SD_TYPE || !can_search )
445     {
446         bool flat = ( currentView == iconView ||
447                       currentView == listView ||
448                       currentView == picFlowView );
449         model->filter( searchText,
450                        flat ? currentView->rootIndex() : QModelIndex(),
451                        !flat );
452     }
453 }
454
455 void StandardPLPanel::searchDelayed( const QString& searchText )
456 {
457     int type;
458     QString name;
459     bool can_search;
460     p_selector->getCurrentItemInfos( &type, &can_search, &name );
461
462     if( type == SD_TYPE && can_search )
463     {
464         if( !name.isEmpty() && !searchText.isEmpty() )
465             playlist_ServicesDiscoveryControl( THEPL, qtu( name ), SD_CMD_SEARCH,
466                                               qtu( searchText ) );
467     }
468 }
469
470 /* Set the root of the new Playlist */
471 /* This activated by the selector selection */
472 void StandardPLPanel::setRootItem( playlist_item_t *p_item, bool b )
473 {
474 #ifdef SQL_MEDIA_LIBRARY
475     if( b )
476     {
477         msg_Dbg( p_intf, "Setting the SQL ML" );
478         if ( model->switchToModel( VLCProxyModel::SQLML_MODEL ) )
479             currentView->setModel( model );
480     }
481     else
482 #else
483     Q_UNUSED( b );
484 #endif
485     {
486         if ( model->switchToModel( VLCProxyModel::PL_MODEL ) )
487             model->rebuild( p_item );
488     }
489 }
490
491 void StandardPLPanel::browseInto( const QModelIndex &index )
492 {
493     if( currentView == iconView || currentView == listView || currentView == picFlowView )
494     {
495
496         currentView->setRootIndex( index );
497
498         /* When going toward root in LocationBar, scroll to the item
499            that was previously as root */
500         QModelIndex newIndex = model->indexByPLID(currentRootIndexPLId,0);
501         while( newIndex.isValid() && (newIndex.parent() != index) )
502             newIndex = newIndex.parent();
503         if( newIndex.isValid() )
504             currentView->scrollTo( newIndex );
505
506         /* Store new rootindexid*/
507         currentRootIndexPLId = model->itemId( index, PLAYLIST_ID );
508
509         model->ensureArtRequested( index );
510     }
511
512     emit viewChanged( index );
513 }
514
515 void StandardPLPanel::browseInto()
516 {
517     browseInto( (currentRootIndexPLId != -1 && currentView != treeView) ?
518                  model->indexByPLID( currentRootIndexPLId, 0 ) :
519                  QModelIndex() );
520 }
521
522 void StandardPLPanel::wheelEvent( QWheelEvent *e )
523 {
524     if( e->modifiers() & Qt::ControlModifier ) {
525         int numSteps = e->delta() / 8 / 15;
526         if( numSteps > 0)
527             increaseZoom();
528         else if( numSteps < 0)
529             decreaseZoom();
530     }
531     // Accept this event in order to prevent unwanted volume up/down changes
532     e->accept();
533 }
534
535 bool StandardPLPanel::eventFilter ( QObject *obj, QEvent * event )
536 {
537     if (event->type() == QEvent::KeyPress)
538     {
539         QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
540         if( keyEvent->key() == Qt::Key_Delete ||
541             keyEvent->key() == Qt::Key_Backspace )
542         {
543             deleteSelection();
544             return true;
545         }
546     }
547     else if ( event->type() == QEvent::Paint )
548     {/* Warn! Don't filter events from anything else than views ! */
549         if ( model->rowCount() == 0 && p_selector->getCurrentItemCategory() == PL_ITEM_TYPE )
550         {
551             QWidget *viewport = qobject_cast<QWidget *>( obj );
552             QStylePainter painter( viewport );
553             QPixmap dropzone(":/dropzone");
554             QRect rect = viewport->geometry();
555             QSize size = rect.size() / 2 - dropzone.size() / 2;
556             rect.adjust( 0, size.height(), 0 , 0 );
557             painter.drawItemPixmap( rect, Qt::AlignHCenter, dropzone );
558             /* now select the zone just below the drop zone and let Qt center
559                the text by itself */
560             rect.adjust( 0, dropzone.size().height() + 10, 0, 0 );
561             rect.setRight( viewport->geometry().width() );
562             rect.setLeft( 0 );
563             painter.drawItemText( rect,
564                                   Qt::AlignHCenter,
565                                   palette(),
566                                   true,
567                                   qtr("Playlist is currently empty.\n"
568                                       "Drop a file here or select a "
569                                       "media source from the left."),
570                                   QPalette::Text );
571         }
572         else if ( spinnerAnimation->state() == PixmapAnimator::Running )
573         {
574             if ( currentView->model()->rowCount() )
575                 spinnerAnimation->stop(); /* Trick until SD emits events */
576             else
577             {
578                 QWidget *viewport = qobject_cast<QWidget *>( obj );
579                 QStylePainter painter( viewport );
580                 QPixmap *spinner = spinnerAnimation->getPixmap();
581                 QPoint point = viewport->geometry().center();
582                 point -= QPoint( spinner->size().width() / 2, spinner->size().height() / 2 );
583                 painter.drawPixmap( point, *spinner );
584             }
585         }
586     }
587     return false;
588 }
589
590 void StandardPLPanel::deleteSelection()
591 {
592     QModelIndexList list = currentView->selectionModel()->selectedIndexes();
593     model->doDelete( list );
594 }
595
596 void StandardPLPanel::createIconView()
597 {
598     iconView = new PlIconView( model, this );
599     iconView->setContextMenuPolicy( Qt::CustomContextMenu );
600     CONNECT( iconView, customContextMenuRequested( const QPoint & ),
601              this, popupPlView( const QPoint & ) );
602     CONNECT( iconView, activated( const QModelIndex & ),
603              this, activate( const QModelIndex & ) );
604     iconView->installEventFilter( this );
605     iconView->viewport()->installEventFilter( this );
606     viewStack->addWidget( iconView );
607 }
608
609 void StandardPLPanel::createListView()
610 {
611     listView = new PlListView( model, this );
612     listView->setContextMenuPolicy( Qt::CustomContextMenu );
613     CONNECT( listView, customContextMenuRequested( const QPoint & ),
614              this, popupPlView( const QPoint & ) );
615     CONNECT( listView, activated( const QModelIndex & ),
616              this, activate( const QModelIndex & ) );
617     listView->installEventFilter( this );
618     listView->viewport()->installEventFilter( this );
619     viewStack->addWidget( listView );
620 }
621
622 void StandardPLPanel::createCoverView()
623 {
624     picFlowView = new PicFlowView( model, this );
625     picFlowView->setContextMenuPolicy( Qt::CustomContextMenu );
626     CONNECT( picFlowView, customContextMenuRequested( const QPoint & ),
627              this, popupPlView( const QPoint & ) );
628     CONNECT( picFlowView, activated( const QModelIndex & ),
629              this, activate( const QModelIndex & ) );
630     viewStack->addWidget( picFlowView );
631     picFlowView->installEventFilter( this );
632 }
633
634 void StandardPLPanel::createTreeView()
635 {
636     /* Create and configure the QTreeView */
637     treeView = new PlTreeView( model, this );
638
639     /* setModel after setSortingEnabled(true), or the model will sort immediately! */
640
641     /* Connections for the TreeView */
642     CONNECT( treeView, activated( const QModelIndex& ),
643              this, activate( const QModelIndex& ) );
644     CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
645              this, popupSelectColumn( QPoint ) );
646     CONNECT( treeView, customContextMenuRequested( const QPoint & ),
647              this, popupPlView( const QPoint & ) );
648     treeView->installEventFilter( this );
649     treeView->viewport()->installEventFilter( this );
650
651     /* SignalMapper for columns */
652     selectColumnsSigMapper = new QSignalMapper( this );
653     CONNECT( selectColumnsSigMapper, mapped( int ),
654              this, toggleColumnShown( int ) );
655
656     viewStack->addWidget( treeView );
657 }
658
659 void StandardPLPanel::updateZoom( int i )
660 {
661     if ( i < 5 - QApplication::font().pointSize() ) return;
662     if ( i > 3 + QApplication::font().pointSize() ) return;
663     i_zoom = i;
664 #define A_ZOOM( view ) \
665     if ( view ) \
666     qobject_cast<AbstractPlViewItemDelegate*>( view->itemDelegate() )->setZoom( i_zoom )
667     /* Can't iterate as picflow & tree aren't using custom delegate */
668     A_ZOOM( iconView );
669     A_ZOOM( listView );
670 #undef A_ZOOM
671 }
672
673 void StandardPLPanel::showView( int i_view )
674 {
675     bool b_treeViewCreated = false;
676
677     switch( i_view )
678     {
679     case ICON_VIEW:
680     {
681         if( iconView == NULL )
682             createIconView();
683         currentView = iconView;
684         break;
685     }
686     case LIST_VIEW:
687     {
688         if( listView == NULL )
689             createListView();
690         currentView = listView;
691         break;
692     }
693     case PICTUREFLOW_VIEW:
694     {
695         if( picFlowView == NULL )
696             createCoverView();
697         currentView = picFlowView;
698         break;
699     }
700     default:
701     case TREE_VIEW:
702     {
703         if( treeView == NULL )
704         {
705             createTreeView();
706             b_treeViewCreated = true;
707         }
708         currentView = treeView;
709         break;
710     }
711     }
712
713     currentView->setModel( model );
714
715     /* Restoring the header Columns must come after changeModel */
716     if( b_treeViewCreated )
717     {
718         assert( treeView );
719         if( getSettings()->contains( "Playlist/headerStateV2" ) )
720         {
721             treeView->header()->restoreState(getSettings()
722                     ->value( "Playlist/headerStateV2" ).toByteArray() );
723             /* if there is allready stuff in playlist, we don't sort it and we reset
724                sorting */
725             if( model->rowCount() )
726             {
727                 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
728             }
729         }
730         else
731         {
732             for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
733             {
734                 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
735                 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
736                 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
737             }
738         }
739     }
740
741     updateZoom( i_zoom );
742     viewStack->setCurrentWidget( currentView );
743     browseInto();
744     gotoPlayingItem();
745 }
746
747 void StandardPLPanel::setWaiting( bool b )
748 {
749     if ( b )
750     {
751         spinnerAnimation->setLoopCount( 20 ); /* Trick until SD emits an event */
752         spinnerAnimation->start();
753     }
754     else
755         spinnerAnimation->stop();
756 }
757
758 void StandardPLPanel::updateViewport()
759 {
760     /* A single update on parent widget won't work */
761     currentView->viewport()->repaint();
762 }
763
764 int StandardPLPanel::currentViewIndex() const
765 {
766     if( currentView == treeView )
767         return TREE_VIEW;
768     else if( currentView == iconView )
769         return ICON_VIEW;
770     else if( currentView == listView )
771         return LIST_VIEW;
772     else
773         return PICTUREFLOW_VIEW;
774 }
775
776 void StandardPLPanel::cycleViews()
777 {
778     if( currentView == iconView )
779         showView( TREE_VIEW );
780     else if( currentView == treeView )
781         showView( LIST_VIEW );
782     else if( currentView == listView )
783 #ifndef NDEBUG
784         showView( PICTUREFLOW_VIEW  );
785     else if( currentView == picFlowView )
786 #endif
787         showView( ICON_VIEW );
788     else
789         assert( 0 );
790 }
791
792 void StandardPLPanel::activate( const QModelIndex &index )
793 {
794     if( currentView->model() == model )
795     {
796         /* If we are not a leaf node */
797         if( !index.data( VLCModelSubInterface::IsLeafNodeRole ).toBool() )
798         {
799             if( currentView != treeView )
800                 browseInto( index );
801         }
802         else
803         {
804             playlist_Lock( THEPL );
805             playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index, PLAYLIST_ID ) );
806             if ( p_item )
807             {
808                 p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
809                 lastActivatedPLItemId = p_item->i_id;
810             }
811             playlist_Unlock( THEPL );
812             if ( p_item && index.isValid() )
813                 model->activateItem( index );
814         }
815     }
816 }
817
818 void StandardPLPanel::browseInto( int i_pl_item_id )
819 {
820     if( i_pl_item_id != lastActivatedPLItemId ) return;
821
822     QModelIndex index = model->indexByPLID( i_pl_item_id, 0 );
823
824     if( currentView == treeView )
825         treeView->setExpanded( index, true );
826     else
827         browseInto( index );
828
829     lastActivatedPLItemId = -1;
830 }