]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Qt: update playlist dialog on rootChanged signal from model
[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     CONNECT( model, rootChanged(), this, handleRootChange() );
142 }
143
144 StandardPLPanel::~StandardPLPanel()
145 {
146     getSettings()->beginGroup("Playlist");
147     if( treeView )
148         getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
149     getSettings()->setValue( "view-mode", ( currentView == iconView ) ? ICON_VIEW : TREE_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 void StandardPLPanel::removeItem( int i_id )
265 {
266     model->removeItem( i_id );
267 }
268
269 /* Delete and Suppr key remove the selection
270    FilterKey function and code function */
271 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
272 {
273     switch( e->key() )
274     {
275     case Qt::Key_Back:
276     case Qt::Key_Delete:
277         deleteSelection();
278         break;
279     }
280 }
281
282 void StandardPLPanel::deleteSelection()
283 {
284     QItemSelectionModel *selection = currentView->selectionModel();
285     QModelIndexList list = selection->selectedIndexes();
286     model->doDelete( list );
287 }
288
289 void StandardPLPanel::createIconView()
290 {
291     iconView = new PlIconView( model, this );
292     iconView->setContextMenuPolicy( Qt::CustomContextMenu );
293     CONNECT( iconView, customContextMenuRequested( const QPoint & ),
294              this, popupPlView( const QPoint & ) );
295     CONNECT( iconView, activated( const QModelIndex & ),
296              this, activate( const QModelIndex & ) );
297     CONNECT( locationBar, invoked( const QModelIndex & ),
298              iconView, setRootIndex( const QModelIndex & ) );
299
300     layout->addWidget( iconView, 1, 0, 1, -1 );
301 }
302
303 void StandardPLPanel::createTreeView()
304 {
305     /* Create and configure the QTreeView */
306     treeView = new QTreeView;
307
308     treeView->setIconSize( QSize( 20, 20 ) );
309     treeView->setAlternatingRowColors( true );
310     treeView->setAnimated( true );
311     treeView->setUniformRowHeights( true );
312     treeView->setSortingEnabled( true );
313     treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
314     treeView->header()->setSortIndicatorShown( true );
315     treeView->header()->setClickable( true );
316     treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
317
318     treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
319     treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
320     treeView->setDragEnabled( true );
321     treeView->setAcceptDrops( true );
322     treeView->setDropIndicatorShown( true );
323     treeView->setContextMenuPolicy( Qt::CustomContextMenu );
324
325     /* setModel after setSortingEnabled(true), or the model will sort immediately! */
326     treeView->setModel( model );
327
328     if( getSettings()->contains( "headerStateV2" ) )
329     {
330         treeView->header()->restoreState(
331                 getSettings()->value( "headerStateV2" ).toByteArray() );
332     }
333     else
334     {
335         for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
336         {
337             treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
338             if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
339             else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
340         }
341     }
342
343     /* Connections for the TreeView */
344     CONNECT( treeView, activated( const QModelIndex& ),
345              this, activate( const QModelIndex& ) );
346     CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
347              this, popupSelectColumn( QPoint ) );
348     CONNECT( treeView, customContextMenuRequested( const QPoint & ),
349              this, popupPlView( const QPoint & ) );
350
351     /* SignalMapper for columns */
352     selectColumnsSigMapper = new QSignalMapper( this );
353     CONNECT( selectColumnsSigMapper, mapped( int ),
354              this, toggleColumnShown( int ) );
355
356     /* Finish the layout */
357     layout->addWidget( treeView, 1, 0, 1, -1 );
358 }
359
360 void StandardPLPanel::showView( int i_view )
361 {
362     switch( i_view )
363     {
364     case TREE_VIEW:
365     {
366         if( treeView == NULL )
367             createTreeView();
368         locationBar->setIndex( treeView->rootIndex() );
369         if( iconView ) iconView->hide();
370         treeView->show();
371         currentView = treeView;
372         treeViewAction->setChecked( true );
373         break;
374     }
375     case ICON_VIEW:
376     {
377         if( iconView == NULL )
378             createIconView();
379
380         locationBar->setIndex( iconView->rootIndex() );
381         if( treeView ) treeView->hide();
382         iconView->show();
383         currentView = iconView;
384         iconViewAction->setChecked( true );
385         break;
386     }
387     default:;
388     }
389 }
390
391 void StandardPLPanel::cycleViews()
392 {
393     if( currentView == iconView )
394         showView( TREE_VIEW );
395     else if( currentView == treeView )
396         showView( ICON_VIEW );
397     else
398         assert( 0 );
399 }
400
401 void StandardPLPanel::wheelEvent( QWheelEvent *e )
402 {
403     // Accept this event in order to prevent unwanted volume up/down changes
404     e->accept();
405 }
406
407 void StandardPLPanel::activate( const QModelIndex &index )
408 {
409     if( model->hasChildren( index ) )
410     {
411         if( currentView == iconView ) {
412             iconView->setRootIndex( index );
413             //title->setText( index.data().toString() );
414             locationBar->setIndex( index );
415         }
416     }
417     else
418     {
419         playlist_Lock( THEPL );
420         playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
421         p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
422         last_activated_id = p_item->p_input->i_id;//model->getItem( index )->inputItem()->i_id;
423         playlist_Unlock( THEPL );
424         model->activateItem( index );
425     }
426 }
427
428 void StandardPLPanel::browseInto( input_item_t *p_input )
429 {
430
431     if( p_input->i_id != last_activated_id ) return;
432
433     playlist_Lock( THEPL );
434
435     playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
436     if( !p_item )
437     {
438         playlist_Unlock( THEPL );
439         return;
440     }
441
442     QModelIndex index = model->index( p_item->i_id, 0 );
443
444     playlist_Unlock( THEPL );
445
446     if( currentView == iconView ) {
447         iconView->setRootIndex( index );
448         locationBar->setIndex( index );
449     }
450     else
451         treeView->setExpanded( index, true );
452
453     last_activated_id = -1;
454
455
456 }
457
458 LocationBar::LocationBar( PLModel *m )
459 {
460     model = m;
461     mapper = new QSignalMapper( this );
462     CONNECT( mapper, mapped( int ), this, invoke( int ) );
463
464     box = new QHBoxLayout;
465     box->setSpacing( 0 );
466     box->setContentsMargins( 0, 0, 0, 0 );
467     setLayout( box );
468 }
469
470 void LocationBar::setIndex( const QModelIndex &index )
471 {
472     qDeleteAll( buttons );
473     buttons.clear();
474     QModelIndex i = index;
475     bool bold = true;
476     while( true )
477     {
478         PLItem *item = model->getItem( i );
479
480         char *fb_name = input_item_GetTitleFbName( item->inputItem() );
481         QString text = qfu(fb_name);
482         free(fb_name);
483         QAbstractButton *btn = new LocationButton( text, bold, i.isValid() );
484         if( bold ) btn->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
485         box->insertWidget( 0, btn, bold ? 1 : 0 );
486         buttons.append( btn );
487
488         mapper->setMapping( btn, item->id() );
489         CONNECT( btn, clicked( ), mapper, map( ) );
490
491         bold = false;
492
493         if( i.isValid() ) i = i.parent();
494         else break;
495     }
496 }
497
498 void LocationBar::setRootIndex()
499 {
500     setIndex( QModelIndex() );
501 }
502
503 void LocationBar::invoke( int i_id )
504 {
505     QModelIndex index = model->index( i_id, 0 );
506     setIndex( index );
507     emit invoked ( index );
508 }
509
510 LocationButton::LocationButton( const QString &text, bool bold, bool arrow )
511   : b_arrow( arrow )
512 {
513     QFont font;
514     font.setBold( bold );
515     setFont( font );
516     setText( text );
517 }
518
519 #define PADDING 4
520
521 void LocationButton::paintEvent ( QPaintEvent * event )
522 {
523     QStyleOptionButton option;
524     option.initFrom( this );
525     //option.rect = rect();
526     //option.features = QStyleOptionButton::Flat;
527     option.state |= QStyle::State_Enabled;
528     //option.state |= isChecked() ? QStyle::State_On : QStyle::State_Off;
529     //if( isDown() ) option.state |= QStyle::State_Sunken;
530     QPainter p( this );
531
532     if( underMouse() )
533         style()->drawControl( QStyle::CE_PushButtonBevel, &option, &p );
534
535     int margin = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this) + PADDING;
536
537     QRect rect = option.rect.adjusted( b_arrow ? 15 + margin : margin, 0, margin * -1, 0 );
538     p.drawText( rect, Qt::AlignVCenter,
539                 fontMetrics().elidedText( text(), Qt::ElideRight, rect.width() ) );
540
541     if( b_arrow )
542     {
543         option.rect.setX( margin );
544         option.rect.setWidth( 8 );
545         style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
546     }
547 }
548
549 QSize LocationButton::sizeHint() const
550 {
551     int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,0,this);
552     QSize s( fontMetrics().boundingRect( text() ).size() );
553     s.setWidth( s.width() + ( 2 * frameWidth ) + ( 2 * PADDING ) + ( b_arrow ? 15 : 0 ) );
554     s.setHeight( QPushButton::sizeHint().height() );
555     return s;
556 }
557
558 #undef PADDING