Qt: correctly display the right treeView columns
[vlc.git] / modules / gui / qt4 / components / playlist / standardpanel.cpp
blob85258c1ce96efa732dc925098e4cf9c4c917a3a2
1 /*****************************************************************************
2 * standardpanel.cpp : The "standard" playlist panel : just a treeview
3 ****************************************************************************
4 * Copyright © 2000-2010 VideoLAN
5 * $Id$
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 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
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>
45 #include <QMenu>
46 #include <QKeyEvent>
47 #include <QWheelEvent>
48 #include <QStackedLayout>
49 #include <QSignalMapper>
50 #include <QSettings>
52 #include <assert.h>
54 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
55 intf_thread_t *_p_intf,
56 playlist_item_t *p_root,
57 PLSelector *_p_selector,
58 PLModel *_p_model,
59 MLModel *_p_plmodel)
60 : QWidget( _parent ),
61 model( _p_model ),
62 mlmodel( _p_plmodel),
63 p_intf( _p_intf ),
64 p_selector( _p_selector )
66 viewStack = new QStackedLayout( this );
67 viewStack->setSpacing( 0 ); viewStack->setMargin( 0 );
68 setMinimumWidth( 300 );
70 iconView = NULL;
71 treeView = NULL;
72 listView = NULL;
73 picFlowView = NULL;
75 currentRootIndexId = -1;
76 lastActivatedId = -1;
78 /* Saved Settings */
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, currentIndexChanged( const QModelIndex& ),
86 this, handleExpansion( const QModelIndex& ) );
87 CONNECT( model, rootIndexChanged(), this, browseInto() );
89 setRootItem( p_root, false );
92 StandardPLPanel::~StandardPLPanel()
94 getSettings()->beginGroup("Playlist");
95 if( treeView )
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 )
128 QMenu menu;
129 assert( treeView );
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 )
152 int type;
153 QString name;
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(),
162 !flat );
166 void StandardPLPanel::searchDelayed( const QString& searchText )
168 int type;
169 QString name;
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,
176 qtu( searchText ) );
180 /* Set the root of the new Playlist */
181 /* This activated by the selector selection */
182 void StandardPLPanel::setRootItem( playlist_item_t *p_item, bool b )
184 #ifdef MEDIA_LIBRARY
185 if( b )
187 msg_Dbg( p_intf, "Setting the SQL ML" );
188 currentView->setModel( mlmodel );
190 else
191 #else
192 Q_UNUSED( b );
193 #endif
195 msg_Dbg( p_intf, "Normal PL/ML or SD" );
196 if( currentView->model() != model )
197 currentView->setModel( model );
198 model->rebuild( p_item );
202 void StandardPLPanel::browseInto( const QModelIndex &index )
204 if( currentView == iconView || currentView == listView || currentView == picFlowView )
206 currentRootIndexId = model->itemId( index );
207 currentView->setRootIndex( index );
210 emit viewChanged( index );
213 void StandardPLPanel::browseInto()
215 browseInto( (currentRootIndexId != -1 && currentView != treeView) ?
216 model->index( currentRootIndexId, 0 ) :
217 QModelIndex() );
220 void StandardPLPanel::wheelEvent( QWheelEvent *e )
222 // Accept this event in order to prevent unwanted volume up/down changes
223 e->accept();
226 bool StandardPLPanel::eventFilter ( QObject *, QEvent * event )
228 if (event->type() == QEvent::KeyPress)
230 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
231 if( keyEvent->key() == Qt::Key_Delete ||
232 keyEvent->key() == Qt::Key_Backspace )
234 deleteSelection();
235 return true;
238 return false;
241 void StandardPLPanel::deleteSelection()
243 QItemSelectionModel *selection = currentView->selectionModel();
244 QModelIndexList list = selection->selectedIndexes();
245 model->doDelete( list );
248 void StandardPLPanel::createIconView()
250 iconView = new PlIconView( model, this );
251 iconView->setContextMenuPolicy( Qt::CustomContextMenu );
252 CONNECT( iconView, customContextMenuRequested( const QPoint & ),
253 this, popupPlView( const QPoint & ) );
254 CONNECT( iconView, activated( const QModelIndex & ),
255 this, activate( const QModelIndex & ) );
256 iconView->installEventFilter( this );
257 viewStack->addWidget( iconView );
260 void StandardPLPanel::createListView()
262 listView = new PlListView( model, this );
263 listView->setContextMenuPolicy( Qt::CustomContextMenu );
264 CONNECT( listView, customContextMenuRequested( const QPoint & ),
265 this, popupPlView( const QPoint & ) );
266 CONNECT( listView, activated( const QModelIndex & ),
267 this, activate( const QModelIndex & ) );
268 listView->installEventFilter( this );
269 viewStack->addWidget( listView );
272 void StandardPLPanel::createCoverView()
274 picFlowView = new PicFlowView( model, this );
275 picFlowView->setContextMenuPolicy( Qt::CustomContextMenu );
276 CONNECT( picFlowView, customContextMenuRequested( const QPoint & ),
277 this, popupPlView( const QPoint & ) );
278 CONNECT( picFlowView, activated( const QModelIndex & ),
279 this, activate( const QModelIndex & ) );
280 viewStack->addWidget( picFlowView );
281 picFlowView->installEventFilter( this );
284 void StandardPLPanel::createTreeView()
286 /* Create and configure the QTreeView */
287 treeView = new PlTreeView;
289 treeView->setIconSize( QSize( 20, 20 ) );
290 treeView->setAlternatingRowColors( true );
291 treeView->setAnimated( true );
292 treeView->setUniformRowHeights( true );
293 treeView->setSortingEnabled( true );
294 treeView->setAttribute( Qt::WA_MacShowFocusRect, false );
295 treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
296 treeView->header()->setSortIndicatorShown( true );
297 treeView->header()->setClickable( true );
298 treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
300 treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
301 treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
302 treeView->setDragEnabled( true );
303 treeView->setAcceptDrops( true );
304 treeView->setDropIndicatorShown( true );
305 treeView->setContextMenuPolicy( Qt::CustomContextMenu );
307 /* setModel after setSortingEnabled(true), or the model will sort immediately! */
309 /* Connections for the TreeView */
310 CONNECT( treeView, activated( const QModelIndex& ),
311 this, activate( const QModelIndex& ) );
312 CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
313 this, popupSelectColumn( QPoint ) );
314 CONNECT( treeView, customContextMenuRequested( const QPoint & ),
315 this, popupPlView( const QPoint & ) );
316 treeView->installEventFilter( this );
318 /* SignalMapper for columns */
319 selectColumnsSigMapper = new QSignalMapper( this );
320 CONNECT( selectColumnsSigMapper, mapped( int ),
321 this, toggleColumnShown( int ) );
323 viewStack->addWidget( treeView );
326 void StandardPLPanel::changeModel( bool b_ml )
328 #ifdef MEDIA_LIBRARY
329 VLCModel *mod;
330 if( b_ml )
331 mod = mlmodel;
332 else
333 mod = model;
334 if( currentView->model() != mod )
335 currentView->setModel( mod );
336 #else
337 Q_UNUSED( b_ml );
338 if( currentView->model() != model )
339 currentView->setModel( model );
340 #endif
343 void StandardPLPanel::showView( int i_view )
345 bool b_treeViewCreated = false;
347 switch( i_view )
349 case ICON_VIEW:
351 if( iconView == NULL )
352 createIconView();
353 currentView = iconView;
354 break;
356 case LIST_VIEW:
358 if( listView == NULL )
359 createListView();
360 currentView = listView;
361 break;
363 case PICTUREFLOW_VIEW:
365 if( picFlowView == NULL )
366 createCoverView();
367 currentView = picFlowView;
368 break;
370 default:
371 case TREE_VIEW:
373 if( treeView == NULL )
375 createTreeView();
376 b_treeViewCreated = true;
378 currentView = treeView;
379 break;
383 changeModel( false );
385 /* Restoring the header Columns must come after changeModel */
386 if( b_treeViewCreated )
388 assert( treeView );
389 if( getSettings()->contains( "Playlist/headerStateV2" ) )
391 treeView->header()->restoreState(getSettings()
392 ->value( "Playlist/headerStateV2" ).toByteArray() );
394 else
396 for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
398 treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
399 if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
400 else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
405 viewStack->setCurrentWidget( currentView );
406 browseInto();
407 gotoPlayingItem();
410 int StandardPLPanel::currentViewIndex() const
412 if( currentView == treeView )
413 return TREE_VIEW;
414 else if( currentView == iconView )
415 return ICON_VIEW;
416 else if( currentView == listView )
417 return LIST_VIEW;
418 else
419 return PICTUREFLOW_VIEW;
422 void StandardPLPanel::cycleViews()
424 if( currentView == iconView )
425 showView( TREE_VIEW );
426 else if( currentView == treeView )
427 showView( LIST_VIEW );
428 else if( currentView == listView )
429 #ifndef NDEBUG
430 showView( PICTUREFLOW_VIEW );
431 else if( currentView == picFlowView )
432 #endif
433 showView( ICON_VIEW );
434 else
435 assert( 0 );
438 void StandardPLPanel::activate( const QModelIndex &index )
440 if( currentView->model() == model )
442 /* If we are not a leaf node */
443 if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
445 if( currentView != treeView )
446 browseInto( index );
448 else
450 playlist_Lock( THEPL );
451 playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
452 p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
453 lastActivatedId = p_item->p_input->i_id;
454 playlist_Unlock( THEPL );
455 model->activateItem( index );
460 void StandardPLPanel::browseInto( int i_id )
462 if( i_id != lastActivatedId ) return;
464 QModelIndex index = model->index( i_id, 0 );
465 playlist_Unlock( THEPL );
467 if( currentView == treeView )
468 treeView->setExpanded( index, true );
469 else
470 browseInto( index );
472 lastActivatedId = -1;