]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Qt4: refactor saving panel view
[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 ),
61                   model( _p_model ),
62                   mlmodel( _p_plmodel),
63                   p_intf( _p_intf ),
64                   p_selector( _p_selector )
65 {
66     viewStack = new QStackedLayout( this );
67     viewStack->setSpacing( 0 ); viewStack->setMargin( 0 );
68     setMinimumWidth( 300 );
69
70     iconView    = NULL;
71     treeView    = NULL;
72     listView    = NULL;
73     picFlowView = NULL;
74
75     currentRootIndexId  = -1;
76     lastActivatedId     = -1;
77
78     /* Saved Settings */
79     int i_savedViewMode = getSettings()->value( "Playlist/view-mode", TREE_VIEW ).toInt();
80     showView( i_savedViewMode );
81
82     DCONNECT( THEMIM, leafBecameParent( int ),
83               this, browseInto( int ) );
84
85     CONNECT( model, currentChanged( const QModelIndex& ),
86              this, handleExpansion( const QModelIndex& ) );
87     CONNECT( model, rootChanged(), this, browseInto() );
88
89     setRoot( p_root, false );
90 }
91
92 StandardPLPanel::~StandardPLPanel()
93 {
94     getSettings()->beginGroup("Playlist");
95     if( treeView )
96         getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
97     getSettings()->setValue( "view-mode", currentViewIndex() );
98     getSettings()->endGroup();
99 }
100
101 /* Unused anymore, but might be useful, like in right-click menu */
102 void StandardPLPanel::gotoPlayingItem()
103 {
104     currentView->scrollTo( model->currentIndex() );
105 }
106
107 void StandardPLPanel::handleExpansion( const QModelIndex& index )
108 {
109     assert( currentView );
110     if( currentRootIndexId != -1 && currentRootIndexId != model->itemId( index.parent() ) )
111         browseInto( index.parent() );
112     currentView->scrollTo( index );
113 }
114
115 void StandardPLPanel::popupPlView( const QPoint &point )
116 {
117     QModelIndex index = currentView->indexAt( point );
118     QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
119     QItemSelectionModel *selection = currentView->selectionModel();
120     QModelIndexList list = selection->selectedIndexes();
121
122     if( !model->popup( index, globalPoint, list ) )
123         QVLCMenu::PopupMenu( p_intf, true );
124 }
125
126 void StandardPLPanel::popupSelectColumn( QPoint )
127 {
128     QMenu menu;
129     assert( treeView );
130
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++ )
134     {
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() );
140     }
141     menu.exec( QCursor::pos() );
142 }
143
144 void StandardPLPanel::toggleColumnShown( int i )
145 {
146     treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
147 }
148
149 /* Search in the playlist */
150 void StandardPLPanel::search( const QString& searchText )
151 {
152     int type;
153     QString name;
154     p_selector->getCurrentSelectedItem( &type, &name );
155     if( type != SD_TYPE )
156     {
157         bool flat = ( currentView == iconView ||
158                       currentView == listView ||
159                       currentView == picFlowView );
160         model->search( searchText,
161                        flat ? currentView->rootIndex() : QModelIndex(),
162                        !flat );
163     }
164 }
165
166 void StandardPLPanel::searchDelayed( const QString& searchText )
167 {
168     int type;
169     QString name;
170     p_selector->getCurrentSelectedItem( &type, &name );
171
172     if( type == SD_TYPE )
173     {
174         if( !name.isEmpty() && !searchText.isEmpty() )
175             playlist_ServicesDiscoveryControl( THEPL, qtu( name ), SD_CMD_SEARCH,
176                                               qtu( searchText ) );
177     }
178 }
179
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 )
183 {
184 #ifdef MEDIA_LIBRARY
185     if( b )
186     {
187         msg_Dbg( p_intf, "Setting the SQL ML" );
188         currentView->setModel( mlmodel );
189     }
190     else
191 #endif
192     {
193         msg_Dbg( p_intf, "Normal PL/ML or SD" );
194         if( currentView->model() != model )
195             currentView->setModel( model );
196         model->rebuild( p_item );
197     }
198 }
199
200 void StandardPLPanel::browseInto( const QModelIndex &index )
201 {
202     if( currentView == iconView || currentView == listView || currentView == picFlowView )
203     {
204         currentRootIndexId = model->itemId( index );
205         currentView->setRootIndex( index );
206     }
207
208     emit viewChanged( index );
209 }
210
211 void StandardPLPanel::browseInto()
212 {
213     browseInto( (currentRootIndexId != -1 && currentView != treeView) ?
214                  model->index( currentRootIndexId, 0 ) :
215                  QModelIndex() );
216 }
217
218 void StandardPLPanel::wheelEvent( QWheelEvent *e )
219 {
220     // Accept this event in order to prevent unwanted volume up/down changes
221     e->accept();
222 }
223
224 bool StandardPLPanel::eventFilter ( QObject *, QEvent * event )
225 {
226     if (event->type() == QEvent::KeyPress)
227     {
228         QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
229         if( keyEvent->key() == Qt::Key_Delete ||
230             keyEvent->key() == Qt::Key_Backspace )
231         {
232             deleteSelection();
233             return true;
234         }
235     }
236     return false;
237 }
238
239 void StandardPLPanel::deleteSelection()
240 {
241     QItemSelectionModel *selection = currentView->selectionModel();
242     QModelIndexList list = selection->selectedIndexes();
243     model->doDelete( list );
244 }
245
246 void StandardPLPanel::createIconView()
247 {
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 );
256 }
257
258 void StandardPLPanel::createListView()
259 {
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 );
268 }
269
270 void StandardPLPanel::createCoverView()
271 {
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 );
280 }
281
282 void StandardPLPanel::createTreeView()
283 {
284     /* Create and configure the QTreeView */
285     treeView = new PlTreeView;
286
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 );
297
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 );
304
305     /* setModel after setSortingEnabled(true), or the model will sort immediately! */
306
307     getSettings()->beginGroup("Playlist");
308
309     if( getSettings()->contains( "headerStateV2" ) )
310     {
311         treeView->header()->restoreState(
312                 getSettings()->value( "headerStateV2" ).toByteArray() );
313     }
314     else
315     {
316         for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
317         {
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 );
321         }
322     }
323
324     getSettings()->endGroup();
325
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 );
334
335     /* SignalMapper for columns */
336     selectColumnsSigMapper = new QSignalMapper( this );
337     CONNECT( selectColumnsSigMapper, mapped( int ),
338              this, toggleColumnShown( int ) );
339
340     viewStack->addWidget( treeView );
341 }
342
343 void StandardPLPanel::changeModel( bool b_ml )
344 {
345 #ifdef MEDIA_LIBRARY
346     VLCModel *mod;
347     if( b_ml )
348         mod = mlmodel;
349     else
350         mod = model;
351     if( currentView->model() != mod )
352         currentView->setModel( mod );
353 #endif
354 }
355
356 void StandardPLPanel::showView( int i_view )
357 {
358
359     switch( i_view )
360     {
361     case ICON_VIEW:
362     {
363         if( iconView == NULL )
364             createIconView();
365         currentView = iconView;
366         break;
367     }
368     case LIST_VIEW:
369     {
370         if( listView == NULL )
371             createListView();
372         currentView = listView;
373         break;
374     }
375     case PICTUREFLOW_VIEW:
376     {
377         if( picFlowView == NULL )
378             createCoverView();
379         currentView = picFlowView;
380         break;
381     }
382     default:
383     case TREE_VIEW:
384     {
385         if( treeView == NULL )
386             createTreeView();
387         currentView = treeView;
388         break;
389     }
390     }
391
392     changeModel( false );
393
394     viewStack->setCurrentWidget( currentView );
395     browseInto();
396     gotoPlayingItem();
397 }
398
399 int StandardPLPanel::currentViewIndex() const
400 {
401     if( currentView == treeView )
402         return TREE_VIEW;
403     else if( currentView == iconView )
404         return ICON_VIEW;
405     else if( currentView == listView )
406         return LIST_VIEW;
407     else
408         return PICTUREFLOW_VIEW;
409 }
410
411 void StandardPLPanel::cycleViews()
412 {
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 );
421     else
422         assert( 0 );
423 }
424
425 void StandardPLPanel::activate( const QModelIndex &index )
426 {
427     if( currentView->model() == model )
428     {
429         /* If we are not a leaf node */
430         if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
431         {
432             if( currentView != treeView )
433                 browseInto( index );
434         }
435         else
436         {
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 );
443         }
444     }
445 }
446
447 void StandardPLPanel::browseInto( int i_id )
448 {
449     if( i_id != lastActivatedId ) return;
450
451     QModelIndex index = model->index( i_id, 0 );
452     playlist_Unlock( THEPL );
453
454     if( currentView == treeView )
455         treeView->setExpanded( index, true );
456     else
457         browseInto( index );
458
459     lastActivatedId = -1;
460 }