Qt: fix build with --disable-media-library
[vlc/vlc-skelet.git] / modules / gui / qt4 / components / playlist / playlist.cpp
blobfd46068ff216be037fa3f74ff060694a0d81c277
1 /*****************************************************************************
2 * playlist.cpp : Custom widgets for the playlist
3 ****************************************************************************
4 * Copyright © 2007-2010 the VideoLAN team
5 * $Id$
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Jean-Baptiste Kempf <jb@videolan.org>
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.
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.
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 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
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 */
36 #include "util/searchlineedit.hpp"
38 #include "input_manager.hpp" /* art signal */
39 #include "main_interface.hpp" /* DropEvent TODO remove this*/
41 #include <QMenu>
42 #include <QSignalMapper>
44 /**********************************************************************
45 * Playlist Widget. The embedded playlist
46 **********************************************************************/
48 PlaylistWidget::PlaylistWidget( intf_thread_t *_p_i, QWidget *_par )
49 : QWidget( _par ), p_intf ( _p_i )
52 setContentsMargins( 0, 3, 0, 3 );
54 QGridLayout *layout = new QGridLayout( this );
55 layout->setMargin( 0 ); layout->setSpacing( 0 );
57 /*******************
58 * Left *
59 *******************/
60 /* We use a QSplitter for the left part */
61 leftSplitter = new QSplitter( Qt::Vertical, this );
63 /* Source Selector */
64 PLSelector *selector = new PLSelector( this, p_intf );
65 leftSplitter->addWidget( selector);
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 );
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 );
80 CONNECT( THEMIM->getIM(), artChanged( QString ),
81 art, showArtUpdate( const QString& ) );
83 leftSplitter->addWidget( artContainer );
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;
94 setMinimumWidth( 400 );
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
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 & ) );
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 );
118 /* View selection menu */
119 QSignalMapper *viewSelectionMapper = new QSignalMapper( this );
120 CONNECT( viewSelectionMapper, mapped( int ), mainView, showView( int ) );
122 QActionGroup *actionGroup = new QActionGroup( this );
124 for( int i = 0; i < StandardPLPanel::VIEW_COUNT; i++ )
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() );
131 viewActions[0]->setChecked( true );
133 QMenu *viewMenu = new QMenu( viewButton );
134 viewMenu->addActions( actionGroup->actions() );
135 viewButton->setMenu( viewMenu );
136 CONNECT( viewButton, clicked(), mainView, cycleViews() );
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 & ) );
148 CONNECT( mainView, viewChanged( const QModelIndex& ),
149 this, changeView( const QModelIndex &) );
150 layout->setColumnStretch( 3, 3 );
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 );
157 /* */
158 split = new PlaylistSplitter( this );
160 /* Add the two sides of the QSplitter */
161 split->addWidget( leftSplitter );
162 split->addWidget( mainView );
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 );
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();
180 layout->addWidget( split, 1, 0, 1, -1 );
181 setAcceptDrops( true );
182 setWindowTitle( qtr( "Playlist" ) );
183 setWindowRole( "vlc-playlist" );
184 setWindowIcon( QApplication::windowIcon() );
187 PlaylistWidget::~PlaylistWidget()
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" );
196 void PlaylistWidget::dropEvent( QDropEvent *event )
198 if( p_intf->p_sys->p_mi )
199 p_intf->p_sys->p_mi->dropEventPlay( event, false );
201 void PlaylistWidget::dragEnterEvent( QDragEnterEvent *event )
203 event->acceptProposedAction();
206 void PlaylistWidget::closeEvent( QCloseEvent *event )
208 if( THEDP->isDying() )
210 p_intf->p_sys->p_mi->playlistVisible = true;
211 event->accept();
213 else
215 p_intf->p_sys->p_mi->playlistVisible = false;
216 hide();
217 event->ignore();
221 void PlaylistWidget::forceHide()
223 leftSplitter->hide();
224 mainView->hide();
225 updateGeometry();
228 void PlaylistWidget::forceShow()
230 leftSplitter->show();
231 mainView->show();
232 updateGeometry();
235 void PlaylistWidget::changeView( const QModelIndex& index )
237 searchEdit->clear();
238 locationBar->setIndex( index );
239 int i = mainView->currentViewIndex();
240 viewActions[i]->setChecked(true);
243 #include <QSignalMapper>
244 #include <QMenu>
245 #include <QPainter>
246 LocationBar::LocationBar( PLModel *m )
248 model = m;
249 mapper = new QSignalMapper( this );
250 CONNECT( mapper, mapped( int ), this, invoke( int ) );
252 btnMore = new LocationButton( "...", false, true, this );
253 menuMore = new QMenu( this );
254 btnMore->setMenu( menuMore );
257 void LocationBar::setIndex( const QModelIndex &index )
259 qDeleteAll( buttons );
260 buttons.clear();
261 qDeleteAll( actions );
262 actions.clear();
264 QModelIndex i = index;
265 bool first = true;
267 while( true )
269 PLItem *item = model->getItem( i );
270 QString text;
272 char *fb_name = input_item_GetTitle( item->inputItem() );
273 if( !EMPTY_STR( fb_name ) )
274 text = qfu(fb_name);
275 else
277 fb_name = input_item_GetName( item->inputItem() );
278 text = qtr(fb_name);
280 free(fb_name);
282 QAbstractButton *btn = new LocationButton( text, first, !first, this );
283 btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
284 buttons.append( btn );
286 QAction *action = new QAction( text, this );
287 actions.append( action );
288 CONNECT( btn, clicked(), action, trigger() );
290 mapper->setMapping( action, item->id() );
291 CONNECT( action, triggered(), mapper, map() );
293 first = false;
295 if( i.isValid() ) i = i.parent();
296 else break;
299 QString prefix;
300 for( int a = actions.count() - 1; a >= 0 ; a-- )
302 actions[a]->setText( prefix + actions[a]->text() );
303 prefix += QString(" ");
306 if( isVisible() ) layOut( size() );
309 void LocationBar::setRootIndex()
311 setIndex( QModelIndex() );
314 void LocationBar::invoke( int i_id )
316 QModelIndex index = model->index( i_id, 0 );
317 emit invoked ( index );
320 void LocationBar::layOut( const QSize& size )
322 menuMore->clear();
323 widths.clear();
325 int count = buttons.count();
326 int totalWidth = 0;
327 for( int i = 0; i < count; i++ )
329 int w = buttons[i]->sizeHint().width();
330 widths.append( w );
331 totalWidth += w;
332 if( totalWidth > size.width() ) break;
335 int x = 0;
336 int shown = widths.count();
338 if( totalWidth > size.width() && count > 1 )
340 QSize sz = btnMore->sizeHint();
341 btnMore->setGeometry( 0, 0, sz.width(), size.height() );
342 btnMore->show();
343 x = sz.width();
344 totalWidth += x;
346 else
348 btnMore->hide();
350 for( int i = count - 1; i >= 0; i-- )
352 if( totalWidth <= size.width() || i == 0)
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];
359 else
361 menuMore->addAction( actions[i] );
362 buttons[i]->hide();
363 if( i < shown ) totalWidth -= widths[i];
368 void LocationBar::resizeEvent ( QResizeEvent * event )
370 layOut( event->size() );
373 QSize LocationBar::sizeHint() const
375 return btnMore->sizeHint();
378 LocationButton::LocationButton( const QString &text, bool bold,
379 bool arrow, QWidget * parent )
380 : b_arrow( arrow ), QPushButton( parent )
382 QFont font;
383 font.setBold( bold );
384 setFont( font );
385 setText( text );
388 #define PADDING 4
390 void LocationButton::paintEvent ( QPaintEvent * event )
392 QStyleOptionButton option;
393 option.initFrom( this );
394 option.state |= QStyle::State_Enabled;
395 QPainter p( this );
397 if( underMouse() )
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();
409 QRect r = option.rect.adjusted( PADDING, 0, -PADDING - (b_arrow ? 10 : 0), 0 );
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 );
418 if( b_arrow )
420 option.rect.setWidth( 10 );
421 option.rect.moveRight( rect().right() );
422 style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
426 QSize LocationButton::sizeHint() const
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;
436 #undef PADDING
438 #ifdef Q_WS_MAC
439 QSplitterHandle *PlaylistSplitter::createHandle()
441 return new SplitterHandle( orientation(), this );
444 SplitterHandle::SplitterHandle( Qt::Orientation orientation, QSplitter * parent )
445 : QSplitterHandle( orientation, parent)
449 QSize SplitterHandle::sizeHint() const
451 return (orientation() == Qt::Horizontal) ? QSize( 1, height() ) : QSize( width(), 1 );
454 void SplitterHandle::paintEvent(QPaintEvent *event)
456 QPainter painter( this );
457 painter.fillRect( event->rect(), QColor(81, 81, 81) );
459 #endif /* __APPLE__ */