1 /*****************************************************************************
2 * standardpanel.cpp : The "standard" playlist panel : just a treeview
3 ****************************************************************************
4 * Copyright (C) 2000-2009 VideoLAN
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * JB 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 "dialogs_provider.hpp"
31 #include "components/playlist/playlist_model.hpp"
32 #include "components/playlist/standardpanel.hpp"
33 #include "components/playlist/icon_view.hpp"
34 #include "util/customwidgets.hpp"
37 #include <vlc_intf_strings.h>
39 #include <QPushButton>
40 #include <QHeaderView>
42 #include <QModelIndexList>
45 #include <QSignalMapper>
46 #include <QWheelEvent>
47 #include <QToolButton>
48 #include <QFontMetrics>
55 static const QString viewNames[] = { qtr( "Detailed View" ),
59 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
60 intf_thread_t *_p_intf,
61 playlist_t *p_playlist,
62 playlist_item_t *p_root ):
63 QWidget( _parent ), p_intf( _p_intf )
65 layout = new QGridLayout( this );
66 layout->setSpacing( 0 ); layout->setMargin( 0 );
67 setMinimumWidth( 300 );
73 model = new PLModel( p_playlist, p_intf, p_root, this );
75 currentRootIndexId = -1;
78 locationBar = new LocationBar( model );
79 locationBar->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
80 layout->addWidget( locationBar, 0, 1 );
81 layout->setColumnStretch( 1, 100 );
82 CONNECT( locationBar, invoked( const QModelIndex & ),
83 this, browseInto( const QModelIndex & ) );
85 layout->setColumnStretch( 2, 1 );
87 searchEdit = new SearchLineEdit( this );
88 searchEdit->setMaximumWidth( 250 );
89 searchEdit->setMinimumWidth( 80 );
90 layout->addWidget( searchEdit, 0, 3 );
91 CONNECT( searchEdit, textChanged( const QString& ),
92 this, search( const QString& ) );
93 layout->setColumnStretch( 3, 50 );
95 /* Add item to the playlist button */
96 addButton = new QToolButton;
97 addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
98 addButton->setMaximumWidth( 30 );
99 BUTTONACT( addButton, popupAdd() );
100 layout->addWidget( addButton, 0, 0 );
102 /* Button to switch views */
103 QToolButton *viewButton = new QToolButton( this );
104 viewButton->setIcon( style()->standardIcon( QStyle::SP_FileDialogDetailedView ) );
105 layout->addWidget( viewButton, 0, 4 );
107 /* View selection menu */
108 viewSelectionMapper = new QSignalMapper( this );
109 CONNECT( viewSelectionMapper, mapped( int ), this, showView( int ) );
111 QActionGroup *actionGroup = new QActionGroup( this );
113 for( int i = 0; i < VIEW_COUNT; i++ )
115 viewActions[i] = actionGroup->addAction( viewNames[i] );
116 viewActions[i]->setCheckable( true );
117 viewSelectionMapper->setMapping( viewActions[i], i );
118 CONNECT( viewActions[i], triggered(), viewSelectionMapper, map() );
121 BUTTONACT( viewButton, cycleViews() );
122 QMenu *viewMenu = new QMenu( this );
123 viewMenu->addActions( actionGroup->actions() );
125 viewButton->setMenu( viewMenu );
128 getSettings()->beginGroup("Playlist");
130 int i_viewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
131 showView( i_viewMode );
133 getSettings()->endGroup();
135 CONNECT( THEMIM, leafBecameParent( input_item_t *),
136 this, browseInto( input_item_t * ) );
138 CONNECT( model, currentChanged( const QModelIndex& ),
139 this, handleExpansion( const QModelIndex& ) );
140 CONNECT( model, rootChanged(), this, handleRootChange() );
143 StandardPLPanel::~StandardPLPanel()
145 getSettings()->beginGroup("Playlist");
147 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
148 if( currentView == treeView )
149 getSettings()->setValue( "view-mode", TREE_VIEW );
150 else if( currentView == listView )
151 getSettings()->setValue( "view-mode", LIST_VIEW );
152 else if( currentView == iconView )
153 getSettings()->setValue( "view-mode", ICON_VIEW );
154 getSettings()->endGroup();
157 /* Unused anymore, but might be useful, like in right-click menu */
158 void StandardPLPanel::gotoPlayingItem()
160 currentView->scrollTo( model->currentIndex() );
163 void StandardPLPanel::handleExpansion( const QModelIndex& index )
165 assert( currentView );
166 currentView->scrollTo( index );
169 void StandardPLPanel::handleRootChange()
171 /* needed for popupAdd() */
172 PLItem *root = model->getItem( QModelIndex() );
173 currentRootId = root->id();
177 /* enable/disable adding */
178 if( currentRootId == THEPL->p_playing->i_id )
180 addButton->setEnabled( true );
181 addButton->setToolTip( qtr(I_PL_ADDPL) );
183 else if( THEPL->p_media_library &&
184 currentRootId == THEPL->p_media_library->i_id )
186 addButton->setEnabled( true );
187 addButton->setToolTip( qtr(I_PL_ADDML) );
190 addButton->setEnabled( false );
193 /* PopupAdd Menu for the Add Menu */
194 void StandardPLPanel::popupAdd()
197 if( currentRootId == THEPL->p_playing->i_id )
199 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
200 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
201 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
203 else if( THEPL->p_media_library &&
204 currentRootId == THEPL->p_media_library->i_id )
206 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
207 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
208 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
211 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
212 + QPoint( 0, addButton->height() ) );
215 void StandardPLPanel::popupPlView( const QPoint &point )
217 QModelIndex index = currentView->indexAt( point );
218 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
219 if( !index.isValid() ){
220 QVLCMenu::PopupMenu( p_intf, true );
224 QItemSelectionModel *selection = currentView->selectionModel();
225 QModelIndexList list = selection->selectedIndexes();
226 model->popup( index, globalPoint, list );
230 void StandardPLPanel::popupSelectColumn( QPoint pos )
235 /* We do not offer the option to hide index 0 column, or
236 * QTreeView will behave weird */
238 for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
240 QAction* option = menu.addAction(
241 qfu( psz_column_title( i ) ) );
242 option->setCheckable( true );
243 option->setChecked( !treeView->isColumnHidden( j ) );
244 selectColumnsSigMapper->setMapping( option, j );
245 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
247 menu.exec( QCursor::pos() );
250 void StandardPLPanel::toggleColumnShown( int i )
252 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
255 /* Search in the playlist */
256 void StandardPLPanel::search( const QString& searchText )
258 bool flat = currentView == iconView || currentView == listView;
259 model->search( searchText,
260 flat ? currentView->rootIndex() : QModelIndex(),
264 /* Set the root of the new Playlist */
265 /* This activated by the selector selection */
266 void StandardPLPanel::setRoot( playlist_item_t *p_item )
268 model->rebuild( p_item );
271 void StandardPLPanel::browseInto( const QModelIndex &index )
273 if( currentView == iconView || currentView == listView )
275 currentRootIndexId = model->itemId( index );;
276 currentView->setRootIndex( index );
279 locationBar->setIndex( index );
280 model->search( QString(), index, false );
284 void StandardPLPanel::browseInto( )
286 browseInto( currentRootIndexId != -1 && currentView != treeView ?
287 model->index( currentRootIndexId, 0 ) :
291 /* Delete and Suppr key remove the selection
292 FilterKey function and code function */
293 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
304 void StandardPLPanel::deleteSelection()
306 QItemSelectionModel *selection = currentView->selectionModel();
307 QModelIndexList list = selection->selectedIndexes();
308 model->doDelete( list );
311 void StandardPLPanel::createIconView()
313 iconView = new PlIconView( model, this );
314 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
315 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
316 this, popupPlView( const QPoint & ) );
317 CONNECT( iconView, activated( const QModelIndex & ),
318 this, activate( const QModelIndex & ) );
320 layout->addWidget( iconView, 1, 0, 1, -1 );
323 void StandardPLPanel::createListView()
325 listView = new PlListView( model, this );
326 listView->setContextMenuPolicy( Qt::CustomContextMenu );
327 CONNECT( listView, customContextMenuRequested( const QPoint & ),
328 this, popupPlView( const QPoint & ) );
329 CONNECT( listView, activated( const QModelIndex & ),
330 this, activate( const QModelIndex & ) );
332 layout->addWidget( listView, 1, 0, 1, -1 );
336 void StandardPLPanel::createTreeView()
338 /* Create and configure the QTreeView */
339 treeView = new QTreeView;
341 treeView->setIconSize( QSize( 20, 20 ) );
342 treeView->setAlternatingRowColors( true );
343 treeView->setAnimated( true );
344 treeView->setUniformRowHeights( true );
345 treeView->setSortingEnabled( true );
346 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
347 treeView->header()->setSortIndicatorShown( true );
348 treeView->header()->setClickable( true );
349 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
351 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
352 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
353 treeView->setDragEnabled( true );
354 treeView->setAcceptDrops( true );
355 treeView->setDropIndicatorShown( true );
356 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
358 /* setModel after setSortingEnabled(true), or the model will sort immediately! */
359 treeView->setModel( model );
361 if( getSettings()->contains( "headerStateV2" ) )
363 treeView->header()->restoreState(
364 getSettings()->value( "headerStateV2" ).toByteArray() );
368 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
370 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
371 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
372 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
376 /* Connections for the TreeView */
377 CONNECT( treeView, activated( const QModelIndex& ),
378 this, activate( const QModelIndex& ) );
379 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
380 this, popupSelectColumn( QPoint ) );
381 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
382 this, popupPlView( const QPoint & ) );
384 /* SignalMapper for columns */
385 selectColumnsSigMapper = new QSignalMapper( this );
386 CONNECT( selectColumnsSigMapper, mapped( int ),
387 this, toggleColumnShown( int ) );
389 /* Finish the layout */
390 layout->addWidget( treeView, 1, 0, 1, -1 );
393 void StandardPLPanel::showView( int i_view )
399 if( treeView == NULL )
401 if( iconView ) iconView->hide();
402 if( listView ) listView->hide();
404 currentView = treeView;
405 viewActions[i_view]->setChecked( true );
410 if( iconView == NULL )
413 if( treeView ) treeView->hide();
414 if( listView ) listView->hide();
416 currentView = iconView;
417 viewActions[i_view]->setChecked( true );
422 if( listView == NULL )
425 if( treeView ) treeView->hide();
426 if( iconView ) iconView->hide();
428 currentView = listView;
429 viewActions[i_view]->setChecked( true );
438 void StandardPLPanel::cycleViews()
440 if( currentView == iconView )
441 showView( TREE_VIEW );
442 else if( currentView == treeView )
443 showView( LIST_VIEW );
444 else if( currentView == listView )
445 showView( ICON_VIEW );
450 void StandardPLPanel::wheelEvent( QWheelEvent *e )
452 // Accept this event in order to prevent unwanted volume up/down changes
456 void StandardPLPanel::activate( const QModelIndex &index )
458 if( model->hasChildren( index ) )
460 if( currentView != treeView )
465 playlist_Lock( THEPL );
466 playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
467 p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
468 lastActivatedId = p_item->p_input->i_id;
469 playlist_Unlock( THEPL );
470 model->activateItem( index );
474 void StandardPLPanel::browseInto( input_item_t *p_input )
477 if( p_input->i_id != lastActivatedId ) return;
479 playlist_Lock( THEPL );
481 playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
484 playlist_Unlock( THEPL );
488 QModelIndex index = model->index( p_item->i_id, 0 );
490 playlist_Unlock( THEPL );
492 if( currentView == treeView )
493 treeView->setExpanded( index, true );
497 lastActivatedId = -1;
502 LocationBar::LocationBar( PLModel *m )
505 mapper = new QSignalMapper( this );
506 CONNECT( mapper, mapped( int ), this, invoke( int ) );
508 box = new QHBoxLayout;
509 box->setSpacing( 0 );
510 box->setContentsMargins( 0, 0, 0, 0 );
514 void LocationBar::setIndex( const QModelIndex &index )
516 qDeleteAll( buttons );
518 QModelIndex i = index;
522 PLItem *item = model->getItem( i );
524 char *fb_name = input_item_GetTitleFbName( item->inputItem() );
525 QString text = qfu(fb_name);
527 QAbstractButton *btn = new LocationButton( text, bold, i.isValid() );
528 btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
529 box->insertWidget( 0, btn, bold ? 1 : 0 );
530 buttons.append( btn );
532 mapper->setMapping( btn, item->id() );
533 CONNECT( btn, clicked( ), mapper, map( ) );
537 if( i.isValid() ) i = i.parent();
542 void LocationBar::setRootIndex()
544 setIndex( QModelIndex() );
547 void LocationBar::invoke( int i_id )
549 QModelIndex index = model->index( i_id, 0 );
550 emit invoked ( index );
553 LocationButton::LocationButton( const QString &text, bool bold, bool arrow )
557 font.setBold( bold );
564 void LocationButton::paintEvent ( QPaintEvent * event )
566 QStyleOptionButton option;
567 option.initFrom( this );
568 option.state |= QStyle::State_Enabled;
572 style()->drawControl( QStyle::CE_PushButtonBevel, &option, &p );
574 int margin = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this) + PADDING;
576 QRect rect = option.rect.adjusted( b_arrow ? 15 + margin : margin, 0, margin * -1, 0 );
577 p.drawText( rect, Qt::AlignVCenter,
578 fontMetrics().elidedText( text(), Qt::ElideRight, rect.width() ) );
582 option.rect.setX( margin );
583 option.rect.setWidth( 8 );
584 style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
588 QSize LocationButton::sizeHint() const
590 int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this);
591 QSize s( fontMetrics().boundingRect( text() ).size() );
592 s.setWidth( s.width() + ( 2 * frameWidth ) + ( 2 * PADDING ) + ( b_arrow ? 15 : 0 ) );
593 s.setHeight( QPushButton::sizeHint().height() );