1 /*****************************************************************************
2 * standardpanel.cpp : The "standard" playlist panel : just a treeview
3 ****************************************************************************
4 * Copyright © 2000-2010 VideoLAN
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Jean-Baptiste Kempf <jb@videolan.org>
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.
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.
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 *****************************************************************************/
29 #include "components/playlist/standardpanel.hpp"
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 */
43 #include <vlc_services_discovery.h> /* SD_CMD_SEARCH */
44 #include <vlc_intf_strings.h> /* POP_ */
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:" ) )
52 #include <QHeaderView>
55 #include <QWheelEvent>
56 #include <QStackedLayout>
57 #include <QSignalMapper>
59 #include <QStylePainter>
60 #include <QInputDialog>
61 #include <QDesktopServices>
67 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
68 intf_thread_t *_p_intf,
69 playlist_item_t *p_root,
70 PLSelector *_p_selector,
77 p_selector( _p_selector )
79 viewStack = new QStackedLayout( this );
80 viewStack->setSpacing( 0 ); viewStack->setMargin( 0 );
81 setMinimumWidth( 300 );
88 currentRootIndexId = -1;
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() );
100 int i_savedViewMode = getSettings()->value( "Playlist/view-mode", TREE_VIEW ).toInt();
101 i_zoom = getSettings()->value( "Playlist/zoom", 0 ).toInt();
103 showView( i_savedViewMode );
105 DCONNECT( THEMIM, leafBecameParent( int ),
106 this, browseInto( int ) );
108 CONNECT( model, currentIndexChanged( const QModelIndex& ),
109 this, handleExpansion( const QModelIndex& ) );
110 CONNECT( model, rootIndexChanged(), this, browseInto() );
112 setRootItem( p_root, false );
115 StandardPLPanel::~StandardPLPanel()
117 getSettings()->beginGroup("Playlist");
119 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
120 getSettings()->setValue( "view-mode", currentViewIndex() );
121 getSettings()->setValue( "zoom", i_zoom );
122 getSettings()->endGroup();
125 /* Unused anymore, but might be useful, like in right-click menu */
126 void StandardPLPanel::gotoPlayingItem()
128 currentView->scrollTo( model->currentIndex() );
131 void StandardPLPanel::handleExpansion( const QModelIndex& index )
133 assert( currentView );
134 if( currentRootIndexId != -1 && currentRootIndexId != model->itemId( index.parent() ) )
135 browseInto( index.parent() );
136 currentView->scrollTo( index );
139 void StandardPLPanel::popupPlView( const QPoint &point )
141 QModelIndex index = currentView->indexAt( point );
142 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
143 QItemSelectionModel *selection = currentView->selectionModel();
144 QModelIndexList list = selection->selectedRows();
146 if( !popup( index, globalPoint, list ) )
147 VLCMenuBar::PopupMenu( p_intf, true );
150 /*********** Popup *********/
151 bool StandardPLPanel::popup( const QModelIndex & index, const QPoint &point, const QModelIndexList &selectionlist )
153 VLCModel *model = qobject_cast<VLCModel*>(currentView->model());
154 QModelIndexList callerAsList;
155 callerAsList << ( index.isValid() ? index : QModelIndex() );
156 popupIndex = index; /* suitable for modal only */
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 ) )
167 PLModel::actionsContainerType container;
169 /* Play/Stream/Info static actions */
170 if( index.isValid() )
172 ADD_MENU_ENTRY( QIcon( ":/menu/play" ), qtr(I_POP_PLAY),
173 container.ACTION_PLAY, callerAsList );
175 menu.addAction( QIcon( ":/menu/stream" ), qtr(I_POP_STREAM),
176 this, SLOT( popupStream() ) );
178 menu.addAction( QIcon(), qtr(I_POP_SAVE),
179 this, SLOT( popupSave() ) );
181 menu.addAction( QIcon( ":/menu/info" ), qtr(I_POP_INFO),
182 this, SLOT( popupInfoDialog() ) );
186 if( model->getURI( index ).startsWith( "file://" ) )
187 menu.addAction( QIcon( ":/type/folder-grey" ), qtr(I_POP_EXPLORE),
188 this, SLOT( popupExplore() ) );
191 /* In PL or ML, allow to add a file/folder */
192 if( model->canEdit() )
194 QIcon addIcon( ":/buttons/playlist/playlist_add" );
196 if( model->isTree() )
197 menu.addAction( addIcon, qtr(I_POP_NEWFOLDER),
198 this, SLOT( popupPromptAndCreateNode() ) );
201 if( model->isCurrentItem( model->rootIndex(), PLModel::IN_PLAYLIST ) )
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()) );
207 else if( model->isCurrentItem( model->rootIndex(), PLModel::IN_MEDIALIBRARY ) )
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() ) );
215 if( index.isValid() )
217 if( !model->isCurrentItem( model->rootIndex(), PLModel::IN_PLAYLIST ) )
219 ADD_MENU_ENTRY( QIcon(), qtr(I_PL_ADDPL),
220 container.ACTION_ADDTOPLAYLIST, selectionlist );
227 if( index.isValid() )
229 ADD_MENU_ENTRY( QIcon( ":/buttons/playlist/playlist_remove" ), qtr(I_POP_DEL),
230 container.ACTION_REMOVE, selectionlist );
233 if( model->canEdit() ) {
234 menu.addAction( QIcon( ":/toolbar/clear" ), qtr("Clear the playlist"),
235 model, SLOT( clearPlaylist() ) );
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 )
249 action = sortingMenu->addAction( qfu( psz_column_title( Column ) ) + " " + qtr("Ascending") );
250 container.column = model->columnFromMeta(Column) + 1;
251 action->setData( QVariant::fromValue( container ) );
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 ) );
257 menu.addMenu( sortingMenu );
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 );
265 CONNECT( &menu, triggered( QAction * ), model, actionSlot( QAction * ) );
267 menu.addMenu( StandardPLPanel::viewSelectionMenu( this ) );
269 /* Display and forward the result */
270 if( !menu.isEmpty() )
272 menu.exec( point ); return true;
276 #undef ADD_MENU_ENTRY
279 QMenu* StandardPLPanel::viewSelectionMenu( StandardPLPanel *panel )
281 QMenu *viewMenu = new QMenu( qtr( "Playlist View Mode" ) );
282 QSignalMapper *viewSelectionMapper = new QSignalMapper( viewMenu );
283 CONNECT( viewSelectionMapper, mapped( int ), panel, showView( int ) );
285 QActionGroup *viewGroup = new QActionGroup( viewMenu );
286 # define MAX_VIEW StandardPLPanel::VIEW_COUNT
287 for( int i = 0; i < MAX_VIEW; i++ )
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 );
300 void StandardPLPanel::popupSelectColumn( QPoint )
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++ )
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() );
315 menu.exec( QCursor::pos() );
318 void StandardPLPanel::popupPromptAndCreateNode()
321 QString name = QInputDialog::getText( PlaylistDialog::getInstance( p_intf ),
322 qtr( I_NEW_DIR ), qtr( I_NEW_DIR_NAME ),
323 QLineEdit::Normal, QString(), &ok);
325 qobject_cast<VLCModel *>(currentView->model())->createNode( popupIndex, name );
328 void StandardPLPanel::popupInfoDialog()
330 if( popupIndex.isValid() )
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 ),
341 void StandardPLPanel::popupExplore()
343 VLCModel *model = qobject_cast<VLCModel *>(currentView->model());
344 QString uri = model->getURI( popupIndex );
347 if( ! uri.isEmpty() )
348 path = make_path( uri.toLatin1().constData() );
353 QDesktopServices::openUrl(
354 QUrl::fromLocalFile( QFileInfo( qfu( path ) ).absolutePath() ) );
359 void StandardPLPanel::popupStream()
361 VLCModel *model = qobject_cast<VLCModel *>(currentView->model());
362 QString uri = model->getURI( popupIndex );
363 if ( ! uri.isEmpty() )
364 THEDP->streamingDialog( NULL, uri, false );
367 void StandardPLPanel::popupSave()
369 VLCModel *model = qobject_cast<VLCModel *>(currentView->model());
370 QString uri = model->getURI( popupIndex );
371 if ( ! uri.isEmpty() )
372 THEDP->streamingDialog( NULL, uri );
375 void StandardPLPanel::toggleColumnShown( int i )
377 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
380 /* Search in the playlist */
381 void StandardPLPanel::search( const QString& searchText )
386 p_selector->getCurrentItemInfos( &type, &can_search, &name );
388 if( type != SD_TYPE || !can_search )
390 bool flat = ( currentView == iconView ||
391 currentView == listView ||
392 currentView == picFlowView );
393 model->search( searchText,
394 flat ? currentView->rootIndex() : QModelIndex(),
399 void StandardPLPanel::searchDelayed( const QString& searchText )
404 p_selector->getCurrentItemInfos( &type, &can_search, &name );
406 if( type == SD_TYPE && can_search )
408 if( !name.isEmpty() && !searchText.isEmpty() )
409 playlist_ServicesDiscoveryControl( THEPL, qtu( name ), SD_CMD_SEARCH,
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 )
421 msg_Dbg( p_intf, "Setting the SQL ML" );
422 currentView->setModel( mlmodel );
429 if( currentView->model() != model )
430 currentView->setModel( model );
431 model->rebuild( p_item );
435 void StandardPLPanel::browseInto( const QModelIndex &index )
437 if( currentView == iconView || currentView == listView || currentView == picFlowView )
440 currentView->setRootIndex( index );
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 );
450 /* Store new rootindexid*/
451 currentRootIndexId = model->itemId( index );
452 model->ensureArtRequested( index );
455 emit viewChanged( index );
458 void StandardPLPanel::browseInto()
460 browseInto( (currentRootIndexId != -1 && currentView != treeView) ?
461 model->index( currentRootIndexId, 0 ) :
465 void StandardPLPanel::wheelEvent( QWheelEvent *e )
467 if( e->modifiers() & Qt::ControlModifier ) {
468 int numSteps = e->delta() / 8 / 15;
471 else if( numSteps < 0)
474 // Accept this event in order to prevent unwanted volume up/down changes
478 bool StandardPLPanel::eventFilter ( QObject *obj, QEvent * event )
480 if (event->type() == QEvent::KeyPress)
482 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
483 if( keyEvent->key() == Qt::Key_Delete ||
484 keyEvent->key() == Qt::Key_Backspace )
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 )
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() );
506 painter.drawItemText( rect,
510 qtr("Playlist is currently empty.\n"
511 "Drop a file here or select a "
512 "media source from the left."),
515 else if ( spinnerAnimation->state() == PixmapAnimator::Running )
517 if ( currentView->model()->rowCount() )
518 spinnerAnimation->stop(); /* Trick until SD emits events */
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 );
533 void StandardPLPanel::deleteSelection()
535 QItemSelectionModel *selection = currentView->selectionModel();
536 QModelIndexList list = selection->selectedIndexes();
537 model->doDelete( list );
540 void StandardPLPanel::createIconView()
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 );
553 void StandardPLPanel::createListView()
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 );
566 void StandardPLPanel::createCoverView()
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 );
578 void StandardPLPanel::createTreeView()
580 /* Create and configure the QTreeView */
581 treeView = new PlTreeView( model, this );
583 /* setModel after setSortingEnabled(true), or the model will sort immediately! */
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 );
595 /* SignalMapper for columns */
596 selectColumnsSigMapper = new QSignalMapper( this );
597 CONNECT( selectColumnsSigMapper, mapped( int ),
598 this, toggleColumnShown( int ) );
600 viewStack->addWidget( treeView );
603 void StandardPLPanel::updateZoom( int i )
605 if ( i < 5 - QApplication::font().pointSize() ) return;
606 if ( i > 3 + QApplication::font().pointSize() ) return;
608 #define A_ZOOM( view ) \
610 qobject_cast<AbstractPlViewItemDelegate*>( view->itemDelegate() )->setZoom( i_zoom )
611 /* Can't iterate as picflow & tree aren't using custom delegate */
617 void StandardPLPanel::changeModel( bool b_ml )
625 if( currentView->model() != mod )
626 currentView->setModel( mod );
629 if( currentView->model() != model )
630 currentView->setModel( model );
634 void StandardPLPanel::showView( int i_view )
636 bool b_treeViewCreated = false;
642 if( iconView == NULL )
644 currentView = iconView;
649 if( listView == NULL )
651 currentView = listView;
654 case PICTUREFLOW_VIEW:
656 if( picFlowView == NULL )
658 currentView = picFlowView;
664 if( treeView == NULL )
667 b_treeViewCreated = true;
669 currentView = treeView;
674 changeModel( false );
676 /* Restoring the header Columns must come after changeModel */
677 if( b_treeViewCreated )
680 if( getSettings()->contains( "Playlist/headerStateV2" ) )
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
686 if( model->rowCount() )
688 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
693 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
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 );
702 updateZoom( i_zoom );
703 viewStack->setCurrentWidget( currentView );
708 void StandardPLPanel::setWaiting( bool b )
712 spinnerAnimation->setLoopCount( 20 ); /* Trick until SD emits an event */
713 spinnerAnimation->start();
716 spinnerAnimation->stop();
719 void StandardPLPanel::updateViewport()
721 /* A single update on parent widget won't work */
722 currentView->viewport()->repaint();
725 int StandardPLPanel::currentViewIndex() const
727 if( currentView == treeView )
729 else if( currentView == iconView )
731 else if( currentView == listView )
734 return PICTUREFLOW_VIEW;
737 void StandardPLPanel::cycleViews()
739 if( currentView == iconView )
740 showView( TREE_VIEW );
741 else if( currentView == treeView )
742 showView( LIST_VIEW );
743 else if( currentView == listView )
745 showView( PICTUREFLOW_VIEW );
746 else if( currentView == picFlowView )
748 showView( ICON_VIEW );
753 void StandardPLPanel::activate( const QModelIndex &index )
755 if( currentView->model() == model )
757 /* If we are not a leaf node */
758 if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
760 if( currentView != treeView )
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 );
775 void StandardPLPanel::browseInto( int i_id )
777 if( i_id != lastActivatedId ) return;
779 QModelIndex index = model->index( i_id, 0 );
781 if( currentView == treeView )
782 treeView->setExpanded( index, true );
786 lastActivatedId = -1;