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 &) );
160 /* Connect the activation of the selector to a redefining of the PL */
161 DCONNECT( selector, categoryActivated( playlist_item_t *, bool ),
162 mainView, setRoot( playlist_item_t *, bool ) );
163 mainView->setRoot( p_root, false );
166 split = new PlaylistSplitter( this );
168 /* Add the two sides of the QSplitter */
169 split->addWidget( leftSplitter );
170 split->addWidget( mainView );
173 sizeList << 180 << 420 ;
174 split->setSizes( sizeList );
175 split->setStretchFactor( 0, 0 );
176 split->setStretchFactor( 1, 3 );
177 split->setCollapsible( 1, false );
178 leftSplitter->setMaximumWidth( 250 );
180 /* In case we want to keep the splitter information */
181 // components shall never write there setting to a fixed location, may infer
182 // with other uses of the same component...
183 getSettings()->beginGroup("Playlist");
184 split->restoreState( getSettings()->value("splitterSizes").toByteArray());
185 leftSplitter->restoreState( getSettings()->value("leftSplitterGeometry").toByteArray() );
186 getSettings()->endGroup();
188 layout->addWidget( split, 1, 0, 1, -1 );
190 setAcceptDrops( true );
191 setWindowTitle( qtr( "Playlist" ) );
192 setWindowRole( "vlc-playlist" );
193 setWindowIcon( QApplication::windowIcon() );
196 PlaylistWidget::~PlaylistWidget()
198 getSettings()->beginGroup("Playlist");
199 getSettings()->setValue( "splitterSizes", split->saveState() );
200 getSettings()->setValue( "leftSplitterGeometry", leftSplitter->saveState() );
201 getSettings()->endGroup();
202 msg_Dbg( p_intf, "Playlist Destroyed" );
205 void PlaylistWidget::dropEvent( QDropEvent *event )
207 if( p_intf->p_sys->p_mi )
208 p_intf->p_sys->p_mi->dropEventPlay( event, false );
210 void PlaylistWidget::dragEnterEvent( QDragEnterEvent *event )
212 event->acceptProposedAction();
215 void PlaylistWidget::closeEvent( QCloseEvent *event )
217 if( THEDP->isDying() )
219 p_intf->p_sys->p_mi->playlistVisible = true;
224 p_intf->p_sys->p_mi->playlistVisible = false;
230 void PlaylistWidget::forceHide()
232 leftSplitter->hide();
237 void PlaylistWidget::forceShow()
239 leftSplitter->show();
244 void PlaylistWidget::changeView( const QModelIndex& index )
247 locationBar->setIndex( index );
248 int i = mainView->currentViewIndex();
249 viewActions[i]->setChecked(true);
252 #include <QSignalMapper>
255 LocationBar::LocationBar( PLModel *m )
258 mapper = new QSignalMapper( this );
259 CONNECT( mapper, mapped( int ), this, invoke( int ) );
261 btnMore = new LocationButton( "...", false, true, this );
262 menuMore = new QMenu( this );
263 btnMore->setMenu( menuMore );
266 void LocationBar::setIndex( const QModelIndex &index )
268 qDeleteAll( buttons );
270 qDeleteAll( actions );
273 QModelIndex i = index;
278 PLItem *item = model->getItem( i );
281 char *fb_name = input_item_GetTitle( item->inputItem() );
282 if( EMPTY_STR( fb_name ) )
285 fb_name = input_item_GetName( item->inputItem() );
290 QAbstractButton *btn = new LocationButton( text, first, !first, this );
291 btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
292 buttons.append( btn );
294 QAction *action = new QAction( text, this );
295 actions.append( action );
296 CONNECT( btn, clicked(), action, trigger() );
298 mapper->setMapping( action, item->id() );
299 CONNECT( action, triggered(), mapper, map() );
303 if( i.isValid() ) i = i.parent();
308 for( int a = actions.count() - 1; a >= 0 ; a-- )
310 actions[a]->setText( prefix + actions[a]->text() );
311 prefix += QString(" ");
314 if( isVisible() ) layOut( size() );
317 void LocationBar::setRootIndex()
319 setIndex( QModelIndex() );
322 void LocationBar::invoke( int i_id )
324 QModelIndex index = model->index( i_id, 0 );
325 emit invoked ( index );
328 void LocationBar::layOut( const QSize& size )
333 int count = buttons.count();
335 for( int i = 0; i < count; i++ )
337 int w = buttons[i]->sizeHint().width();
340 if( totalWidth > size.width() ) break;
344 int shown = widths.count();
346 if( totalWidth > size.width() && count > 1 )
348 QSize sz = btnMore->sizeHint();
349 btnMore->setGeometry( 0, 0, sz.width(), size.height() );
358 for( int i = count - 1; i >= 0; i-- )
360 if( totalWidth <= size.width() || i == 0)
362 buttons[i]->setGeometry( x, 0, qMin( size.width() - x, widths[i] ), size.height() );
365 totalWidth -= widths[i];
369 menuMore->addAction( actions[i] );
371 if( i < shown ) totalWidth -= widths[i];
376 void LocationBar::resizeEvent ( QResizeEvent * event )
378 layOut( event->size() );
381 QSize LocationBar::sizeHint() const
383 return btnMore->sizeHint();
386 LocationButton::LocationButton( const QString &text, bool bold,
387 bool arrow, QWidget * parent )
388 : QPushButton( parent ), b_arrow( arrow )
391 font.setBold( bold );
398 void LocationButton::paintEvent ( QPaintEvent * )
400 QStyleOptionButton option;
401 option.initFrom( this );
402 option.state |= QStyle::State_Enabled;
408 p.setRenderHint( QPainter::Antialiasing, true );
409 QColor c = palette().color( QPalette::Highlight );
411 p.setBrush( c.lighter( 150 ) );
413 p.drawRoundedRect( option.rect.adjusted( 0, 2, 0, -2 ), 5, 5 );
417 QRect r = option.rect.adjusted( PADDING, 0, -PADDING - (b_arrow ? 10 : 0), 0 );
419 QString str( text() );
420 /* This check is absurd, but either it is not done properly inside elidedText(),
421 or boundingRect() is wrong */
422 if( r.width() < fontMetrics().boundingRect( text() ).width() )
423 str = fontMetrics().elidedText( text(), Qt::ElideRight, r.width() );
424 p.drawText( r, Qt::AlignVCenter | Qt::AlignLeft, str );
428 option.rect.setWidth( 10 );
429 option.rect.moveRight( rect().right() );
430 style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
434 QSize LocationButton::sizeHint() const
436 QSize s( fontMetrics().boundingRect( text() ).size() );
437 /* Add two pixels to width: font metrics are buggy, if you pass text through elidation
438 with exactly the width of its bounding rect, sometimes it still elides */
439 s.setWidth( s.width() + ( 2 * PADDING ) + ( b_arrow ? 10 : 0 ) + 2 );
440 s.setHeight( s.height() + 2 * PADDING );
447 QSplitterHandle *PlaylistSplitter::createHandle()
449 return new SplitterHandle( orientation(), this );
452 SplitterHandle::SplitterHandle( Qt::Orientation orientation, QSplitter * parent )
453 : QSplitterHandle( orientation, parent)
457 QSize SplitterHandle::sizeHint() const
459 return (orientation() == Qt::Horizontal) ? QSize( 1, height() ) : QSize( width(), 1 );
462 void SplitterHandle::paintEvent(QPaintEvent *event)
464 QPainter painter( this );
465 painter.fillRect( event->rect(), QColor(81, 81, 81) );
467 #endif /* __APPLE__ */