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::Ignored, QSizePolicy::Preferred );
80 layout->addWidget( locationBar, 0, 0 );
81 layout->setColumnStretch( 0, 5 );
82 CONNECT( locationBar, invoked( const QModelIndex & ),
83 this, browseInto( const QModelIndex & ) );
85 searchEdit = new SearchLineEdit( this );
86 searchEdit->setMaximumWidth( 250 );
87 searchEdit->setMinimumWidth( 80 );
88 layout->addWidget( searchEdit, 0, 2 );
89 CONNECT( searchEdit, textChanged( const QString& ),
90 this, search( const QString& ) );
91 layout->setColumnStretch( 2, 3 );
93 /* Button to switch views */
94 QToolButton *viewButton = new QToolButton( this );
95 viewButton->setIcon( style()->standardIcon( QStyle::SP_FileDialogDetailedView ) );
96 layout->addWidget( viewButton, 0, 1 );
98 /* View selection menu */
99 viewSelectionMapper = new QSignalMapper( this );
100 CONNECT( viewSelectionMapper, mapped( int ), this, showView( int ) );
102 QActionGroup *actionGroup = new QActionGroup( this );
104 for( int i = 0; i < VIEW_COUNT; i++ )
106 viewActions[i] = actionGroup->addAction( viewNames[i] );
107 viewActions[i]->setCheckable( true );
108 viewSelectionMapper->setMapping( viewActions[i], i );
109 CONNECT( viewActions[i], triggered(), viewSelectionMapper, map() );
112 BUTTONACT( viewButton, cycleViews() );
113 QMenu *viewMenu = new QMenu( this );
114 viewMenu->addActions( actionGroup->actions() );
116 viewButton->setMenu( viewMenu );
119 getSettings()->beginGroup("Playlist");
121 int i_viewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
122 showView( i_viewMode );
124 getSettings()->endGroup();
126 DCONNECT( THEMIM, leafBecameParent( input_item_t *),
127 this, browseInto( input_item_t * ) );
129 CONNECT( model, currentChanged( const QModelIndex& ),
130 this, handleExpansion( const QModelIndex& ) );
131 CONNECT( model, rootChanged(), this, handleRootChange() );
134 StandardPLPanel::~StandardPLPanel()
136 getSettings()->beginGroup("Playlist");
138 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
139 if( currentView == treeView )
140 getSettings()->setValue( "view-mode", TREE_VIEW );
141 else if( currentView == listView )
142 getSettings()->setValue( "view-mode", LIST_VIEW );
143 else if( currentView == iconView )
144 getSettings()->setValue( "view-mode", ICON_VIEW );
145 getSettings()->endGroup();
148 /* Unused anymore, but might be useful, like in right-click menu */
149 void StandardPLPanel::gotoPlayingItem()
151 currentView->scrollTo( model->currentIndex() );
154 void StandardPLPanel::handleExpansion( const QModelIndex& index )
156 assert( currentView );
157 currentView->scrollTo( index );
160 void StandardPLPanel::handleRootChange()
165 void StandardPLPanel::popupPlView( const QPoint &point )
167 QModelIndex index = currentView->indexAt( point );
168 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
169 QItemSelectionModel *selection = currentView->selectionModel();
170 QModelIndexList list = selection->selectedIndexes();
172 if( !model->popup( index, globalPoint, list ) )
173 QVLCMenu::PopupMenu( p_intf, true );
176 void StandardPLPanel::popupSelectColumn( QPoint pos )
181 /* We do not offer the option to hide index 0 column, or
182 * QTreeView will behave weird */
184 for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
186 QAction* option = menu.addAction(
187 qfu( psz_column_title( i ) ) );
188 option->setCheckable( true );
189 option->setChecked( !treeView->isColumnHidden( j ) );
190 selectColumnsSigMapper->setMapping( option, j );
191 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
193 menu.exec( QCursor::pos() );
196 void StandardPLPanel::toggleColumnShown( int i )
198 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
201 /* Search in the playlist */
202 void StandardPLPanel::search( const QString& searchText )
204 bool flat = currentView == iconView || currentView == listView;
205 model->search( searchText,
206 flat ? currentView->rootIndex() : QModelIndex(),
210 /* Set the root of the new Playlist */
211 /* This activated by the selector selection */
212 void StandardPLPanel::setRoot( playlist_item_t *p_item )
214 model->rebuild( p_item );
217 void StandardPLPanel::browseInto( const QModelIndex &index )
219 if( currentView == iconView || currentView == listView )
221 currentRootIndexId = model->itemId( index );;
222 currentView->setRootIndex( index );
225 locationBar->setIndex( index );
229 void StandardPLPanel::browseInto( )
231 browseInto( currentRootIndexId != -1 && currentView != treeView ?
232 model->index( currentRootIndexId, 0 ) :
236 void StandardPLPanel::wheelEvent( QWheelEvent *e )
238 // Accept this event in order to prevent unwanted volume up/down changes
242 bool StandardPLPanel::eventFilter ( QObject * watched, QEvent * event )
244 if (event->type() == QEvent::KeyPress)
246 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
247 if( keyEvent->key() == Qt::Key_Delete ||
248 keyEvent->key() == Qt::Key_Backspace )
257 void StandardPLPanel::deleteSelection()
259 QItemSelectionModel *selection = currentView->selectionModel();
260 QModelIndexList list = selection->selectedIndexes();
261 model->doDelete( list );
264 void StandardPLPanel::createIconView()
266 iconView = new PlIconView( model, this );
267 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
268 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
269 this, popupPlView( const QPoint & ) );
270 CONNECT( iconView, activated( const QModelIndex & ),
271 this, activate( const QModelIndex & ) );
272 iconView->installEventFilter( this );
273 layout->addWidget( iconView, 1, 0, 1, -1 );
276 void StandardPLPanel::createListView()
278 listView = new PlListView( model, this );
279 listView->setContextMenuPolicy( Qt::CustomContextMenu );
280 CONNECT( listView, customContextMenuRequested( const QPoint & ),
281 this, popupPlView( const QPoint & ) );
282 CONNECT( listView, activated( const QModelIndex & ),
283 this, activate( const QModelIndex & ) );
284 listView->installEventFilter( this );
285 layout->addWidget( listView, 1, 0, 1, -1 );
289 void StandardPLPanel::createTreeView()
291 /* Create and configure the QTreeView */
292 treeView = new QTreeView;
294 treeView->setIconSize( QSize( 20, 20 ) );
295 treeView->setAlternatingRowColors( true );
296 treeView->setAnimated( true );
297 treeView->setUniformRowHeights( true );
298 treeView->setSortingEnabled( true );
299 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
300 treeView->header()->setSortIndicatorShown( true );
301 treeView->header()->setClickable( true );
302 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
304 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
305 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
306 treeView->setDragEnabled( true );
307 treeView->setAcceptDrops( true );
308 treeView->setDropIndicatorShown( true );
309 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
311 /* setModel after setSortingEnabled(true), or the model will sort immediately! */
312 treeView->setModel( model );
314 getSettings()->beginGroup("Playlist");
315 if( getSettings()->contains( "headerStateV2" ) )
317 treeView->header()->restoreState(
318 getSettings()->value( "headerStateV2" ).toByteArray() );
322 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
324 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
325 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
326 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
329 getSettings()->endGroup();
331 /* Connections for the TreeView */
332 CONNECT( treeView, activated( const QModelIndex& ),
333 this, activate( const QModelIndex& ) );
334 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
335 this, popupSelectColumn( QPoint ) );
336 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
337 this, popupPlView( const QPoint & ) );
338 treeView->installEventFilter( this );
340 /* SignalMapper for columns */
341 selectColumnsSigMapper = new QSignalMapper( this );
342 CONNECT( selectColumnsSigMapper, mapped( int ),
343 this, toggleColumnShown( int ) );
345 /* Finish the layout */
346 layout->addWidget( treeView, 1, 0, 1, -1 );
349 void StandardPLPanel::showView( int i_view )
355 if( treeView == NULL )
357 if( iconView ) iconView->hide();
358 if( listView ) listView->hide();
360 currentView = treeView;
361 viewActions[i_view]->setChecked( true );
366 if( iconView == NULL )
369 if( treeView ) treeView->hide();
370 if( listView ) listView->hide();
372 currentView = iconView;
373 viewActions[i_view]->setChecked( true );
378 if( listView == NULL )
381 if( treeView ) treeView->hide();
382 if( iconView ) iconView->hide();
384 currentView = listView;
385 viewActions[i_view]->setChecked( true );
395 void StandardPLPanel::cycleViews()
397 if( currentView == iconView )
398 showView( TREE_VIEW );
399 else if( currentView == treeView )
400 showView( LIST_VIEW );
401 else if( currentView == listView )
402 showView( ICON_VIEW );
407 void StandardPLPanel::activate( const QModelIndex &index )
409 if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
411 if( currentView != treeView )
416 playlist_Lock( THEPL );
417 playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
418 p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
419 lastActivatedId = p_item->p_input->i_id;
420 playlist_Unlock( THEPL );
421 model->activateItem( index );
425 void StandardPLPanel::browseInto( input_item_t *p_input )
428 if( p_input->i_id != lastActivatedId ) return;
430 playlist_Lock( THEPL );
432 playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
435 playlist_Unlock( THEPL );
439 QModelIndex index = model->index( p_item->i_id, 0 );
441 playlist_Unlock( THEPL );
443 if( currentView == treeView )
444 treeView->setExpanded( index, true );
448 lastActivatedId = -1;
453 LocationBar::LocationBar( PLModel *m )
456 mapper = new QSignalMapper( this );
457 CONNECT( mapper, mapped( int ), this, invoke( int ) );
459 btnMore = new LocationButton( "...", false, true, this );
460 menuMore = new QMenu( this );
461 btnMore->setMenu( menuMore );
464 void LocationBar::setIndex( const QModelIndex &index )
466 qDeleteAll( buttons );
468 qDeleteAll( actions );
471 QModelIndex i = index;
476 PLItem *item = model->getItem( i );
478 char *fb_name = input_item_GetTitleFbName( item->inputItem() );
479 QString text = qfu(fb_name);
482 QAbstractButton *btn = new LocationButton( text, first, !first, this );
483 btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
484 buttons.append( btn );
486 QAction *action = new QAction( text, this );
487 actions.append( action );
488 CONNECT( btn, clicked(), action, trigger() );
490 mapper->setMapping( action, item->id() );
491 CONNECT( action, triggered(), mapper, map() );
495 if( i.isValid() ) i = i.parent();
500 for( int a = actions.count() - 1; a >= 0 ; a-- )
502 actions[a]->setText( prefix + actions[a]->text() );
503 prefix += QString(" ");
506 if( isVisible() ) layOut( size() );
509 void LocationBar::setRootIndex()
511 setIndex( QModelIndex() );
514 void LocationBar::invoke( int i_id )
516 QModelIndex index = model->index( i_id, 0 );
517 emit invoked ( index );
520 void LocationBar::layOut( const QSize& size )
525 int count = buttons.count();
527 for( int i = 0; i < count; i++ )
529 int w = buttons[i]->sizeHint().width();
532 if( totalWidth > size.width() ) break;
536 int shown = widths.count();
538 if( totalWidth > size.width() && count > 1 )
540 QSize sz = btnMore->sizeHint();
541 btnMore->setGeometry( 0, 0, sz.width(), size.height() );
550 for( int i = count - 1; i >= 0; i-- )
552 if( totalWidth <= size.width() || i == 0)
554 buttons[i]->setGeometry( x, 0, qMin( size.width() - x, widths[i] ), size.height() );
557 totalWidth -= widths[i];
561 menuMore->addAction( actions[i] );
563 if( i < shown ) totalWidth -= widths[i];
568 void LocationBar::resizeEvent ( QResizeEvent * event )
570 layOut( event->size() );
573 QSize LocationBar::sizeHint() const
575 return btnMore->sizeHint();
578 LocationButton::LocationButton( const QString &text, bool bold,
579 bool arrow, QWidget * parent )
580 : b_arrow( arrow ), QPushButton( parent )
583 font.setBold( bold );
590 void LocationButton::paintEvent ( QPaintEvent * event )
592 QStyleOptionButton option;
593 option.initFrom( this );
594 option.state |= QStyle::State_Enabled;
600 p.setRenderHint( QPainter::Antialiasing, true );
601 QColor c = palette().color( QPalette::Highlight );
603 p.setBrush( c.lighter( 150 ) );
605 p.drawRoundedRect( option.rect.adjusted( 0, 2, 0, -2 ), 5, 5 );
609 QRect r = option.rect.adjusted( PADDING, 0, -PADDING - (b_arrow ? 10 : 0), 0 );
611 QString str( text() );
612 /* This check is absurd, but either it is not done properly inside elidedText(),
613 or boundingRect() is wrong */
614 if( r.width() < fontMetrics().boundingRect( text() ).width() )
615 str = fontMetrics().elidedText( text(), Qt::ElideRight, r.width() );
616 p.drawText( r, Qt::AlignVCenter | Qt::AlignLeft, str );
620 option.rect.setWidth( 10 );
621 option.rect.moveRight( rect().right() );
622 style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
626 QSize LocationButton::sizeHint() const
628 QSize s( fontMetrics().boundingRect( text() ).size() );
629 /* Add two pixels to width: font metrics are buggy, if you pass text through elidation
630 with exactly the width of its bounding rect, sometimes it still elides */
631 s.setWidth( s.width() + ( 2 * PADDING ) + ( b_arrow ? 10 : 0 ) + 2 );
632 s.setHeight( s.height() + 2 * PADDING );