]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
95fa90e58714fd811324c9f3cf68ac794e822780
[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, currentIndexChanged( 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 #else
192     Q_UNUSED( b );
193 #endif
194     {
195         msg_Dbg( p_intf, "Normal PL/ML or SD" );
196         if( currentView->model() != model )
197             currentView->setModel( model );
198         model->rebuild( p_item );
199     }
200 }
201
202 void StandardPLPanel::browseInto( const QModelIndex &index )
203 {
204     if( currentView == iconView || currentView == listView || currentView == picFlowView )
205     {
206         currentRootIndexId = model->itemId( index );
207         currentView->setRootIndex( index );
208     }
209
210     emit viewChanged( index );
211 }
212
213 void StandardPLPanel::browseInto()
214 {
215     browseInto( (currentRootIndexId != -1 && currentView != treeView) ?
216                  model->index( currentRootIndexId, 0 ) :
217                  QModelIndex() );
218 }
219
220 void StandardPLPanel::wheelEvent( QWheelEvent *e )
221 {
222     // Accept this event in order to prevent unwanted volume up/down changes
223     e->accept();
224 }
225
226 bool StandardPLPanel::eventFilter ( QObject *, QEvent * event )
227 {
228     if (event->type() == QEvent::KeyPress)
229     {
230         QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
231         if( keyEvent->key() == Qt::Key_Delete ||
232             keyEvent->key() == Qt::Key_Backspace )
233         {
234             deleteSelection();
235             return true;
236         }
237     }
238     return false;
239 }
240
241 void StandardPLPanel::deleteSelection()
242 {
243     QItemSelectionModel *selection = currentView->selectionModel();
244     QModelIndexList list = selection->selectedIndexes();
245     model->doDelete( list );
246 }
247
248 void StandardPLPanel::createIconView()
249 {
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 );
258 }
259
260 void StandardPLPanel::createListView()
261 {
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 );
270 }
271
272 void StandardPLPanel::createCoverView()
273 {
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 );
282 }
283
284 void StandardPLPanel::createTreeView()
285 {
286     /* Create and configure the QTreeView */
287     treeView = new PlTreeView;
288
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 );
299
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 );
306
307     /* setModel after setSortingEnabled(true), or the model will sort immediately! */
308
309     getSettings()->beginGroup("Playlist");
310
311     if( getSettings()->contains( "headerStateV2" ) )
312     {
313         treeView->header()->restoreState(
314                 getSettings()->value( "headerStateV2" ).toByteArray() );
315     }
316     else
317     {
318         for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
319         {
320             treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
321             if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
322             else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
323         }
324     }
325
326     getSettings()->endGroup();
327
328     /* Connections for the TreeView */
329     CONNECT( treeView, activated( const QModelIndex& ),
330              this, activate( const QModelIndex& ) );
331     CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
332              this, popupSelectColumn( QPoint ) );
333     CONNECT( treeView, customContextMenuRequested( const QPoint & ),
334              this, popupPlView( const QPoint & ) );
335     treeView->installEventFilter( this );
336
337     /* SignalMapper for columns */
338     selectColumnsSigMapper = new QSignalMapper( this );
339     CONNECT( selectColumnsSigMapper, mapped( int ),
340              this, toggleColumnShown( int ) );
341
342     viewStack->addWidget( treeView );
343 }
344
345 void StandardPLPanel::changeModel( bool b_ml )
346 {
347 #ifdef MEDIA_LIBRARY
348     VLCModel *mod;
349     if( b_ml )
350         mod = mlmodel;
351     else
352         mod = model;
353     if( currentView->model() != mod )
354         currentView->setModel( mod );
355 #else
356     Q_UNUSED( b_ml );
357     if( currentView->model() != model )
358         currentView->setModel( model );
359 #endif
360 }
361
362 void StandardPLPanel::showView( int i_view )
363 {
364
365     switch( i_view )
366     {
367     case ICON_VIEW:
368     {
369         if( iconView == NULL )
370             createIconView();
371         currentView = iconView;
372         break;
373     }
374     case LIST_VIEW:
375     {
376         if( listView == NULL )
377             createListView();
378         currentView = listView;
379         break;
380     }
381     case PICTUREFLOW_VIEW:
382     {
383         if( picFlowView == NULL )
384             createCoverView();
385         currentView = picFlowView;
386         break;
387     }
388     default:
389     case TREE_VIEW:
390     {
391         if( treeView == NULL )
392             createTreeView();
393         currentView = treeView;
394         break;
395     }
396     }
397
398     changeModel( false );
399
400     viewStack->setCurrentWidget( currentView );
401     browseInto();
402     gotoPlayingItem();
403 }
404
405 int StandardPLPanel::currentViewIndex() const
406 {
407     if( currentView == treeView )
408         return TREE_VIEW;
409     else if( currentView == iconView )
410         return ICON_VIEW;
411     else if( currentView == listView )
412         return LIST_VIEW;
413     else
414         return PICTUREFLOW_VIEW;
415 }
416
417 void StandardPLPanel::cycleViews()
418 {
419     if( currentView == iconView )
420         showView( TREE_VIEW );
421     else if( currentView == treeView )
422         showView( LIST_VIEW );
423     else if( currentView == listView )
424 #ifndef NDEBUG
425         showView( PICTUREFLOW_VIEW  );
426     else if( currentView == picFlowView )
427 #endif
428         showView( ICON_VIEW );
429     else
430         assert( 0 );
431 }
432
433 void StandardPLPanel::activate( const QModelIndex &index )
434 {
435     if( currentView->model() == model )
436     {
437         /* If we are not a leaf node */
438         if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
439         {
440             if( currentView != treeView )
441                 browseInto( index );
442         }
443         else
444         {
445             playlist_Lock( THEPL );
446             playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
447             p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
448             lastActivatedId = p_item->p_input->i_id;
449             playlist_Unlock( THEPL );
450             model->activateItem( index );
451         }
452     }
453 }
454
455 void StandardPLPanel::browseInto( int i_id )
456 {
457     if( i_id != lastActivatedId ) return;
458
459     QModelIndex index = model->index( i_id, 0 );
460     playlist_Unlock( THEPL );
461
462     if( currentView == treeView )
463         treeView->setExpanded( index, true );
464     else
465         browseInto( index );
466
467     lastActivatedId = -1;
468 }