]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
various modules: adjust to new playlist design
[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 "util/customwidgets.hpp"
35
36 #include <vlc_intf_strings.h>
37
38 #include <QPushButton>
39 #include <QHeaderView>
40 #include <QKeyEvent>
41 #include <QModelIndexList>
42 #include <QLabel>
43 #include <QMenu>
44 #include <QSignalMapper>
45 #include <QWheelEvent>
46 #include <QToolButton>
47 #include <QFontMetrics>
48
49 #include <assert.h>
50
51 #include "sorting.h"
52
53 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
54                                   intf_thread_t *_p_intf,
55                                   playlist_t *p_playlist,
56                                   playlist_item_t *p_root ):
57                                   QWidget( _parent ), p_intf( _p_intf )
58 {
59     layout = new QGridLayout( this );
60     layout->setSpacing( 0 ); layout->setMargin( 0 );
61     setMinimumWidth( 300 );
62
63     iconView = NULL;
64     treeView = NULL;
65
66     model = new PLModel( p_playlist, p_intf, p_root, this );
67     currentRootId = -1;
68     last_activated_id = -1;
69
70     /* Title label */
71     /*title = new QLabel;
72     QFont titleFont;
73     titleFont.setPointSize( titleFont.pointSize() + 6 );
74     titleFont.setFamily( "Verdana" );
75     title->setFont( titleFont );
76     layout->addWidget( title, 0, 0 );*/
77
78     locationBar = new LocationBar( model );
79     layout->addWidget( locationBar, 0, 0 );
80
81     /* A Spacer and the search possibilities */
82     layout->setColumnStretch( 1, 10 );
83
84     SearchLineEdit *search = new SearchLineEdit( this );
85     search->setMaximumWidth( 300 );
86     layout->addWidget( search, 0, 4 );
87     CONNECT( search, textChanged( const QString& ),
88              this, search( const QString& ) );
89     layout->setColumnStretch( 4, 2 );
90
91     /* Add item to the playlist button */
92     addButton = new QPushButton;
93     addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
94     addButton->setMaximumWidth( 30 );
95     BUTTONACT( addButton, popupAdd() );
96     layout->addWidget( addButton, 0, 3 );
97
98     /* Button to switch views */
99     QToolButton *viewButton = new QToolButton( this );
100     viewButton->setIcon( style()->standardIcon( QStyle::SP_FileDialogContentsView ) );
101     layout->addWidget( viewButton, 0, 2 );
102
103     /* View selection menu */
104     viewSelectionMapper = new QSignalMapper( this );
105     CONNECT( viewSelectionMapper, mapped( int ), this, showView( int ) );
106
107     QActionGroup *actionGroup = new QActionGroup( this );
108
109     treeViewAction = actionGroup->addAction( "Detailed view" );
110     treeViewAction->setCheckable( true );
111     viewSelectionMapper->setMapping( treeViewAction, TREE_VIEW );
112     CONNECT( treeViewAction, triggered(), viewSelectionMapper, map() );
113
114     iconViewAction = actionGroup->addAction( "Icon view" );
115     iconViewAction->setCheckable( true );
116     viewSelectionMapper->setMapping( iconViewAction, ICON_VIEW );
117     CONNECT( iconViewAction, triggered(), viewSelectionMapper, map() );
118
119     BUTTONACT( viewButton, cycleViews() );
120     QMenu *viewMenu = new QMenu( this );
121     viewMenu->addActions( actionGroup->actions() );
122
123     viewButton->setMenu( viewMenu );
124
125     /* Saved Settings */
126     getSettings()->beginGroup("Playlist");
127
128     int i_viewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
129     showView( i_viewMode );
130
131     getSettings()->endGroup();
132
133     CONNECT( THEMIM, leafBecameParent( input_item_t *),
134              this, browseInto( input_item_t * ) );
135
136     CONNECT( model, currentChanged( const QModelIndex& ),
137              this, handleExpansion( const QModelIndex& ) );
138 }
139
140 StandardPLPanel::~StandardPLPanel()
141 {
142     getSettings()->beginGroup("Playlist");
143     if( treeView )
144         getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
145     getSettings()->setValue( "view-mode", ( currentView == iconView ) ? ICON_VIEW : TREE_VIEW );
146     getSettings()->endGroup();
147 }
148
149 /* Unused anymore, but might be useful, like in right-click menu */
150 void StandardPLPanel::gotoPlayingItem()
151 {
152     currentView->scrollTo( model->currentIndex() );
153 }
154
155 void StandardPLPanel::handleExpansion( const QModelIndex& index )
156 {
157     assert( currentView );
158     currentView->scrollTo( index );
159 }
160
161 /* PopupAdd Menu for the Add Menu */
162 void StandardPLPanel::popupAdd()
163 {
164     QMenu popup;
165     if( currentRootId == THEPL->p_local_category->i_id ||
166         currentRootId == THEPL->p_local_onelevel->i_id )
167     {
168         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
169         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
170         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
171     }
172     else if( ( THEPL->p_ml_category &&
173                 currentRootId == THEPL->p_ml_category->i_id ) ||
174              ( THEPL->p_ml_onelevel &&
175                 currentRootId == THEPL->p_ml_onelevel->i_id ) )
176     {
177         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
178         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
179         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
180     }
181
182     popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
183                         + QPoint( 0, addButton->height() ) );
184 }
185
186 void StandardPLPanel::popupPlView( const QPoint &point )
187 {
188     QModelIndex index = currentView->indexAt( point );
189     QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
190     QItemSelectionModel *selection = currentView->selectionModel();
191     QModelIndexList list = selection->selectedIndexes();
192     model->popup( index, globalPoint, list );
193 }
194
195 void StandardPLPanel::popupSelectColumn( QPoint pos )
196 {
197     QMenu menu;
198     assert( treeView );
199
200     /* We do not offer the option to hide index 0 column, or
201     * QTreeView will behave weird */
202     int i, j;
203     for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
204     {
205         QAction* option = menu.addAction(
206             qfu( psz_column_title( i ) ) );
207         option->setCheckable( true );
208         option->setChecked( !treeView->isColumnHidden( j ) );
209         selectColumnsSigMapper->setMapping( option, j );
210         CONNECT( option, triggered(), selectColumnsSigMapper, map() );
211     }
212     menu.exec( QCursor::pos() );
213 }
214
215 void StandardPLPanel::toggleColumnShown( int i )
216 {
217     treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
218 }
219
220 /* Search in the playlist */
221 void StandardPLPanel::search( const QString& searchText )
222 {
223     model->search( searchText );
224 }
225
226 /* Set the root of the new Playlist */
227 /* This activated by the selector selection */
228 void StandardPLPanel::setRoot( playlist_item_t *p_item )
229 {
230     QPL_LOCK;
231     assert( p_item );
232
233     /* needed for popupAdd() */
234     currentRootId = p_item->i_id;
235
236     /* cosmetics, ..still need playlist locking.. */
237     /*char *psz_title = input_item_GetName( p_item->p_input );
238     title->setText( qfu(psz_title) );
239     free( psz_title );*/
240
241     QPL_UNLOCK;
242
243     /* do THE job */
244     model->rebuild( p_item );
245
246     locationBar->setIndex( QModelIndex() );
247
248     /* enable/disable adding */
249     if( p_item == THEPL->p_local_category ||
250         p_item == THEPL->p_local_onelevel )
251     {
252         addButton->setEnabled( true );
253         addButton->setToolTip( qtr(I_PL_ADDPL) );
254     }
255     else if( ( THEPL->p_ml_category && p_item == THEPL->p_ml_category) ||
256               ( THEPL->p_ml_onelevel && p_item == THEPL->p_ml_onelevel ) )
257     {
258         addButton->setEnabled( true );
259         addButton->setToolTip( qtr(I_PL_ADDML) );
260     }
261     else
262         addButton->setEnabled( false );
263 }
264
265 void StandardPLPanel::removeItem( int i_id )
266 {
267     model->removeItem( i_id );
268 }
269
270 /* Delete and Suppr key remove the selection
271    FilterKey function and code function */
272 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
273 {
274     switch( e->key() )
275     {
276     case Qt::Key_Back:
277     case Qt::Key_Delete:
278         deleteSelection();
279         break;
280     }
281 }
282
283 void StandardPLPanel::deleteSelection()
284 {
285     QItemSelectionModel *selection = currentView->selectionModel();
286     QModelIndexList list = selection->selectedIndexes();
287     model->doDelete( list );
288 }
289
290 void StandardPLPanel::createIconView()
291 {
292     iconView = new PlIconView( model, this );
293     iconView->setContextMenuPolicy( Qt::CustomContextMenu );
294     CONNECT( iconView, customContextMenuRequested( const QPoint & ),
295              this, popupPlView( const QPoint & ) );
296     CONNECT( iconView, activated( const QModelIndex & ),
297              this, activate( const QModelIndex & ) );
298     CONNECT( locationBar, invoked( const QModelIndex & ),
299              iconView, setRootIndex( const QModelIndex & ) );
300
301     layout->addWidget( iconView, 1, 0, 1, -1 );
302 }
303
304 void StandardPLPanel::createTreeView()
305 {
306     /* Create and configure the QTreeView */
307     treeView = new QTreeView;
308
309     treeView->setIconSize( QSize( 20, 20 ) );
310     treeView->setAlternatingRowColors( true );
311     treeView->setAnimated( true );
312     treeView->setUniformRowHeights( true );
313     treeView->setSortingEnabled( true );
314     treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
315     treeView->header()->setSortIndicatorShown( true );
316     treeView->header()->setClickable( true );
317     treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
318
319     treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
320     treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
321     treeView->setDragEnabled( true );
322     treeView->setAcceptDrops( true );
323     treeView->setDropIndicatorShown( true );
324     treeView->setContextMenuPolicy( Qt::CustomContextMenu );
325
326     /* setModel after setSortingEnabled(true), or the model will sort immediately! */
327     treeView->setModel( model );
328
329     if( getSettings()->contains( "headerStateV2" ) )
330     {
331         treeView->header()->restoreState(
332                 getSettings()->value( "headerStateV2" ).toByteArray() );
333     }
334     else
335     {
336         for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
337         {
338             treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
339             if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
340             else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
341         }
342     }
343
344     /* Connections for the TreeView */
345     CONNECT( treeView, activated( const QModelIndex& ),
346              this, activate( const QModelIndex& ) );
347     CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
348              this, popupSelectColumn( QPoint ) );
349     CONNECT( treeView, customContextMenuRequested( const QPoint & ),
350              this, popupPlView( const QPoint & ) );
351
352     /* SignalMapper for columns */
353     selectColumnsSigMapper = new QSignalMapper( this );
354     CONNECT( selectColumnsSigMapper, mapped( int ),
355              this, toggleColumnShown( int ) );
356
357     /* Finish the layout */
358     layout->addWidget( treeView, 1, 0, 1, -1 );
359 }
360
361 void StandardPLPanel::showView( int i_view )
362 {
363     switch( i_view )
364     {
365     case TREE_VIEW:
366     {
367         if( treeView == NULL )
368             createTreeView();
369         locationBar->setIndex( treeView->rootIndex() );
370         if( iconView ) iconView->hide();
371         treeView->show();
372         currentView = treeView;
373         treeViewAction->setChecked( true );
374         break;
375     }
376     case ICON_VIEW:
377     {
378         if( iconView == NULL )
379             createIconView();
380
381         locationBar->setIndex( iconView->rootIndex() );
382         if( treeView ) treeView->hide();
383         iconView->show();
384         currentView = iconView;
385         iconViewAction->setChecked( true );
386         break;
387     }
388     default:;
389     }
390 }
391
392 void StandardPLPanel::cycleViews()
393 {
394     if( currentView == iconView )
395         showView( TREE_VIEW );
396     else if( currentView == treeView )
397         showView( ICON_VIEW );
398     else
399         assert( 0 );
400 }
401
402 void StandardPLPanel::wheelEvent( QWheelEvent *e )
403 {
404     // Accept this event in order to prevent unwanted volume up/down changes
405     e->accept();
406 }
407
408 void StandardPLPanel::activate( const QModelIndex &index )
409 {
410     if( model->hasChildren( index ) )
411     {
412         if( currentView == iconView ) {
413             iconView->setRootIndex( index );
414             //title->setText( index.data().toString() );
415             locationBar->setIndex( index );
416         }
417     }
418     else
419     {
420         playlist_Lock( THEPL );
421         playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
422         p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
423         last_activated_id = p_item->p_input->i_id;//model->getItem( index )->inputItem()->i_id;
424         playlist_Unlock( THEPL );
425         model->activateItem( index );
426     }
427 }
428
429 void StandardPLPanel::browseInto( input_item_t *p_input )
430 {
431
432     if( p_input->i_id != last_activated_id ) return;
433
434     playlist_Lock( THEPL );
435
436     playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
437     if( !p_item )
438     {
439         playlist_Unlock( THEPL );
440         return;
441     }
442
443     QModelIndex index = model->index( p_item->i_id, 0 );
444
445     playlist_Unlock( THEPL );
446
447     if( currentView == iconView ) {
448         iconView->setRootIndex( index );
449         locationBar->setIndex( index );
450     }
451     else
452         treeView->setExpanded( index, true );
453
454     last_activated_id = -1;
455
456
457 }
458
459 LocationBar::LocationBar( PLModel *m )
460 {
461     model = m;
462     mapper = new QSignalMapper( this );
463     CONNECT( mapper, mapped( int ), this, invoke( int ) );
464 }
465
466 void LocationBar::setIndex( const QModelIndex &index )
467 {
468     clear();
469     QAction *prev = NULL;
470     QModelIndex i = index;
471     QFont font;
472     QFontMetrics metrics( font );
473     font.setBold( true );
474     while( true )
475     {
476         PLItem *item = model->getItem( i );
477
478         QToolButton *btn = new QToolButton;
479         char *fb_name = input_item_GetTitleFbName( item->inputItem() );
480         QString text = qfu(fb_name);
481         free(fb_name);
482         text = QString("/ ") + metrics.elidedText( text, Qt::ElideRight, 150 );
483         btn->setText( text );
484         btn->setFont( font );
485         prev = insertWidget( prev, btn );
486
487         mapper->setMapping( btn, item->id() );
488         CONNECT( btn, clicked( ), mapper, map( ) );
489
490         font = QFont();
491
492         if( i.isValid() ) i = i.parent();
493         else break;
494     }
495 }
496
497 void LocationBar::invoke( int i_id )
498 {
499     QModelIndex index = model->index( i_id, 0 );
500     setIndex( index );
501     emit invoked ( index );
502 }