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 "components/playlist/selector.hpp"
35 #include "util/customwidgets.hpp"
38 #include <vlc_intf_strings.h>
40 #include <QPushButton>
41 #include <QHeaderView>
43 #include <QModelIndexList>
46 #include <QSignalMapper>
47 #include <QWheelEvent>
48 #include <QToolButton>
49 #include <QFontMetrics>
51 #include <QStackedLayout>
57 static const QString viewNames[] = { qtr( "Detailed View" ),
61 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
62 intf_thread_t *_p_intf,
63 playlist_t *p_playlist,
64 playlist_item_t *p_root,
65 PLSelector *_p_selector ):
66 QWidget( _parent ), p_intf( _p_intf ),
67 p_selector( _p_selector )
69 layout = new QGridLayout( this );
70 layout->setSpacing( 0 ); layout->setMargin( 0 );
71 setMinimumWidth( 300 );
76 viewStack = new QStackedLayout();
77 layout->addLayout( viewStack, 1, 0, 1, -1 );
79 model = new PLModel( p_playlist, p_intf, p_root, this );
81 currentRootIndexId = -1;
84 locationBar = new LocationBar( model );
85 locationBar->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Preferred );
86 layout->addWidget( locationBar, 0, 0 );
87 layout->setColumnStretch( 0, 5 );
88 CONNECT( locationBar, invoked( const QModelIndex & ),
89 this, browseInto( const QModelIndex & ) );
91 searchEdit = new SearchLineEdit( this );
92 searchEdit->setMaximumWidth( 250 );
93 searchEdit->setMinimumWidth( 80 );
94 layout->addWidget( searchEdit, 0, 2 );
95 CONNECT( searchEdit, textChanged( const QString& ),
96 this, search( const QString& ) );
97 layout->setColumnStretch( 2, 3 );
99 /* Button to switch views */
100 QToolButton *viewButton = new QToolButton( this );
101 viewButton->setIcon( style()->standardIcon( QStyle::SP_FileDialogDetailedView ) );
102 viewButton->setToolTip( qtr("Change playlistview"));
103 layout->addWidget( viewButton, 0, 1 );
105 /* View selection menu */
106 viewSelectionMapper = new QSignalMapper( this );
107 CONNECT( viewSelectionMapper, mapped( int ), this, showView( int ) );
109 QActionGroup *actionGroup = new QActionGroup( this );
111 for( int i = 0; i < VIEW_COUNT; i++ )
113 viewActions[i] = actionGroup->addAction( viewNames[i] );
114 viewActions[i]->setCheckable( true );
115 viewSelectionMapper->setMapping( viewActions[i], i );
116 CONNECT( viewActions[i], triggered(), viewSelectionMapper, map() );
119 BUTTONACT( viewButton, cycleViews() );
120 QMenu *viewMenu = new QMenu( this );
121 viewMenu->addActions( actionGroup->actions() );
123 viewButton->setMenu( viewMenu );
126 getSettings()->beginGroup("Playlist");
128 int i_viewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
130 getSettings()->endGroup();
132 showView( i_viewMode );
134 DCONNECT( THEMIM, leafBecameParent( input_item_t *),
135 this, browseInto( input_item_t * ) );
137 CONNECT( model, currentChanged( const QModelIndex& ),
138 this, handleExpansion( const QModelIndex& ) );
139 CONNECT( model, rootChanged(), this, handleRootChange() );
142 StandardPLPanel::~StandardPLPanel()
144 getSettings()->beginGroup("Playlist");
146 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
147 if( currentView == treeView )
148 getSettings()->setValue( "view-mode", TREE_VIEW );
149 else if( currentView == listView )
150 getSettings()->setValue( "view-mode", LIST_VIEW );
151 else if( currentView == iconView )
152 getSettings()->setValue( "view-mode", ICON_VIEW );
153 getSettings()->endGroup();
156 /* Unused anymore, but might be useful, like in right-click menu */
157 void StandardPLPanel::gotoPlayingItem()
159 currentView->scrollTo( model->currentIndex() );
162 void StandardPLPanel::handleExpansion( const QModelIndex& index )
164 assert( currentView );
165 if( currentRootIndexId != -1 && currentRootIndexId != model->itemId( index.parent() ) )
166 browseInto( index.parent() );
167 currentView->scrollTo( index );
170 void StandardPLPanel::handleRootChange()
175 void StandardPLPanel::popupPlView( const QPoint &point )
177 QModelIndex index = currentView->indexAt( point );
178 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
179 QItemSelectionModel *selection = currentView->selectionModel();
180 QModelIndexList list = selection->selectedIndexes();
182 if( !model->popup( index, globalPoint, list ) )
183 QVLCMenu::PopupMenu( p_intf, true );
186 void StandardPLPanel::popupSelectColumn( QPoint pos )
191 /* We do not offer the option to hide index 0 column, or
192 * QTreeView will behave weird */
194 for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
196 QAction* option = menu.addAction(
197 qfu( psz_column_title( i ) ) );
198 option->setCheckable( true );
199 option->setChecked( !treeView->isColumnHidden( j ) );
200 selectColumnsSigMapper->setMapping( option, j );
201 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
203 menu.exec( QCursor::pos() );
206 void StandardPLPanel::toggleColumnShown( int i )
208 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
211 /* Search in the playlist */
212 void StandardPLPanel::search( const QString& searchText )
216 p_selector->getCurrentSelectedItem( &type, &name );
217 if( type != SD_TYPE )
219 bool flat = currentView == iconView || currentView == listView;
220 model->search( searchText,
221 flat ? currentView->rootIndex() : QModelIndex(),
226 if( !name.isEmpty() )
227 playlist_QueryServicesDiscovery( THEPL, qtu(name), qtu(searchText) );
231 /* Set the root of the new Playlist */
232 /* This activated by the selector selection */
233 void StandardPLPanel::setRoot( playlist_item_t *p_item )
235 model->rebuild( p_item );
238 void StandardPLPanel::browseInto( const QModelIndex &index )
240 if( currentView == iconView || currentView == listView )
242 currentRootIndexId = model->itemId( index );
243 currentView->setRootIndex( index );
246 locationBar->setIndex( index );
250 void StandardPLPanel::browseInto( )
252 browseInto( currentRootIndexId != -1 && currentView != treeView ?
253 model->index( currentRootIndexId, 0 ) :
257 void StandardPLPanel::wheelEvent( QWheelEvent *e )
259 // Accept this event in order to prevent unwanted volume up/down changes
263 bool StandardPLPanel::eventFilter ( QObject * watched, QEvent * event )
265 if (event->type() == QEvent::KeyPress)
267 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
268 if( keyEvent->key() == Qt::Key_Delete ||
269 keyEvent->key() == Qt::Key_Backspace )
278 void StandardPLPanel::deleteSelection()
280 QItemSelectionModel *selection = currentView->selectionModel();
281 QModelIndexList list = selection->selectedIndexes();
282 model->doDelete( list );
285 void StandardPLPanel::createIconView()
287 iconView = new PlIconView( model, this );
288 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
289 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
290 this, popupPlView( const QPoint & ) );
291 CONNECT( iconView, activated( const QModelIndex & ),
292 this, activate( const QModelIndex & ) );
293 iconView->installEventFilter( this );
294 viewStack->addWidget( iconView );
297 void StandardPLPanel::createListView()
299 listView = new PlListView( model, this );
300 listView->setContextMenuPolicy( Qt::CustomContextMenu );
301 CONNECT( listView, customContextMenuRequested( const QPoint & ),
302 this, popupPlView( const QPoint & ) );
303 CONNECT( listView, activated( const QModelIndex & ),
304 this, activate( const QModelIndex & ) );
305 listView->installEventFilter( this );
306 viewStack->addWidget( listView );
310 void StandardPLPanel::createTreeView()
312 /* Create and configure the QTreeView */
313 treeView = new PlTreeView;
315 treeView->setIconSize( QSize( 20, 20 ) );
316 treeView->setAlternatingRowColors( true );
317 treeView->setAnimated( true );
318 treeView->setUniformRowHeights( true );
319 treeView->setSortingEnabled( true );
320 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
321 treeView->header()->setSortIndicatorShown( true );
322 treeView->header()->setClickable( true );
323 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
325 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
326 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
327 treeView->setDragEnabled( true );
328 treeView->setAcceptDrops( true );
329 treeView->setDropIndicatorShown( true );
330 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
332 /* setModel after setSortingEnabled(true), or the model will sort immediately! */
333 treeView->setModel( model );
335 getSettings()->beginGroup("Playlist");
337 if( getSettings()->contains( "headerStateV2" ) )
339 treeView->header()->restoreState(
340 getSettings()->value( "headerStateV2" ).toByteArray() );
344 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
346 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
347 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
348 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
352 getSettings()->endGroup();
354 /* Connections for the TreeView */
355 CONNECT( treeView, activated( const QModelIndex& ),
356 this, activate( const QModelIndex& ) );
357 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
358 this, popupSelectColumn( QPoint ) );
359 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
360 this, popupPlView( const QPoint & ) );
361 treeView->installEventFilter( this );
363 /* SignalMapper for columns */
364 selectColumnsSigMapper = new QSignalMapper( this );
365 CONNECT( selectColumnsSigMapper, mapped( int ),
366 this, toggleColumnShown( int ) );
368 /* Finish the layout */
369 viewStack->addWidget( treeView );
372 void StandardPLPanel::showView( int i_view )
378 if( treeView == NULL )
380 currentView = treeView;
385 if( iconView == NULL )
387 currentView = iconView;
392 if( listView == NULL )
394 currentView = listView;
400 viewStack->setCurrentWidget( currentView );
401 viewActions[i_view]->setChecked( true );
406 void StandardPLPanel::cycleViews()
408 if( currentView == iconView )
409 showView( TREE_VIEW );
410 else if( currentView == treeView )
411 showView( LIST_VIEW );
412 else if( currentView == listView )
413 showView( ICON_VIEW );
418 void StandardPLPanel::activate( const QModelIndex &index )
420 if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
422 if( currentView != treeView )
427 playlist_Lock( THEPL );
428 playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
429 p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
430 lastActivatedId = p_item->p_input->i_id;
431 playlist_Unlock( THEPL );
432 model->activateItem( index );
436 void StandardPLPanel::browseInto( input_item_t *p_input )
439 if( p_input->i_id != lastActivatedId ) return;
441 playlist_Lock( THEPL );
443 playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
446 playlist_Unlock( THEPL );
450 QModelIndex index = model->index( p_item->i_id, 0 );
452 playlist_Unlock( THEPL );
454 if( currentView == treeView )
455 treeView->setExpanded( index, true );
459 lastActivatedId = -1;
464 LocationBar::LocationBar( PLModel *m )
467 mapper = new QSignalMapper( this );
468 CONNECT( mapper, mapped( int ), this, invoke( int ) );
470 btnMore = new LocationButton( "...", false, true, this );
471 menuMore = new QMenu( this );
472 btnMore->setMenu( menuMore );
475 void LocationBar::setIndex( const QModelIndex &index )
477 qDeleteAll( buttons );
479 qDeleteAll( actions );
482 QModelIndex i = index;
487 PLItem *item = model->getItem( i );
489 char *fb_name = input_item_GetTitleFbName( item->inputItem() );
490 QString text = qfu(fb_name);
493 QAbstractButton *btn = new LocationButton( text, first, !first, this );
494 btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
495 buttons.append( btn );
497 QAction *action = new QAction( text, this );
498 actions.append( action );
499 CONNECT( btn, clicked(), action, trigger() );
501 mapper->setMapping( action, item->id() );
502 CONNECT( action, triggered(), mapper, map() );
506 if( i.isValid() ) i = i.parent();
511 for( int a = actions.count() - 1; a >= 0 ; a-- )
513 actions[a]->setText( prefix + actions[a]->text() );
514 prefix += QString(" ");
517 if( isVisible() ) layOut( size() );
520 void LocationBar::setRootIndex()
522 setIndex( QModelIndex() );
525 void LocationBar::invoke( int i_id )
527 QModelIndex index = model->index( i_id, 0 );
528 emit invoked ( index );
531 void LocationBar::layOut( const QSize& size )
536 int count = buttons.count();
538 for( int i = 0; i < count; i++ )
540 int w = buttons[i]->sizeHint().width();
543 if( totalWidth > size.width() ) break;
547 int shown = widths.count();
549 if( totalWidth > size.width() && count > 1 )
551 QSize sz = btnMore->sizeHint();
552 btnMore->setGeometry( 0, 0, sz.width(), size.height() );
561 for( int i = count - 1; i >= 0; i-- )
563 if( totalWidth <= size.width() || i == 0)
565 buttons[i]->setGeometry( x, 0, qMin( size.width() - x, widths[i] ), size.height() );
568 totalWidth -= widths[i];
572 menuMore->addAction( actions[i] );
574 if( i < shown ) totalWidth -= widths[i];
579 void LocationBar::resizeEvent ( QResizeEvent * event )
581 layOut( event->size() );
584 QSize LocationBar::sizeHint() const
586 return btnMore->sizeHint();
589 LocationButton::LocationButton( const QString &text, bool bold,
590 bool arrow, QWidget * parent )
591 : b_arrow( arrow ), QPushButton( parent )
594 font.setBold( bold );
601 void LocationButton::paintEvent ( QPaintEvent * event )
603 QStyleOptionButton option;
604 option.initFrom( this );
605 option.state |= QStyle::State_Enabled;
611 p.setRenderHint( QPainter::Antialiasing, true );
612 QColor c = palette().color( QPalette::Highlight );
614 p.setBrush( c.lighter( 150 ) );
616 p.drawRoundedRect( option.rect.adjusted( 0, 2, 0, -2 ), 5, 5 );
620 QRect r = option.rect.adjusted( PADDING, 0, -PADDING - (b_arrow ? 10 : 0), 0 );
622 QString str( text() );
623 /* This check is absurd, but either it is not done properly inside elidedText(),
624 or boundingRect() is wrong */
625 if( r.width() < fontMetrics().boundingRect( text() ).width() )
626 str = fontMetrics().elidedText( text(), Qt::ElideRight, r.width() );
627 p.drawText( r, Qt::AlignVCenter | Qt::AlignLeft, str );
631 option.rect.setWidth( 10 );
632 option.rect.moveRight( rect().right() );
633 style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
637 QSize LocationButton::sizeHint() const
639 QSize s( fontMetrics().boundingRect( text() ).size() );
640 /* Add two pixels to width: font metrics are buggy, if you pass text through elidation
641 with exactly the width of its bounding rect, sometimes it still elides */
642 s.setWidth( s.width() + ( 2 * PADDING ) + ( b_arrow ? 10 : 0 ) + 2 );
643 s.setHeight( s.height() + 2 * PADDING );