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, 0 );
81 layout->setColumnStretch( 0, 100 );
82 CONNECT( locationBar, invoked( const QModelIndex & ),
83 this, browseInto( const QModelIndex & ) );
85 layout->setColumnStretch( 1, 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 /* Button to switch views */
96 QToolButton *viewButton = new QToolButton( this );
97 viewButton->setIcon( style()->standardIcon( QStyle::SP_FileDialogDetailedView ) );
98 layout->addWidget( viewButton, 0, 2 );
100 /* View selection menu */
101 viewSelectionMapper = new QSignalMapper( this );
102 CONNECT( viewSelectionMapper, mapped( int ), this, showView( int ) );
104 QActionGroup *actionGroup = new QActionGroup( this );
106 for( int i = 0; i < VIEW_COUNT; i++ )
108 viewActions[i] = actionGroup->addAction( viewNames[i] );
109 viewActions[i]->setCheckable( true );
110 viewSelectionMapper->setMapping( viewActions[i], i );
111 CONNECT( viewActions[i], triggered(), viewSelectionMapper, map() );
114 BUTTONACT( viewButton, cycleViews() );
115 QMenu *viewMenu = new QMenu( this );
116 viewMenu->addActions( actionGroup->actions() );
118 viewButton->setMenu( viewMenu );
121 getSettings()->beginGroup("Playlist");
123 int i_viewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
124 showView( i_viewMode );
126 getSettings()->endGroup();
128 CONNECT( THEMIM, leafBecameParent( input_item_t *),
129 this, browseInto( input_item_t * ) );
131 CONNECT( model, currentChanged( const QModelIndex& ),
132 this, handleExpansion( const QModelIndex& ) );
133 CONNECT( model, rootChanged(), this, handleRootChange() );
136 StandardPLPanel::~StandardPLPanel()
138 getSettings()->beginGroup("Playlist");
140 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
141 if( currentView == treeView )
142 getSettings()->setValue( "view-mode", TREE_VIEW );
143 else if( currentView == listView )
144 getSettings()->setValue( "view-mode", LIST_VIEW );
145 else if( currentView == iconView )
146 getSettings()->setValue( "view-mode", ICON_VIEW );
147 getSettings()->endGroup();
150 /* Unused anymore, but might be useful, like in right-click menu */
151 void StandardPLPanel::gotoPlayingItem()
153 currentView->scrollTo( model->currentIndex() );
156 void StandardPLPanel::handleExpansion( const QModelIndex& index )
158 assert( currentView );
159 currentView->scrollTo( index );
162 void StandardPLPanel::handleRootChange()
167 void StandardPLPanel::popupPlView( const QPoint &point )
169 QModelIndex index = currentView->indexAt( point );
170 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
171 QItemSelectionModel *selection = currentView->selectionModel();
172 QModelIndexList list = selection->selectedIndexes();
174 if( !model->popup( index, globalPoint, list ) )
175 QVLCMenu::PopupMenu( p_intf, true );
178 void StandardPLPanel::popupSelectColumn( QPoint pos )
183 /* We do not offer the option to hide index 0 column, or
184 * QTreeView will behave weird */
186 for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
188 QAction* option = menu.addAction(
189 qfu( psz_column_title( i ) ) );
190 option->setCheckable( true );
191 option->setChecked( !treeView->isColumnHidden( j ) );
192 selectColumnsSigMapper->setMapping( option, j );
193 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
195 menu.exec( QCursor::pos() );
198 void StandardPLPanel::toggleColumnShown( int i )
200 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
203 /* Search in the playlist */
204 void StandardPLPanel::search( const QString& searchText )
206 bool flat = currentView == iconView || currentView == listView;
207 model->search( searchText,
208 flat ? currentView->rootIndex() : QModelIndex(),
212 /* Set the root of the new Playlist */
213 /* This activated by the selector selection */
214 void StandardPLPanel::setRoot( playlist_item_t *p_item )
216 model->rebuild( p_item );
219 void StandardPLPanel::browseInto( const QModelIndex &index )
221 if( currentView == iconView || currentView == listView )
223 currentRootIndexId = model->itemId( index );;
224 currentView->setRootIndex( index );
227 locationBar->setIndex( index );
228 model->search( QString(), index, false );
232 void StandardPLPanel::browseInto( )
234 browseInto( currentRootIndexId != -1 && currentView != treeView ?
235 model->index( currentRootIndexId, 0 ) :
239 void StandardPLPanel::wheelEvent( QWheelEvent *e )
241 // Accept this event in order to prevent unwanted volume up/down changes
245 bool StandardPLPanel::eventFilter ( QObject * watched, QEvent * event )
247 if (event->type() == QEvent::KeyPress)
249 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
250 if( keyEvent->key() == Qt::Key_Delete ||
251 keyEvent->key() == Qt::Key_Backspace )
260 void StandardPLPanel::deleteSelection()
262 QItemSelectionModel *selection = currentView->selectionModel();
263 QModelIndexList list = selection->selectedIndexes();
264 model->doDelete( list );
267 void StandardPLPanel::createIconView()
269 iconView = new PlIconView( model, this );
270 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
271 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
272 this, popupPlView( const QPoint & ) );
273 CONNECT( iconView, activated( const QModelIndex & ),
274 this, activate( const QModelIndex & ) );
275 iconView->installEventFilter( this );
276 layout->addWidget( iconView, 1, 0, 1, -1 );
279 void StandardPLPanel::createListView()
281 listView = new PlListView( model, this );
282 listView->setContextMenuPolicy( Qt::CustomContextMenu );
283 CONNECT( listView, customContextMenuRequested( const QPoint & ),
284 this, popupPlView( const QPoint & ) );
285 CONNECT( listView, activated( const QModelIndex & ),
286 this, activate( const QModelIndex & ) );
287 listView->installEventFilter( this );
288 layout->addWidget( listView, 1, 0, 1, -1 );
292 void StandardPLPanel::createTreeView()
294 /* Create and configure the QTreeView */
295 treeView = new QTreeView;
297 treeView->setIconSize( QSize( 20, 20 ) );
298 treeView->setAlternatingRowColors( true );
299 treeView->setAnimated( true );
300 treeView->setUniformRowHeights( true );
301 treeView->setSortingEnabled( true );
302 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
303 treeView->header()->setSortIndicatorShown( true );
304 treeView->header()->setClickable( true );
305 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
307 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
308 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
309 treeView->setDragEnabled( true );
310 treeView->setAcceptDrops( true );
311 treeView->setDropIndicatorShown( true );
312 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
314 /* setModel after setSortingEnabled(true), or the model will sort immediately! */
315 treeView->setModel( model );
317 if( getSettings()->contains( "headerStateV2" ) )
319 treeView->header()->restoreState(
320 getSettings()->value( "headerStateV2" ).toByteArray() );
324 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
326 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
327 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
328 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
332 /* Connections for the TreeView */
333 CONNECT( treeView, activated( const QModelIndex& ),
334 this, activate( const QModelIndex& ) );
335 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
336 this, popupSelectColumn( QPoint ) );
337 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
338 this, popupPlView( const QPoint & ) );
339 treeView->installEventFilter( this );
341 /* SignalMapper for columns */
342 selectColumnsSigMapper = new QSignalMapper( this );
343 CONNECT( selectColumnsSigMapper, mapped( int ),
344 this, toggleColumnShown( int ) );
346 /* Finish the layout */
347 layout->addWidget( treeView, 1, 0, 1, -1 );
350 void StandardPLPanel::showView( int i_view )
356 if( treeView == NULL )
358 if( iconView ) iconView->hide();
359 if( listView ) listView->hide();
361 currentView = treeView;
362 viewActions[i_view]->setChecked( true );
367 if( iconView == NULL )
370 if( treeView ) treeView->hide();
371 if( listView ) listView->hide();
373 currentView = iconView;
374 viewActions[i_view]->setChecked( true );
379 if( listView == NULL )
382 if( treeView ) treeView->hide();
383 if( iconView ) iconView->hide();
385 currentView = listView;
386 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( model->hasChildren( index ) )
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 box = new QHBoxLayout;
460 box->setSpacing( 0 );
461 box->setContentsMargins( 0, 0, 0, 0 );
465 void LocationBar::setIndex( const QModelIndex &index )
467 qDeleteAll( buttons );
469 QModelIndex i = index;
473 PLItem *item = model->getItem( i );
475 char *fb_name = input_item_GetTitleFbName( item->inputItem() );
476 QString text = qfu(fb_name);
478 QAbstractButton *btn = new LocationButton( text, bold, i.isValid() );
479 btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
480 box->insertWidget( 0, btn, bold ? 1 : 0 );
481 buttons.append( btn );
483 mapper->setMapping( btn, item->id() );
484 CONNECT( btn, clicked( ), mapper, map( ) );
488 if( i.isValid() ) i = i.parent();
493 void LocationBar::setRootIndex()
495 setIndex( QModelIndex() );
498 void LocationBar::invoke( int i_id )
500 QModelIndex index = model->index( i_id, 0 );
501 emit invoked ( index );
504 LocationButton::LocationButton( const QString &text, bool bold, bool arrow )
508 font.setBold( bold );
515 void LocationButton::paintEvent ( QPaintEvent * event )
517 QStyleOptionButton option;
518 option.initFrom( this );
519 option.state |= QStyle::State_Enabled;
523 style()->drawControl( QStyle::CE_PushButtonBevel, &option, &p );
525 int margin = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this) + PADDING;
527 QRect rect = option.rect.adjusted( b_arrow ? 15 + margin : margin, 0, margin * -1, 0 );
528 p.drawText( rect, Qt::AlignVCenter,
529 fontMetrics().elidedText( text(), Qt::ElideRight, rect.width() ) );
533 option.rect.setX( margin );
534 option.rect.setWidth( 8 );
535 style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
539 QSize LocationButton::sizeHint() const
541 int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this);
542 QSize s( fontMetrics().boundingRect( text() ).size() );
543 s.setWidth( s.width() + ( 2 * frameWidth ) + ( 2 * PADDING ) + ( b_arrow ? 15 : 0 ) );
544 s.setHeight( QPushButton::sizeHint().height() );