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