]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Qt: add a list view
[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 #include "menus.hpp"
36
37 #include <vlc_intf_strings.h>
38
39 #include <QPushButton>
40 #include <QHeaderView>
41 #include <QKeyEvent>
42 #include <QModelIndexList>
43 #include <QLabel>
44 #include <QMenu>
45 #include <QSignalMapper>
46 #include <QWheelEvent>
47 #include <QToolButton>
48 #include <QFontMetrics>
49 #include <QPainter>
50
51 #include <assert.h>
52
53 #include "sorting.h"
54
55 static const QString viewNames[] = { qtr( "Detailed View" ),
56                                      qtr( "Icon View" ),
57                                      qtr( "List View" ) };
58
59 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
60                                   intf_thread_t *_p_intf,
61                                   playlist_t *p_playlist,
62                                   playlist_item_t *p_root ):
63                                   QWidget( _parent ), p_intf( _p_intf )
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
73     model = new PLModel( p_playlist, p_intf, p_root, this );
74     currentRootId = -1;
75     last_activated_id = -1;
76
77     locationBar = new LocationBar( model );
78     layout->addWidget( locationBar, 0, 0 );
79     CONNECT( model, rootChanged(), locationBar, setRootIndex() );
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, 10 );
90
91     /* Add item to the playlist button */
92     addButton = new QToolButton;
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_FileDialogDetailedView ) );
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     for( int i = 0; i < VIEW_COUNT; i++ )
110     {
111         viewActions[i] = actionGroup->addAction( viewNames[i] );
112         viewActions[i]->setCheckable( true );
113         viewSelectionMapper->setMapping( viewActions[i], i );
114         CONNECT( viewActions[i], triggered(), viewSelectionMapper, map() );
115     }
116
117     BUTTONACT( viewButton, cycleViews() );
118     QMenu *viewMenu = new QMenu( this );
119     viewMenu->addActions( actionGroup->actions() );
120
121     viewButton->setMenu( viewMenu );
122
123     /* Saved Settings */
124     getSettings()->beginGroup("Playlist");
125
126     int i_viewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
127     showView( i_viewMode );
128
129     getSettings()->endGroup();
130
131     CONNECT( THEMIM, leafBecameParent( input_item_t *),
132              this, browseInto( input_item_t * ) );
133
134     CONNECT( model, currentChanged( const QModelIndex& ),
135              this, handleExpansion( const QModelIndex& ) );
136     CONNECT( model, rootChanged(), this, handleRootChange() );
137 }
138
139 StandardPLPanel::~StandardPLPanel()
140 {
141     getSettings()->beginGroup("Playlist");
142     if( treeView )
143         getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
144     if( currentView == treeView )
145         getSettings()->setValue( "view-mode", TREE_VIEW );
146     else if( currentView == listView )
147         getSettings()->setValue( "view-mode", LIST_VIEW );
148     else if( currentView == iconView )
149         getSettings()->setValue( "view-mode", ICON_VIEW );
150     getSettings()->endGroup();
151 }
152
153 /* Unused anymore, but might be useful, like in right-click menu */
154 void StandardPLPanel::gotoPlayingItem()
155 {
156     currentView->scrollTo( model->currentIndex() );
157 }
158
159 void StandardPLPanel::handleExpansion( const QModelIndex& index )
160 {
161     assert( currentView );
162     currentView->scrollTo( index );
163 }
164
165 void StandardPLPanel::handleRootChange()
166 {
167     /* needed for popupAdd() */
168     PLItem *root = model->getItem( QModelIndex() );
169     currentRootId = root->id();
170
171     locationBar->setIndex( QModelIndex() );
172
173     /* enable/disable adding */
174     if( currentRootId == THEPL->p_playing->i_id )
175     {
176         addButton->setEnabled( true );
177         addButton->setToolTip( qtr(I_PL_ADDPL) );
178     }
179     else if( THEPL->p_media_library &&
180              currentRootId == THEPL->p_media_library->i_id )
181     {
182         addButton->setEnabled( true );
183         addButton->setToolTip( qtr(I_PL_ADDML) );
184     }
185     else
186         addButton->setEnabled( false );
187 }
188
189 /* PopupAdd Menu for the Add Menu */
190 void StandardPLPanel::popupAdd()
191 {
192     QMenu popup;
193     if( currentRootId == THEPL->p_playing->i_id )
194     {
195         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
196         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
197         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
198     }
199     else if( THEPL->p_media_library &&
200                 currentRootId == THEPL->p_media_library->i_id )
201     {
202         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
203         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
204         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
205     }
206
207     popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
208                         + QPoint( 0, addButton->height() ) );
209 }
210
211 void StandardPLPanel::popupPlView( const QPoint &point )
212 {
213     QModelIndex index = currentView->indexAt( point );
214     QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
215     if( !index.isValid() ){
216         QVLCMenu::PopupMenu( p_intf, true );
217     }
218     else
219     {
220         QItemSelectionModel *selection = currentView->selectionModel();
221         QModelIndexList list = selection->selectedIndexes();
222         model->popup( index, globalPoint, list );
223     }
224 }
225
226 void StandardPLPanel::popupSelectColumn( QPoint pos )
227 {
228     QMenu menu;
229     assert( treeView );
230
231     /* We do not offer the option to hide index 0 column, or
232     * QTreeView will behave weird */
233     int i, j;
234     for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
235     {
236         QAction* option = menu.addAction(
237             qfu( psz_column_title( i ) ) );
238         option->setCheckable( true );
239         option->setChecked( !treeView->isColumnHidden( j ) );
240         selectColumnsSigMapper->setMapping( option, j );
241         CONNECT( option, triggered(), selectColumnsSigMapper, map() );
242     }
243     menu.exec( QCursor::pos() );
244 }
245
246 void StandardPLPanel::toggleColumnShown( int i )
247 {
248     treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
249 }
250
251 /* Search in the playlist */
252 void StandardPLPanel::search( const QString& searchText )
253 {
254     model->search( searchText );
255 }
256
257 /* Set the root of the new Playlist */
258 /* This activated by the selector selection */
259 void StandardPLPanel::setRoot( playlist_item_t *p_item )
260 {
261     model->rebuild( p_item );
262 }
263
264 /* Delete and Suppr key remove the selection
265    FilterKey function and code function */
266 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
267 {
268     switch( e->key() )
269     {
270     case Qt::Key_Back:
271     case Qt::Key_Delete:
272         deleteSelection();
273         break;
274     }
275 }
276
277 void StandardPLPanel::deleteSelection()
278 {
279     QItemSelectionModel *selection = currentView->selectionModel();
280     QModelIndexList list = selection->selectedIndexes();
281     model->doDelete( list );
282 }
283
284 void StandardPLPanel::createIconView()
285 {
286     iconView = new PlIconView( model, this );
287     iconView->setContextMenuPolicy( Qt::CustomContextMenu );
288     CONNECT( iconView, customContextMenuRequested( const QPoint & ),
289              this, popupPlView( const QPoint & ) );
290     CONNECT( iconView, activated( const QModelIndex & ),
291              this, activate( const QModelIndex & ) );
292     CONNECT( locationBar, invoked( const QModelIndex & ),
293              iconView, setRootIndex( const QModelIndex & ) );
294
295     layout->addWidget( iconView, 1, 0, 1, -1 );
296 }
297
298 void StandardPLPanel::createListView()
299 {
300     listView = new PlListView( model, this );
301     listView->setContextMenuPolicy( Qt::CustomContextMenu );
302     CONNECT( listView, customContextMenuRequested( const QPoint & ),
303              this, popupPlView( const QPoint & ) );
304     CONNECT( listView, activated( const QModelIndex & ),
305              this, activate( const QModelIndex & ) );
306     CONNECT( locationBar, invoked( const QModelIndex & ),
307              listView, setRootIndex( const QModelIndex & ) );
308
309     layout->addWidget( listView, 1, 0, 1, -1 );
310 }
311
312
313 void StandardPLPanel::createTreeView()
314 {
315     /* Create and configure the QTreeView */
316     treeView = new QTreeView;
317
318     treeView->setIconSize( QSize( 20, 20 ) );
319     treeView->setAlternatingRowColors( true );
320     treeView->setAnimated( true );
321     treeView->setUniformRowHeights( true );
322     treeView->setSortingEnabled( true );
323     treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
324     treeView->header()->setSortIndicatorShown( true );
325     treeView->header()->setClickable( true );
326     treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
327
328     treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
329     treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
330     treeView->setDragEnabled( true );
331     treeView->setAcceptDrops( true );
332     treeView->setDropIndicatorShown( true );
333     treeView->setContextMenuPolicy( Qt::CustomContextMenu );
334
335     /* setModel after setSortingEnabled(true), or the model will sort immediately! */
336     treeView->setModel( model );
337
338     if( getSettings()->contains( "headerStateV2" ) )
339     {
340         treeView->header()->restoreState(
341                 getSettings()->value( "headerStateV2" ).toByteArray() );
342     }
343     else
344     {
345         for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
346         {
347             treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
348             if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
349             else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
350         }
351     }
352
353     /* Connections for the TreeView */
354     CONNECT( treeView, activated( const QModelIndex& ),
355              this, activate( const QModelIndex& ) );
356     CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
357              this, popupSelectColumn( QPoint ) );
358     CONNECT( treeView, customContextMenuRequested( const QPoint & ),
359              this, popupPlView( const QPoint & ) );
360
361     /* SignalMapper for columns */
362     selectColumnsSigMapper = new QSignalMapper( this );
363     CONNECT( selectColumnsSigMapper, mapped( int ),
364              this, toggleColumnShown( int ) );
365
366     /* Finish the layout */
367     layout->addWidget( treeView, 1, 0, 1, -1 );
368 }
369
370 void StandardPLPanel::showView( int i_view )
371 {
372     switch( i_view )
373     {
374     case TREE_VIEW:
375     {
376         if( treeView == NULL )
377             createTreeView();
378         locationBar->setIndex( treeView->rootIndex() );
379         if( iconView ) iconView->hide();
380         if( listView ) listView->hide();
381         treeView->show();
382         currentView = treeView;
383         viewActions[i_view]->setChecked( true );
384         break;
385     }
386     case ICON_VIEW:
387     {
388         if( iconView == NULL )
389             createIconView();
390
391         if( treeView ) treeView->hide();
392         if( listView ) {
393             listView->hide();
394             iconView->setRootIndex( listView->rootIndex() );
395         }
396         locationBar->setIndex( iconView->rootIndex() );
397         iconView->show();
398         currentView = iconView;
399         viewActions[i_view]->setChecked( true );
400         break;
401     }
402     case LIST_VIEW:
403     {
404         if( listView == NULL )
405             createListView();
406
407         if( treeView ) treeView->hide();
408         if( iconView ) {
409             iconView->hide();
410             listView->setRootIndex( iconView->rootIndex() );
411         }
412         locationBar->setIndex( listView->rootIndex() );
413         listView->show();
414         currentView = listView;
415         viewActions[i_view]->setChecked( true );
416         break;
417     }
418     default:;
419     }
420 }
421
422 void StandardPLPanel::cycleViews()
423 {
424     if( currentView == iconView )
425         showView( TREE_VIEW );
426     else if( currentView == treeView )
427         showView( LIST_VIEW );
428     else if( currentView == listView )
429         showView( ICON_VIEW );
430     else
431         assert( 0 );
432 }
433
434 void StandardPLPanel::wheelEvent( QWheelEvent *e )
435 {
436     // Accept this event in order to prevent unwanted volume up/down changes
437     e->accept();
438 }
439
440 void StandardPLPanel::activate( const QModelIndex &index )
441 {
442     if( model->hasChildren( index ) )
443     {
444         if( currentView == iconView || currentView == listView ) {
445             currentView->setRootIndex( index );
446             locationBar->setIndex( index );
447         }
448     }
449     else
450     {
451         playlist_Lock( THEPL );
452         playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
453         p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
454         last_activated_id = p_item->p_input->i_id;
455         playlist_Unlock( THEPL );
456         model->activateItem( index );
457     }
458 }
459
460 void StandardPLPanel::browseInto( input_item_t *p_input )
461 {
462
463     if( p_input->i_id != last_activated_id ) return;
464
465     playlist_Lock( THEPL );
466
467     playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
468     if( !p_item )
469     {
470         playlist_Unlock( THEPL );
471         return;
472     }
473
474     QModelIndex index = model->index( p_item->i_id, 0 );
475
476     playlist_Unlock( THEPL );
477
478     if( currentView == iconView || currentView == listView ) {
479         currentView->setRootIndex( index );
480         locationBar->setIndex( index );
481     }
482     else
483         treeView->setExpanded( index, true );
484
485     last_activated_id = -1;
486
487
488 }
489
490 LocationBar::LocationBar( PLModel *m )
491 {
492     model = m;
493     mapper = new QSignalMapper( this );
494     CONNECT( mapper, mapped( int ), this, invoke( int ) );
495
496     box = new QHBoxLayout;
497     box->setSpacing( 0 );
498     box->setContentsMargins( 0, 0, 0, 0 );
499     setLayout( box );
500 }
501
502 void LocationBar::setIndex( const QModelIndex &index )
503 {
504     qDeleteAll( buttons );
505     buttons.clear();
506     QModelIndex i = index;
507     bool bold = true;
508     while( true )
509     {
510         PLItem *item = model->getItem( i );
511
512         char *fb_name = input_item_GetTitleFbName( item->inputItem() );
513         QString text = qfu(fb_name);
514         free(fb_name);
515         QAbstractButton *btn = new LocationButton( text, bold, i.isValid() );
516         if( bold ) btn->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
517         box->insertWidget( 0, btn, bold ? 1 : 0 );
518         buttons.append( btn );
519
520         mapper->setMapping( btn, item->id() );
521         CONNECT( btn, clicked( ), mapper, map( ) );
522
523         bold = false;
524
525         if( i.isValid() ) i = i.parent();
526         else break;
527     }
528 }
529
530 void LocationBar::setRootIndex()
531 {
532     setIndex( QModelIndex() );
533 }
534
535 void LocationBar::invoke( int i_id )
536 {
537     QModelIndex index = model->index( i_id, 0 );
538     setIndex( index );
539     emit invoked ( index );
540 }
541
542 LocationButton::LocationButton( const QString &text, bool bold, bool arrow )
543   : b_arrow( arrow )
544 {
545     QFont font;
546     font.setBold( bold );
547     setFont( font );
548     setText( text );
549 }
550
551 #define PADDING 4
552
553 void LocationButton::paintEvent ( QPaintEvent * event )
554 {
555     QStyleOptionButton option;
556     option.initFrom( this );
557     option.state |= QStyle::State_Enabled;
558     QPainter p( this );
559
560     if( underMouse() )
561         style()->drawControl( QStyle::CE_PushButtonBevel, &option, &p );
562
563     int margin = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this) + PADDING;
564
565     QRect rect = option.rect.adjusted( b_arrow ? 15 + margin : margin, 0, margin * -1, 0 );
566     p.drawText( rect, Qt::AlignVCenter,
567                 fontMetrics().elidedText( text(), Qt::ElideRight, rect.width() ) );
568
569     if( b_arrow )
570     {
571         option.rect.setX( margin );
572         option.rect.setWidth( 8 );
573         style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
574     }
575 }
576
577 QSize LocationButton::sizeHint() const
578 {
579     int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this);
580     QSize s( fontMetrics().boundingRect( text() ).size() );
581     s.setWidth( s.width() + ( 2 * frameWidth ) + ( 2 * PADDING ) + ( b_arrow ? 15 : 0 ) );
582     s.setHeight( QPushButton::sizeHint().height() );
583     return s;
584 }
585
586 #undef PADDING