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