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 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
56 intf_thread_t *_p_intf,
57 playlist_t *p_playlist,
58 playlist_item_t *p_root ):
59 QWidget( _parent ), p_intf( _p_intf )
61 layout = new QGridLayout( this );
62 layout->setSpacing( 0 ); layout->setMargin( 0 );
63 setMinimumWidth( 300 );
68 model = new PLModel( p_playlist, p_intf, p_root, this );
70 last_activated_id = -1;
72 locationBar = new LocationBar( model );
73 layout->addWidget( locationBar, 0, 0 );
74 CONNECT( model, rootChanged(), locationBar, setRootIndex() );
76 /* A Spacer and the search possibilities */
77 layout->setColumnStretch( 1, 10 );
79 SearchLineEdit *search = new SearchLineEdit( this );
80 search->setMaximumWidth( 300 );
81 layout->addWidget( search, 0, 4 );
82 CONNECT( search, textChanged( const QString& ),
83 this, search( const QString& ) );
84 layout->setColumnStretch( 4, 10 );
86 /* Add item to the playlist button */
87 addButton = new QToolButton;
88 addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
89 addButton->setMaximumWidth( 30 );
90 BUTTONACT( addButton, popupAdd() );
91 layout->addWidget( addButton, 0, 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, 2 );
98 /* View selection menu */
99 viewSelectionMapper = new QSignalMapper( this );
100 CONNECT( viewSelectionMapper, mapped( int ), this, showView( int ) );
102 QActionGroup *actionGroup = new QActionGroup( this );
104 treeViewAction = actionGroup->addAction( "Detailed view" );
105 treeViewAction->setCheckable( true );
106 viewSelectionMapper->setMapping( treeViewAction, TREE_VIEW );
107 CONNECT( treeViewAction, triggered(), viewSelectionMapper, map() );
109 iconViewAction = actionGroup->addAction( "Icon view" );
110 iconViewAction->setCheckable( true );
111 viewSelectionMapper->setMapping( iconViewAction, ICON_VIEW );
112 CONNECT( iconViewAction, 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 getSettings()->setValue( "view-mode", ( currentView == iconView ) ? ICON_VIEW : TREE_VIEW );
142 getSettings()->endGroup();
145 /* Unused anymore, but might be useful, like in right-click menu */
146 void StandardPLPanel::gotoPlayingItem()
148 currentView->scrollTo( model->currentIndex() );
151 void StandardPLPanel::handleExpansion( const QModelIndex& index )
153 assert( currentView );
154 currentView->scrollTo( index );
157 void StandardPLPanel::handleRootChange()
159 /* needed for popupAdd() */
160 PLItem *root = model->getItem( QModelIndex() );
161 currentRootId = root->id();
163 locationBar->setIndex( QModelIndex() );
165 /* enable/disable adding */
166 if( currentRootId == THEPL->p_playing->i_id )
168 addButton->setEnabled( true );
169 addButton->setToolTip( qtr(I_PL_ADDPL) );
171 else if( THEPL->p_media_library &&
172 currentRootId == THEPL->p_media_library->i_id )
174 addButton->setEnabled( true );
175 addButton->setToolTip( qtr(I_PL_ADDML) );
178 addButton->setEnabled( false );
181 /* PopupAdd Menu for the Add Menu */
182 void StandardPLPanel::popupAdd()
185 if( currentRootId == THEPL->p_playing->i_id )
187 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
188 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
189 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
191 else if( THEPL->p_media_library &&
192 currentRootId == THEPL->p_media_library->i_id )
194 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
195 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
196 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
199 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
200 + QPoint( 0, addButton->height() ) );
203 void StandardPLPanel::popupPlView( const QPoint &point )
205 QModelIndex index = currentView->indexAt( point );
206 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
207 if( !index.isValid() ){
208 QVLCMenu::PopupMenu( p_intf, true );
212 QItemSelectionModel *selection = currentView->selectionModel();
213 QModelIndexList list = selection->selectedIndexes();
214 model->popup( index, globalPoint, list );
218 void StandardPLPanel::popupSelectColumn( QPoint pos )
223 /* We do not offer the option to hide index 0 column, or
224 * QTreeView will behave weird */
226 for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
228 QAction* option = menu.addAction(
229 qfu( psz_column_title( i ) ) );
230 option->setCheckable( true );
231 option->setChecked( !treeView->isColumnHidden( j ) );
232 selectColumnsSigMapper->setMapping( option, j );
233 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
235 menu.exec( QCursor::pos() );
238 void StandardPLPanel::toggleColumnShown( int i )
240 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
243 /* Search in the playlist */
244 void StandardPLPanel::search( const QString& searchText )
246 model->search( searchText );
249 /* Set the root of the new Playlist */
250 /* This activated by the selector selection */
251 void StandardPLPanel::setRoot( playlist_item_t *p_item )
253 model->rebuild( p_item );
256 /* Delete and Suppr key remove the selection
257 FilterKey function and code function */
258 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
269 void StandardPLPanel::deleteSelection()
271 QItemSelectionModel *selection = currentView->selectionModel();
272 QModelIndexList list = selection->selectedIndexes();
273 model->doDelete( list );
276 void StandardPLPanel::createIconView()
278 iconView = new PlIconView( model, this );
279 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
280 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
281 this, popupPlView( const QPoint & ) );
282 CONNECT( iconView, activated( const QModelIndex & ),
283 this, activate( const QModelIndex & ) );
284 CONNECT( locationBar, invoked( const QModelIndex & ),
285 iconView, setRootIndex( const QModelIndex & ) );
287 layout->addWidget( iconView, 1, 0, 1, -1 );
290 void StandardPLPanel::createTreeView()
292 /* Create and configure the QTreeView */
293 treeView = new QTreeView;
295 treeView->setIconSize( QSize( 20, 20 ) );
296 treeView->setAlternatingRowColors( true );
297 treeView->setAnimated( true );
298 treeView->setUniformRowHeights( true );
299 treeView->setSortingEnabled( true );
300 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
301 treeView->header()->setSortIndicatorShown( true );
302 treeView->header()->setClickable( true );
303 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
305 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
306 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
307 treeView->setDragEnabled( true );
308 treeView->setAcceptDrops( true );
309 treeView->setDropIndicatorShown( true );
310 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
312 /* setModel after setSortingEnabled(true), or the model will sort immediately! */
313 treeView->setModel( model );
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 );
330 /* Connections for the TreeView */
331 CONNECT( treeView, activated( const QModelIndex& ),
332 this, activate( const QModelIndex& ) );
333 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
334 this, popupSelectColumn( QPoint ) );
335 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
336 this, popupPlView( const QPoint & ) );
338 /* SignalMapper for columns */
339 selectColumnsSigMapper = new QSignalMapper( this );
340 CONNECT( selectColumnsSigMapper, mapped( int ),
341 this, toggleColumnShown( int ) );
343 /* Finish the layout */
344 layout->addWidget( treeView, 1, 0, 1, -1 );
347 void StandardPLPanel::showView( int i_view )
353 if( treeView == NULL )
355 locationBar->setIndex( treeView->rootIndex() );
356 if( iconView ) iconView->hide();
358 currentView = treeView;
359 treeViewAction->setChecked( true );
364 if( iconView == NULL )
367 locationBar->setIndex( iconView->rootIndex() );
368 if( treeView ) treeView->hide();
370 currentView = iconView;
371 iconViewAction->setChecked( true );
378 void StandardPLPanel::cycleViews()
380 if( currentView == iconView )
381 showView( TREE_VIEW );
382 else if( currentView == treeView )
383 showView( ICON_VIEW );
388 void StandardPLPanel::wheelEvent( QWheelEvent *e )
390 // Accept this event in order to prevent unwanted volume up/down changes
394 void StandardPLPanel::activate( const QModelIndex &index )
396 if( model->hasChildren( index ) )
398 if( currentView == iconView ) {
399 iconView->setRootIndex( index );
400 locationBar->setIndex( index );
405 playlist_Lock( THEPL );
406 playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
407 p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
408 last_activated_id = p_item->p_input->i_id;
409 playlist_Unlock( THEPL );
410 model->activateItem( index );
414 void StandardPLPanel::browseInto( input_item_t *p_input )
417 if( p_input->i_id != last_activated_id ) return;
419 playlist_Lock( THEPL );
421 playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
424 playlist_Unlock( THEPL );
428 QModelIndex index = model->index( p_item->i_id, 0 );
430 playlist_Unlock( THEPL );
432 if( currentView == iconView ) {
433 iconView->setRootIndex( index );
434 locationBar->setIndex( index );
437 treeView->setExpanded( index, true );
439 last_activated_id = -1;
444 LocationBar::LocationBar( PLModel *m )
447 mapper = new QSignalMapper( this );
448 CONNECT( mapper, mapped( int ), this, invoke( int ) );
450 box = new QHBoxLayout;
451 box->setSpacing( 0 );
452 box->setContentsMargins( 0, 0, 0, 0 );
456 void LocationBar::setIndex( const QModelIndex &index )
458 qDeleteAll( buttons );
460 QModelIndex i = index;
464 PLItem *item = model->getItem( i );
466 char *fb_name = input_item_GetTitleFbName( item->inputItem() );
467 QString text = qfu(fb_name);
469 QAbstractButton *btn = new LocationButton( text, bold, i.isValid() );
470 if( bold ) btn->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
471 box->insertWidget( 0, btn, bold ? 1 : 0 );
472 buttons.append( btn );
474 mapper->setMapping( btn, item->id() );
475 CONNECT( btn, clicked( ), mapper, map( ) );
479 if( i.isValid() ) i = i.parent();
484 void LocationBar::setRootIndex()
486 setIndex( QModelIndex() );
489 void LocationBar::invoke( int i_id )
491 QModelIndex index = model->index( i_id, 0 );
493 emit invoked ( index );
496 LocationButton::LocationButton( const QString &text, bool bold, bool arrow )
500 font.setBold( bold );
507 void LocationButton::paintEvent ( QPaintEvent * event )
509 QStyleOptionButton option;
510 option.initFrom( this );
511 option.state |= QStyle::State_Enabled;
515 style()->drawControl( QStyle::CE_PushButtonBevel, &option, &p );
517 int margin = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this) + PADDING;
519 QRect rect = option.rect.adjusted( b_arrow ? 15 + margin : margin, 0, margin * -1, 0 );
520 p.drawText( rect, Qt::AlignVCenter,
521 fontMetrics().elidedText( text(), Qt::ElideRight, rect.width() ) );
525 option.rect.setX( margin );
526 option.rect.setWidth( 8 );
527 style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
531 QSize LocationButton::sizeHint() const
533 int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this);
534 QSize s( fontMetrics().boundingRect( text() ).size() );
535 s.setWidth( s.width() + ( 2 * frameWidth ) + ( 2 * PADDING ) + ( b_arrow ? 15 : 0 ) );
536 s.setHeight( QPushButton::sizeHint().height() );