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