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