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