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 <QSpacerItem>
47 /**********************************************************************
48 * Playlist Widget. The embedded playlist
49 **********************************************************************/
51 PlaylistWidget::PlaylistWidget( intf_thread_t *_p_i, QWidget *_par )
52 : QWidget( _par ), p_intf ( _p_i )
55 setContentsMargins( 0, 3, 0, 3 );
57 QGridLayout *layout = new QGridLayout( this );
58 layout->setMargin( 0 ); layout->setSpacing( 0 );
63 /* We use a QSplitter for the left part */
64 leftSplitter = new QSplitter( Qt::Vertical, this );
67 PLSelector *selector = new PLSelector( this, p_intf );
68 leftSplitter->addWidget( selector);
70 /* Create a Container for the Art Label
71 in order to have a beautiful resizing for the selector above it */
72 QWidget *artContainer = new QWidget;
73 QHBoxLayout *artContLay = new QHBoxLayout( artContainer );
74 artContLay->setMargin( 0 );
75 artContLay->setSpacing( 0 );
76 artContainer->setMaximumHeight( 128 );
79 CoverArtLabel *art = new CoverArtLabel( artContainer, p_intf );
80 art->setToolTip( qtr( "Double click to get media information" ) );
81 artContLay->addWidget( art, 1 );
83 CONNECT( THEMIM->getIM(), artChanged( QString ),
84 art, showArtUpdate( const QString& ) );
86 leftSplitter->addWidget( artContainer );
91 /* Initialisation of the playlist */
92 playlist_t * p_playlist = THEPL;
94 playlist_item_t *p_root = p_playlist->p_playing;
97 setMinimumWidth( 400 );
99 PLModel *model = new PLModel( p_playlist, p_intf, p_root, this );
101 MLModel *mlmodel = new MLModel( p_intf, this );
102 mainView = new StandardPLPanel( this, p_intf, p_root, selector, model, mlmodel );
104 mainView = new StandardPLPanel( this, p_intf, p_root, selector, model, NULL );
108 locationBar = new LocationBar( model );
109 locationBar->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Preferred );
110 layout->addWidget( locationBar, 0, 0, 1, 2 );
111 layout->setColumnStretch( 0, 5 );
112 CONNECT( locationBar, invoked( const QModelIndex & ),
113 mainView, browseInto( const QModelIndex & ) );
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 layout->addWidget( viewButton, 0, 2 );
121 /* View selection menu */
122 QSignalMapper *viewSelectionMapper = new QSignalMapper( this );
123 CONNECT( viewSelectionMapper, mapped( int ), mainView, showView( int ) );
125 QActionGroup *actionGroup = new QActionGroup( this );
127 for( int i = 0; i < StandardPLPanel::VIEW_COUNT; i++ )
129 viewActions[i] = actionGroup->addAction( viewNames[i] );
130 viewActions[i]->setCheckable( true );
131 viewSelectionMapper->setMapping( viewActions[i], i );
132 CONNECT( viewActions[i], triggered(), viewSelectionMapper, map() );
134 viewActions[0]->setChecked( true );
136 QMenu *viewMenu = new QMenu( viewButton );
137 viewMenu->addActions( actionGroup->actions() );
138 viewButton->setMenu( viewMenu );
139 CONNECT( viewButton, clicked(), mainView, cycleViews() );
142 searchEdit = new SearchLineEdit( this );
143 searchEdit->setMaximumWidth( 250 );
144 searchEdit->setMinimumWidth( 80 );
145 layout->addWidget( searchEdit, 0, 3 );
146 CONNECT( searchEdit, textChanged( const QString& ),
147 mainView, search( const QString& ) );
148 CONNECT( searchEdit, searchDelayedChanged( const QString& ),
149 mainView, searchDelayed( const QString & ) );
151 CONNECT( mainView, viewChanged( const QModelIndex& ),
152 this, changeView( const QModelIndex &) );
153 layout->setColumnStretch( 3, 3 );
155 /* Connect the activation of the selector to a redefining of the PL */
156 DCONNECT( selector, categoryActivated( playlist_item_t *, bool ),
157 mainView, setRoot( playlist_item_t *, bool ) );
158 mainView->setRoot( p_root, false );
161 split = new PlaylistSplitter( this );
163 /* Add the two sides of the QSplitter */
164 split->addWidget( leftSplitter );
165 split->addWidget( mainView );
168 sizeList << 180 << 420 ;
169 split->setSizes( sizeList );
170 split->setStretchFactor( 0, 0 );
171 split->setStretchFactor( 1, 3 );
172 split->setCollapsible( 1, false );
173 leftSplitter->setMaximumWidth( 250 );
175 /* In case we want to keep the splitter information */
176 // components shall never write there setting to a fixed location, may infer
177 // with other uses of the same component...
178 getSettings()->beginGroup("Playlist");
179 split->restoreState( getSettings()->value("splitterSizes").toByteArray());
180 leftSplitter->restoreState( getSettings()->value("leftSplitterGeometry").toByteArray() );
181 getSettings()->endGroup();
183 layout->addWidget( split, 1, 0, 1, -1 );
186 QSlider *zoomSlider = new QSlider( Qt::Horizontal, this );
187 zoomSlider->setRange( -10, 10);
188 zoomSlider->setPageStep( 3 );
189 zoomSlider->setValue( model->getZoom() );
190 CONNECT( zoomSlider, valueChanged( int ), model, changeZoom( int ) );
192 /* pad our zoom slider */
193 QGridLayout *sliderLayout = new QGridLayout;
194 QSpacerItem* sliderSpacer =
195 new QSpacerItem( mainView->getScrollBarsSize(),
196 mainView->getScrollBarsSize(),
197 QSizePolicy::Fixed, QSizePolicy::Fixed );
198 sliderLayout->addWidget( zoomSlider, 0, 0, Qt::AlignCenter );
199 sliderLayout->addItem( sliderSpacer, 1, 1, Qt::AlignCenter );
201 layout->addLayout( sliderLayout, 1, 3, ( Qt::AlignBottom | Qt::AlignRight ) );
203 setAcceptDrops( true );
204 setWindowTitle( qtr( "Playlist" ) );
205 setWindowRole( "vlc-playlist" );
206 setWindowIcon( QApplication::windowIcon() );
209 PlaylistWidget::~PlaylistWidget()
211 getSettings()->beginGroup("Playlist");
212 getSettings()->setValue( "splitterSizes", split->saveState() );
213 getSettings()->setValue( "leftSplitterGeometry", leftSplitter->saveState() );
214 getSettings()->endGroup();
215 msg_Dbg( p_intf, "Playlist Destroyed" );
218 void PlaylistWidget::dropEvent( QDropEvent *event )
220 if( p_intf->p_sys->p_mi )
221 p_intf->p_sys->p_mi->dropEventPlay( event, false );
223 void PlaylistWidget::dragEnterEvent( QDragEnterEvent *event )
225 event->acceptProposedAction();
228 void PlaylistWidget::closeEvent( QCloseEvent *event )
230 if( THEDP->isDying() )
232 p_intf->p_sys->p_mi->playlistVisible = true;
237 p_intf->p_sys->p_mi->playlistVisible = false;
243 void PlaylistWidget::forceHide()
245 leftSplitter->hide();
250 void PlaylistWidget::forceShow()
252 leftSplitter->show();
257 void PlaylistWidget::changeView( const QModelIndex& index )
260 locationBar->setIndex( index );
261 int i = mainView->currentViewIndex();
262 viewActions[i]->setChecked(true);
265 #include <QSignalMapper>
268 LocationBar::LocationBar( PLModel *m )
271 mapper = new QSignalMapper( this );
272 CONNECT( mapper, mapped( int ), this, invoke( int ) );
274 btnMore = new LocationButton( "...", false, true, this );
275 menuMore = new QMenu( this );
276 btnMore->setMenu( menuMore );
279 void LocationBar::setIndex( const QModelIndex &index )
281 qDeleteAll( buttons );
283 qDeleteAll( actions );
286 QModelIndex i = index;
291 PLItem *item = model->getItem( i );
294 char *fb_name = input_item_GetTitle( item->inputItem() );
295 if( EMPTY_STR( fb_name ) )
298 fb_name = input_item_GetName( item->inputItem() );
303 QAbstractButton *btn = new LocationButton( text, first, !first, this );
304 btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
305 buttons.append( btn );
307 QAction *action = new QAction( text, this );
308 actions.append( action );
309 CONNECT( btn, clicked(), action, trigger() );
311 mapper->setMapping( action, item->id() );
312 CONNECT( action, triggered(), mapper, map() );
316 if( i.isValid() ) i = i.parent();
321 for( int a = actions.count() - 1; a >= 0 ; a-- )
323 actions[a]->setText( prefix + actions[a]->text() );
324 prefix += QString(" ");
327 if( isVisible() ) layOut( size() );
330 void LocationBar::setRootIndex()
332 setIndex( QModelIndex() );
335 void LocationBar::invoke( int i_id )
337 QModelIndex index = model->index( i_id, 0 );
338 emit invoked ( index );
341 void LocationBar::layOut( const QSize& size )
346 int count = buttons.count();
348 for( int i = 0; i < count; i++ )
350 int w = buttons[i]->sizeHint().width();
353 if( totalWidth > size.width() ) break;
357 int shown = widths.count();
359 if( totalWidth > size.width() && count > 1 )
361 QSize sz = btnMore->sizeHint();
362 btnMore->setGeometry( 0, 0, sz.width(), size.height() );
371 for( int i = count - 1; i >= 0; i-- )
373 if( totalWidth <= size.width() || i == 0)
375 buttons[i]->setGeometry( x, 0, qMin( size.width() - x, widths[i] ), size.height() );
378 totalWidth -= widths[i];
382 menuMore->addAction( actions[i] );
384 if( i < shown ) totalWidth -= widths[i];
389 void LocationBar::resizeEvent ( QResizeEvent * event )
391 layOut( event->size() );
394 QSize LocationBar::sizeHint() const
396 return btnMore->sizeHint();
399 LocationButton::LocationButton( const QString &text, bool bold,
400 bool arrow, QWidget * parent )
401 : QPushButton( parent ), b_arrow( arrow )
404 font.setBold( bold );
411 void LocationButton::paintEvent ( QPaintEvent * )
413 QStyleOptionButton option;
414 option.initFrom( this );
415 option.state |= QStyle::State_Enabled;
421 p.setRenderHint( QPainter::Antialiasing, true );
422 QColor c = palette().color( QPalette::Highlight );
424 p.setBrush( c.lighter( 150 ) );
426 p.drawRoundedRect( option.rect.adjusted( 0, 2, 0, -2 ), 5, 5 );
430 QRect r = option.rect.adjusted( PADDING, 0, -PADDING - (b_arrow ? 10 : 0), 0 );
432 QString str( text() );
433 /* This check is absurd, but either it is not done properly inside elidedText(),
434 or boundingRect() is wrong */
435 if( r.width() < fontMetrics().boundingRect( text() ).width() )
436 str = fontMetrics().elidedText( text(), Qt::ElideRight, r.width() );
437 p.drawText( r, Qt::AlignVCenter | Qt::AlignLeft, str );
441 option.rect.setWidth( 10 );
442 option.rect.moveRight( rect().right() );
443 style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
447 QSize LocationButton::sizeHint() const
449 QSize s( fontMetrics().boundingRect( text() ).size() );
450 /* Add two pixels to width: font metrics are buggy, if you pass text through elidation
451 with exactly the width of its bounding rect, sometimes it still elides */
452 s.setWidth( s.width() + ( 2 * PADDING ) + ( b_arrow ? 10 : 0 ) + 2 );
453 s.setHeight( s.height() + 2 * PADDING );
460 QSplitterHandle *PlaylistSplitter::createHandle()
462 return new SplitterHandle( orientation(), this );
465 SplitterHandle::SplitterHandle( Qt::Orientation orientation, QSplitter * parent )
466 : QSplitterHandle( orientation, parent)
470 QSize SplitterHandle::sizeHint() const
472 return (orientation() == Qt::Horizontal) ? QSize( 1, height() ) : QSize( width(), 1 );
475 void SplitterHandle::paintEvent(QPaintEvent *event)
477 QPainter painter( this );
478 painter.fillRect( event->rect(), QColor(81, 81, 81) );
480 #endif /* __APPLE__ */