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