]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
Qt: reindent
[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
36 #include <vlc_intf_strings.h>
37
38 #include <QPushButton>
39 #include <QHeaderView>
40 #include <QKeyEvent>
41 #include <QModelIndexList>
42 #include <QLabel>
43 #include <QMenu>
44 #include <QSignalMapper>
45 #include <QWheelEvent>
46 #include <QToolButton>
47 #include <QFontMetrics>
48
49 #include <assert.h>
50
51 #include "sorting.h"
52
53 StandardPLPanel::StandardPLPanel( PlaylistWidget *_parent,
54                                   intf_thread_t *_p_intf,
55                                   playlist_t *p_playlist,
56                                   playlist_item_t *p_root ):
57                                   QWidget( _parent ), p_intf( _p_intf )
58 {
59     layout = new QGridLayout( this );
60     layout->setSpacing( 0 ); layout->setMargin( 0 );
61     setMinimumWidth( 300 );
62
63     iconView = NULL;
64     treeView = NULL;
65
66     model = new PLModel( p_playlist, p_intf, p_root, this );
67     currentRootId = -1;
68     last_activated_id = -1;
69
70     /* Title label */
71     /*title = new QLabel;
72     QFont titleFont;
73     titleFont.setPointSize( titleFont.pointSize() + 6 );
74     titleFont.setFamily( "Verdana" );
75     title->setFont( titleFont );
76     layout->addWidget( title, 0, 0 );*/
77
78     locationBar = new LocationBar( model );
79     layout->addWidget( locationBar, 0, 0 );
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, 2 );
90
91     /* Add item to the playlist button */
92     addButton = new QPushButton;
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_FileDialogContentsView ) );
101     layout->addWidget( viewButton, 0, 2 );
102
103     /* View selection menu */
104     viewSelectionMapper = new QSignalMapper;
105     CONNECT( viewSelectionMapper, mapped( int ), this, showView( int ) );
106
107     QActionGroup *actionGroup = new QActionGroup( this );
108
109     treeViewAction = actionGroup->addAction( "Detailed view" );
110     treeViewAction->setCheckable( true );
111     viewSelectionMapper->setMapping( treeViewAction, TREE_VIEW );
112     CONNECT( treeViewAction, triggered(), viewSelectionMapper, map() );
113
114     iconViewAction = actionGroup->addAction( "Icon view" );
115     iconViewAction->setCheckable( true );
116     viewSelectionMapper->setMapping( iconViewAction, ICON_VIEW );
117     CONNECT( iconViewAction, triggered(), viewSelectionMapper, map() );
118
119     BUTTONACT( viewButton, cycleViews() );
120     QMenu *viewMenu = new QMenu( this );
121     viewMenu->addActions( actionGroup->actions() );
122
123     viewButton->setMenu( viewMenu );
124
125     /* Saved Settings */
126     getSettings()->beginGroup("Playlist");
127
128     int i_viewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
129     showView( i_viewMode );
130
131     getSettings()->endGroup();
132
133     CONNECT( THEMIM, leafBecameParent( input_item_t *),
134              this, browseInto( input_item_t * ) );
135
136     CONNECT( model, currentChanged( const QModelIndex& ),
137              this, handleExpansion( const QModelIndex& ) );
138 }
139
140 StandardPLPanel::~StandardPLPanel()
141 {
142     getSettings()->beginGroup("Playlist");
143     if( treeView )
144         getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
145     getSettings()->setValue( "view-mode", ( currentView == iconView ) ? ICON_VIEW : TREE_VIEW );
146     getSettings()->endGroup();
147 }
148
149 /* Unused anymore, but might be useful, like in right-click menu */
150 void StandardPLPanel::gotoPlayingItem()
151 {
152     currentView->scrollTo( model->currentIndex() );
153 }
154
155 void StandardPLPanel::handleExpansion( const QModelIndex& index )
156 {
157     assert( currentView );
158     currentView->scrollTo( index );
159 }
160
161 /* PopupAdd Menu for the Add Menu */
162 void StandardPLPanel::popupAdd()
163 {
164     QMenu popup;
165     if( currentRootId == THEPL->p_local_category->i_id ||
166         currentRootId == THEPL->p_local_onelevel->i_id )
167     {
168         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
169         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
170         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
171     }
172     else if( ( THEPL->p_ml_category &&
173                 currentRootId == THEPL->p_ml_category->i_id ) ||
174              ( THEPL->p_ml_onelevel &&
175                 currentRootId == THEPL->p_ml_onelevel->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     QItemSelectionModel *selection = currentView->selectionModel();
191     QModelIndexList list = selection->selectedIndexes();
192     model->popup( index, globalPoint, list );
193 }
194
195 void StandardPLPanel::popupSelectColumn( QPoint pos )
196 {
197     QMenu menu;
198     assert( treeView );
199
200     /* We do not offer the option to hide index 0 column, or
201     * QTreeView will behave weird */
202     int i, j;
203     for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
204     {
205         QAction* option = menu.addAction(
206             qfu( psz_column_title( i ) ) );
207         option->setCheckable( true );
208         option->setChecked( !treeView->isColumnHidden( j ) );
209         selectColumnsSigMapper->setMapping( option, j );
210         CONNECT( option, triggered(), selectColumnsSigMapper, map() );
211     }
212     menu.exec( QCursor::pos() );
213 }
214
215 void StandardPLPanel::toggleColumnShown( int i )
216 {
217     treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
218 }
219
220 /* Search in the playlist */
221 void StandardPLPanel::search( const QString& searchText )
222 {
223     model->search( searchText );
224 }
225
226 /* Set the root of the new Playlist */
227 /* This activated by the selector selection */
228 void StandardPLPanel::setRoot( playlist_item_t *p_item )
229 {
230     QPL_LOCK;
231     assert( p_item );
232
233     playlist_item_t *p_pref_item = playlist_GetPreferredNode( THEPL, p_item );
234     if( p_pref_item ) p_item = p_pref_item;
235
236     /* needed for popupAdd() */
237     currentRootId = p_item->i_id;
238
239     /* cosmetics, ..still need playlist locking.. */
240     /*char *psz_title = input_item_GetName( p_item->p_input );
241     title->setText( qfu(psz_title) );
242     free( psz_title );*/
243
244     QPL_UNLOCK;
245
246     /* do THE job */
247     model->rebuild( p_item );
248
249     locationBar->setIndex( QModelIndex() );
250
251     /* enable/disable adding */
252     if( p_item == THEPL->p_local_category ||
253         p_item == THEPL->p_local_onelevel )
254     {
255         addButton->setEnabled( true );
256         addButton->setToolTip( qtr(I_PL_ADDPL) );
257     }
258     else if( ( THEPL->p_ml_category && p_item == THEPL->p_ml_category) ||
259               ( THEPL->p_ml_onelevel && p_item == THEPL->p_ml_onelevel ) )
260     {
261         addButton->setEnabled( true );
262         addButton->setToolTip( qtr(I_PL_ADDML) );
263     }
264     else
265         addButton->setEnabled( false );
266 }
267
268 void StandardPLPanel::removeItem( int i_id )
269 {
270     model->removeItem( i_id );
271 }
272
273 /* Delete and Suppr key remove the selection
274    FilterKey function and code function */
275 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
276 {
277     switch( e->key() )
278     {
279     case Qt::Key_Back:
280     case Qt::Key_Delete:
281         deleteSelection();
282         break;
283     }
284 }
285
286 void StandardPLPanel::deleteSelection()
287 {
288     QItemSelectionModel *selection = currentView->selectionModel();
289     QModelIndexList list = selection->selectedIndexes();
290     model->doDelete( list );
291 }
292
293 void StandardPLPanel::createIconView()
294 {
295     iconView = new PlIconView( model, this );
296     iconView->setContextMenuPolicy( Qt::CustomContextMenu );
297     CONNECT( iconView, customContextMenuRequested( const QPoint & ),
298              this, popupPlView( const QPoint & ) );
299     CONNECT( iconView, activated( const QModelIndex & ),
300              this, activate( const QModelIndex & ) );
301     CONNECT( locationBar, invoked( const QModelIndex & ),
302              iconView, setRootIndex( const QModelIndex & ) );
303
304     layout->addWidget( iconView, 1, 0, 1, -1 );
305 }
306
307 void StandardPLPanel::createTreeView()
308 {
309     /* Create and configure the QTreeView */
310     treeView = new QTreeView;
311
312     treeView->setIconSize( QSize( 20, 20 ) );
313     treeView->setAlternatingRowColors( true );
314     treeView->setAnimated( true );
315     treeView->setUniformRowHeights( true );
316     treeView->setSortingEnabled( true );
317     treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
318     treeView->header()->setSortIndicatorShown( true );
319     treeView->header()->setClickable( true );
320     treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
321
322     treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
323     treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
324     treeView->setDragEnabled( true );
325     treeView->setAcceptDrops( true );
326     treeView->setDropIndicatorShown( true );
327     treeView->setContextMenuPolicy( Qt::CustomContextMenu );
328
329     /* setModel after setSortingEnabled(true), or the model will sort immediately! */
330     treeView->setModel( model );
331
332     if( getSettings()->contains( "headerStateV2" ) )
333     {
334         treeView->header()->restoreState(
335                 getSettings()->value( "headerStateV2" ).toByteArray() );
336     }
337     else
338     {
339         for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
340         {
341             treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
342             if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
343             else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
344         }
345     }
346
347     /* Connections for the TreeView */
348     CONNECT( treeView, activated( const QModelIndex& ),
349              this, activate( const QModelIndex& ) );
350     CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
351              this, popupSelectColumn( QPoint ) );
352     CONNECT( treeView, customContextMenuRequested( const QPoint & ),
353              this, popupPlView( const QPoint & ) );
354
355     /* SignalMapper for columns */
356     selectColumnsSigMapper = new QSignalMapper( this );
357     CONNECT( selectColumnsSigMapper, mapped( int ),
358              this, toggleColumnShown( int ) );
359
360     /* Finish the layout */
361     layout->addWidget( treeView, 1, 0, 1, -1 );
362 }
363
364 void StandardPLPanel::showView( int i_view )
365 {
366     switch( i_view )
367     {
368     case TREE_VIEW:
369     {
370         if( treeView == NULL )
371             createTreeView();
372         locationBar->setIndex( treeView->rootIndex() );
373         if( iconView ) iconView->hide();
374         treeView->show();
375         currentView = treeView;
376         treeViewAction->setChecked( true );
377         break;
378     }
379     case ICON_VIEW:
380     {
381         if( iconView == NULL )
382             createIconView();
383
384         locationBar->setIndex( iconView->rootIndex() );
385         if( treeView ) treeView->hide();
386         iconView->show();
387         currentView = iconView;
388         iconViewAction->setChecked( true );
389         break;
390     }
391     default:;
392     }
393 }
394
395 void StandardPLPanel::cycleViews()
396 {
397     if( currentView == iconView )
398         showView( TREE_VIEW );
399     else if( currentView == treeView )
400         showView( ICON_VIEW );
401     else
402         assert( 0 );
403 }
404
405 void StandardPLPanel::wheelEvent( QWheelEvent *e )
406 {
407     // Accept this event in order to prevent unwanted volume up/down changes
408     e->accept();
409 }
410
411 void StandardPLPanel::activate( const QModelIndex &index )
412 {
413     if( model->hasChildren( index ) )
414     {
415         if( currentView == iconView ) {
416             iconView->setRootIndex( index );
417             //title->setText( index.data().toString() );
418             locationBar->setIndex( index );
419         }
420     }
421     else
422     {
423         playlist_Lock( THEPL );
424         playlist_item_t *p_item = playlist_ItemGetById( THEPL, model->itemId( index ) );
425         p_item->i_flags |= PLAYLIST_SUBITEM_STOP_FLAG;
426         last_activated_id = p_item->p_input->i_id;//model->getItem( index )->inputItem()->i_id;
427         playlist_Unlock( THEPL );
428         model->activateItem( index );
429     }
430 }
431
432 void StandardPLPanel::browseInto( input_item_t *p_input )
433 {
434
435     if( p_input->i_id != last_activated_id ) return;
436
437     playlist_Lock( THEPL );
438
439     playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input );
440     assert( p_item != NULL );
441
442     QModelIndex index = model->index( p_item->i_id, 0 );
443     if( currentView == iconView ) {
444         iconView->setRootIndex( index );
445         locationBar->setIndex( index );
446     }
447     else
448         treeView->setExpanded( index, true );
449
450     last_activated_id = -1;
451
452     playlist_Unlock( THEPL );
453 }
454
455 LocationBar::LocationBar( PLModel *m )
456 {
457     model = m;
458     mapper = new QSignalMapper( this );
459     CONNECT( mapper, mapped( int ), this, invoke( int ) );
460 }
461
462 void LocationBar::setIndex( const QModelIndex &index )
463 {
464     clear();
465     QAction *prev = NULL;
466     QModelIndex i = index;
467     QFont font;
468     QFontMetrics metrics( font );
469     font.setBold( true );
470     while( true )
471     {
472         PLItem *item = model->getItem( i );
473
474         QToolButton *btn = new QToolButton;
475         char *fb_name = input_item_GetTitleFbName( item->inputItem() );
476         QString text = qfu(fb_name);
477         free(fb_name);
478         text = QString("/ ") + metrics.elidedText( text, Qt::ElideRight, 150 );
479         btn->setText( text );
480         btn->setFont( font );
481         prev = insertWidget( prev, btn );
482
483         mapper->setMapping( btn, item->id() );
484         CONNECT( btn, clicked( ), mapper, map( ) );
485
486         font = QFont();
487
488         if( i.isValid() ) i = i.parent();
489         else break;
490     }
491 }
492
493 void LocationBar::invoke( int i_id )
494 {
495     QModelIndex index = model->index( i_id, 0 );
496     setIndex( index );
497     emit invoked ( index );
498 }