]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Qt: kill a bunch of warnings
[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( input_item_t *),
83               this, browseInto( input_item_t * ) );
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     if( currentView == treeView )
98         getSettings()->setValue( "view-mode", TREE_VIEW );
99     else if( currentView == listView )
100         getSettings()->setValue( "view-mode", LIST_VIEW );
101     else if( currentView == iconView )
102         getSettings()->setValue( "view-mode", ICON_VIEW );
103     else if( currentView == picFlowView )
104         getSettings()->setValue( "view-mode", PICTUREFLOW_VIEW );
105     getSettings()->endGroup();
106 }
107
108 /* Unused anymore, but might be useful, like in right-click menu */
109 void StandardPLPanel::gotoPlayingItem()
110 {
111     currentView->scrollTo( model->currentIndex() );
112 }
113
114 void StandardPLPanel::handleExpansion( const QModelIndex& index )
115 {
116     assert( currentView );
117     if( currentRootIndexId != -1 && currentRootIndexId != model->itemId( index.parent() ) )
118         browseInto( index.parent() );
119     currentView->scrollTo( index );
120 }
121
122 void StandardPLPanel::popupPlView( const QPoint &point )
123 {
124     QModelIndex index = currentView->indexAt( point );
125     QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
126     QItemSelectionModel *selection = currentView->selectionModel();
127     QModelIndexList list = selection->selectedIndexes();
128
129     if( !model->popup( index, globalPoint, list ) )
130         QVLCMenu::PopupMenu( p_intf, true );
131 }
132
133 void StandardPLPanel::popupSelectColumn( QPoint pos )
134 {
135     QMenu menu;
136     assert( treeView );
137
138     /* We do not offer the option to hide index 0 column, or
139      * QTreeView will behave weird */
140     for( int i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
141     {
142         QAction* option = menu.addAction( qfu( psz_column_title( i ) ) );
143         option->setCheckable( true );
144         option->setChecked( !treeView->isColumnHidden( j ) );
145         selectColumnsSigMapper->setMapping( option, j );
146         CONNECT( option, triggered(), selectColumnsSigMapper, map() );
147     }
148     menu.exec( QCursor::pos() );
149 }
150
151 void StandardPLPanel::toggleColumnShown( int i )
152 {
153     treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
154 }
155
156 /* Search in the playlist */
157 void StandardPLPanel::search( const QString& searchText )
158 {
159     int type;
160     QString name;
161     p_selector->getCurrentSelectedItem( &type, &name );
162     if( type != SD_TYPE )
163     {
164         bool flat = ( currentView == iconView ||
165                       currentView == listView ||
166                       currentView == picFlowView );
167         model->search( searchText,
168                        flat ? currentView->rootIndex() : QModelIndex(),
169                        !flat );
170     }
171 }
172
173 void StandardPLPanel::searchDelayed( const QString& searchText )
174 {
175     int type;
176     QString name;
177     p_selector->getCurrentSelectedItem( &type, &name );
178
179     if( type == SD_TYPE )
180     {
181         if( !name.isEmpty() && !searchText.isEmpty() )
182             playlist_ServicesDiscoveryControl( THEPL, qtu( name ), SD_CMD_SEARCH,
183                                               qtu( searchText ) );
184     }
185 }
186
187 /* Set the root of the new Playlist */
188 /* This activated by the selector selection */
189 void StandardPLPanel::setRoot( playlist_item_t *p_item, bool b )
190 {
191 #ifdef MEDIA_LIBRARY
192     if( b )
193     {
194         msg_Dbg( p_intf, "Setting the SQL ML" );
195         currentView->setModel( mlmodel );
196     }
197     else
198 #endif
199     {
200         msg_Dbg( p_intf, "Normal PL/ML or SD" );
201         if( currentView->model() != model )
202             currentView->setModel( model );
203         model->rebuild( p_item );
204     }
205 }
206
207 void StandardPLPanel::browseInto( const QModelIndex &index )
208 {
209     if( currentView == iconView || currentView == listView || currentView == picFlowView )
210     {
211         currentRootIndexId = model->itemId( index );
212         currentView->setRootIndex( index );
213     }
214
215     emit viewChanged( index );
216 }
217
218 void StandardPLPanel::browseInto()
219 {
220     browseInto( (currentRootIndexId != -1 && currentView != treeView) ?
221                  model->index( currentRootIndexId, 0 ) :
222                  QModelIndex() );
223 }
224
225 void StandardPLPanel::wheelEvent( QWheelEvent *e )
226 {
227     // Accept this event in order to prevent unwanted volume up/down changes
228     e->accept();
229 }
230
231 bool StandardPLPanel::eventFilter ( QObject * watched, QEvent * event )
232 {
233     if (event->type() == QEvent::KeyPress)
234     {
235         QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
236         if( keyEvent->key() == Qt::Key_Delete ||
237             keyEvent->key() == Qt::Key_Backspace )
238         {
239             deleteSelection();
240             return true;
241         }
242     }
243     return false;
244 }
245
246 void StandardPLPanel::deleteSelection()
247 {
248     QItemSelectionModel *selection = currentView->selectionModel();
249     QModelIndexList list = selection->selectedIndexes();
250     model->doDelete( list );
251 }
252
253 void StandardPLPanel::createIconView()
254 {
255     iconView = new PlIconView( model, this );
256     iconView->setContextMenuPolicy( Qt::CustomContextMenu );
257     CONNECT( iconView, customContextMenuRequested( const QPoint & ),
258              this, popupPlView( const QPoint & ) );
259     CONNECT( iconView, activated( const QModelIndex & ),
260              this, activate( const QModelIndex & ) );
261     iconView->installEventFilter( this );
262     viewStack->addWidget( iconView );
263 }
264
265 void StandardPLPanel::createListView()
266 {
267     listView = new PlListView( model, this );
268     listView->setContextMenuPolicy( Qt::CustomContextMenu );
269     CONNECT( listView, customContextMenuRequested( const QPoint & ),
270              this, popupPlView( const QPoint & ) );
271     CONNECT( listView, activated( const QModelIndex & ),
272              this, activate( const QModelIndex & ) );
273     listView->installEventFilter( this );
274     viewStack->addWidget( listView );
275 }
276
277 void StandardPLPanel::createCoverView()
278 {
279     picFlowView = new PicFlowView( model, this );
280     picFlowView->setContextMenuPolicy( Qt::CustomContextMenu );
281     CONNECT( picFlowView, customContextMenuRequested( const QPoint & ),
282              this, popupPlView( const QPoint & ) );
283     CONNECT( picFlowView, activated( const QModelIndex & ),
284              this, activate( const QModelIndex & ) );
285     viewStack->addWidget( picFlowView );
286     picFlowView->installEventFilter( this );
287 }
288
289 void StandardPLPanel::createTreeView()
290 {
291     /* Create and configure the QTreeView */
292     treeView = new PlTreeView;
293
294     treeView->setIconSize( QSize( 20, 20 ) );
295     treeView->setAlternatingRowColors( true );
296     treeView->setAnimated( true );
297     treeView->setUniformRowHeights( true );
298     treeView->setSortingEnabled( true );
299     treeView->setAttribute( Qt::WA_MacShowFocusRect, false );
300     treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
301     treeView->header()->setSortIndicatorShown( true );
302     treeView->header()->setClickable( true );
303     treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
304
305     treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
306     treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
307     treeView->setDragEnabled( true );
308     treeView->setAcceptDrops( true );
309     treeView->setDropIndicatorShown( true );
310     treeView->setContextMenuPolicy( Qt::CustomContextMenu );
311
312     /* setModel after setSortingEnabled(true), or the model will sort immediately! */
313
314     getSettings()->beginGroup("Playlist");
315
316     if( getSettings()->contains( "headerStateV2" ) )
317     {
318         treeView->header()->restoreState(
319                 getSettings()->value( "headerStateV2" ).toByteArray() );
320     }
321     else
322     {
323         for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
324         {
325             treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
326             if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
327             else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
328         }
329     }
330
331     getSettings()->endGroup();
332
333     /* Connections for the TreeView */
334     CONNECT( treeView, activated( const QModelIndex& ),
335              this, activate( const QModelIndex& ) );
336     CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
337              this, popupSelectColumn( QPoint ) );
338     CONNECT( treeView, customContextMenuRequested( const QPoint & ),
339              this, popupPlView( const QPoint & ) );
340     treeView->installEventFilter( this );
341
342     /* SignalMapper for columns */
343     selectColumnsSigMapper = new QSignalMapper( this );
344     CONNECT( selectColumnsSigMapper, mapped( int ),
345              this, toggleColumnShown( int ) );
346
347     viewStack->addWidget( treeView );
348 }
349
350 void StandardPLPanel::changeModel( bool b_ml )
351 {
352 #ifdef MEDIA_LIBRARY
353     VLCModel *mod;
354     if( b_ml )
355         mod = mlmodel;
356     else
357         mod = model;
358     if( currentView->model() != mod )
359         currentView->setModel( mod );
360 #endif
361 }
362
363 void StandardPLPanel::showView( int i_view )
364 {
365
366     switch( i_view )
367     {
368     case ICON_VIEW:
369     {
370         if( iconView == NULL )
371             createIconView();
372         currentView = iconView;
373         break;
374     }
375     case LIST_VIEW:
376     {
377         if( listView == NULL )
378             createListView();
379         currentView = listView;
380         break;
381     }
382     case PICTUREFLOW_VIEW:
383     {
384         if( picFlowView == NULL )
385             createCoverView();
386         currentView = picFlowView;
387         break;
388     }
389     default:
390     case TREE_VIEW:
391     {
392         if( treeView == NULL )
393             createTreeView();
394         currentView = treeView;
395         break;
396     }
397     }
398
399     changeModel( false );
400
401     viewStack->setCurrentWidget( currentView );
402     browseInto();
403     gotoPlayingItem();
404 }
405
406 int StandardPLPanel::currentViewIndex() const
407 {
408     if( currentView == treeView )
409         return TREE_VIEW;
410     else if( currentView == iconView )
411         return ICON_VIEW;
412     else if( currentView == listView )
413         return LIST_VIEW;
414     else
415         return PICTUREFLOW_VIEW;
416 }
417
418 void StandardPLPanel::cycleViews()
419 {
420     if( currentView == iconView )
421         showView( TREE_VIEW );
422     else if( currentView == treeView )
423         showView( LIST_VIEW );
424     else if( currentView == listView )
425         showView( PICTUREFLOW_VIEW  );
426     else if( currentView == picFlowView )
427         showView( ICON_VIEW );
428     else
429         assert( 0 );
430 }
431
432 void StandardPLPanel::activate( const QModelIndex &index )
433 {
434     if( currentView->model() == model )
435     {
436         /* If we are not a leaf node */
437         if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
438         {
439             if( currentView != treeView )
440                 browseInto( index );
441         }
442         else
443         {
444             playlist_Lock( THEPL );
445             playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
446             p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
447             lastActivatedId = p_item->p_input->i_id;
448             playlist_Unlock( THEPL );
449             model->activateItem( index );
450         }
451     }
452 }
453
454 void StandardPLPanel::browseInto( input_item_t *p_input )
455 {
456     if( p_input->i_id != lastActivatedId ) return;
457
458     playlist_Lock( THEPL );
459
460     playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
461     if( !p_item )
462     {
463         playlist_Unlock( THEPL );
464         return;
465     }
466
467     QModelIndex index = model->index( p_item->i_id, 0 );
468     playlist_Unlock( THEPL );
469
470     if( currentView == treeView )
471         treeView->setExpanded( index, true );
472     else
473         browseInto( index );
474
475     lastActivatedId = -1;
476 }