1 /*****************************************************************************
2 * playlist.cpp : Custom widgets for the playlist
3 ****************************************************************************
4 * Copyright © 2007-2010 the VideoLAN team
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 *****************************************************************************/
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*/
42 #include <QSignalMapper>
44 #include <QStackedWidget>
46 /**********************************************************************
47 * Playlist Widget. The embedded playlist
48 **********************************************************************/
50 PlaylistWidget::PlaylistWidget( intf_thread_t *_p_i, QWidget *_par )
51 : QWidget( _par ), p_intf ( _p_i )
54 setContentsMargins( 0, 3, 0, 3 );
56 QGridLayout *layout = new QGridLayout( this );
57 layout->setMargin( 0 ); layout->setSpacing( 0 );
62 /* We use a QSplitter for the left part */
63 leftSplitter = new QSplitter( Qt::Vertical, this );
66 PLSelector *selector = new PLSelector( this, p_intf );
67 leftSplitter->addWidget( selector);
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 );
75 CoverArtLabel *art = new CoverArtLabel( artContainer, p_intf );
76 art->setToolTip( qtr( "Double click to get media information" ) );
77 artContainer->addWidget( art );
79 CONNECT( THEMIM->getIM(), artChanged( QString ),
80 art, showArtUpdate( const QString& ) );
82 leftSplitter->addWidget( artContainer );
87 /* Initialisation of the playlist */
88 playlist_t * p_playlist = THEPL;
90 playlist_item_t *p_root = p_playlist->p_playing;
93 setMinimumWidth( 400 );
95 PLModel *model = PLModel::getPLModel( p_intf );
97 MLModel *mlmodel = new MLModel( p_intf, this );
98 mainView = new StandardPLPanel( this, p_intf, p_root, selector, model, mlmodel );
100 mainView = new StandardPLPanel( this, p_intf, p_root, selector, model, NULL );
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 & ) );
111 QHBoxLayout *topbarLayout = new QHBoxLayout();
112 layout->addLayout( topbarLayout, 0, 1 );
113 topbarLayout->setSpacing( 10 );
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 );
121 /* View selection menu */
122 QSignalMapper *viewSelectionMapper = new QSignalMapper( this );
123 CONNECT( viewSelectionMapper, mapped( int ), mainView, showView( int ) );
125 QActionGroup *actionGroup = new QActionGroup( this );
128 # define MAX_VIEW StandardPLPanel::VIEW_COUNT
130 # define MAX_VIEW StandardPLPanel::VIEW_COUNT - 1
132 for( int i = 0; i < MAX_VIEW; i++ )
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() );
139 viewActions[0]->setChecked( true );
141 QMenu *viewMenu = new QMenu( viewButton );
142 viewMenu->addActions( actionGroup->actions() );
143 viewButton->setMenu( viewMenu );
144 CONNECT( viewButton, clicked(), mainView, cycleViews() );
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 & ) );
157 CONNECT( mainView, viewChanged( const QModelIndex& ),
158 this, changeView( const QModelIndex &) );
161 QSlider *zoomSlider = new QSlider( Qt::Horizontal, this );
162 zoomSlider->setRange( -10, 10);
163 zoomSlider->setPageStep( 3 );
164 zoomSlider->setValue( model->getZoom() );
165 zoomSlider->setToolTip( qtr("Zoom playlist") );
166 CONNECT( zoomSlider, valueChanged( int ), model, changeZoom( int ) );
167 topbarLayout->addWidget( zoomSlider );
169 /* Connect the activation of the selector to a redefining of the PL */
170 DCONNECT( selector, categoryActivated( playlist_item_t *, bool ),
171 mainView, setRoot( playlist_item_t *, bool ) );
172 mainView->setRoot( p_root, false );
175 split = new PlaylistSplitter( this );
177 /* Add the two sides of the QSplitter */
178 split->addWidget( leftSplitter );
179 split->addWidget( mainView );
182 sizeList << 180 << 420 ;
183 split->setSizes( sizeList );
184 split->setStretchFactor( 0, 0 );
185 split->setStretchFactor( 1, 3 );
186 split->setCollapsible( 1, false );
187 leftSplitter->setMaximumWidth( 250 );
189 /* In case we want to keep the splitter information */
190 // components shall never write there setting to a fixed location, may infer
191 // with other uses of the same component...
192 getSettings()->beginGroup("Playlist");
193 split->restoreState( getSettings()->value("splitterSizes").toByteArray());
194 leftSplitter->restoreState( getSettings()->value("leftSplitterGeometry").toByteArray() );
195 getSettings()->endGroup();
197 layout->addWidget( split, 1, 0, 1, -1 );
199 setAcceptDrops( true );
200 setWindowTitle( qtr( "Playlist" ) );
201 setWindowRole( "vlc-playlist" );
202 setWindowIcon( QApplication::windowIcon() );
205 PlaylistWidget::~PlaylistWidget()
207 getSettings()->beginGroup("Playlist");
208 getSettings()->setValue( "splitterSizes", split->saveState() );
209 getSettings()->setValue( "leftSplitterGeometry", leftSplitter->saveState() );
210 getSettings()->endGroup();
211 msg_Dbg( p_intf, "Playlist Destroyed" );
214 void PlaylistWidget::dropEvent( QDropEvent *event )
216 if( p_intf->p_sys->p_mi )
217 p_intf->p_sys->p_mi->dropEventPlay( event, false );
219 void PlaylistWidget::dragEnterEvent( QDragEnterEvent *event )
221 event->acceptProposedAction();
224 void PlaylistWidget::closeEvent( QCloseEvent *event )
226 if( THEDP->isDying() )
228 p_intf->p_sys->p_mi->playlistVisible = true;
233 p_intf->p_sys->p_mi->playlistVisible = false;
239 void PlaylistWidget::forceHide()
241 leftSplitter->hide();
246 void PlaylistWidget::forceShow()
248 leftSplitter->show();
253 void PlaylistWidget::changeView( const QModelIndex& index )
256 locationBar->setIndex( index );
257 int i = mainView->currentViewIndex();
258 viewActions[i]->setChecked(true);
261 #include <QSignalMapper>
264 LocationBar::LocationBar( PLModel *m )
267 mapper = new QSignalMapper( this );
268 CONNECT( mapper, mapped( int ), this, invoke( int ) );
270 btnMore = new LocationButton( "...", false, true, this );
271 menuMore = new QMenu( this );
272 btnMore->setMenu( menuMore );
275 void LocationBar::setIndex( const QModelIndex &index )
277 qDeleteAll( buttons );
279 qDeleteAll( actions );
282 QModelIndex i = index;
287 PLItem *item = model->getItem( i );
290 char *fb_name = input_item_GetTitle( item->inputItem() );
291 if( EMPTY_STR( fb_name ) )
294 fb_name = input_item_GetName( item->inputItem() );
299 QAbstractButton *btn = new LocationButton( text, first, !first, this );
300 btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
301 buttons.append( btn );
303 QAction *action = new QAction( text, this );
304 actions.append( action );
305 CONNECT( btn, clicked(), action, trigger() );
307 mapper->setMapping( action, item->id() );
308 CONNECT( action, triggered(), mapper, map() );
312 if( i.isValid() ) i = i.parent();
317 for( int a = actions.count() - 1; a >= 0 ; a-- )
319 actions[a]->setText( prefix + actions[a]->text() );
320 prefix += QString(" ");
323 if( isVisible() ) layOut( size() );
326 void LocationBar::setRootIndex()
328 setIndex( QModelIndex() );
331 void LocationBar::invoke( int i_id )
333 QModelIndex index = model->index( i_id, 0 );
334 emit invoked ( index );
337 void LocationBar::layOut( const QSize& size )
342 int count = buttons.count();
344 for( int i = 0; i < count; i++ )
346 int w = buttons[i]->sizeHint().width();
349 if( totalWidth > size.width() ) break;
353 int shown = widths.count();
355 if( totalWidth > size.width() && count > 1 )
357 QSize sz = btnMore->sizeHint();
358 btnMore->setGeometry( 0, 0, sz.width(), size.height() );
367 for( int i = count - 1; i >= 0; i-- )
369 if( totalWidth <= size.width() || i == 0)
371 buttons[i]->setGeometry( x, 0, qMin( size.width() - x, widths[i] ), size.height() );
374 totalWidth -= widths[i];
378 menuMore->addAction( actions[i] );
380 if( i < shown ) totalWidth -= widths[i];
385 void LocationBar::resizeEvent ( QResizeEvent * event )
387 layOut( event->size() );
390 QSize LocationBar::sizeHint() const
392 return btnMore->sizeHint();
395 LocationButton::LocationButton( const QString &text, bool bold,
396 bool arrow, QWidget * parent )
397 : QPushButton( parent ), b_arrow( arrow )
400 font.setBold( bold );
407 void LocationButton::paintEvent ( QPaintEvent * )
409 QStyleOptionButton option;
410 option.initFrom( this );
411 option.state |= QStyle::State_Enabled;
417 p.setRenderHint( QPainter::Antialiasing, true );
418 QColor c = palette().color( QPalette::Highlight );
420 p.setBrush( c.lighter( 150 ) );
422 p.drawRoundedRect( option.rect.adjusted( 0, 2, 0, -2 ), 5, 5 );
426 QRect r = option.rect.adjusted( PADDING, 0, -PADDING - (b_arrow ? 10 : 0), 0 );
428 QString str( text() );
429 /* This check is absurd, but either it is not done properly inside elidedText(),
430 or boundingRect() is wrong */
431 if( r.width() < fontMetrics().boundingRect( text() ).width() )
432 str = fontMetrics().elidedText( text(), Qt::ElideRight, r.width() );
433 p.drawText( r, Qt::AlignVCenter | Qt::AlignLeft, str );
437 option.rect.setWidth( 10 );
438 option.rect.moveRight( rect().right() );
439 style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
443 QSize LocationButton::sizeHint() const
445 QSize s( fontMetrics().boundingRect( text() ).size() );
446 /* Add two pixels to width: font metrics are buggy, if you pass text through elidation
447 with exactly the width of its bounding rect, sometimes it still elides */
448 s.setWidth( s.width() + ( 2 * PADDING ) + ( b_arrow ? 10 : 0 ) + 2 );
449 s.setHeight( s.height() + 2 * PADDING );
456 QSplitterHandle *PlaylistSplitter::createHandle()
458 return new SplitterHandle( orientation(), this );
461 SplitterHandle::SplitterHandle( Qt::Orientation orientation, QSplitter * parent )
462 : QSplitterHandle( orientation, parent)
466 QSize SplitterHandle::sizeHint() const
468 return (orientation() == Qt::Horizontal) ? QSize( 1, height() ) : QSize( width(), 1 );
471 void SplitterHandle::paintEvent(QPaintEvent *event)
473 QPainter painter( this );
474 painter.fillRect( event->rect(), QColor(81, 81, 81) );
476 #endif /* __APPLE__ */