1 /*****************************************************************************
2 * standardpanel.cpp : The "standard" playlist panel : just a treeview
3 ****************************************************************************
4 * Copyright © 2000-2010 VideoLAN
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Jean-Baptiste 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 "components/playlist/standardpanel.hpp"
31 #include "components/playlist/vlc_model.hpp" /* VLCModel */
32 #include "components/playlist/playlist_model.hpp" /* PLModel */
33 #include "components/playlist/ml_model.hpp" /* MLModel */
34 #include "components/playlist/views.hpp" /* 3 views */
35 #include "components/playlist/selector.hpp" /* PLSelector */
36 #include "menus.hpp" /* Popup */
37 #include "input_manager.hpp" /* THEMIM */
39 #include "sorting.h" /* Columns order */
41 #include <vlc_services_discovery.h> /* SD_CMD_SEARCH */
43 #include <QHeaderView>
44 #include <QModelIndexList>
47 #include <QWheelEvent>
48 #include <QStackedLayout>
49 #include <QSignalMapper>
55 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
56 intf_thread_t *_p_intf,
57 playlist_item_t *p_root,
58 PLSelector *_p_selector,
65 p_selector( _p_selector )
67 viewStack = new QStackedLayout( this );
68 viewStack->setSpacing( 0 ); viewStack->setMargin( 0 );
69 setMinimumWidth( 300 );
76 currentRootIndexId = -1;
80 int i_savedViewMode = getSettings()->value( "Playlist/view-mode", TREE_VIEW ).toInt();
81 showView( i_savedViewMode );
83 DCONNECT( THEMIM, leafBecameParent( int ),
84 this, browseInto( int ) );
86 CONNECT( model, currentChanged( const QModelIndex& ),
87 this, handleExpansion( const QModelIndex& ) );
88 CONNECT( model, rootChanged(), this, browseInto() );
90 setRoot( p_root, false );
93 StandardPLPanel::~StandardPLPanel()
95 getSettings()->beginGroup("Playlist");
97 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
98 getSettings()->setValue( "view-mode", currentViewIndex() );
99 getSettings()->endGroup();
102 /* Unused anymore, but might be useful, like in right-click menu */
103 void StandardPLPanel::gotoPlayingItem()
105 currentView->scrollTo( model->currentIndex() );
108 void StandardPLPanel::handleExpansion( const QModelIndex& index )
110 assert( currentView );
111 if( currentRootIndexId != -1 && currentRootIndexId != model->itemId( index.parent() ) )
112 browseInto( index.parent() );
113 currentView->scrollTo( index );
116 void StandardPLPanel::popupPlView( const QPoint &point )
118 QModelIndex index = currentView->indexAt( point );
119 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
120 QItemSelectionModel *selection = currentView->selectionModel();
121 QModelIndexList list = selection->selectedIndexes();
123 if( !model->popup( index, globalPoint, list ) )
124 QVLCMenu::PopupMenu( p_intf, true );
127 void StandardPLPanel::popupSelectColumn( QPoint )
132 /* We do not offer the option to hide index 0 column, or
133 * QTreeView will behave weird */
134 for( int i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
136 QAction* option = menu.addAction( qfu( psz_column_title( i ) ) );
137 option->setCheckable( true );
138 option->setChecked( !treeView->isColumnHidden( j ) );
139 selectColumnsSigMapper->setMapping( option, j );
140 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
142 menu.exec( QCursor::pos() );
145 void StandardPLPanel::toggleColumnShown( int i )
147 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
150 /* Search in the playlist */
151 void StandardPLPanel::search( const QString& searchText )
155 p_selector->getCurrentSelectedItem( &type, &name );
156 if( type != SD_TYPE )
158 bool flat = ( currentView == iconView ||
159 currentView == listView ||
160 currentView == picFlowView );
161 model->search( searchText,
162 flat ? currentView->rootIndex() : QModelIndex(),
167 void StandardPLPanel::searchDelayed( const QString& searchText )
171 p_selector->getCurrentSelectedItem( &type, &name );
173 if( type == SD_TYPE )
175 if( !name.isEmpty() && !searchText.isEmpty() )
176 playlist_ServicesDiscoveryControl( THEPL, qtu( name ), SD_CMD_SEARCH,
181 /* Set the root of the new Playlist */
182 /* This activated by the selector selection */
183 void StandardPLPanel::setRoot( playlist_item_t *p_item, bool b )
188 msg_Dbg( p_intf, "Setting the SQL ML" );
189 currentView->setModel( mlmodel );
194 msg_Dbg( p_intf, "Normal PL/ML or SD" );
195 if( currentView->model() != model )
196 currentView->setModel( model );
197 model->rebuild( p_item );
201 void StandardPLPanel::browseInto( const QModelIndex &index )
203 if( currentView == iconView || currentView == listView || currentView == picFlowView )
205 currentRootIndexId = model->itemId( index );
206 currentView->setRootIndex( index );
209 emit viewChanged( index );
212 void StandardPLPanel::browseInto()
214 browseInto( (currentRootIndexId != -1 && currentView != treeView) ?
215 model->index( currentRootIndexId, 0 ) :
219 void StandardPLPanel::wheelEvent( QWheelEvent *e )
221 // Accept this event in order to prevent unwanted volume up/down changes
225 bool StandardPLPanel::eventFilter ( QObject *, QEvent * event )
227 if (event->type() == QEvent::KeyPress)
229 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
230 if( keyEvent->key() == Qt::Key_Delete ||
231 keyEvent->key() == Qt::Key_Backspace )
240 void StandardPLPanel::deleteSelection()
242 QItemSelectionModel *selection = currentView->selectionModel();
243 QModelIndexList list = selection->selectedIndexes();
244 model->doDelete( list );
247 void StandardPLPanel::createIconView()
249 iconView = new PlIconView( model, this );
250 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
251 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
252 this, popupPlView( const QPoint & ) );
253 CONNECT( iconView, activated( const QModelIndex & ),
254 this, activate( const QModelIndex & ) );
255 iconView->installEventFilter( this );
256 viewStack->addWidget( iconView );
259 void StandardPLPanel::createListView()
261 listView = new PlListView( model, this );
262 listView->setContextMenuPolicy( Qt::CustomContextMenu );
263 CONNECT( listView, customContextMenuRequested( const QPoint & ),
264 this, popupPlView( const QPoint & ) );
265 CONNECT( listView, activated( const QModelIndex & ),
266 this, activate( const QModelIndex & ) );
267 listView->installEventFilter( this );
268 viewStack->addWidget( listView );
271 void StandardPLPanel::createCoverView()
273 picFlowView = new PicFlowView( model, this );
274 picFlowView->setContextMenuPolicy( Qt::CustomContextMenu );
275 CONNECT( picFlowView, customContextMenuRequested( const QPoint & ),
276 this, popupPlView( const QPoint & ) );
277 CONNECT( picFlowView, activated( const QModelIndex & ),
278 this, activate( const QModelIndex & ) );
279 viewStack->addWidget( picFlowView );
280 picFlowView->installEventFilter( this );
283 void StandardPLPanel::createTreeView()
285 /* Create and configure the QTreeView */
286 treeView = new PlTreeView;
288 treeView->setIconSize( QSize( 20, 20 ) );
289 treeView->setAlternatingRowColors( true );
290 treeView->setAnimated( true );
291 treeView->setUniformRowHeights( true );
292 treeView->setSortingEnabled( true );
293 treeView->setAttribute( Qt::WA_MacShowFocusRect, false );
294 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
295 treeView->header()->setSortIndicatorShown( true );
296 treeView->header()->setClickable( true );
297 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
299 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
300 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
301 treeView->setDragEnabled( true );
302 treeView->setAcceptDrops( true );
303 treeView->setDropIndicatorShown( true );
304 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
306 /* setModel after setSortingEnabled(true), or the model will sort immediately! */
308 getSettings()->beginGroup("Playlist");
310 if( getSettings()->contains( "headerStateV2" ) )
312 treeView->header()->restoreState(
313 getSettings()->value( "headerStateV2" ).toByteArray() );
317 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
319 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
320 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
321 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
325 getSettings()->endGroup();
327 /* Connections for the TreeView */
328 CONNECT( treeView, activated( const QModelIndex& ),
329 this, activate( const QModelIndex& ) );
330 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
331 this, popupSelectColumn( QPoint ) );
332 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
333 this, popupPlView( const QPoint & ) );
334 treeView->installEventFilter( this );
336 /* SignalMapper for columns */
337 selectColumnsSigMapper = new QSignalMapper( this );
338 CONNECT( selectColumnsSigMapper, mapped( int ),
339 this, toggleColumnShown( int ) );
341 viewStack->addWidget( treeView );
344 void StandardPLPanel::changeModel( bool b_ml )
352 if( currentView->model() != mod )
353 currentView->setModel( mod );
355 if( currentView->model() != model )
356 currentView->setModel( model );
360 void StandardPLPanel::showView( int i_view )
367 if( iconView == NULL )
369 currentView = iconView;
374 if( listView == NULL )
376 currentView = listView;
379 case PICTUREFLOW_VIEW:
381 if( picFlowView == NULL )
383 currentView = picFlowView;
389 if( treeView == NULL )
391 currentView = treeView;
396 changeModel( false );
398 viewStack->setCurrentWidget( currentView );
403 int StandardPLPanel::currentViewIndex() const
405 if( currentView == treeView )
407 else if( currentView == iconView )
409 else if( currentView == listView )
412 return PICTUREFLOW_VIEW;
415 int StandardPLPanel::getScrollBarsSize() const
417 /* FIXME: should return a set in case of different widths */
418 return currentView->verticalScrollBar()->sizeHint().width();
421 void StandardPLPanel::cycleViews()
423 if( currentView == iconView )
424 showView( TREE_VIEW );
425 else if( currentView == treeView )
426 showView( LIST_VIEW );
427 else if( currentView == listView )
428 showView( PICTUREFLOW_VIEW );
429 else if( currentView == picFlowView )
430 showView( ICON_VIEW );
435 void StandardPLPanel::activate( const QModelIndex &index )
437 if( currentView->model() == model )
439 /* If we are not a leaf node */
440 if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
442 if( currentView != treeView )
447 playlist_Lock( THEPL );
448 playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
449 p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
450 lastActivatedId = p_item->p_input->i_id;
451 playlist_Unlock( THEPL );
452 model->activateItem( index );
457 void StandardPLPanel::browseInto( int i_id )
459 if( i_id != lastActivatedId ) return;
461 QModelIndex index = model->index( i_id, 0 );
462 playlist_Unlock( THEPL );
464 if( currentView == treeView )
465 treeView->setExpanded( index, true );
469 lastActivatedId = -1;