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>
54 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
55 intf_thread_t *_p_intf,
56 playlist_item_t *p_root,
57 PLSelector *_p_selector,
60 : QWidget( _parent ), p_intf( _p_intf ),
61 p_selector( _p_selector ), model( _p_model ),
64 viewStack = new QStackedLayout( this );
65 viewStack->setSpacing( 0 ); viewStack->setMargin( 0 );
66 setMinimumWidth( 300 );
73 currentRootIndexId = -1;
77 getSettings()->beginGroup("Playlist");
78 int i_savedViewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
79 getSettings()->endGroup();
80 /* Limit the saved value to a possible one inside [0, VIEW_COUNT[ */
81 if(i_savedViewMode < 0 || i_savedViewMode >= VIEW_COUNT)
84 showView( i_savedViewMode );
86 DCONNECT( THEMIM, leafBecameParent( input_item_t *),
87 this, browseInto( input_item_t * ) );
89 CONNECT( model, currentChanged( const QModelIndex& ),
90 this, handleExpansion( const QModelIndex& ) );
91 CONNECT( model, rootChanged(), this, handleRootChange() );
93 setRoot( p_root, false );
96 StandardPLPanel::~StandardPLPanel()
98 getSettings()->beginGroup("Playlist");
100 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
101 if( currentView == treeView )
102 getSettings()->setValue( "view-mode", TREE_VIEW );
103 else if( currentView == listView )
104 getSettings()->setValue( "view-mode", LIST_VIEW );
105 else if( currentView == iconView )
106 getSettings()->setValue( "view-mode", ICON_VIEW );
107 else if( currentView == picFlowView )
108 getSettings()->setValue( "view-mode", PICTUREFLOW_VIEW );
109 getSettings()->endGroup();
112 /* Unused anymore, but might be useful, like in right-click menu */
113 void StandardPLPanel::gotoPlayingItem()
115 currentView->scrollTo( model->currentIndex() );
118 void StandardPLPanel::handleExpansion( const QModelIndex& index )
120 assert( currentView );
121 if( currentRootIndexId != -1 && currentRootIndexId != model->itemId( index.parent() ) )
122 browseInto( index.parent() );
123 currentView->scrollTo( index );
126 void StandardPLPanel::handleRootChange()
131 void StandardPLPanel::popupPlView( const QPoint &point )
133 QModelIndex index = currentView->indexAt( point );
134 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
135 QItemSelectionModel *selection = currentView->selectionModel();
136 QModelIndexList list = selection->selectedIndexes();
138 if( !model->popup( index, globalPoint, list ) )
139 QVLCMenu::PopupMenu( p_intf, true );
142 void StandardPLPanel::popupSelectColumn( QPoint pos )
147 /* We do not offer the option to hide index 0 column, or
148 * QTreeView will behave weird */
150 for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
152 QAction* option = menu.addAction( qfu( psz_column_title( i ) ) );
153 option->setCheckable( true );
154 option->setChecked( !treeView->isColumnHidden( j ) );
155 selectColumnsSigMapper->setMapping( option, j );
156 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
158 menu.exec( QCursor::pos() );
161 void StandardPLPanel::toggleColumnShown( int i )
163 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
166 /* Search in the playlist */
167 void StandardPLPanel::search( const QString& searchText )
171 p_selector->getCurrentSelectedItem( &type, &name );
172 if( type != SD_TYPE )
174 bool flat = currentView == iconView || currentView == listView || currentView == picFlowView;
175 model->search( searchText,
176 flat ? currentView->rootIndex() : QModelIndex(),
181 void StandardPLPanel::searchDelayed( const QString& searchText )
185 p_selector->getCurrentSelectedItem( &type, &name );
187 if( type == SD_TYPE )
189 if( !name.isEmpty() && !searchText.isEmpty() )
190 playlist_ServicesDiscoveryControl( THEPL, qtu( name ), SD_CMD_SEARCH, qtu( searchText ) );
194 /* Set the root of the new Playlist */
195 /* This activated by the selector selection */
196 void StandardPLPanel::setRoot( playlist_item_t *p_item, bool b )
200 msg_Dbg( p_intf, "Setting the SQL ML" );
201 currentView->setModel( mlmodel );
205 msg_Dbg( p_intf, "Normal PL/ML or SD" );
206 if( currentView->model() != model )
207 currentView->setModel( model );
208 model->rebuild( p_item );
212 void StandardPLPanel::browseInto( const QModelIndex &index )
214 if( currentView == iconView || currentView == listView || currentView == picFlowView )
216 currentRootIndexId = model->itemId( index );
217 currentView->setRootIndex( index );
220 emit viewChanged( index );
223 void StandardPLPanel::browseInto( )
225 browseInto( currentRootIndexId != -1 && currentView != treeView ?
226 model->index( currentRootIndexId, 0 ) :
230 void StandardPLPanel::wheelEvent( QWheelEvent *e )
232 // Accept this event in order to prevent unwanted volume up/down changes
236 bool StandardPLPanel::eventFilter ( QObject * watched, QEvent * event )
238 if (event->type() == QEvent::KeyPress)
240 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
241 if( keyEvent->key() == Qt::Key_Delete ||
242 keyEvent->key() == Qt::Key_Backspace )
251 void StandardPLPanel::deleteSelection()
253 QItemSelectionModel *selection = currentView->selectionModel();
254 QModelIndexList list = selection->selectedIndexes();
255 model->doDelete( list );
258 void StandardPLPanel::createIconView()
260 iconView = new PlIconView( model, this );
261 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
262 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
263 this, popupPlView( const QPoint & ) );
264 CONNECT( iconView, activated( const QModelIndex & ),
265 this, activate( const QModelIndex & ) );
266 iconView->installEventFilter( this );
267 viewStack->addWidget( iconView );
270 void StandardPLPanel::createListView()
272 listView = new PlListView( model, this );
273 listView->setContextMenuPolicy( Qt::CustomContextMenu );
274 CONNECT( listView, customContextMenuRequested( const QPoint & ),
275 this, popupPlView( const QPoint & ) );
276 CONNECT( listView, activated( const QModelIndex & ),
277 this, activate( const QModelIndex & ) );
278 listView->installEventFilter( this );
279 viewStack->addWidget( listView );
282 void StandardPLPanel::createCoverView()
284 picFlowView = new PicFlowView( model, this );
285 picFlowView->setContextMenuPolicy( Qt::CustomContextMenu );
286 CONNECT( picFlowView, customContextMenuRequested( const QPoint & ),
287 this, popupPlView( const QPoint & ) );
288 CONNECT( picFlowView, activated( const QModelIndex & ),
289 this, activate( const QModelIndex & ) );
290 viewStack->addWidget( picFlowView );
291 picFlowView->installEventFilter( this );
294 void StandardPLPanel::createTreeView()
296 /* Create and configure the QTreeView */
297 treeView = new PlTreeView;
299 treeView->setIconSize( QSize( 20, 20 ) );
300 treeView->setAlternatingRowColors( true );
301 treeView->setAnimated( true );
302 treeView->setUniformRowHeights( true );
303 treeView->setSortingEnabled( true );
304 treeView->setAttribute( Qt::WA_MacShowFocusRect, false );
305 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
306 treeView->header()->setSortIndicatorShown( true );
307 treeView->header()->setClickable( true );
308 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
310 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
311 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
312 treeView->setDragEnabled( true );
313 treeView->setAcceptDrops( true );
314 treeView->setDropIndicatorShown( true );
315 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
317 /* setModel after setSortingEnabled(true), or the model will sort immediately! */
319 getSettings()->beginGroup("Playlist");
321 if( getSettings()->contains( "headerStateV2" ) )
323 treeView->header()->restoreState(
324 getSettings()->value( "headerStateV2" ).toByteArray() );
328 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
330 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
331 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
332 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
336 getSettings()->endGroup();
338 /* Connections for the TreeView */
339 CONNECT( treeView, activated( const QModelIndex& ),
340 this, activate( const QModelIndex& ) );
341 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
342 this, popupSelectColumn( QPoint ) );
343 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
344 this, popupPlView( const QPoint & ) );
345 treeView->installEventFilter( this );
347 /* SignalMapper for columns */
348 selectColumnsSigMapper = new QSignalMapper( this );
349 CONNECT( selectColumnsSigMapper, mapped( int ),
350 this, toggleColumnShown( int ) );
352 viewStack->addWidget( treeView );
355 void StandardPLPanel::changeModel( bool b_ml )
362 if( currentView->model() != mod )
363 currentView->setModel( mod );
366 void StandardPLPanel::showView( int i_view )
373 if( treeView == NULL )
375 currentView = treeView;
380 if( iconView == NULL )
382 currentView = iconView;
387 if( listView == NULL )
389 currentView = listView;
392 case PICTUREFLOW_VIEW:
394 if( picFlowView == NULL )
396 currentView = picFlowView;
402 changeModel( false );
404 viewStack->setCurrentWidget( currentView );
409 const int StandardPLPanel::currentViewIndex()
411 if( currentView == treeView )
413 else if( currentView == iconView )
415 else if( currentView == listView )
418 return PICTUREFLOW_VIEW;
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( input_item_t *p_input )
459 if( p_input->i_id != lastActivatedId ) return;
461 playlist_Lock( THEPL );
463 playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
466 playlist_Unlock( THEPL );
470 QModelIndex index = model->index( p_item->i_id, 0 );
471 playlist_Unlock( THEPL );
473 if( currentView == treeView )
474 treeView->setExpanded( index, true );
478 lastActivatedId = -1;