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,
64 p_selector( _p_selector )
66 viewStack = new QStackedLayout( this );
67 viewStack->setSpacing( 0 ); viewStack->setMargin( 0 );
68 setMinimumWidth( 300 );
75 currentRootIndexId = -1;
79 int i_savedViewMode = getSettings()->value( "Playlist/view-mode", TREE_VIEW ).toInt();
80 showView( i_savedViewMode );
82 DCONNECT( THEMIM, leafBecameParent( int ),
83 this, browseInto( int ) );
85 CONNECT( model, currentChanged( const QModelIndex& ),
86 this, handleExpansion( const QModelIndex& ) );
87 CONNECT( model, rootChanged(), this, browseInto() );
89 setRoot( p_root, false );
92 StandardPLPanel::~StandardPLPanel()
94 getSettings()->beginGroup("Playlist");
96 getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
97 getSettings()->setValue( "view-mode", currentViewIndex() );
98 getSettings()->endGroup();
101 /* Unused anymore, but might be useful, like in right-click menu */
102 void StandardPLPanel::gotoPlayingItem()
104 currentView->scrollTo( model->currentIndex() );
107 void StandardPLPanel::handleExpansion( const QModelIndex& index )
109 assert( currentView );
110 if( currentRootIndexId != -1 && currentRootIndexId != model->itemId( index.parent() ) )
111 browseInto( index.parent() );
112 currentView->scrollTo( index );
115 void StandardPLPanel::popupPlView( const QPoint &point )
117 QModelIndex index = currentView->indexAt( point );
118 QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
119 QItemSelectionModel *selection = currentView->selectionModel();
120 QModelIndexList list = selection->selectedIndexes();
122 if( !model->popup( index, globalPoint, list ) )
123 QVLCMenu::PopupMenu( p_intf, true );
126 void StandardPLPanel::popupSelectColumn( QPoint )
131 /* We do not offer the option to hide index 0 column, or
132 * QTreeView will behave weird */
133 for( int i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
135 QAction* option = menu.addAction( qfu( psz_column_title( i ) ) );
136 option->setCheckable( true );
137 option->setChecked( !treeView->isColumnHidden( j ) );
138 selectColumnsSigMapper->setMapping( option, j );
139 CONNECT( option, triggered(), selectColumnsSigMapper, map() );
141 menu.exec( QCursor::pos() );
144 void StandardPLPanel::toggleColumnShown( int i )
146 treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
149 /* Search in the playlist */
150 void StandardPLPanel::search( const QString& searchText )
154 p_selector->getCurrentSelectedItem( &type, &name );
155 if( type != SD_TYPE )
157 bool flat = ( currentView == iconView ||
158 currentView == listView ||
159 currentView == picFlowView );
160 model->search( searchText,
161 flat ? currentView->rootIndex() : QModelIndex(),
166 void StandardPLPanel::searchDelayed( const QString& searchText )
170 p_selector->getCurrentSelectedItem( &type, &name );
172 if( type == SD_TYPE )
174 if( !name.isEmpty() && !searchText.isEmpty() )
175 playlist_ServicesDiscoveryControl( THEPL, qtu( name ), SD_CMD_SEARCH,
180 /* Set the root of the new Playlist */
181 /* This activated by the selector selection */
182 void StandardPLPanel::setRoot( playlist_item_t *p_item, bool b )
187 msg_Dbg( p_intf, "Setting the SQL ML" );
188 currentView->setModel( mlmodel );
193 msg_Dbg( p_intf, "Normal PL/ML or SD" );
194 if( currentView->model() != model )
195 currentView->setModel( model );
196 model->rebuild( p_item );
200 void StandardPLPanel::browseInto( const QModelIndex &index )
202 if( currentView == iconView || currentView == listView || currentView == picFlowView )
204 currentRootIndexId = model->itemId( index );
205 currentView->setRootIndex( index );
208 emit viewChanged( index );
211 void StandardPLPanel::browseInto()
213 browseInto( (currentRootIndexId != -1 && currentView != treeView) ?
214 model->index( currentRootIndexId, 0 ) :
218 void StandardPLPanel::wheelEvent( QWheelEvent *e )
220 // Accept this event in order to prevent unwanted volume up/down changes
224 bool StandardPLPanel::eventFilter ( QObject *, QEvent * event )
226 if (event->type() == QEvent::KeyPress)
228 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
229 if( keyEvent->key() == Qt::Key_Delete ||
230 keyEvent->key() == Qt::Key_Backspace )
239 void StandardPLPanel::deleteSelection()
241 QItemSelectionModel *selection = currentView->selectionModel();
242 QModelIndexList list = selection->selectedIndexes();
243 model->doDelete( list );
246 void StandardPLPanel::createIconView()
248 iconView = new PlIconView( model, this );
249 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
250 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
251 this, popupPlView( const QPoint & ) );
252 CONNECT( iconView, activated( const QModelIndex & ),
253 this, activate( const QModelIndex & ) );
254 iconView->installEventFilter( this );
255 viewStack->addWidget( iconView );
258 void StandardPLPanel::createListView()
260 listView = new PlListView( model, this );
261 listView->setContextMenuPolicy( Qt::CustomContextMenu );
262 CONNECT( listView, customContextMenuRequested( const QPoint & ),
263 this, popupPlView( const QPoint & ) );
264 CONNECT( listView, activated( const QModelIndex & ),
265 this, activate( const QModelIndex & ) );
266 listView->installEventFilter( this );
267 viewStack->addWidget( listView );
270 void StandardPLPanel::createCoverView()
272 picFlowView = new PicFlowView( model, this );
273 picFlowView->setContextMenuPolicy( Qt::CustomContextMenu );
274 CONNECT( picFlowView, customContextMenuRequested( const QPoint & ),
275 this, popupPlView( const QPoint & ) );
276 CONNECT( picFlowView, activated( const QModelIndex & ),
277 this, activate( const QModelIndex & ) );
278 viewStack->addWidget( picFlowView );
279 picFlowView->installEventFilter( this );
282 void StandardPLPanel::createTreeView()
284 /* Create and configure the QTreeView */
285 treeView = new PlTreeView;
287 treeView->setIconSize( QSize( 20, 20 ) );
288 treeView->setAlternatingRowColors( true );
289 treeView->setAnimated( true );
290 treeView->setUniformRowHeights( true );
291 treeView->setSortingEnabled( true );
292 treeView->setAttribute( Qt::WA_MacShowFocusRect, false );
293 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
294 treeView->header()->setSortIndicatorShown( true );
295 treeView->header()->setClickable( true );
296 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
298 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
299 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
300 treeView->setDragEnabled( true );
301 treeView->setAcceptDrops( true );
302 treeView->setDropIndicatorShown( true );
303 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
305 /* setModel after setSortingEnabled(true), or the model will sort immediately! */
307 getSettings()->beginGroup("Playlist");
309 if( getSettings()->contains( "headerStateV2" ) )
311 treeView->header()->restoreState(
312 getSettings()->value( "headerStateV2" ).toByteArray() );
316 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
318 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
319 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
320 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
324 getSettings()->endGroup();
326 /* Connections for the TreeView */
327 CONNECT( treeView, activated( const QModelIndex& ),
328 this, activate( const QModelIndex& ) );
329 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
330 this, popupSelectColumn( QPoint ) );
331 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
332 this, popupPlView( const QPoint & ) );
333 treeView->installEventFilter( this );
335 /* SignalMapper for columns */
336 selectColumnsSigMapper = new QSignalMapper( this );
337 CONNECT( selectColumnsSigMapper, mapped( int ),
338 this, toggleColumnShown( int ) );
340 viewStack->addWidget( treeView );
343 void StandardPLPanel::changeModel( bool b_ml )
351 if( currentView->model() != mod )
352 currentView->setModel( mod );
356 void StandardPLPanel::showView( int i_view )
363 if( iconView == NULL )
365 currentView = iconView;
370 if( listView == NULL )
372 currentView = listView;
375 case PICTUREFLOW_VIEW:
377 if( picFlowView == NULL )
379 currentView = picFlowView;
385 if( treeView == NULL )
387 currentView = treeView;
392 changeModel( false );
394 viewStack->setCurrentWidget( currentView );
399 int StandardPLPanel::currentViewIndex() const
401 if( currentView == treeView )
403 else if( currentView == iconView )
405 else if( currentView == listView )
408 return PICTUREFLOW_VIEW;
411 void StandardPLPanel::cycleViews()
413 if( currentView == iconView )
414 showView( TREE_VIEW );
415 else if( currentView == treeView )
416 showView( LIST_VIEW );
417 else if( currentView == listView )
418 showView( PICTUREFLOW_VIEW );
419 else if( currentView == picFlowView )
420 showView( ICON_VIEW );
425 void StandardPLPanel::activate( const QModelIndex &index )
427 if( currentView->model() == model )
429 /* If we are not a leaf node */
430 if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
432 if( currentView != treeView )
437 playlist_Lock( THEPL );
438 playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
439 p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
440 lastActivatedId = p_item->p_input->i_id;
441 playlist_Unlock( THEPL );
442 model->activateItem( index );
447 void StandardPLPanel::browseInto( int i_id )
449 if( i_id != lastActivatedId ) return;
451 QModelIndex index = model->index( i_id, 0 );
452 playlist_Unlock( THEPL );
454 if( currentView == treeView )
455 treeView->setExpanded( index, true );
459 lastActivatedId = -1;