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>
50 #include <QStackedLayout>
56 static const QString viewNames[] = { qtr( "Detailed View" ),
60 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
61 intf_thread_t *_p_intf,
62 playlist_t *p_playlist,
63 playlist_item_t *p_root ):
64 QWidget( _parent ), p_intf( _p_intf )
66 layout = new QGridLayout( this );
67 layout->setSpacing( 0 ); layout->setMargin( 0 );
68 setMinimumWidth( 300 );
73 viewStack = new QStackedLayout();
74 layout->addLayout( viewStack, 1, 0, 1, -1 );
76 model = new PLModel( p_playlist, p_intf, p_root, this );
78 currentRootIndexId = -1;
81 locationBar = new LocationBar( model );
82 locationBar->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Preferred );
83 layout->addWidget( locationBar, 0, 0 );
84 layout->setColumnStretch( 0, 5 );
85 CONNECT( locationBar, invoked( const QModelIndex & ),
86 this, browseInto( const QModelIndex & ) );
88 searchEdit = new SearchLineEdit( this );
89 searchEdit->setMaximumWidth( 250 );
90 searchEdit->setMinimumWidth( 80 );
91 layout->addWidget( searchEdit, 0, 2 );
92 CONNECT( searchEdit, textChanged( const QString& ),
93 this, search( const QString& ) );
94 layout->setColumnStretch( 2, 3 );
96 /* Button to switch views */
97 QToolButton *viewButton = new QToolButton( this );
98 viewButton->setIcon( style()->standardIcon( QStyle::SP_FileDialogDetailedView ) );
99 viewButton->setToolTip( qtr("Change playlistview"));
100 layout->addWidget( viewButton, 0, 1 );
102 /* View selection menu */
103 viewSelectionMapper = new QSignalMapper( this );
104 CONNECT( viewSelectionMapper, mapped( int ), this, showView( int ) );
106 QActionGroup *actionGroup = new QActionGroup( this );
108 for( int i = 0; i < VIEW_COUNT; i++ )
110 viewActions[i] = actionGroup->addAction( viewNames[i] );
111 viewActions[i]->setCheckable( true );
112 viewSelectionMapper->setMapping( viewActions[i], i );
113 CONNECT( viewActions[i], triggered(), viewSelectionMapper, map() );
116 BUTTONACT( viewButton, cycleViews() );
117 QMenu *viewMenu = new QMenu( this );
118 viewMenu->addActions( actionGroup->actions() );
120 viewButton->setMenu( viewMenu );
123 getSettings()->beginGroup("Playlist");
125 int i_viewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
127 getSettings()->endGroup();
129 showView( i_viewMode );
131 DCONNECT( THEMIM, leafBecameParent( input_item_t *),
132 this, browseInto( input_item_t * ) );
134 CONNECT( model, currentChanged( const QModelIndex& ),
135 this, handleExpansion( const QModelIndex& ) );
136 CONNECT( model, rootChanged(), this, handleRootChange() );
139 StandardPLPanel::~StandardPLPanel()
141 getSettings()->beginGroup("Playlist");
143 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
144 if( currentView == treeView )
145 getSettings()->setValue( "view-mode", TREE_VIEW );
146 else if( currentView == listView )
147 getSettings()->setValue( "view-mode", LIST_VIEW );
148 else if( currentView == iconView )
149 getSettings()->setValue( "view-mode", ICON_VIEW );
150 getSettings()->endGroup();
153 /* Unused anymore, but might be useful, like in right-click menu */
154 void StandardPLPanel::gotoPlayingItem()
156 currentView->scrollTo( model->currentIndex() );
159 void StandardPLPanel::handleExpansion( const QModelIndex& index )
161 assert( currentView );
162 if( currentRootIndexId != -1 && currentRootIndexId != model->itemId( index.parent() ) )
163 browseInto( index.parent() );
164 currentView->scrollTo( index );
167 void StandardPLPanel::handleRootChange()
172 void StandardPLPanel::popupPlView( const QPoint &point )
174 QModelIndex index = currentView->indexAt( point );
175 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
176 QItemSelectionModel *selection = currentView->selectionModel();
177 QModelIndexList list = selection->selectedIndexes();
179 if( !model->popup( index, globalPoint, list ) )
180 QVLCMenu::PopupMenu( p_intf, true );
183 void StandardPLPanel::popupSelectColumn( QPoint pos )
188 /* We do not offer the option to hide index 0 column, or
189 * QTreeView will behave weird */
191 for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
193 QAction* option = menu.addAction(
194 qfu( psz_column_title( i ) ) );
195 option->setCheckable( true );
196 option->setChecked( !treeView->isColumnHidden( j ) );
197 selectColumnsSigMapper->setMapping( option, j );
198 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
200 menu.exec( QCursor::pos() );
203 void StandardPLPanel::toggleColumnShown( int i )
205 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
208 /* Search in the playlist */
209 void StandardPLPanel::search( const QString& searchText )
211 bool flat = currentView == iconView || currentView == listView;
212 model->search( searchText,
213 flat ? currentView->rootIndex() : QModelIndex(),
217 /* Set the root of the new Playlist */
218 /* This activated by the selector selection */
219 void StandardPLPanel::setRoot( playlist_item_t *p_item )
221 model->rebuild( p_item );
224 void StandardPLPanel::browseInto( const QModelIndex &index )
226 if( currentView == iconView || currentView == listView )
228 currentRootIndexId = model->itemId( index );
229 currentView->setRootIndex( index );
232 locationBar->setIndex( index );
236 void StandardPLPanel::browseInto( )
238 browseInto( currentRootIndexId != -1 && currentView != treeView ?
239 model->index( currentRootIndexId, 0 ) :
243 void StandardPLPanel::wheelEvent( QWheelEvent *e )
245 // Accept this event in order to prevent unwanted volume up/down changes
249 bool StandardPLPanel::eventFilter ( QObject * watched, QEvent * event )
251 if (event->type() == QEvent::KeyPress)
253 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
254 if( keyEvent->key() == Qt::Key_Delete ||
255 keyEvent->key() == Qt::Key_Backspace )
264 void StandardPLPanel::deleteSelection()
266 QItemSelectionModel *selection = currentView->selectionModel();
267 QModelIndexList list = selection->selectedIndexes();
268 model->doDelete( list );
271 void StandardPLPanel::createIconView()
273 iconView = new PlIconView( model, this );
274 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
275 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
276 this, popupPlView( const QPoint & ) );
277 CONNECT( iconView, activated( const QModelIndex & ),
278 this, activate( const QModelIndex & ) );
279 iconView->installEventFilter( this );
280 viewStack->addWidget( iconView );
283 void StandardPLPanel::createListView()
285 listView = new PlListView( model, this );
286 listView->setContextMenuPolicy( Qt::CustomContextMenu );
287 CONNECT( listView, customContextMenuRequested( const QPoint & ),
288 this, popupPlView( const QPoint & ) );
289 CONNECT( listView, activated( const QModelIndex & ),
290 this, activate( const QModelIndex & ) );
291 listView->installEventFilter( this );
292 viewStack->addWidget( listView );
296 void StandardPLPanel::createTreeView()
298 /* Create and configure the QTreeView */
299 treeView = new PlTreeView;
301 treeView->setIconSize( QSize( 20, 20 ) );
302 treeView->setAlternatingRowColors( true );
303 treeView->setAnimated( true );
304 treeView->setUniformRowHeights( true );
305 treeView->setSortingEnabled( true );
306 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
307 treeView->header()->setSortIndicatorShown( true );
308 treeView->header()->setClickable( true );
309 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
311 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
312 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
313 treeView->setDragEnabled( true );
314 treeView->setAcceptDrops( true );
315 treeView->setDropIndicatorShown( true );
316 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
318 /* setModel after setSortingEnabled(true), or the model will sort immediately! */
319 treeView->setModel( model );
321 getSettings()->beginGroup("Playlist");
323 if( getSettings()->contains( "headerStateV2" ) )
325 treeView->header()->restoreState(
326 getSettings()->value( "headerStateV2" ).toByteArray() );
330 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
332 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
333 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
334 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
338 getSettings()->endGroup();
340 /* Connections for the TreeView */
341 CONNECT( treeView, activated( const QModelIndex& ),
342 this, activate( const QModelIndex& ) );
343 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
344 this, popupSelectColumn( QPoint ) );
345 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
346 this, popupPlView( const QPoint & ) );
347 treeView->installEventFilter( this );
349 /* SignalMapper for columns */
350 selectColumnsSigMapper = new QSignalMapper( this );
351 CONNECT( selectColumnsSigMapper, mapped( int ),
352 this, toggleColumnShown( int ) );
354 /* Finish the layout */
355 viewStack->addWidget( treeView );
358 void StandardPLPanel::showView( int i_view )
364 if( treeView == NULL )
366 currentView = treeView;
371 if( iconView == NULL )
373 currentView = iconView;
378 if( listView == NULL )
380 currentView = listView;
386 viewStack->setCurrentWidget( currentView );
387 viewActions[i_view]->setChecked( true );
392 void StandardPLPanel::cycleViews()
394 if( currentView == iconView )
395 showView( TREE_VIEW );
396 else if( currentView == treeView )
397 showView( LIST_VIEW );
398 else if( currentView == listView )
399 showView( ICON_VIEW );
404 void StandardPLPanel::activate( const QModelIndex &index )
406 if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
408 if( currentView != treeView )
413 playlist_Lock( THEPL );
414 playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
415 p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
416 lastActivatedId = p_item->p_input->i_id;
417 playlist_Unlock( THEPL );
418 model->activateItem( index );
422 void StandardPLPanel::browseInto( input_item_t *p_input )
425 if( p_input->i_id != lastActivatedId ) return;
427 playlist_Lock( THEPL );
429 playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
432 playlist_Unlock( THEPL );
436 QModelIndex index = model->index( p_item->i_id, 0 );
438 playlist_Unlock( THEPL );
440 if( currentView == treeView )
441 treeView->setExpanded( index, true );
445 lastActivatedId = -1;
450 LocationBar::LocationBar( PLModel *m )
453 mapper = new QSignalMapper( this );
454 CONNECT( mapper, mapped( int ), this, invoke( int ) );
456 btnMore = new LocationButton( "...", false, true, this );
457 menuMore = new QMenu( this );
458 btnMore->setMenu( menuMore );
461 void LocationBar::setIndex( const QModelIndex &index )
463 qDeleteAll( buttons );
465 qDeleteAll( actions );
468 QModelIndex i = index;
473 PLItem *item = model->getItem( i );
475 char *fb_name = input_item_GetTitleFbName( item->inputItem() );
476 QString text = qfu(fb_name);
479 QAbstractButton *btn = new LocationButton( text, first, !first, this );
480 btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
481 buttons.append( btn );
483 QAction *action = new QAction( text, this );
484 actions.append( action );
485 CONNECT( btn, clicked(), action, trigger() );
487 mapper->setMapping( action, item->id() );
488 CONNECT( action, triggered(), mapper, map() );
492 if( i.isValid() ) i = i.parent();
497 for( int a = actions.count() - 1; a >= 0 ; a-- )
499 actions[a]->setText( prefix + actions[a]->text() );
500 prefix += QString(" ");
503 if( isVisible() ) layOut( size() );
506 void LocationBar::setRootIndex()
508 setIndex( QModelIndex() );
511 void LocationBar::invoke( int i_id )
513 QModelIndex index = model->index( i_id, 0 );
514 emit invoked ( index );
517 void LocationBar::layOut( const QSize& size )
522 int count = buttons.count();
524 for( int i = 0; i < count; i++ )
526 int w = buttons[i]->sizeHint().width();
529 if( totalWidth > size.width() ) break;
533 int shown = widths.count();
535 if( totalWidth > size.width() && count > 1 )
537 QSize sz = btnMore->sizeHint();
538 btnMore->setGeometry( 0, 0, sz.width(), size.height() );
547 for( int i = count - 1; i >= 0; i-- )
549 if( totalWidth <= size.width() || i == 0)
551 buttons[i]->setGeometry( x, 0, qMin( size.width() - x, widths[i] ), size.height() );
554 totalWidth -= widths[i];
558 menuMore->addAction( actions[i] );
560 if( i < shown ) totalWidth -= widths[i];
565 void LocationBar::resizeEvent ( QResizeEvent * event )
567 layOut( event->size() );
570 QSize LocationBar::sizeHint() const
572 return btnMore->sizeHint();
575 LocationButton::LocationButton( const QString &text, bool bold,
576 bool arrow, QWidget * parent )
577 : b_arrow( arrow ), QPushButton( parent )
580 font.setBold( bold );
587 void LocationButton::paintEvent ( QPaintEvent * event )
589 QStyleOptionButton option;
590 option.initFrom( this );
591 option.state |= QStyle::State_Enabled;
597 p.setRenderHint( QPainter::Antialiasing, true );
598 QColor c = palette().color( QPalette::Highlight );
600 p.setBrush( c.lighter( 150 ) );
602 p.drawRoundedRect( option.rect.adjusted( 0, 2, 0, -2 ), 5, 5 );
606 QRect r = option.rect.adjusted( PADDING, 0, -PADDING - (b_arrow ? 10 : 0), 0 );
608 QString str( text() );
609 /* This check is absurd, but either it is not done properly inside elidedText(),
610 or boundingRect() is wrong */
611 if( r.width() < fontMetrics().boundingRect( text() ).width() )
612 str = fontMetrics().elidedText( text(), Qt::ElideRight, r.width() );
613 p.drawText( r, Qt::AlignVCenter | Qt::AlignLeft, str );
617 option.rect.setWidth( 10 );
618 option.rect.moveRight( rect().right() );
619 style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
623 QSize LocationButton::sizeHint() const
625 QSize s( fontMetrics().boundingRect( text() ).size() );
626 /* Add two pixels to width: font metrics are buggy, if you pass text through elidation
627 with exactly the width of its bounding rect, sometimes it still elides */
628 s.setWidth( s.width() + ( 2 * PADDING ) + ( b_arrow ? 10 : 0 ) + 2 );
629 s.setHeight( s.height() + 2 * PADDING );