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