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