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