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>
45 /**********************************************************************
46 * Playlist Widget. The embedded playlist
47 **********************************************************************/
49 PlaylistWidget::PlaylistWidget( intf_thread_t *_p_i, QWidget *_par )
50 : QWidget( _par ), p_intf ( _p_i )
53 setContentsMargins( 0, 3, 0, 3 );
55 QGridLayout *layout = new QGridLayout( this );
56 layout->setMargin( 0 ); layout->setSpacing( 0 );
61 /* We use a QSplitter for the left part */
62 leftSplitter = new QSplitter( Qt::Vertical, this );
65 PLSelector *selector = new PLSelector( this, p_intf );
66 leftSplitter->addWidget( selector);
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 );
77 CoverArtLabel *art = new CoverArtLabel( artContainer, p_intf );
78 art->setToolTip( qtr( "Double click to get media information" ) );
79 artContLay->addWidget( art, 1 );
81 CONNECT( THEMIM->getIM(), artChanged( QString ),
82 art, showArtUpdate( const QString& ) );
84 leftSplitter->addWidget( artContainer );
89 /* Initialisation of the playlist */
90 playlist_t * p_playlist = THEPL;
92 playlist_item_t *p_root = p_playlist->p_playing;
95 setMinimumWidth( 400 );
97 PLModel *model = new PLModel( p_playlist, p_intf, p_root, this );
99 MLModel *mlmodel = new MLModel( p_intf, this );
100 mainView = new StandardPLPanel( this, p_intf, p_root, selector, model, mlmodel );
102 mainView = new StandardPLPanel( this, p_intf, p_root, selector, model, NULL );
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 & ) );
113 QHBoxLayout *topbarLayout = new QHBoxLayout();
114 layout->addLayout( topbarLayout, 0, 1 );
115 topbarLayout->setSpacing( 10 );
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 );
123 /* View selection menu */
124 QSignalMapper *viewSelectionMapper = new QSignalMapper( this );
125 CONNECT( viewSelectionMapper, mapped( int ), mainView, showView( int ) );
127 QActionGroup *actionGroup = new QActionGroup( this );
129 for( int i = 0; i < StandardPLPanel::VIEW_COUNT; i++ )
131 viewActions[i] = actionGroup->addAction( viewNames[i] );
132 viewActions[i]->setCheckable( true );
133 viewSelectionMapper->setMapping( viewActions[i], i );
134 CONNECT( viewActions[i], triggered(), viewSelectionMapper, map() );
136 viewActions[0]->setChecked( true );
138 QMenu *viewMenu = new QMenu( viewButton );
139 viewMenu->addActions( actionGroup->actions() );
140 viewButton->setMenu( viewMenu );
141 CONNECT( viewButton, clicked(), mainView, cycleViews() );
144 searchEdit = new SearchLineEdit( this );
145 searchEdit->setMaximumWidth( 250 );
146 searchEdit->setMinimumWidth( 80 );
147 searchEdit->setToolTip( qtr("Search the playlist") );
148 topbarLayout->addWidget( searchEdit );
149 CONNECT( searchEdit, textChanged( const QString& ),
150 mainView, search( const QString& ) );
151 CONNECT( searchEdit, searchDelayedChanged( const QString& ),
152 mainView, searchDelayed( const QString & ) );
154 CONNECT( mainView, viewChanged( const QModelIndex& ),
155 this, changeView( const QModelIndex &) );
158 QSlider *zoomSlider = new QSlider( Qt::Horizontal, this );
159 zoomSlider->setRange( -10, 10);
160 zoomSlider->setPageStep( 3 );
161 zoomSlider->setValue( model->getZoom() );
162 zoomSlider->setToolTip( qtr("Zoom playlist") );
163 CONNECT( zoomSlider, valueChanged( int ), model, changeZoom( int ) );
164 topbarLayout->addWidget( zoomSlider );
166 /* Connect the activation of the selector to a redefining of the PL */
167 DCONNECT( selector, categoryActivated( playlist_item_t *, bool ),
168 mainView, setRoot( playlist_item_t *, bool ) );
169 mainView->setRoot( p_root, false );
172 split = new PlaylistSplitter( this );
174 /* Add the two sides of the QSplitter */
175 split->addWidget( leftSplitter );
176 split->addWidget( mainView );
179 sizeList << 180 << 420 ;
180 split->setSizes( sizeList );
181 split->setStretchFactor( 0, 0 );
182 split->setStretchFactor( 1, 3 );
183 split->setCollapsible( 1, false );
184 leftSplitter->setMaximumWidth( 250 );
186 /* In case we want to keep the splitter information */
187 // components shall never write there setting to a fixed location, may infer
188 // with other uses of the same component...
189 getSettings()->beginGroup("Playlist");
190 split->restoreState( getSettings()->value("splitterSizes").toByteArray());
191 leftSplitter->restoreState( getSettings()->value("leftSplitterGeometry").toByteArray() );
192 getSettings()->endGroup();
194 layout->addWidget( split, 1, 0, 1, -1 );
196 setAcceptDrops( true );
197 setWindowTitle( qtr( "Playlist" ) );
198 setWindowRole( "vlc-playlist" );
199 setWindowIcon( QApplication::windowIcon() );
202 PlaylistWidget::~PlaylistWidget()
204 getSettings()->beginGroup("Playlist");
205 getSettings()->setValue( "splitterSizes", split->saveState() );
206 getSettings()->setValue( "leftSplitterGeometry", leftSplitter->saveState() );
207 getSettings()->endGroup();
208 msg_Dbg( p_intf, "Playlist Destroyed" );
211 void PlaylistWidget::dropEvent( QDropEvent *event )
213 if( p_intf->p_sys->p_mi )
214 p_intf->p_sys->p_mi->dropEventPlay( event, false );
216 void PlaylistWidget::dragEnterEvent( QDragEnterEvent *event )
218 event->acceptProposedAction();
221 void PlaylistWidget::closeEvent( QCloseEvent *event )
223 if( THEDP->isDying() )
225 p_intf->p_sys->p_mi->playlistVisible = true;
230 p_intf->p_sys->p_mi->playlistVisible = false;
236 void PlaylistWidget::forceHide()
238 leftSplitter->hide();
243 void PlaylistWidget::forceShow()
245 leftSplitter->show();
250 void PlaylistWidget::changeView( const QModelIndex& index )
253 locationBar->setIndex( index );
254 int i = mainView->currentViewIndex();
255 viewActions[i]->setChecked(true);
258 #include <QSignalMapper>
261 LocationBar::LocationBar( PLModel *m )
264 mapper = new QSignalMapper( this );
265 CONNECT( mapper, mapped( int ), this, invoke( int ) );
267 btnMore = new LocationButton( "...", false, true, this );
268 menuMore = new QMenu( this );
269 btnMore->setMenu( menuMore );
272 void LocationBar::setIndex( const QModelIndex &index )
274 qDeleteAll( buttons );
276 qDeleteAll( actions );
279 QModelIndex i = index;
284 PLItem *item = model->getItem( i );
287 char *fb_name = input_item_GetTitle( item->inputItem() );
288 if( EMPTY_STR( fb_name ) )
291 fb_name = input_item_GetName( item->inputItem() );
296 QAbstractButton *btn = new LocationButton( text, first, !first, this );
297 btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
298 buttons.append( btn );
300 QAction *action = new QAction( text, this );
301 actions.append( action );
302 CONNECT( btn, clicked(), action, trigger() );
304 mapper->setMapping( action, item->id() );
305 CONNECT( action, triggered(), mapper, map() );
309 if( i.isValid() ) i = i.parent();
314 for( int a = actions.count() - 1; a >= 0 ; a-- )
316 actions[a]->setText( prefix + actions[a]->text() );
317 prefix += QString(" ");
320 if( isVisible() ) layOut( size() );
323 void LocationBar::setRootIndex()
325 setIndex( QModelIndex() );
328 void LocationBar::invoke( int i_id )
330 QModelIndex index = model->index( i_id, 0 );
331 emit invoked ( index );
334 void LocationBar::layOut( const QSize& size )
339 int count = buttons.count();
341 for( int i = 0; i < count; i++ )
343 int w = buttons[i]->sizeHint().width();
346 if( totalWidth > size.width() ) break;
350 int shown = widths.count();
352 if( totalWidth > size.width() && count > 1 )
354 QSize sz = btnMore->sizeHint();
355 btnMore->setGeometry( 0, 0, sz.width(), size.height() );
364 for( int i = count - 1; i >= 0; i-- )
366 if( totalWidth <= size.width() || i == 0)
368 buttons[i]->setGeometry( x, 0, qMin( size.width() - x, widths[i] ), size.height() );
371 totalWidth -= widths[i];
375 menuMore->addAction( actions[i] );
377 if( i < shown ) totalWidth -= widths[i];
382 void LocationBar::resizeEvent ( QResizeEvent * event )
384 layOut( event->size() );
387 QSize LocationBar::sizeHint() const
389 return btnMore->sizeHint();
392 LocationButton::LocationButton( const QString &text, bool bold,
393 bool arrow, QWidget * parent )
394 : QPushButton( parent ), b_arrow( arrow )
397 font.setBold( bold );
404 void LocationButton::paintEvent ( QPaintEvent * )
406 QStyleOptionButton option;
407 option.initFrom( this );
408 option.state |= QStyle::State_Enabled;
414 p.setRenderHint( QPainter::Antialiasing, true );
415 QColor c = palette().color( QPalette::Highlight );
417 p.setBrush( c.lighter( 150 ) );
419 p.drawRoundedRect( option.rect.adjusted( 0, 2, 0, -2 ), 5, 5 );
423 QRect r = option.rect.adjusted( PADDING, 0, -PADDING - (b_arrow ? 10 : 0), 0 );
425 QString str( text() );
426 /* This check is absurd, but either it is not done properly inside elidedText(),
427 or boundingRect() is wrong */
428 if( r.width() < fontMetrics().boundingRect( text() ).width() )
429 str = fontMetrics().elidedText( text(), Qt::ElideRight, r.width() );
430 p.drawText( r, Qt::AlignVCenter | Qt::AlignLeft, str );
434 option.rect.setWidth( 10 );
435 option.rect.moveRight( rect().right() );
436 style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
440 QSize LocationButton::sizeHint() const
442 QSize s( fontMetrics().boundingRect( text() ).size() );
443 /* Add two pixels to width: font metrics are buggy, if you pass text through elidation
444 with exactly the width of its bounding rect, sometimes it still elides */
445 s.setWidth( s.width() + ( 2 * PADDING ) + ( b_arrow ? 10 : 0 ) + 2 );
446 s.setHeight( s.height() + 2 * PADDING );
453 QSplitterHandle *PlaylistSplitter::createHandle()
455 return new SplitterHandle( orientation(), this );
458 SplitterHandle::SplitterHandle( Qt::Orientation orientation, QSplitter * parent )
459 : QSplitterHandle( orientation, parent)
463 QSize SplitterHandle::sizeHint() const
465 return (orientation() == Qt::Horizontal) ? QSize( 1, height() ) : QSize( width(), 1 );
468 void SplitterHandle::paintEvent(QPaintEvent *event)
470 QPainter painter( this );
471 painter.fillRect( event->rect(), QColor(81, 81, 81) );
473 #endif /* __APPLE__ */