]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Qt: playlist code refactoring
[vlc] / modules / gui / qt4 / components / playlist / standardpanel.cpp
1 /*****************************************************************************
2  * standardpanel.cpp : The "standard" playlist panel : just a treeview
3  ****************************************************************************
4  * Copyright (C) 2000-2009 VideoLAN
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          JB 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 "dialogs_provider.hpp"
30
31 #include "components/playlist/playlist_model.hpp"
32 #include "components/playlist/standardpanel.hpp"
33 #include "components/playlist/icon_view.hpp"
34 #include "components/playlist/selector.hpp"
35 #include "util/customwidgets.hpp"
36 #include "menus.hpp"
37 #include "input_manager.hpp"
38
39 #include <vlc_intf_strings.h>
40
41 #include <QPushButton>
42 #include <QHeaderView>
43 #include <QKeyEvent>
44 #include <QModelIndexList>
45 #include <QLabel>
46 #include <QMenu>
47 #include <QWheelEvent>
48 #include <QToolButton>
49 #include <QFontMetrics>
50 #include <QStackedLayout>
51
52 #include <assert.h>
53
54 #include "sorting.h"
55
56
57 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
58                                   intf_thread_t *_p_intf,
59                                   playlist_t *p_playlist,
60                                   playlist_item_t *p_root,
61                                   PLSelector *_p_selector ):
62                                   QWidget( _parent ), p_intf( _p_intf ),
63                                   p_selector( _p_selector )
64 {
65     layout = new QGridLayout( this );
66     layout->setSpacing( 0 ); layout->setMargin( 0 );
67     setMinimumWidth( 300 );
68
69     iconView = NULL;
70     treeView = NULL;
71     listView = NULL;
72     viewStack = new QStackedLayout();
73     layout->addLayout( viewStack, 1, 0, 1, -1 );
74
75     model = new PLModel( p_playlist, p_intf, p_root, this );
76     currentRootId = -1;
77     currentRootIndexId = -1;
78     lastActivatedId = -1;
79
80     /* Saved Settings */
81     getSettings()->beginGroup("Playlist");
82
83     int i_viewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
84
85     getSettings()->endGroup();
86
87     showView( i_viewMode );
88
89     DCONNECT( THEMIM, leafBecameParent( input_item_t *),
90               this, browseInto( input_item_t * ) );
91
92     CONNECT( model, currentChanged( const QModelIndex& ),
93              this, handleExpansion( const QModelIndex& ) );
94     CONNECT( model, rootChanged(), this, handleRootChange() );
95 }
96
97 StandardPLPanel::~StandardPLPanel()
98 {
99     getSettings()->beginGroup("Playlist");
100     if( treeView )
101         getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
102     if( currentView == treeView )
103         getSettings()->setValue( "view-mode", TREE_VIEW );
104     else if( currentView == listView )
105         getSettings()->setValue( "view-mode", LIST_VIEW );
106     else if( currentView == iconView )
107         getSettings()->setValue( "view-mode", ICON_VIEW );
108     getSettings()->endGroup();
109 }
110
111 /* Unused anymore, but might be useful, like in right-click menu */
112 void StandardPLPanel::gotoPlayingItem()
113 {
114     currentView->scrollTo( model->currentIndex() );
115 }
116
117 void StandardPLPanel::handleExpansion( const QModelIndex& index )
118 {
119     assert( currentView );
120     if( currentRootIndexId != -1 && currentRootIndexId != model->itemId( index.parent() ) )
121         browseInto( index.parent() );
122     currentView->scrollTo( index );
123 }
124
125 void StandardPLPanel::handleRootChange()
126 {
127     browseInto();
128 }
129
130 void StandardPLPanel::popupPlView( const QPoint &point )
131 {
132     QModelIndex index = currentView->indexAt( point );
133     QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
134     QItemSelectionModel *selection = currentView->selectionModel();
135     QModelIndexList list = selection->selectedIndexes();
136
137     if( !model->popup( index, globalPoint, list ) )
138         QVLCMenu::PopupMenu( p_intf, true );
139 }
140
141 void StandardPLPanel::popupSelectColumn( QPoint pos )
142 {
143     QMenu menu;
144     assert( treeView );
145
146     /* We do not offer the option to hide index 0 column, or
147     * QTreeView will behave weird */
148     int i, j;
149     for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
150     {
151         QAction* option = menu.addAction(
152             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;
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_QueryServicesDiscovery( THEPL, qtu( name ), 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 )
197 {
198     model->rebuild( p_item );
199 }
200
201 void StandardPLPanel::browseInto( const QModelIndex &index )
202 {
203     if( currentView == iconView || currentView == listView )
204     {
205         currentRootIndexId = model->itemId( index );
206         currentView->setRootIndex( index );
207     }
208
209     emit viewChanged( index );
210 }
211
212 void StandardPLPanel::browseInto( )
213 {
214     browseInto( currentRootIndexId != -1 && currentView != treeView ?
215                 model->index( currentRootIndexId, 0 ) :
216                 QModelIndex() );
217 }
218
219 void StandardPLPanel::wheelEvent( QWheelEvent *e )
220 {
221     // Accept this event in order to prevent unwanted volume up/down changes
222     e->accept();
223 }
224
225 bool StandardPLPanel::eventFilter ( QObject * watched, QEvent * event )
226 {
227     if (event->type() == QEvent::KeyPress)
228     {
229         QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
230         if( keyEvent->key() == Qt::Key_Delete ||
231             keyEvent->key() == Qt::Key_Backspace )
232         {
233             deleteSelection();
234             return true;
235         }
236     }
237     return false;
238 }
239
240 void StandardPLPanel::deleteSelection()
241 {
242     QItemSelectionModel *selection = currentView->selectionModel();
243     QModelIndexList list = selection->selectedIndexes();
244     model->doDelete( list );
245 }
246
247 void StandardPLPanel::createIconView()
248 {
249     iconView = new PlIconView( model, this );
250     iconView->setContextMenuPolicy( Qt::CustomContextMenu );
251     CONNECT( iconView, customContextMenuRequested( const QPoint & ),
252              this, popupPlView( const QPoint & ) );
253     CONNECT( iconView, activated( const QModelIndex & ),
254              this, activate( const QModelIndex & ) );
255     iconView->installEventFilter( this );
256     viewStack->addWidget( iconView );
257 }
258
259 void StandardPLPanel::createListView()
260 {
261     listView = new PlListView( model, this );
262     listView->setContextMenuPolicy( Qt::CustomContextMenu );
263     CONNECT( listView, customContextMenuRequested( const QPoint & ),
264              this, popupPlView( const QPoint & ) );
265     CONNECT( listView, activated( const QModelIndex & ),
266              this, activate( const QModelIndex & ) );
267     listView->installEventFilter( this );
268     viewStack->addWidget( listView );
269 }
270
271
272 void StandardPLPanel::createTreeView()
273 {
274     /* Create and configure the QTreeView */
275     treeView = new PlTreeView;
276
277     treeView->setIconSize( QSize( 20, 20 ) );
278     treeView->setAlternatingRowColors( true );
279     treeView->setAnimated( true );
280     treeView->setUniformRowHeights( true );
281     treeView->setSortingEnabled( true );
282     treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
283     treeView->header()->setSortIndicatorShown( true );
284     treeView->header()->setClickable( true );
285     treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
286
287     treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
288     treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
289     treeView->setDragEnabled( true );
290     treeView->setAcceptDrops( true );
291     treeView->setDropIndicatorShown( true );
292     treeView->setContextMenuPolicy( Qt::CustomContextMenu );
293
294     /* setModel after setSortingEnabled(true), or the model will sort immediately! */
295     treeView->setModel( model );
296
297     getSettings()->beginGroup("Playlist");
298
299     if( getSettings()->contains( "headerStateV2" ) )
300     {
301         treeView->header()->restoreState(
302                 getSettings()->value( "headerStateV2" ).toByteArray() );
303     }
304     else
305     {
306         for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
307         {
308             treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
309             if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
310             else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
311         }
312     }
313
314     getSettings()->endGroup();
315
316     /* Connections for the TreeView */
317     CONNECT( treeView, activated( const QModelIndex& ),
318              this, activate( const QModelIndex& ) );
319     CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
320              this, popupSelectColumn( QPoint ) );
321     CONNECT( treeView, customContextMenuRequested( const QPoint & ),
322              this, popupPlView( const QPoint & ) );
323     treeView->installEventFilter( this );
324
325     /* SignalMapper for columns */
326     selectColumnsSigMapper = new QSignalMapper( this );
327     CONNECT( selectColumnsSigMapper, mapped( int ),
328              this, toggleColumnShown( int ) );
329
330     /* Finish the layout */
331     viewStack->addWidget( treeView );
332 }
333
334 void StandardPLPanel::showView( int i_view )
335 {
336     switch( i_view )
337     {
338     case TREE_VIEW:
339     {
340         if( treeView == NULL )
341             createTreeView();
342         currentView = treeView;
343         break;
344     }
345     case ICON_VIEW:
346     {
347         if( iconView == NULL )
348             createIconView();
349         currentView = iconView;
350         break;
351     }
352     case LIST_VIEW:
353     {
354         if( listView == NULL )
355             createListView();
356         currentView = listView;
357         break;
358     }
359     default: return;
360     }
361
362     viewStack->setCurrentWidget( currentView );
363     //viewActions[i_view]->setChecked( true );
364     browseInto();
365     gotoPlayingItem();
366 }
367
368 void StandardPLPanel::cycleViews()
369 {
370     if( currentView == iconView )
371         showView( TREE_VIEW );
372     else if( currentView == treeView )
373         showView( LIST_VIEW );
374     else if( currentView == listView )
375         showView( ICON_VIEW );
376     else
377         assert( 0 );
378 }
379
380 void StandardPLPanel::activate( const QModelIndex &index )
381 {
382     if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
383     {
384         if( currentView != treeView )
385             browseInto( index );
386     }
387     else
388     {
389         playlist_Lock( THEPL );
390         playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
391         p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
392         lastActivatedId = p_item->p_input->i_id;
393         playlist_Unlock( THEPL );
394         model->activateItem( index );
395     }
396 }
397
398 void StandardPLPanel::browseInto( input_item_t *p_input )
399 {
400
401     if( p_input->i_id != lastActivatedId ) return;
402
403     playlist_Lock( THEPL );
404
405     playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
406     if( !p_item )
407     {
408         playlist_Unlock( THEPL );
409         return;
410     }
411
412     QModelIndex index = model->index( p_item->i_id, 0 );
413
414     playlist_Unlock( THEPL );
415
416     if( currentView == treeView )
417         treeView->setExpanded( index, true );
418     else
419         browseInto( index );
420
421     lastActivatedId = -1;
422
423 }