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"
36 #include <vlc_intf_strings.h>
38 #include <QPushButton>
39 #include <QHeaderView>
41 #include <QModelIndexList>
44 #include <QSignalMapper>
45 #include <QWheelEvent>
46 #include <QToolButton>
47 #include <QFontMetrics>
53 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
54 intf_thread_t *_p_intf,
55 playlist_t *p_playlist,
56 playlist_item_t *p_root ):
57 QWidget( _parent ), p_intf( _p_intf )
59 layout = new QGridLayout( this );
60 layout->setSpacing( 0 ); layout->setMargin( 0 );
61 setMinimumWidth( 300 );
66 model = new PLModel( p_playlist, p_intf, p_root, this );
68 last_activated_id = -1;
73 titleFont.setPointSize( titleFont.pointSize() + 6 );
74 titleFont.setFamily( "Verdana" );
75 title->setFont( titleFont );
76 layout->addWidget( title, 0, 0 );*/
78 locationBar = new LocationBar( model );
79 layout->addWidget( locationBar, 0, 0 );
81 /* A Spacer and the search possibilities */
82 layout->setColumnStretch( 1, 10 );
84 SearchLineEdit *search = new SearchLineEdit( this );
85 search->setMaximumWidth( 300 );
86 layout->addWidget( search, 0, 4 );
87 CONNECT( search, textChanged( const QString& ),
88 this, search( const QString& ) );
89 layout->setColumnStretch( 4, 2 );
91 /* Add item to the playlist button */
92 addButton = new QToolButton;
93 addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
94 addButton->setMaximumWidth( 30 );
95 BUTTONACT( addButton, popupAdd() );
96 layout->addWidget( addButton, 0, 3 );
98 /* Button to switch views */
99 QToolButton *viewButton = new QToolButton( this );
100 viewButton->setIcon( style()->standardIcon( QStyle::SP_FileDialogDetailedView ) );
101 layout->addWidget( viewButton, 0, 2 );
103 /* View selection menu */
104 viewSelectionMapper = new QSignalMapper( this );
105 CONNECT( viewSelectionMapper, mapped( int ), this, showView( int ) );
107 QActionGroup *actionGroup = new QActionGroup( this );
109 treeViewAction = actionGroup->addAction( "Detailed view" );
110 treeViewAction->setCheckable( true );
111 viewSelectionMapper->setMapping( treeViewAction, TREE_VIEW );
112 CONNECT( treeViewAction, triggered(), viewSelectionMapper, map() );
114 iconViewAction = actionGroup->addAction( "Icon view" );
115 iconViewAction->setCheckable( true );
116 viewSelectionMapper->setMapping( iconViewAction, ICON_VIEW );
117 CONNECT( iconViewAction, 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();
129 showView( i_viewMode );
131 getSettings()->endGroup();
133 CONNECT( THEMIM, leafBecameParent( input_item_t *),
134 this, browseInto( input_item_t * ) );
136 CONNECT( model, currentChanged( const QModelIndex& ),
137 this, handleExpansion( const QModelIndex& ) );
140 StandardPLPanel::~StandardPLPanel()
142 getSettings()->beginGroup("Playlist");
144 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
145 getSettings()->setValue( "view-mode", ( currentView == iconView ) ? ICON_VIEW : TREE_VIEW );
146 getSettings()->endGroup();
149 /* Unused anymore, but might be useful, like in right-click menu */
150 void StandardPLPanel::gotoPlayingItem()
152 currentView->scrollTo( model->currentIndex() );
155 void StandardPLPanel::handleExpansion( const QModelIndex& index )
157 assert( currentView );
158 currentView->scrollTo( index );
161 /* PopupAdd Menu for the Add Menu */
162 void StandardPLPanel::popupAdd()
165 if( currentRootId == THEPL->p_playing->i_id )
167 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
168 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
169 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
171 else if( THEPL->p_media_library &&
172 currentRootId == THEPL->p_media_library->i_id )
174 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
175 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
176 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
179 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
180 + QPoint( 0, addButton->height() ) );
183 void StandardPLPanel::popupPlView( const QPoint &point )
185 QModelIndex index = currentView->indexAt( point );
186 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
187 QItemSelectionModel *selection = currentView->selectionModel();
188 QModelIndexList list = selection->selectedIndexes();
189 model->popup( index, globalPoint, list );
192 void StandardPLPanel::popupSelectColumn( QPoint pos )
197 /* We do not offer the option to hide index 0 column, or
198 * QTreeView will behave weird */
200 for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
202 QAction* option = menu.addAction(
203 qfu( psz_column_title( i ) ) );
204 option->setCheckable( true );
205 option->setChecked( !treeView->isColumnHidden( j ) );
206 selectColumnsSigMapper->setMapping( option, j );
207 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
209 menu.exec( QCursor::pos() );
212 void StandardPLPanel::toggleColumnShown( int i )
214 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
217 /* Search in the playlist */
218 void StandardPLPanel::search( const QString& searchText )
220 model->search( searchText );
223 /* Set the root of the new Playlist */
224 /* This activated by the selector selection */
225 void StandardPLPanel::setRoot( playlist_item_t *p_item )
230 /* needed for popupAdd() */
231 currentRootId = p_item->i_id;
233 /* cosmetics, ..still need playlist locking.. */
234 /*char *psz_title = input_item_GetName( p_item->p_input );
235 title->setText( qfu(psz_title) );
241 model->rebuild( p_item );
243 locationBar->setIndex( QModelIndex() );
245 /* enable/disable adding */
246 if( p_item == THEPL->p_playing )
248 addButton->setEnabled( true );
249 addButton->setToolTip( qtr(I_PL_ADDPL) );
251 else if( THEPL->p_media_library && p_item == THEPL->p_media_library )
253 addButton->setEnabled( true );
254 addButton->setToolTip( qtr(I_PL_ADDML) );
257 addButton->setEnabled( false );
260 void StandardPLPanel::removeItem( int i_id )
262 model->removeItem( i_id );
265 /* Delete and Suppr key remove the selection
266 FilterKey function and code function */
267 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
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 CONNECT( locationBar, invoked( const QModelIndex & ),
294 iconView, setRootIndex( const QModelIndex & ) );
296 layout->addWidget( iconView, 1, 0, 1, -1 );
299 void StandardPLPanel::createTreeView()
301 /* Create and configure the QTreeView */
302 treeView = new QTreeView;
304 treeView->setIconSize( QSize( 20, 20 ) );
305 treeView->setAlternatingRowColors( true );
306 treeView->setAnimated( true );
307 treeView->setUniformRowHeights( true );
308 treeView->setSortingEnabled( true );
309 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
310 treeView->header()->setSortIndicatorShown( true );
311 treeView->header()->setClickable( true );
312 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
314 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
315 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
316 treeView->setDragEnabled( true );
317 treeView->setAcceptDrops( true );
318 treeView->setDropIndicatorShown( true );
319 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
321 /* setModel after setSortingEnabled(true), or the model will sort immediately! */
322 treeView->setModel( model );
324 if( getSettings()->contains( "headerStateV2" ) )
326 treeView->header()->restoreState(
327 getSettings()->value( "headerStateV2" ).toByteArray() );
331 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
333 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
334 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
335 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
339 /* Connections for the TreeView */
340 CONNECT( treeView, activated( const QModelIndex& ),
341 this, activate( const QModelIndex& ) );
342 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
343 this, popupSelectColumn( QPoint ) );
344 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
345 this, popupPlView( const QPoint & ) );
347 /* SignalMapper for columns */
348 selectColumnsSigMapper = new QSignalMapper( this );
349 CONNECT( selectColumnsSigMapper, mapped( int ),
350 this, toggleColumnShown( int ) );
352 /* Finish the layout */
353 layout->addWidget( treeView, 1, 0, 1, -1 );
356 void StandardPLPanel::showView( int i_view )
362 if( treeView == NULL )
364 locationBar->setIndex( treeView->rootIndex() );
365 if( iconView ) iconView->hide();
367 currentView = treeView;
368 treeViewAction->setChecked( true );
373 if( iconView == NULL )
376 locationBar->setIndex( iconView->rootIndex() );
377 if( treeView ) treeView->hide();
379 currentView = iconView;
380 iconViewAction->setChecked( true );
387 void StandardPLPanel::cycleViews()
389 if( currentView == iconView )
390 showView( TREE_VIEW );
391 else if( currentView == treeView )
392 showView( ICON_VIEW );
397 void StandardPLPanel::wheelEvent( QWheelEvent *e )
399 // Accept this event in order to prevent unwanted volume up/down changes
403 void StandardPLPanel::activate( const QModelIndex &index )
405 if( model->hasChildren( index ) )
407 if( currentView == iconView ) {
408 iconView->setRootIndex( index );
409 //title->setText( index.data().toString() );
410 locationBar->setIndex( index );
415 playlist_Lock( THEPL );
416 playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
417 p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
418 last_activated_id = p_item->p_input->i_id;//model->getItem( index )->inputItem()->i_id;
419 playlist_Unlock( THEPL );
420 model->activateItem( index );
424 void StandardPLPanel::browseInto( input_item_t *p_input )
427 if( p_input->i_id != last_activated_id ) return;
429 playlist_Lock( THEPL );
431 playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
434 playlist_Unlock( THEPL );
438 QModelIndex index = model->index( p_item->i_id, 0 );
440 playlist_Unlock( THEPL );
442 if( currentView == iconView ) {
443 iconView->setRootIndex( index );
444 locationBar->setIndex( index );
447 treeView->setExpanded( index, true );
449 last_activated_id = -1;
454 LocationBar::LocationBar( PLModel *m )
457 mapper = new QSignalMapper( this );
458 CONNECT( mapper, mapped( int ), this, invoke( int ) );
461 void LocationBar::setIndex( const QModelIndex &index )
464 QAction *prev = NULL;
465 QModelIndex i = index;
467 QFontMetrics metrics( font );
468 font.setBold( true );
471 PLItem *item = model->getItem( i );
473 QToolButton *btn = new QToolButton;
474 char *fb_name = input_item_GetTitleFbName( item->inputItem() );
475 QString text = qfu(fb_name);
477 text = QString("> ") + metrics.elidedText( text, Qt::ElideRight, 150 );
478 btn->setText( text );
479 btn->setFont( font );
480 prev = insertWidget( prev, btn );
482 mapper->setMapping( btn, item->id() );
483 CONNECT( btn, clicked( ), mapper, map( ) );
487 if( i.isValid() ) i = i.parent();
492 void LocationBar::invoke( int i_id )
494 QModelIndex index = model->index( i_id, 0 );
496 emit invoked ( index );