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 "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 */
42 #include <vlc_services_discovery.h> /* SD_CMD_SEARCH */
43 #include <vlc_intf_strings.h> /* POP_ */
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:" ) )
51 #include <QHeaderView>
54 #include <QWheelEvent>
55 #include <QStackedLayout>
56 #include <QSignalMapper>
58 #include <QStylePainter>
59 #include <QInputDialog>
60 #include <QDesktopServices>
66 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
67 intf_thread_t *_p_intf,
68 playlist_item_t *p_root,
69 PLSelector *_p_selector,
76 p_selector( _p_selector )
78 viewStack = new QStackedLayout( this );
79 viewStack->setSpacing( 0 ); viewStack->setMargin( 0 );
80 setMinimumWidth( 300 );
87 currentRootIndexId = -1;
91 int i_savedViewMode = getSettings()->value( "Playlist/view-mode", TREE_VIEW ).toInt();
92 i_zoom = getSettings()->value( "Playlist/zoom", 0 ).toInt();
94 showView( i_savedViewMode );
96 DCONNECT( THEMIM, leafBecameParent( int ),
97 this, browseInto( int ) );
99 CONNECT( model, currentIndexChanged( const QModelIndex& ),
100 this, handleExpansion( const QModelIndex& ) );
101 CONNECT( model, rootIndexChanged(), this, browseInto() );
103 setRootItem( p_root, false );
106 StandardPLPanel::~StandardPLPanel()
108 getSettings()->beginGroup("Playlist");
110 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
111 getSettings()->setValue( "view-mode", currentViewIndex() );
112 getSettings()->setValue( "zoom", i_zoom );
113 getSettings()->endGroup();
116 /* Unused anymore, but might be useful, like in right-click menu */
117 void StandardPLPanel::gotoPlayingItem()
119 currentView->scrollTo( model->currentIndex() );
122 void StandardPLPanel::handleExpansion( const QModelIndex& index )
124 assert( currentView );
125 if( currentRootIndexId != -1 && currentRootIndexId != model->itemId( index.parent() ) )
126 browseInto( index.parent() );
127 currentView->scrollTo( index );
130 void StandardPLPanel::popupPlView( const QPoint &point )
132 QModelIndex index = currentView->indexAt( point );
133 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
134 QItemSelectionModel *selection = currentView->selectionModel();
135 QModelIndexList list = selection->selectedRows();
137 if( !popup( index, globalPoint, list ) )
138 VLCMenuBar::PopupMenu( p_intf, true );
141 /*********** Popup *********/
142 bool StandardPLPanel::popup( const QModelIndex & index, const QPoint &point, const QModelIndexList &selectionlist )
144 VLCModel *model = qobject_cast<VLCModel*>(currentView->model());
145 QModelIndexList callerAsList;
146 callerAsList << ( index.isValid() ? index : QModelIndex() );
147 popupIndex = index; /* suitable for modal only */
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 ) )
158 PLModel::actionsContainerType container;
160 /* Play/Stream/Info static actions */
161 if( index.isValid() )
163 ADD_MENU_ENTRY( QIcon( ":/menu/play" ), qtr(I_POP_PLAY),
164 container.ACTION_PLAY, callerAsList );
166 menu.addAction( QIcon( ":/menu/stream" ), qtr(I_POP_STREAM),
167 this, SLOT( popupStream() ) );
169 menu.addAction( QIcon(), qtr(I_POP_SAVE),
170 this, SLOT( popupSave() ) );
172 menu.addAction( QIcon( ":/menu/info" ), qtr(I_POP_INFO),
173 this, SLOT( popupInfoDialog() ) );
177 if( model->getURI( index ).startsWith( "file://" ) )
178 menu.addAction( QIcon( ":/type/folder-grey" ), qtr(I_POP_EXPLORE),
179 this, SLOT( popupExplore() ) );
182 /* In PL or ML, allow to add a file/folder */
183 if( model->canEdit() )
185 QIcon addIcon( ":/buttons/playlist/playlist_add" );
187 if( model->isTree() )
188 menu.addAction( addIcon, qtr(I_POP_NEWFOLDER),
189 this, SLOT( popupPromptAndCreateNode() ) );
192 if( model->isCurrentItem( model->rootIndex(), PLModel::IN_PLAYLIST ) )
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()) );
198 else if( model->isCurrentItem( model->rootIndex(), PLModel::IN_MEDIALIBRARY ) )
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() ) );
206 if( index.isValid() )
208 if( !model->isCurrentItem( model->rootIndex(), PLModel::IN_PLAYLIST ) )
210 ADD_MENU_ENTRY( QIcon(), qtr("Add to playlist"),
211 container.ACTION_ADDTOPLAYLIST, selectionlist );
218 if( index.isValid() )
220 ADD_MENU_ENTRY( QIcon( ":/buttons/playlist/playlist_remove" ), qtr(I_POP_DEL),
221 container.ACTION_REMOVE, selectionlist );
224 if( model->canEdit() ) {
225 menu.addAction( QIcon( ":/toolbar/clear" ), qtr("Clear playlist"),
226 model, SLOT( clearPlaylist() ) );
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 )
240 action = sortingMenu->addAction( qfu( psz_column_title( Column ) ) + " " + qtr("Ascending") );
241 container.column = model->columnFromMeta(Column) + 1;
242 action->setData( QVariant::fromValue( container ) );
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 ) );
248 menu.addMenu( sortingMenu );
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 );
256 CONNECT( &menu, triggered( QAction * ), model, actionSlot( QAction * ) );
258 menu.addMenu( StandardPLPanel::viewSelectionMenu( this ) );
260 /* Display and forward the result */
261 if( !menu.isEmpty() )
263 menu.exec( point ); return true;
267 #undef ADD_MENU_ENTRY
270 QMenu* StandardPLPanel::viewSelectionMenu( StandardPLPanel *panel )
272 QMenu *viewMenu = new QMenu( qtr( "Playlist View Mode" ) );
273 QSignalMapper *viewSelectionMapper = new QSignalMapper( viewMenu );
274 CONNECT( viewSelectionMapper, mapped( int ), panel, showView( int ) );
276 QActionGroup *viewGroup = new QActionGroup( viewMenu );
277 # define MAX_VIEW StandardPLPanel::VIEW_COUNT
278 for( int i = 0; i < MAX_VIEW; i++ )
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 );
291 void StandardPLPanel::popupSelectColumn( QPoint )
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++ )
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() );
306 menu.exec( QCursor::pos() );
309 void StandardPLPanel::popupPromptAndCreateNode()
312 QString name = QInputDialog::getText( PlaylistDialog::getInstance( p_intf ),
313 qtr( I_NEW_DIR ), qtr( I_NEW_DIR_NAME ),
314 QLineEdit::Normal, QString(), &ok);
316 qobject_cast<VLCModel *>(currentView->model())->createNode( popupIndex, name );
319 void StandardPLPanel::popupInfoDialog()
321 if( popupIndex.isValid() )
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 ),
332 void StandardPLPanel::popupExplore()
334 VLCModel *model = qobject_cast<VLCModel *>(currentView->model());
335 QString uri = model->getURI( popupIndex );
338 if( ! uri.isEmpty() )
339 path = make_path( uri.toAscii().constData() );
344 QDesktopServices::openUrl(
345 QUrl::fromLocalFile( QFileInfo( qfu( path ) ).absolutePath() ) );
350 void StandardPLPanel::popupStream()
352 VLCModel *model = qobject_cast<VLCModel *>(currentView->model());
353 QString uri = model->getURI( popupIndex );
354 if ( ! uri.isEmpty() )
355 THEDP->streamingDialog( NULL, uri, false );
358 void StandardPLPanel::popupSave()
360 VLCModel *model = qobject_cast<VLCModel *>(currentView->model());
361 QString uri = model->getURI( popupIndex );
362 if ( ! uri.isEmpty() )
363 THEDP->streamingDialog( NULL, uri );
366 void StandardPLPanel::toggleColumnShown( int i )
368 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
371 /* Search in the playlist */
372 void StandardPLPanel::search( const QString& searchText )
377 p_selector->getCurrentItemInfos( &type, &can_search, &name );
379 if( type != SD_TYPE || !can_search )
381 bool flat = ( currentView == iconView ||
382 currentView == listView ||
383 currentView == picFlowView );
384 model->search( searchText,
385 flat ? currentView->rootIndex() : QModelIndex(),
390 void StandardPLPanel::searchDelayed( const QString& searchText )
395 p_selector->getCurrentItemInfos( &type, &can_search, &name );
397 if( type == SD_TYPE && can_search )
399 if( !name.isEmpty() && !searchText.isEmpty() )
400 playlist_ServicesDiscoveryControl( THEPL, qtu( name ), SD_CMD_SEARCH,
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 )
412 msg_Dbg( p_intf, "Setting the SQL ML" );
413 currentView->setModel( mlmodel );
420 if( currentView->model() != model )
421 currentView->setModel( model );
422 model->rebuild( p_item );
426 void StandardPLPanel::browseInto( const QModelIndex &index )
428 if( currentView == iconView || currentView == listView || currentView == picFlowView )
431 currentView->setRootIndex( index );
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 );
441 /* Store new rootindexid*/
442 currentRootIndexId = model->itemId( index );
443 model->ensureArtRequested( index );
446 emit viewChanged( index );
449 void StandardPLPanel::browseInto()
451 browseInto( (currentRootIndexId != -1 && currentView != treeView) ?
452 model->index( currentRootIndexId, 0 ) :
456 void StandardPLPanel::wheelEvent( QWheelEvent *e )
458 // Accept this event in order to prevent unwanted volume up/down changes
462 bool StandardPLPanel::eventFilter ( QObject *obj, QEvent * event )
464 if (event->type() == QEvent::KeyPress)
466 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
467 if( keyEvent->key() == Qt::Key_Delete ||
468 keyEvent->key() == Qt::Key_Backspace )
474 else if ( event->type() == QEvent::Paint )
475 {/* Warn! Don't filter events from anything else than views ! */
476 if ( model->rowCount() == 0 && p_selector->getCurrentItemCategory() == PL_ITEM_TYPE )
478 QWidget *viewport = qobject_cast<QWidget *>( obj );
479 QStylePainter painter( viewport );
480 QPixmap dropzone(":/dropzone");
481 QRect rect = viewport->geometry();
482 QSize size = rect.size() / 2 - dropzone.size() / 2;
483 rect.adjust( 0, size.height(), 0 , 0 );
484 painter.drawItemPixmap( rect, Qt::AlignHCenter, dropzone );
485 /* now select the zone just below the drop zone and let Qt center
486 the text by itself */
487 rect.adjust( 0, dropzone.size().height() + 10, 0, 0 );
488 rect.setRight( viewport->geometry().width() );
490 painter.drawItemText( rect,
494 qtr("Playlist is currently empty.\n"
495 "Drop a file here or select a "
496 "media source from the left."),
503 void StandardPLPanel::deleteSelection()
505 QItemSelectionModel *selection = currentView->selectionModel();
506 QModelIndexList list = selection->selectedIndexes();
507 model->doDelete( list );
510 void StandardPLPanel::createIconView()
512 iconView = new PlIconView( model, this );
513 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
514 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
515 this, popupPlView( const QPoint & ) );
516 CONNECT( iconView, activated( const QModelIndex & ),
517 this, activate( const QModelIndex & ) );
518 iconView->installEventFilter( this );
519 iconView->viewport()->installEventFilter( this );
520 viewStack->addWidget( iconView );
523 void StandardPLPanel::createListView()
525 listView = new PlListView( model, this );
526 listView->setContextMenuPolicy( Qt::CustomContextMenu );
527 CONNECT( listView, customContextMenuRequested( const QPoint & ),
528 this, popupPlView( const QPoint & ) );
529 CONNECT( listView, activated( const QModelIndex & ),
530 this, activate( const QModelIndex & ) );
531 listView->installEventFilter( this );
532 listView->viewport()->installEventFilter( this );
533 viewStack->addWidget( listView );
536 void StandardPLPanel::createCoverView()
538 picFlowView = new PicFlowView( model, this );
539 picFlowView->setContextMenuPolicy( Qt::CustomContextMenu );
540 CONNECT( picFlowView, customContextMenuRequested( const QPoint & ),
541 this, popupPlView( const QPoint & ) );
542 CONNECT( picFlowView, activated( const QModelIndex & ),
543 this, activate( const QModelIndex & ) );
544 viewStack->addWidget( picFlowView );
545 picFlowView->installEventFilter( this );
548 void StandardPLPanel::createTreeView()
550 /* Create and configure the QTreeView */
551 treeView = new PlTreeView( model, this );
553 /* setModel after setSortingEnabled(true), or the model will sort immediately! */
555 /* Connections for the TreeView */
556 CONNECT( treeView, activated( const QModelIndex& ),
557 this, activate( const QModelIndex& ) );
558 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
559 this, popupSelectColumn( QPoint ) );
560 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
561 this, popupPlView( const QPoint & ) );
562 treeView->installEventFilter( this );
563 treeView->viewport()->installEventFilter( this );
565 /* SignalMapper for columns */
566 selectColumnsSigMapper = new QSignalMapper( this );
567 CONNECT( selectColumnsSigMapper, mapped( int ),
568 this, toggleColumnShown( int ) );
570 viewStack->addWidget( treeView );
573 void StandardPLPanel::updateZoom( int i )
575 if ( i < 4 - QApplication::font().pointSize() ) return;
577 #define A_ZOOM( view ) \
579 qobject_cast<AbstractPlViewItemDelegate*>( view->itemDelegate() )->setZoom( i_zoom )
580 /* Can't iterate as picflow & tree aren't using custom delegate */
586 void StandardPLPanel::changeModel( bool b_ml )
594 if( currentView->model() != mod )
595 currentView->setModel( mod );
598 if( currentView->model() != model )
599 currentView->setModel( model );
603 void StandardPLPanel::showView( int i_view )
605 bool b_treeViewCreated = false;
611 if( iconView == NULL )
613 currentView = iconView;
618 if( listView == NULL )
620 currentView = listView;
623 case PICTUREFLOW_VIEW:
625 if( picFlowView == NULL )
627 currentView = picFlowView;
633 if( treeView == NULL )
636 b_treeViewCreated = true;
638 currentView = treeView;
643 changeModel( false );
645 /* Restoring the header Columns must come after changeModel */
646 if( b_treeViewCreated )
649 if( getSettings()->contains( "Playlist/headerStateV2" ) )
651 treeView->header()->restoreState(getSettings()
652 ->value( "Playlist/headerStateV2" ).toByteArray() );
653 /* if there is allready stuff in playlist, we don't sort it and we reset
655 if( model->rowCount() )
657 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
662 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
664 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
665 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
666 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
671 updateZoom( i_zoom );
672 viewStack->setCurrentWidget( currentView );
677 int StandardPLPanel::currentViewIndex() const
679 if( currentView == treeView )
681 else if( currentView == iconView )
683 else if( currentView == listView )
686 return PICTUREFLOW_VIEW;
689 void StandardPLPanel::cycleViews()
691 if( currentView == iconView )
692 showView( TREE_VIEW );
693 else if( currentView == treeView )
694 showView( LIST_VIEW );
695 else if( currentView == listView )
697 showView( PICTUREFLOW_VIEW );
698 else if( currentView == picFlowView )
700 showView( ICON_VIEW );
705 void StandardPLPanel::activate( const QModelIndex &index )
707 if( currentView->model() == model )
709 /* If we are not a leaf node */
710 if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
712 if( currentView != treeView )
717 playlist_Lock( THEPL );
718 playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
719 p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
720 lastActivatedId = p_item->p_input->i_id;
721 playlist_Unlock( THEPL );
722 model->activateItem( index );
727 void StandardPLPanel::browseInto( int i_id )
729 if( i_id != lastActivatedId ) return;
731 QModelIndex index = model->index( i_id, 0 );
733 if( currentView == treeView )
734 treeView->setExpanded( index, true );
738 lastActivatedId = -1;