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 QPushButton;
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_FileDialogContentsView ) );
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_local_category->i_id ||
166 currentRootId == THEPL->p_local_onelevel->i_id )
168 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
169 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
170 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
172 else if( ( THEPL->p_ml_category &&
173 currentRootId == THEPL->p_ml_category->i_id ) ||
174 ( THEPL->p_ml_onelevel &&
175 currentRootId == THEPL->p_ml_onelevel->i_id ) )
177 popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
178 popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
179 popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
182 popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
183 + QPoint( 0, addButton->height() ) );
186 void StandardPLPanel::popupPlView( const QPoint &point )
188 QModelIndex index = currentView->indexAt( point );
189 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
190 QItemSelectionModel *selection = currentView->selectionModel();
191 QModelIndexList list = selection->selectedIndexes();
192 model->popup( index, globalPoint, list );
195 void StandardPLPanel::popupSelectColumn( QPoint pos )
200 /* We do not offer the option to hide index 0 column, or
201 * QTreeView will behave weird */
203 for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
205 QAction* option = menu.addAction(
206 qfu( psz_column_title( i ) ) );
207 option->setCheckable( true );
208 option->setChecked( !treeView->isColumnHidden( j ) );
209 selectColumnsSigMapper->setMapping( option, j );
210 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
212 menu.exec( QCursor::pos() );
215 void StandardPLPanel::toggleColumnShown( int i )
217 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
220 /* Search in the playlist */
221 void StandardPLPanel::search( const QString& searchText )
223 model->search( searchText );
226 /* Set the root of the new Playlist */
227 /* This activated by the selector selection */
228 void StandardPLPanel::setRoot( playlist_item_t *p_item )
233 playlist_item_t *p_pref_item = playlist_GetPreferredNode( THEPL, p_item );
234 if( p_pref_item ) p_item = p_pref_item;
236 /* needed for popupAdd() */
237 currentRootId = p_item->i_id;
239 /* cosmetics, ..still need playlist locking.. */
240 /*char *psz_title = input_item_GetName( p_item->p_input );
241 title->setText( qfu(psz_title) );
247 model->rebuild( p_item );
249 locationBar->setIndex( QModelIndex() );
251 /* enable/disable adding */
252 if( p_item == THEPL->p_local_category ||
253 p_item == THEPL->p_local_onelevel )
255 addButton->setEnabled( true );
256 addButton->setToolTip( qtr(I_PL_ADDPL) );
258 else if( ( THEPL->p_ml_category && p_item == THEPL->p_ml_category) ||
259 ( THEPL->p_ml_onelevel && p_item == THEPL->p_ml_onelevel ) )
261 addButton->setEnabled( true );
262 addButton->setToolTip( qtr(I_PL_ADDML) );
265 addButton->setEnabled( false );
268 void StandardPLPanel::removeItem( int i_id )
270 model->removeItem( i_id );
273 /* Delete and Suppr key remove the selection
274 FilterKey function and code function */
275 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
286 void StandardPLPanel::deleteSelection()
288 QItemSelectionModel *selection = currentView->selectionModel();
289 QModelIndexList list = selection->selectedIndexes();
290 model->doDelete( list );
293 void StandardPLPanel::createIconView()
295 iconView = new PlIconView( model, this );
296 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
297 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
298 this, popupPlView( const QPoint & ) );
299 CONNECT( iconView, activated( const QModelIndex & ),
300 this, activate( const QModelIndex & ) );
301 CONNECT( locationBar, invoked( const QModelIndex & ),
302 iconView, setRootIndex( const QModelIndex & ) );
304 layout->addWidget( iconView, 1, 0, 1, -1 );
307 void StandardPLPanel::createTreeView()
309 /* Create and configure the QTreeView */
310 treeView = new QTreeView;
312 treeView->setIconSize( QSize( 20, 20 ) );
313 treeView->setAlternatingRowColors( true );
314 treeView->setAnimated( true );
315 treeView->setUniformRowHeights( true );
316 treeView->setSortingEnabled( true );
317 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
318 treeView->header()->setSortIndicatorShown( true );
319 treeView->header()->setClickable( true );
320 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
322 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
323 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
324 treeView->setDragEnabled( true );
325 treeView->setAcceptDrops( true );
326 treeView->setDropIndicatorShown( true );
327 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
329 /* setModel after setSortingEnabled(true), or the model will sort immediately! */
330 treeView->setModel( model );
332 if( getSettings()->contains( "headerStateV2" ) )
334 treeView->header()->restoreState(
335 getSettings()->value( "headerStateV2" ).toByteArray() );
339 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
341 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
342 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
343 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
347 /* Connections for the TreeView */
348 CONNECT( treeView, activated( const QModelIndex& ),
349 this, activate( const QModelIndex& ) );
350 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
351 this, popupSelectColumn( QPoint ) );
352 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
353 this, popupPlView( const QPoint & ) );
355 /* SignalMapper for columns */
356 selectColumnsSigMapper = new QSignalMapper( this );
357 CONNECT( selectColumnsSigMapper, mapped( int ),
358 this, toggleColumnShown( int ) );
360 /* Finish the layout */
361 layout->addWidget( treeView, 1, 0, 1, -1 );
364 void StandardPLPanel::showView( int i_view )
370 if( treeView == NULL )
372 locationBar->setIndex( treeView->rootIndex() );
373 if( iconView ) iconView->hide();
375 currentView = treeView;
376 treeViewAction->setChecked( true );
381 if( iconView == NULL )
384 locationBar->setIndex( iconView->rootIndex() );
385 if( treeView ) treeView->hide();
387 currentView = iconView;
388 iconViewAction->setChecked( true );
395 void StandardPLPanel::cycleViews()
397 if( currentView == iconView )
398 showView( TREE_VIEW );
399 else if( currentView == treeView )
400 showView( ICON_VIEW );
405 void StandardPLPanel::wheelEvent( QWheelEvent *e )
407 // Accept this event in order to prevent unwanted volume up/down changes
411 void StandardPLPanel::activate( const QModelIndex &index )
413 if( model->hasChildren( index ) )
415 if( currentView == iconView ) {
416 iconView->setRootIndex( index );
417 //title->setText( index.data().toString() );
418 locationBar->setIndex( index );
423 playlist_Lock( THEPL );
424 playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
425 p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
426 last_activated_id = p_item->p_input->i_id;//model->getItem( index )->inputItem()->i_id;
427 playlist_Unlock( THEPL );
428 model->activateItem( index );
432 void StandardPLPanel::browseInto( input_item_t *p_input )
435 if( p_input->i_id != last_activated_id ) return;
437 playlist_Lock( THEPL );
439 playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
440 assert( p_item != NULL );
442 QModelIndex index = model->index( p_item->i_id, 0 );
443 if( currentView == iconView ) {
444 iconView->setRootIndex( index );
445 locationBar->setIndex( index );
448 treeView->setExpanded( index, true );
450 last_activated_id = -1;
452 playlist_Unlock( THEPL );
455 LocationBar::LocationBar( PLModel *m )
458 mapper = new QSignalMapper( this );
459 CONNECT( mapper, mapped( int ), this, invoke( int ) );
462 void LocationBar::setIndex( const QModelIndex &index )
465 QAction *prev = NULL;
466 QModelIndex i = index;
468 QFontMetrics metrics( font );
469 font.setBold( true );
472 PLItem *item = model->getItem( i );
474 QToolButton *btn = new QToolButton;
475 char *fb_name = input_item_GetTitleFbName( item->inputItem() );
476 QString text = qfu(fb_name);
478 text = QString("/ ") + metrics.elidedText( text, Qt::ElideRight, 150 );
479 btn->setText( text );
480 btn->setFont( font );
481 prev = insertWidget( prev, btn );
483 mapper->setMapping( btn, item->id() );
484 CONNECT( btn, clicked( ), mapper, map( ) );
488 if( i.isValid() ) i = i.parent();
493 void LocationBar::invoke( int i_id )
495 QModelIndex index = model->index( i_id, 0 );
497 emit invoked ( index );