]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist.cpp
Qt: fix the behaviour of clean button in playlist
[vlc] / modules / gui / qt4 / components / playlist / playlist.cpp
1 /*****************************************************************************
2  * playlist.cpp : Custom widgets for the playlist
3  ****************************************************************************
4  * Copyright © 2007-2010 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste 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 "components/playlist/playlist.hpp"
30 #include "components/playlist/standardpanel.hpp"  /* MainView */
31 #include "components/playlist/selector.hpp"       /* PLSelector */
32 #include "components/playlist/playlist_model.hpp" /* PLModel */
33 #include "components/playlist/ml_model.hpp"       /* MLModel */
34 #include "components/interface_widgets.hpp"       /* CoverArtLabel */
35
36 #include "util/searchlineedit.hpp"
37
38 #include "input_manager.hpp"                      /* art signal */
39 #include "main_interface.hpp"                     /* DropEvent TODO remove this*/
40
41 #include <QMenu>
42 #include <QSignalMapper>
43 #include <QSlider>
44
45 /**********************************************************************
46  * Playlist Widget. The embedded playlist
47  **********************************************************************/
48
49 PlaylistWidget::PlaylistWidget( intf_thread_t *_p_i, QWidget *_par )
50                : QWidget( _par ), p_intf ( _p_i )
51 {
52
53     setContentsMargins( 0, 3, 0, 3 );
54
55     QGridLayout *layout = new QGridLayout( this );
56     layout->setMargin( 0 ); layout->setSpacing( 0 );
57
58     /*******************
59      * Left            *
60      *******************/
61     /* We use a QSplitter for the left part */
62     leftSplitter = new QSplitter( Qt::Vertical, this );
63
64     /* Source Selector */
65     PLSelector *selector = new PLSelector( this, p_intf );
66     leftSplitter->addWidget( selector);
67
68     /* Create a Container for the Art Label
69        in order to have a beautiful resizing for the selector above it */
70     QWidget *artContainer = new QWidget;
71     QHBoxLayout *artContLay = new QHBoxLayout( artContainer );
72     artContLay->setMargin( 0 );
73     artContLay->setSpacing( 0 );
74     artContainer->setMaximumHeight( 128 );
75
76     /* Art label */
77     CoverArtLabel *art = new CoverArtLabel( artContainer, p_intf );
78     art->setToolTip( qtr( "Double click to get media information" ) );
79     artContLay->addWidget( art, 1 );
80
81     CONNECT( THEMIM->getIM(), artChanged( QString ),
82              art, showArtUpdate( const QString& ) );
83
84     leftSplitter->addWidget( artContainer );
85
86     /*******************
87      * Right           *
88      *******************/
89     /* Initialisation of the playlist */
90     playlist_t * p_playlist = THEPL;
91     PL_LOCK;
92     playlist_item_t *p_root = p_playlist->p_playing;
93     PL_UNLOCK;
94
95     setMinimumWidth( 400 );
96
97     PLModel *model = new PLModel( p_playlist, p_intf, p_root, this );
98 #ifdef MEDIA_LIBRARY
99     MLModel *mlmodel = new MLModel( p_intf, this );
100     mainView = new StandardPLPanel( this, p_intf, p_root, selector, model, mlmodel );
101 #else
102     mainView = new StandardPLPanel( this, p_intf, p_root, selector, model, NULL );
103 #endif
104
105     /* Location Bar */
106     locationBar = new LocationBar( model );
107     locationBar->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Preferred );
108     layout->addWidget( locationBar, 0, 0, 1, 2 );
109     layout->setColumnStretch( 0, 5 );
110     CONNECT( locationBar, invoked( const QModelIndex & ),
111              mainView, browseInto( const QModelIndex & ) );
112
113     /* Button to switch views */
114     QToolButton *viewButton = new QToolButton( this );
115     viewButton->setIcon( style()->standardIcon( QStyle::SP_FileDialogDetailedView ) );
116     viewButton->setToolTip( qtr("Change playlistview") );
117     layout->addWidget( viewButton, 0, 2 );
118
119     /* View selection menu */
120     QSignalMapper *viewSelectionMapper = new QSignalMapper( this );
121     CONNECT( viewSelectionMapper, mapped( int ), mainView, showView( int ) );
122
123     QActionGroup *actionGroup = new QActionGroup( this );
124
125     for( int i = 0; i < StandardPLPanel::VIEW_COUNT; i++ )
126     {
127         viewActions[i] = actionGroup->addAction( viewNames[i] );
128         viewActions[i]->setCheckable( true );
129         viewSelectionMapper->setMapping( viewActions[i], i );
130         CONNECT( viewActions[i], triggered(), viewSelectionMapper, map() );
131     }
132     viewActions[0]->setChecked( true );
133
134     QMenu *viewMenu = new QMenu( viewButton );
135     viewMenu->addActions( actionGroup->actions() );
136     viewButton->setMenu( viewMenu );
137     CONNECT( viewButton, clicked(), mainView, cycleViews() );
138
139     /* Search */
140     searchEdit = new SearchLineEdit( this );
141     searchEdit->setMaximumWidth( 250 );
142     searchEdit->setMinimumWidth( 80 );
143     layout->addWidget( searchEdit, 0, 3 );
144     CONNECT( searchEdit, textChanged( const QString& ),
145              mainView, search( const QString& ) );
146     CONNECT( searchEdit, searchDelayedChanged( const QString& ),
147              mainView, searchDelayed( const QString & ) );
148
149     CONNECT( mainView, viewChanged( const QModelIndex& ),
150              this, changeView( const QModelIndex &) );
151     layout->setColumnStretch( 3, 3 );
152
153     /* Connect the activation of the selector to a redefining of the PL */
154     DCONNECT( selector, categoryActivated( playlist_item_t *, bool ),
155               mainView, setRoot( playlist_item_t *, bool ) );
156     mainView->setRoot( p_root, false );
157
158     /* */
159     split = new PlaylistSplitter( this );
160
161     /* Add the two sides of the QSplitter */
162     split->addWidget( leftSplitter );
163     split->addWidget( mainView );
164
165     QList<int> sizeList;
166     sizeList << 180 << 420 ;
167     split->setSizes( sizeList );
168     split->setStretchFactor( 0, 0 );
169     split->setStretchFactor( 1, 3 );
170     split->setCollapsible( 1, false );
171     leftSplitter->setMaximumWidth( 250 );
172
173     /* In case we want to keep the splitter information */
174     // components shall never write there setting to a fixed location, may infer
175     // with other uses of the same component...
176     getSettings()->beginGroup("Playlist");
177     split->restoreState( getSettings()->value("splitterSizes").toByteArray());
178     leftSplitter->restoreState( getSettings()->value("leftSplitterGeometry").toByteArray() );
179     getSettings()->endGroup();
180
181     layout->addWidget( split, 1, 0, 1, -1 );
182
183     /* Zoom */
184     QSlider *zoomSlider = new QSlider( Qt::Horizontal, this );
185     zoomSlider->setRange( -10, 10);
186     zoomSlider->setPageStep( 3 );
187     zoomSlider->setValue( model->getZoom() );
188     CONNECT( zoomSlider, valueChanged( int ), model, changeZoom( int ) );
189
190     layout->addWidget( zoomSlider, 1, 3, ( Qt::AlignBottom | Qt::AlignRight ) );
191
192     setAcceptDrops( true );
193     setWindowTitle( qtr( "Playlist" ) );
194     setWindowRole( "vlc-playlist" );
195     setWindowIcon( QApplication::windowIcon() );
196 }
197
198 PlaylistWidget::~PlaylistWidget()
199 {
200     getSettings()->beginGroup("Playlist");
201     getSettings()->setValue( "splitterSizes", split->saveState() );
202     getSettings()->setValue( "leftSplitterGeometry", leftSplitter->saveState() );
203     getSettings()->endGroup();
204     msg_Dbg( p_intf, "Playlist Destroyed" );
205 }
206
207 void PlaylistWidget::dropEvent( QDropEvent *event )
208 {
209     if( p_intf->p_sys->p_mi )
210         p_intf->p_sys->p_mi->dropEventPlay( event, false );
211 }
212 void PlaylistWidget::dragEnterEvent( QDragEnterEvent *event )
213 {
214     event->acceptProposedAction();
215 }
216
217 void PlaylistWidget::closeEvent( QCloseEvent *event )
218 {
219     if( THEDP->isDying() )
220     {
221         p_intf->p_sys->p_mi->playlistVisible = true;
222         event->accept();
223     }
224     else
225     {
226         p_intf->p_sys->p_mi->playlistVisible = false;
227         hide();
228         event->ignore();
229     }
230 }
231
232 void PlaylistWidget::forceHide()
233 {
234     leftSplitter->hide();
235     mainView->hide();
236     updateGeometry();
237 }
238
239 void PlaylistWidget::forceShow()
240 {
241     leftSplitter->show();
242     mainView->show();
243     updateGeometry();
244 }
245
246 void PlaylistWidget::changeView( const QModelIndex& index )
247 {
248     searchEdit->clear();
249     locationBar->setIndex( index );
250     int i = mainView->currentViewIndex();
251     viewActions[i]->setChecked(true);
252 }
253
254 #include <QSignalMapper>
255 #include <QMenu>
256 #include <QPainter>
257 LocationBar::LocationBar( PLModel *m )
258 {
259     model = m;
260     mapper = new QSignalMapper( this );
261     CONNECT( mapper, mapped( int ), this, invoke( int ) );
262
263     btnMore = new LocationButton( "...", false, true, this );
264     menuMore = new QMenu( this );
265     btnMore->setMenu( menuMore );
266 }
267
268 void LocationBar::setIndex( const QModelIndex &index )
269 {
270     qDeleteAll( buttons );
271     buttons.clear();
272     qDeleteAll( actions );
273     actions.clear();
274
275     QModelIndex i = index;
276     bool first = true;
277
278     while( true )
279     {
280         PLItem *item = model->getItem( i );
281         QString text;
282
283         char *fb_name = input_item_GetTitle( item->inputItem() );
284         if( EMPTY_STR( fb_name ) )
285         {
286             free( fb_name );
287             fb_name = input_item_GetName( item->inputItem() );
288         }
289         text = qfu(fb_name);
290         free(fb_name);
291
292         QAbstractButton *btn = new LocationButton( text, first, !first, this );
293         btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
294         buttons.append( btn );
295
296         QAction *action = new QAction( text, this );
297         actions.append( action );
298         CONNECT( btn, clicked(), action, trigger() );
299
300         mapper->setMapping( action, item->id() );
301         CONNECT( action, triggered(), mapper, map() );
302
303         first = false;
304
305         if( i.isValid() ) i = i.parent();
306         else break;
307     }
308
309     QString prefix;
310     for( int a = actions.count() - 1; a >= 0 ; a-- )
311     {
312         actions[a]->setText( prefix + actions[a]->text() );
313         prefix += QString("  ");
314     }
315
316     if( isVisible() ) layOut( size() );
317 }
318
319 void LocationBar::setRootIndex()
320 {
321     setIndex( QModelIndex() );
322 }
323
324 void LocationBar::invoke( int i_id )
325 {
326     QModelIndex index = model->index( i_id, 0 );
327     emit invoked ( index );
328 }
329
330 void LocationBar::layOut( const QSize& size )
331 {
332     menuMore->clear();
333     widths.clear();
334
335     int count = buttons.count();
336     int totalWidth = 0;
337     for( int i = 0; i < count; i++ )
338     {
339         int w = buttons[i]->sizeHint().width();
340         widths.append( w );
341         totalWidth += w;
342         if( totalWidth > size.width() ) break;
343     }
344
345     int x = 0;
346     int shown = widths.count();
347
348     if( totalWidth > size.width() && count > 1 )
349     {
350         QSize sz = btnMore->sizeHint();
351         btnMore->setGeometry( 0, 0, sz.width(), size.height() );
352         btnMore->show();
353         x = sz.width();
354         totalWidth += x;
355     }
356     else
357     {
358         btnMore->hide();
359     }
360     for( int i = count - 1; i >= 0; i-- )
361     {
362         if( totalWidth <= size.width() || i == 0)
363         {
364             buttons[i]->setGeometry( x, 0, qMin( size.width() - x, widths[i] ), size.height() );
365             buttons[i]->show();
366             x += widths[i];
367             totalWidth -= widths[i];
368         }
369         else
370         {
371             menuMore->addAction( actions[i] );
372             buttons[i]->hide();
373             if( i < shown ) totalWidth -= widths[i];
374         }
375     }
376 }
377
378 void LocationBar::resizeEvent ( QResizeEvent * event )
379 {
380     layOut( event->size() );
381 }
382
383 QSize LocationBar::sizeHint() const
384 {
385     return btnMore->sizeHint();
386 }
387
388 LocationButton::LocationButton( const QString &text, bool bold,
389                                 bool arrow, QWidget * parent )
390   : QPushButton( parent ), b_arrow( arrow )
391 {
392     QFont font;
393     font.setBold( bold );
394     setFont( font );
395     setText( text );
396 }
397
398 #define PADDING 4
399
400 void LocationButton::paintEvent ( QPaintEvent * )
401 {
402     QStyleOptionButton option;
403     option.initFrom( this );
404     option.state |= QStyle::State_Enabled;
405     QPainter p( this );
406
407     if( underMouse() )
408     {
409         p.save();
410         p.setRenderHint( QPainter::Antialiasing, true );
411         QColor c = palette().color( QPalette::Highlight );
412         p.setPen( c );
413         p.setBrush( c.lighter( 150 ) );
414         p.setOpacity( 0.2 );
415         p.drawRoundedRect( option.rect.adjusted( 0, 2, 0, -2 ), 5, 5 );
416         p.restore();
417     }
418
419     QRect r = option.rect.adjusted( PADDING, 0, -PADDING - (b_arrow ? 10 : 0), 0 );
420
421     QString str( text() );
422     /* This check is absurd, but either it is not done properly inside elidedText(),
423        or boundingRect() is wrong */
424     if( r.width() < fontMetrics().boundingRect( text() ).width() )
425         str = fontMetrics().elidedText( text(), Qt::ElideRight, r.width() );
426     p.drawText( r, Qt::AlignVCenter | Qt::AlignLeft, str );
427
428     if( b_arrow )
429     {
430         option.rect.setWidth( 10 );
431         option.rect.moveRight( rect().right() );
432         style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
433     }
434 }
435
436 QSize LocationButton::sizeHint() const
437 {
438     QSize s( fontMetrics().boundingRect( text() ).size() );
439     /* Add two pixels to width: font metrics are buggy, if you pass text through elidation
440        with exactly the width of its bounding rect, sometimes it still elides */
441     s.setWidth( s.width() + ( 2 * PADDING ) + ( b_arrow ? 10 : 0 ) + 2 );
442     s.setHeight( s.height() + 2 * PADDING );
443     return s;
444 }
445
446 #undef PADDING
447
448 #ifdef Q_WS_MAC
449 QSplitterHandle *PlaylistSplitter::createHandle()
450 {
451     return new SplitterHandle( orientation(), this );
452 }
453
454 SplitterHandle::SplitterHandle( Qt::Orientation orientation, QSplitter * parent )
455                : QSplitterHandle( orientation, parent)
456 {
457 };
458
459 QSize SplitterHandle::sizeHint() const
460 {
461     return (orientation() == Qt::Horizontal) ? QSize( 1, height() ) : QSize( width(), 1 );
462 }
463
464 void SplitterHandle::paintEvent(QPaintEvent *event)
465 {
466     QPainter painter( this );
467     painter.fillRect( event->rect(), QColor(81, 81, 81) );
468 }
469 #endif /* __APPLE__ */