]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/standardpanel.cpp
fix memleak
[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     model = new PLModel( p_playlist, p_intf, p_root, this );
64     CONNECT( model, currentChanged( const QModelIndex& ),
65              this, handleExpansion( const QModelIndex& ) );
66
67     iconView = NULL;
68     treeView = NULL;
69
70     currentRootId = -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
83     /* A Spacer and the search possibilities */
84     layout->setColumnStretch( 1, 10 );
85
86     SearchLineEdit *search = new SearchLineEdit( this );
87     search->setMaximumWidth( 300 );
88     layout->addWidget( search, 0, 4 );
89     CONNECT( search, textChanged( const QString& ),
90              this, search( const QString& ) );
91     layout->setColumnStretch( 4, 2 );
92
93     /* Add item to the playlist button */
94     addButton = new QPushButton;
95     addButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
96     addButton->setMaximumWidth( 30 );
97     BUTTONACT( addButton, popupAdd() );
98     layout->addWidget( addButton, 0, 3 );
99
100     QPushButton *viewButton = new QPushButton( this );
101     viewButton->setIcon( QIcon( ":/buttons/playlist/playlist_add" ) );
102     layout->addWidget( viewButton, 0, 2 );
103     BUTTONACT( viewButton, toggleView() );
104
105     /* Saved Settings */
106     getSettings()->beginGroup("Playlist");
107
108     int i_viewMode = getSettings()->value( "view-mode", TREE_VIEW ).toInt();
109     if( i_viewMode == ICON_VIEW )
110     {
111         createIconView();
112         currentView = iconView;
113     }
114     else
115     {
116         createTreeView();
117         currentView = treeView;
118     }
119
120     getSettings()->endGroup();
121
122     last_activated_id = -1;
123     CONNECT( THEMIM, inputChanged( input_thread_t * ),
124              this, handleInputChange( input_thread_t * ) );
125 }
126
127 StandardPLPanel::~StandardPLPanel()
128 {
129     getSettings()->beginGroup("Playlist");
130     if( treeView )
131         getSettings()->setValue( "headerStateV2", treeView->header()->saveState() );
132     getSettings()->setValue( "view-mode", ( currentView == iconView ) ? ICON_VIEW : TREE_VIEW );
133     getSettings()->endGroup();
134 }
135
136 /* Unused anymore, but might be useful, like in right-click menu */
137 void StandardPLPanel::gotoPlayingItem()
138 {
139     currentView->scrollTo( model->currentIndex() );
140 }
141
142 void StandardPLPanel::handleExpansion( const QModelIndex& index )
143 {
144     assert( currentView );
145     currentView->scrollTo( index );
146 }
147
148 /* PopupAdd Menu for the Add Menu */
149 void StandardPLPanel::popupAdd()
150 {
151     QMenu popup;
152     if( currentRootId == THEPL->p_local_category->i_id ||
153         currentRootId == THEPL->p_local_onelevel->i_id )
154     {
155         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simplePLAppendDialog()) );
156         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( PLAppendDir()) );
157         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( PLAppendDialog()) );
158     }
159     else if( ( THEPL->p_ml_category &&
160                 currentRootId == THEPL->p_ml_category->i_id ) ||
161              ( THEPL->p_ml_onelevel &&
162                 currentRootId == THEPL->p_ml_onelevel->i_id ) )
163     {
164         popup.addAction( qtr(I_PL_ADDF), THEDP, SLOT( simpleMLAppendDialog()) );
165         popup.addAction( qtr(I_PL_ADDDIR), THEDP, SLOT( MLAppendDir() ) );
166         popup.addAction( qtr(I_OP_ADVOP), THEDP, SLOT( MLAppendDialog() ) );
167     }
168
169     popup.exec( QCursor::pos() - addButton->mapFromGlobal( QCursor::pos() )
170                         + QPoint( 0, addButton->height() ) );
171 }
172
173 void StandardPLPanel::popupPlView( const QPoint &point )
174 {
175     QModelIndex index = currentView->indexAt( point );
176     QPoint globalPoint = currentView->viewport()->mapToGlobal( point );
177     QItemSelectionModel *selection = currentView->selectionModel();
178     QModelIndexList list = selection->selectedIndexes();
179     model->popup( index, globalPoint, list );
180 }
181
182 void StandardPLPanel::popupSelectColumn( QPoint pos )
183 {
184     QMenu menu;
185     assert( treeView );
186
187     /* We do not offer the option to hide index 0 column, or
188     * QTreeView will behave weird */
189     int i, j;
190     for( i = 1 << 1, j = 1; i < COLUMN_END; i <<= 1, j++ )
191     {
192         QAction* option = menu.addAction(
193             qfu( psz_column_title( i ) ) );
194         option->setCheckable( true );
195         option->setChecked( !treeView->isColumnHidden( j ) );
196         selectColumnsSigMapper->setMapping( option, j );
197         CONNECT( option, triggered(), selectColumnsSigMapper, map() );
198     }
199     menu.exec( QCursor::pos() );
200 }
201
202 void StandardPLPanel::toggleColumnShown( int i )
203 {
204     treeView->setColumnHidden( i, !treeView->isColumnHidden( i ) );
205 }
206
207 /* Search in the playlist */
208 void StandardPLPanel::search( const QString& searchText )
209 {
210     model->search( searchText );
211 }
212
213 /* Set the root of the new Playlist */
214 /* This activated by the selector selection */
215 void StandardPLPanel::setRoot( playlist_item_t *p_item )
216 {
217     QPL_LOCK;
218     assert( p_item );
219
220     playlist_item_t *p_pref_item = playlist_GetPreferredNode( THEPL, p_item );
221     if( p_pref_item ) p_item = p_pref_item;
222
223     /* needed for popupAdd() */
224     currentRootId = p_item->i_id;
225
226     /* cosmetics, ..still need playlist locking.. */
227     /*char *psz_title = input_item_GetName( p_item->p_input );
228     title->setText( qfu(psz_title) );
229     free( psz_title );*/
230
231     QPL_UNLOCK;
232
233     /* do THE job */
234     model->rebuild( p_item );
235
236     locationBar->setIndex( QModelIndex() );
237
238     /* enable/disable adding */
239     if( p_item == THEPL->p_local_category ||
240         p_item == THEPL->p_local_onelevel )
241     {
242         addButton->setEnabled( true );
243         addButton->setToolTip( qtr(I_PL_ADDPL) );
244     }
245     else if( ( THEPL->p_ml_category && p_item == THEPL->p_ml_category) ||
246               ( THEPL->p_ml_onelevel && p_item == THEPL->p_ml_onelevel ) )
247     {
248         addButton->setEnabled( true );
249         addButton->setToolTip( qtr(I_PL_ADDML) );
250     }
251     else
252         addButton->setEnabled( false );
253 }
254
255 void StandardPLPanel::removeItem( int i_id )
256 {
257     model->removeItem( i_id );
258 }
259
260 /* Delete and Suppr key remove the selection
261    FilterKey function and code function */
262 void StandardPLPanel::keyPressEvent( QKeyEvent *e )
263 {
264     switch( e->key() )
265     {
266     case Qt::Key_Back:
267     case Qt::Key_Delete:
268         deleteSelection();
269         break;
270     }
271 }
272
273 void StandardPLPanel::deleteSelection()
274 {
275     QItemSelectionModel *selection = currentView->selectionModel();
276     QModelIndexList list = selection->selectedIndexes();
277     model->doDelete( list );
278 }
279
280 void StandardPLPanel::createIconView()
281 {
282     iconView = new PlIconView( model, this );
283     iconView->setContextMenuPolicy( Qt::CustomContextMenu );
284     CONNECT( iconView, customContextMenuRequested( const QPoint & ),
285              this, popupPlView( const QPoint & ) );
286     CONNECT( iconView, activated( const QModelIndex & ),
287              this, activate( const QModelIndex & ) );
288     CONNECT( locationBar, invoked( const QModelIndex & ),
289              iconView, setRootIndex( const QModelIndex & ) );
290
291     layout->addWidget( iconView, 1, 0, 1, -1 );
292 }
293
294 void StandardPLPanel::createTreeView()
295 {
296     /* Create and configure the QTreeView */
297     treeView = new QTreeView;
298
299     treeView->setIconSize( QSize( 20, 20 ) );
300     treeView->setAlternatingRowColors( true );
301     treeView->setAnimated( true );
302     treeView->setUniformRowHeights( true );
303     treeView->setSortingEnabled( true );
304     treeView->header()->setSortIndicator( -1 , Qt::AscendingOrder );
305     treeView->header()->setSortIndicatorShown( true );
306     treeView->header()->setClickable( true );
307     treeView->header()->setContextMenuPolicy( Qt::CustomContextMenu );
308
309     treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
310     treeView->setSelectionMode( QAbstractItemView::ExtendedSelection );
311     treeView->setDragEnabled( true );
312     treeView->setAcceptDrops( true );
313     treeView->setDropIndicatorShown( true );
314     treeView->setContextMenuPolicy( Qt::CustomContextMenu );
315
316     /* setModel after setSortingEnabled(true), or the model will sort immediately! */
317     treeView->setModel( model );
318
319     if( getSettings()->contains( "headerStateV2" ) )
320     {
321         treeView->header()->restoreState(
322                 getSettings()->value( "headerStateV2" ).toByteArray() );
323     }
324     else
325     {
326         for( int m = 1, c = 0; m != COLUMN_END; m <<= 1, c++ )
327         {
328             treeView->setColumnHidden( c, !( m & COLUMN_DEFAULT ) );
329             if( m == COLUMN_TITLE ) treeView->header()->resizeSection( c, 200 );
330             else if( m == COLUMN_DURATION ) treeView->header()->resizeSection( c, 80 );
331         }
332     }
333
334     /* Connections for the TreeView */
335     CONNECT( treeView, activated( const QModelIndex& ),
336              this, activate( const QModelIndex& ) );
337     CONNECT( treeView->header(), customContextMenuRequested( const QPoint & ),
338              this, popupSelectColumn( QPoint ) );
339     CONNECT( treeView, customContextMenuRequested( const QPoint & ),
340              this, popupPlView( const QPoint & ) );
341
342     /* SignalMapper for columns */
343     selectColumnsSigMapper = new QSignalMapper( this );
344     CONNECT( selectColumnsSigMapper, mapped( int ),
345              this, toggleColumnShown( int ) );
346
347     /* Finish the layout */
348     layout->addWidget( treeView, 1, 0, 1, -1 );
349 }
350
351 void StandardPLPanel::toggleView()
352 {
353     if( treeView && treeView->isVisible() )
354     {
355         if( iconView == NULL )
356             createIconView();
357
358         locationBar->setIndex( iconView->rootIndex() );
359         treeView->hide();
360         iconView->show();
361         currentView = iconView;
362     }
363     else
364     {
365         if( treeView == NULL )
366             createTreeView();
367
368         locationBar->setIndex( treeView->rootIndex() );
369         iconView->hide();
370         treeView->show();
371         currentView = treeView;
372     }
373 }
374
375 void StandardPLPanel::wheelEvent( QWheelEvent *e )
376 {
377     // Accept this event in order to prevent unwanted volume up/down changes
378     e->accept();
379 }
380
381 void StandardPLPanel::activate( const QModelIndex &index )
382 {
383     last_activated_id = model->itemId( index );
384     if( model->hasChildren( index ) )
385     {
386         if( currentView == iconView ) {
387             iconView->setRootIndex( index );
388             //title->setText( index.data().toString() );
389             locationBar->setIndex( index );
390         }
391     }
392     else
393     {
394         model->activateItem( index );
395     }
396 }
397
398 void StandardPLPanel::handleInputChange( input_thread_t *p_input_thread )
399 {
400     if( currentView != iconView ) return;
401
402     input_item_t *p_input_item = input_GetItem( p_input_thread );
403     if( !p_input_item ) return;
404
405     playlist_Lock( THEPL );
406
407     playlist_item_t *p_item = playlist_ItemGetByInput( THEPL, p_input_item );
408
409     if( p_item  && p_item->p_parent &&
410         p_item->p_parent->i_id == last_activated_id )
411     {
412         QModelIndex index = model->index( p_item->p_parent->i_id, 0 );
413         iconView->setRootIndex( index );
414         //title->setText( index.data().toString() );
415         locationBar->setIndex( index );
416         last_activated_id = p_item->i_id;
417     }
418
419     playlist_Unlock( THEPL );
420 }
421
422 LocationBar::LocationBar( PLModel *m )
423 {
424   model = m;
425   mapper = new QSignalMapper;
426   CONNECT( mapper, mapped( int ), this, invoke( int ) );
427 }
428
429 void LocationBar::setIndex( const QModelIndex &index )
430 {
431   clear();
432   QAction *prev = NULL;
433   QModelIndex i = index;
434   QFont font;
435   QFontMetrics metrics( font );
436   while( true )
437   {
438       QToolButton *btn = new QToolButton;
439       PLItem *item = model->getItem( i );
440       char *fb_name = input_item_GetTitleFbName( item->inputItem() );
441       QString text = qfu(fb_name);
442       free(fb_name);
443       text = QString("/ ") + metrics.elidedText( text, Qt::ElideRight, 150 );
444       btn->setText( text );
445       btn->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
446       prev = insertWidget( prev, btn );
447
448       mapper->setMapping( btn, item->id() );
449       CONNECT( btn, clicked( ), mapper, map( ) );
450
451       if( i.isValid() ) i = i.parent();
452       else break;
453   }
454 }
455
456 void LocationBar::invoke( int i_id )
457 {
458   QModelIndex index = model->index( i_id, 0 );
459   emit invoked ( index );
460 }