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