]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Qt: try to enable PL/ML switching
[vlc] / modules / gui / qt4 / components / playlist / standardpanel.cpp
1 /*****************************************************************************
2  * standardpanel.cpp : The "standard" playlist panel : just a treeview
3  ****************************************************************************
4  * Copyright © 2000-2010 VideoLAN
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "components/playlist/standardpanel.hpp"
30
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 */
38
39 #include "sorting.h"                              /* Columns order */
40
41 #include <vlc_services_discovery.h>               /* SD_CMD_SEARCH */
42
43 #include <QHeaderView>
44 #include <QModelIndexList>
45 #include <QMenu>
46 #include <QKeyEvent>
47 #include <QWheelEvent>
48 #include <QStackedLayout>
49 #include <QSignalMapper>
50 #include <QSettings>
51
52 #include <assert.h>
53
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 ), p_intf( _p_intf ),
61                   p_selector( _p_selector ), model( _p_model ),
62                   mlmodel( _p_plmodel)
63 {
64     viewStack = new QStackedLayout( this );
65     viewStack->setSpacing( 0 ); viewStack->setMargin( 0 );
66     setMinimumWidth( 300 );
67
68     iconView    = NULL;
69     treeView    = NULL;
70     listView    = NULL;
71     picFlowView = NULL;
72
73     currentRootIndexId  = -1;
74     lastActivatedId     = -1;
75
76     /* Saved Settings */
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)
82         i_savedViewMode = 0;
83
84     showView( i_savedViewMode );
85
86     DCONNECT( THEMIM, leafBecameParent( input_item_t *),
87               this, browseInto( input_item_t * ) );
88
89     CONNECT( model, currentChanged( const QModelIndex& ),
90              this, handleExpansion( const QModelIndex& ) );
91     CONNECT( model, rootChanged(), this, handleRootChange() );
92
93     setRoot( p_root, false );
94 }
95
96 StandardPLPanel::~StandardPLPanel()
97 {
98     getSettings()->beginGroup("Playlist");
99     if( treeView )
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();
110 }
111
112 /* Unused anymore, but might be useful, like in right-click menu */
113 void StandardPLPanel::gotoPlayingItem()
114 {
115     currentView->scrollTo( model->currentIndex() );
116 }
117
118 void StandardPLPanel::handleExpansion( const QModelIndex& index )
119 {
120     assert( currentView );
121     if( currentRootIndexId != -1 && currentRootIndexId != model->itemId( index.parent() ) )
122         browseInto( index.parent() );
123     currentView->scrollTo( index );
124 }
125
126 void StandardPLPanel::handleRootChange()
127 {
128     browseInto();
129 }
130
131 void StandardPLPanel::popupPlView( const QPoint &point )
132 {
133     QModelIndex index = currentView->indexAt( point );
134     QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
135     QItemSelectionModel *selection = currentView->selectionModel();
136     QModelIndexList list = selection->selectedIndexes();
137
138     if( !model->popup( index, globalPoint, list ) )
139         QVLCMenu::PopupMenu( p_intf, true );
140 }
141
142 void StandardPLPanel::popupSelectColumn( QPoint pos )
143 {
144     QMenu menu;
145     assert( treeView );
146
147     /* We do not offer the option to hide index 0 column, or
148     * QTreeView will behave weird */
149     int i, j;
150     for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
151     {
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() );
157     }
158     menu.exec( QCursor::pos() );
159 }
160
161 void StandardPLPanel::toggleColumnShown( int i )
162 {
163     treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
164 }
165
166 /* Search in the playlist */
167 void StandardPLPanel::search( const QString& searchText )
168 {
169     int type;
170     QString name;
171     p_selector->getCurrentSelectedItem( &type, &name );
172     if( type != SD_TYPE )
173     {
174         bool flat = currentView == iconView || currentView == listView || currentView == picFlowView;
175         model->search( searchText,
176                        flat ? currentView->rootIndex() : QModelIndex(),
177                        !flat );
178     }
179 }
180
181 void StandardPLPanel::searchDelayed( const QString& searchText )
182 {
183     int type;
184     QString name;
185     p_selector->getCurrentSelectedItem( &type, &name );
186
187     if( type == SD_TYPE )
188     {
189         if( !name.isEmpty() && !searchText.isEmpty() )
190             playlist_ServicesDiscoveryControl( THEPL, qtu( name ), SD_CMD_SEARCH, qtu( searchText ) );
191     }
192 }
193
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 )
197 {
198     if( b )
199     {
200         msg_Dbg( p_intf, "Setting the SQL ML" );
201         currentView->setModel( mlmodel );
202     }
203     else
204     {
205         msg_Dbg( p_intf, "Normal PL/ML or SD" );
206         if( currentView->model() != model )
207             currentView->setModel( model );
208         model->rebuild( p_item );
209     }
210 }
211
212 void StandardPLPanel::browseInto( const QModelIndex &index )
213 {
214     if( currentView == iconView || currentView == listView || currentView == picFlowView )
215     {
216         currentRootIndexId = model->itemId( index );
217         currentView->setRootIndex( index );
218     }
219
220     emit viewChanged( index );
221 }
222
223 void StandardPLPanel::browseInto( )
224 {
225     browseInto( currentRootIndexId != -1 && currentView != treeView ?
226                 model->index( currentRootIndexId, 0 ) :
227                 QModelIndex() );
228 }
229
230 void StandardPLPanel::wheelEvent( QWheelEvent *e )
231 {
232     // Accept this event in order to prevent unwanted volume up/down changes
233     e->accept();
234 }
235
236 bool StandardPLPanel::eventFilter ( QObject * watched, QEvent * event )
237 {
238     if (event->type() == QEvent::KeyPress)
239     {
240         QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
241         if( keyEvent->key() == Qt::Key_Delete ||
242             keyEvent->key() == Qt::Key_Backspace )
243         {
244             deleteSelection();
245             return true;
246         }
247     }
248     return false;
249 }
250
251 void StandardPLPanel::deleteSelection()
252 {
253     QItemSelectionModel *selection = currentView->selectionModel();
254     QModelIndexList list = selection->selectedIndexes();
255     model->doDelete( list );
256 }
257
258 void StandardPLPanel::createIconView()
259 {
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 );
268 }
269
270 void StandardPLPanel::createListView()
271 {
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 );
280 }
281
282 void StandardPLPanel::createCoverView()
283 {
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 );
292 }
293
294 void StandardPLPanel::createTreeView()
295 {
296     /* Create and configure the QTreeView */
297     treeView = new PlTreeView;
298
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 );
309
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 );
316
317     /* setModel after setSortingEnabled(true), or the model will sort immediately! */
318
319     getSettings()->beginGroup("Playlist");
320
321     if( getSettings()->contains( "headerStateV2" ) )
322     {
323         treeView->header()->restoreState(
324                 getSettings()->value( "headerStateV2" ).toByteArray() );
325     }
326     else
327     {
328         for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
329         {
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 );
333         }
334     }
335
336     getSettings()->endGroup();
337
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 );
346
347     /* SignalMapper for columns */
348     selectColumnsSigMapper = new QSignalMapper( this );
349     CONNECT( selectColumnsSigMapper, mapped( int ),
350              this, toggleColumnShown( int ) );
351
352     viewStack->addWidget( treeView );
353 }
354
355 void StandardPLPanel::changeModel( bool b_ml )
356 {
357     VLCModel *mod;
358     if( b_ml )
359         mod = mlmodel;
360     else
361         mod = model;
362     if( currentView->model() != mod )
363         currentView->setModel( mod );
364 }
365
366 void StandardPLPanel::showView( int i_view )
367 {
368
369     switch( i_view )
370     {
371     case TREE_VIEW:
372     {
373         if( treeView == NULL )
374             createTreeView();
375         currentView = treeView;
376         break;
377     }
378     case ICON_VIEW:
379     {
380         if( iconView == NULL )
381             createIconView();
382         currentView = iconView;
383         break;
384     }
385     case LIST_VIEW:
386     {
387         if( listView == NULL )
388             createListView();
389         currentView = listView;
390         break;
391     }
392     case PICTUREFLOW_VIEW:
393     {
394         if( picFlowView == NULL )
395             createCoverView();
396         currentView = picFlowView;
397         break;
398     }
399     default: return;
400     }
401
402     changeModel( false );
403
404     viewStack->setCurrentWidget( currentView );
405     browseInto();
406     gotoPlayingItem();
407 }
408
409 const int StandardPLPanel::currentViewIndex()
410 {
411     if( currentView == treeView )
412         return TREE_VIEW;
413     else if( currentView == iconView )
414         return ICON_VIEW;
415     else if( currentView == listView )
416         return LIST_VIEW;
417     else
418         return PICTUREFLOW_VIEW;
419 }
420
421 void StandardPLPanel::cycleViews()
422 {
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 );
431     else
432         assert( 0 );
433 }
434
435 void StandardPLPanel::activate( const QModelIndex &index )
436 {
437     if( currentView->model() == model )
438     {
439         /* If we are not a leaf node */
440         if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
441         {
442             if( currentView != treeView )
443                 browseInto( index );
444         }
445         else
446         {
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 );
453         }
454     }
455 }
456
457 void StandardPLPanel::browseInto( input_item_t *p_input )
458 {
459     if( p_input->i_id != lastActivatedId ) return;
460
461     playlist_Lock( THEPL );
462
463     playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
464     if( !p_item )
465     {
466         playlist_Unlock( THEPL );
467         return;
468     }
469
470     QModelIndex index = model->index( p_item->i_id, 0 );
471     playlist_Unlock( THEPL );
472
473     if( currentView == treeView )
474         treeView->setExpanded( index, true );
475     else
476         browseInto( index );
477
478     lastActivatedId = -1;
479 }