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/playlist_model.hpp" /* PLModel */
32 #include "components/playlist/views.hpp" /* 3 views */
33 #include "components/playlist/selector.hpp" /* PLSelector */
34 #include "menus.hpp" /* Popup */
35 #include "input_manager.hpp" /* THEMIM */
37 #include "sorting.h" /* Columns order */
39 #include <vlc_services_discovery.h> /* SD_CMD_SEARCH */
41 #include <QHeaderView>
42 #include <QModelIndexList>
45 #include <QWheelEvent>
46 #include <QStackedLayout>
47 #include <QSignalMapper>
52 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
53 intf_thread_t *_p_intf,
54 playlist_item_t *p_root,
55 PLSelector *_p_selector,
57 : QWidget( _parent ), p_intf( _p_intf ),
58 p_selector( _p_selector ), model( _p_model )
60 viewStack = new QStackedLayout( this );
61 viewStack->setSpacing( 0 ); viewStack->setMargin( 0 );
62 setMinimumWidth( 300 );
69 currentRootIndexId = -1;
73 getSettings()->beginGroup("Playlist");
74 int i_savedViewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
75 getSettings()->endGroup();
76 /* Limit the saved value to a possible one inside [0, VIEW_COUNT[ */
77 if(i_savedViewMode < 0 || i_savedViewMode >= VIEW_COUNT)
80 showView( i_savedViewMode );
82 DCONNECT( THEMIM, leafBecameParent( input_item_t *),
83 this, browseInto( input_item_t * ) );
85 CONNECT( model, currentChanged( const QModelIndex& ),
86 this, handleExpansion( const QModelIndex& ) );
87 CONNECT( model, rootChanged(), this, handleRootChange() );
92 StandardPLPanel::~StandardPLPanel()
94 getSettings()->beginGroup("Playlist");
96 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
97 if( currentView == treeView )
98 getSettings()->setValue( "view-mode", TREE_VIEW );
99 else if( currentView == listView )
100 getSettings()->setValue( "view-mode", LIST_VIEW );
101 else if( currentView == iconView )
102 getSettings()->setValue( "view-mode", ICON_VIEW );
103 else if( currentView == picFlowView )
104 getSettings()->setValue( "view-mode", PICTUREFLOW_VIEW );
105 getSettings()->endGroup();
108 /* Unused anymore, but might be useful, like in right-click menu */
109 void StandardPLPanel::gotoPlayingItem()
111 currentView->scrollTo( model->currentIndex() );
114 void StandardPLPanel::handleExpansion( const QModelIndex& index )
116 assert( currentView );
117 if( currentRootIndexId != -1 && currentRootIndexId != model->itemId( index.parent() ) )
118 browseInto( index.parent() );
119 currentView->scrollTo( index );
122 void StandardPLPanel::handleRootChange()
127 void StandardPLPanel::popupPlView( const QPoint &point )
129 QModelIndex index = currentView->indexAt( point );
130 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
131 QItemSelectionModel *selection = currentView->selectionModel();
132 QModelIndexList list = selection->selectedIndexes();
134 if( !model->popup( index, globalPoint, list ) )
135 QVLCMenu::PopupMenu( p_intf, true );
138 void StandardPLPanel::popupSelectColumn( QPoint pos )
143 /* We do not offer the option to hide index 0 column, or
144 * QTreeView will behave weird */
146 for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
148 QAction* option = menu.addAction( qfu( psz_column_title( i ) ) );
149 option->setCheckable( true );
150 option->setChecked( !treeView->isColumnHidden( j ) );
151 selectColumnsSigMapper->setMapping( option, j );
152 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
154 menu.exec( QCursor::pos() );
157 void StandardPLPanel::toggleColumnShown( int i )
159 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
162 /* Search in the playlist */
163 void StandardPLPanel::search( const QString& searchText )
167 p_selector->getCurrentSelectedItem( &type, &name );
168 if( type != SD_TYPE )
170 bool flat = currentView == iconView || currentView == listView || currentView == picFlowView;
171 model->search( searchText,
172 flat ? currentView->rootIndex() : QModelIndex(),
177 void StandardPLPanel::searchDelayed( const QString& searchText )
181 p_selector->getCurrentSelectedItem( &type, &name );
183 if( type == SD_TYPE )
185 if( !name.isEmpty() && !searchText.isEmpty() )
186 playlist_ServicesDiscoveryControl( THEPL, qtu( name ), SD_CMD_SEARCH, qtu( searchText ) );
190 /* Set the root of the new Playlist */
191 /* This activated by the selector selection */
192 void StandardPLPanel::setRoot( playlist_item_t *p_item )
194 model->rebuild( p_item );
197 void StandardPLPanel::browseInto( const QModelIndex &index )
199 if( currentView == iconView || currentView == listView || currentView == picFlowView )
201 currentRootIndexId = model->itemId( index );
202 currentView->setRootIndex( index );
205 emit viewChanged( index );
208 void StandardPLPanel::browseInto( )
210 browseInto( currentRootIndexId != -1 && currentView != treeView ?
211 model->index( currentRootIndexId, 0 ) :
215 void StandardPLPanel::wheelEvent( QWheelEvent *e )
217 // Accept this event in order to prevent unwanted volume up/down changes
221 bool StandardPLPanel::eventFilter ( QObject * watched, QEvent * event )
223 if (event->type() == QEvent::KeyPress)
225 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
226 if( keyEvent->key() == Qt::Key_Delete ||
227 keyEvent->key() == Qt::Key_Backspace )
236 void StandardPLPanel::deleteSelection()
238 QItemSelectionModel *selection = currentView->selectionModel();
239 QModelIndexList list = selection->selectedIndexes();
240 model->doDelete( list );
243 void StandardPLPanel::createIconView()
245 iconView = new PlIconView( model, this );
246 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
247 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
248 this, popupPlView( const QPoint & ) );
249 CONNECT( iconView, activated( const QModelIndex & ),
250 this, activate( const QModelIndex & ) );
251 iconView->installEventFilter( this );
252 viewStack->addWidget( iconView );
255 void StandardPLPanel::createListView()
257 listView = new PlListView( model, this );
258 listView->setContextMenuPolicy( Qt::CustomContextMenu );
259 CONNECT( listView, customContextMenuRequested( const QPoint & ),
260 this, popupPlView( const QPoint & ) );
261 CONNECT( listView, activated( const QModelIndex & ),
262 this, activate( const QModelIndex & ) );
263 listView->installEventFilter( this );
264 viewStack->addWidget( listView );
267 void StandardPLPanel::createCoverView()
269 picFlowView = new PicFlowView( model, this );
270 picFlowView->setContextMenuPolicy( Qt::CustomContextMenu );
271 CONNECT( picFlowView, customContextMenuRequested( const QPoint & ),
272 this, popupPlView( const QPoint & ) );
273 CONNECT( picFlowView, activated( const QModelIndex & ),
274 this, activate( const QModelIndex & ) );
275 viewStack->addWidget( picFlowView );
276 picFlowView->installEventFilter( this );
279 void StandardPLPanel::createTreeView()
281 /* Create and configure the QTreeView */
282 treeView = new PlTreeView;
284 treeView->setIconSize( QSize( 20, 20 ) );
285 treeView->setAlternatingRowColors( true );
286 treeView->setAnimated( true );
287 treeView->setUniformRowHeights( true );
288 treeView->setSortingEnabled( true );
289 treeView->setAttribute( Qt::WA_MacShowFocusRect, false );
290 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
291 treeView->header()->setSortIndicatorShown( true );
292 treeView->header()->setClickable( true );
293 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
295 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
296 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
297 treeView->setDragEnabled( true );
298 treeView->setAcceptDrops( true );
299 treeView->setDropIndicatorShown( true );
300 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
302 /* setModel after setSortingEnabled(true), or the model will sort immediately! */
303 treeView->setModel( model );
305 getSettings()->beginGroup("Playlist");
307 if( getSettings()->contains( "headerStateV2" ) )
309 treeView->header()->restoreState(
310 getSettings()->value( "headerStateV2" ).toByteArray() );
314 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
316 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
317 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
318 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
322 getSettings()->endGroup();
324 /* Connections for the TreeView */
325 CONNECT( treeView, activated( const QModelIndex& ),
326 this, activate( const QModelIndex& ) );
327 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
328 this, popupSelectColumn( QPoint ) );
329 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
330 this, popupPlView( const QPoint & ) );
331 treeView->installEventFilter( this );
333 /* SignalMapper for columns */
334 selectColumnsSigMapper = new QSignalMapper( this );
335 CONNECT( selectColumnsSigMapper, mapped( int ),
336 this, toggleColumnShown( int ) );
338 viewStack->addWidget( treeView );
341 void StandardPLPanel::showView( int i_view )
347 if( treeView == NULL )
349 currentView = treeView;
354 if( iconView == NULL )
356 currentView = iconView;
361 if( listView == NULL )
363 currentView = listView;
366 case PICTUREFLOW_VIEW:
368 if( picFlowView == NULL )
370 currentView = picFlowView;
376 viewStack->setCurrentWidget( currentView );
381 const int StandardPLPanel::getViewNumber()
383 if( currentView == treeView )
385 else if( currentView == iconView )
387 else if( currentView == listView )
390 return PICTUREFLOW_VIEW;
393 void StandardPLPanel::cycleViews()
395 if( currentView == iconView )
396 showView( TREE_VIEW );
397 else if( currentView == treeView )
398 showView( LIST_VIEW );
399 else if( currentView == listView )
400 showView( PICTUREFLOW_VIEW );
401 else if( currentView == picFlowView )
402 showView( ICON_VIEW );
407 void StandardPLPanel::activate( const QModelIndex &index )
409 /* If we are not a leaf node */
410 if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
412 if( currentView != treeView )
417 playlist_Lock( THEPL );
418 playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
419 p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
420 lastActivatedId = p_item->p_input->i_id;
421 playlist_Unlock( THEPL );
422 model->activateItem( index );
426 void StandardPLPanel::browseInto( input_item_t *p_input )
428 if( p_input->i_id != lastActivatedId ) return;
430 playlist_Lock( THEPL );
432 playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
435 playlist_Unlock( THEPL );
439 QModelIndex index = model->index( p_item->i_id, 0 );
440 playlist_Unlock( THEPL );
442 if( currentView == treeView )
443 treeView->setExpanded( index, true );
447 lastActivatedId = -1;