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/interface_widgets.hpp" /* CoverArtLabel */
35 #include "input_manager.hpp" /* art signal */
36 #include "main_interface.hpp" /* DropEvent TODO remove this*/
39 #include <QSignalMapper>
41 /**********************************************************************
42 * Playlist Widget. The embedded playlist
43 **********************************************************************/
45 PlaylistWidget::PlaylistWidget( intf_thread_t *_p_i, QWidget *_par )
46 : QSplitter( _par ), p_intf ( _p_i )
49 setContentsMargins( 3, 3, 3, 3 );
51 setContentsMargins( 0, 3, 0, 3 );
57 /* We use a QSplitter for the left part */
58 leftSplitter = new QSplitter( Qt::Vertical, this );
61 PLSelector *selector = new PLSelector( this, p_intf );
62 leftSplitter->addWidget( selector);
64 /* Create a Container for the Art Label
65 in order to have a beautiful resizing for the selector above it */
66 QWidget *artContainer = new QWidget;
67 QHBoxLayout *artContLay = new QHBoxLayout( artContainer );
68 artContLay->setMargin( 0 );
69 artContLay->setSpacing( 0 );
70 artContainer->setMaximumHeight( 128 );
73 CoverArtLabel *art = new CoverArtLabel( artContainer, p_intf );
74 art->setToolTip( qtr( "Double click to get media information" ) );
75 artContLay->addWidget( art, 1 );
77 CONNECT( THEMIM->getIM(), artChanged( QString ),
78 art, showArtUpdate( const QString& ) );
80 leftSplitter->addWidget( artContainer );
85 /* Initialisation of the playlist */
86 playlist_t * p_playlist = THEPL;
88 playlist_item_t *p_root = THEPL->p_playing;
91 QWidget *rightPanel = new QWidget( this );
92 QGridLayout *layout = new QGridLayout( rightPanel );
93 layout->setSpacing( 0 ); layout->setMargin( 0 );
94 setMinimumWidth( 300 );
96 PLModel *model = new PLModel( p_playlist, p_intf, p_root, this );
97 mainView = new StandardPLPanel( this, p_intf, p_root, selector, model );
100 locationBar = new LocationBar( model );
101 locationBar->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Preferred );
102 layout->addWidget( locationBar, 0, 0 );
103 layout->setColumnStretch( 0, 5 );
104 CONNECT( locationBar, invoked( const QModelIndex & ),
105 mainView, browseInto( const QModelIndex & ) );
107 /* Button to switch views */
108 QToolButton *viewButton = new QToolButton( this );
109 viewButton->setIcon( style()->standardIcon( QStyle::SP_FileDialogDetailedView ) );
110 viewButton->setToolTip( qtr("Change playlistview") );
111 layout->addWidget( viewButton, 0, 1 );
113 /* View selection menu */
114 QSignalMapper *viewSelectionMapper = new QSignalMapper( this );
115 CONNECT( viewSelectionMapper, mapped( int ), mainView, showView( int ) );
117 QActionGroup *actionGroup = new QActionGroup( this );
119 for( int i = 0; i < StandardPLPanel::VIEW_COUNT; i++ )
121 viewActions[i] = actionGroup->addAction( viewNames[i] );
122 viewActions[i]->setCheckable( true );
123 viewSelectionMapper->setMapping( viewActions[i], i );
124 CONNECT( viewActions[i], triggered(), viewSelectionMapper, map() );
126 viewActions[0]->setChecked( true );
128 QMenu *viewMenu = new QMenu( viewButton );
129 viewMenu->addActions( actionGroup->actions() );
130 viewButton->setMenu( viewMenu );
131 CONNECT( viewButton, clicked(), mainView, cycleViews() );
134 searchEdit = new SearchLineEdit( this );
135 searchEdit->setMaximumWidth( 250 );
136 searchEdit->setMinimumWidth( 80 );
137 layout->addWidget( searchEdit, 0, 2 );
138 CONNECT( searchEdit, textEdited( const QString& ),
139 mainView, search( const QString& ) );
140 CONNECT( searchEdit, searchDelayedChanged( const QString& ),
141 mainView, searchDelayed( const QString & ) );
142 CONNECT( mainView, viewChanged( const QModelIndex& ),
143 this, changeView( const QModelIndex &) );
144 layout->setColumnStretch( 2, 3 );
146 /* Connect the activation of the selector to a redefining of the PL */
147 DCONNECT( selector, activated( playlist_item_t * ),
148 mainView, setRoot( playlist_item_t * ) );
150 layout->addWidget( mainView, 1, 0, 1, -1 );
152 /* Add the two sides of the QSplitter */
153 addWidget( leftSplitter );
154 addWidget( rightPanel );
157 sizeList << 180 << 420 ;
158 setSizes( sizeList );
159 //setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Expanding );
160 setStretchFactor( 0, 0 );
161 setStretchFactor( 1, 3 );
162 leftSplitter->setMaximumWidth( 250 );
163 setCollapsible( 1, false );
165 /* In case we want to keep the splitter information */
166 // components shall never write there setting to a fixed location, may infer
167 // with other uses of the same component...
168 getSettings()->beginGroup("Playlist");
169 restoreState( getSettings()->value("splitterSizes").toByteArray());
170 leftSplitter->restoreState( getSettings()->value("leftSplitterGeometry").toByteArray() );
171 getSettings()->endGroup();
173 setAcceptDrops( true );
174 setWindowTitle( qtr( "Playlist" ) );
175 setWindowRole( "vlc-playlist" );
176 setWindowIcon( QApplication::windowIcon() );
179 PlaylistWidget::~PlaylistWidget()
181 getSettings()->beginGroup("Playlist");
182 getSettings()->setValue( "splitterSizes", saveState() );
183 getSettings()->setValue( "leftSplitterGeometry", leftSplitter->saveState() );
184 getSettings()->endGroup();
185 msg_Dbg( p_intf, "Playlist Destroyed" );
188 void PlaylistWidget::dropEvent( QDropEvent *event )
190 if( p_intf->p_sys->p_mi )
191 p_intf->p_sys->p_mi->dropEventPlay( event, false );
193 void PlaylistWidget::dragEnterEvent( QDragEnterEvent *event )
195 event->acceptProposedAction();
198 void PlaylistWidget::closeEvent( QCloseEvent *event )
200 if( THEDP->isDying() )
202 p_intf->p_sys->p_mi->playlistVisible = true;
207 p_intf->p_sys->p_mi->playlistVisible = false;
213 void PlaylistWidget::forceHide()
215 leftSplitter->hide();
220 void PlaylistWidget::forceShow()
222 leftSplitter->show();
227 void PlaylistWidget::changeView( const QModelIndex& index )
230 locationBar->setIndex( index );
231 int i = mainView->getViewNumber();
232 viewActions[i]->setChecked(true);
235 #include <QSignalMapper>
238 LocationBar::LocationBar( PLModel *m )
241 mapper = new QSignalMapper( this );
242 CONNECT( mapper, mapped( int ), this, invoke( int ) );
244 btnMore = new LocationButton( "...", false, true, this );
245 menuMore = new QMenu( this );
246 btnMore->setMenu( menuMore );
249 void LocationBar::setIndex( const QModelIndex &index )
251 qDeleteAll( buttons );
253 qDeleteAll( actions );
256 QModelIndex i = index;
261 PLItem *item = model->getItem( i );
263 char *fb_name = input_item_GetTitleFbName( item->inputItem() );
264 QString text = qfu(fb_name);
267 QAbstractButton *btn = new LocationButton( text, first, !first, this );
268 btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
269 buttons.append( btn );
271 QAction *action = new QAction( text, this );
272 actions.append( action );
273 CONNECT( btn, clicked(), action, trigger() );
275 mapper->setMapping( action, item->id() );
276 CONNECT( action, triggered(), mapper, map() );
280 if( i.isValid() ) i = i.parent();
285 for( int a = actions.count() - 1; a >= 0 ; a-- )
287 actions[a]->setText( prefix + actions[a]->text() );
288 prefix += QString(" ");
291 if( isVisible() ) layOut( size() );
294 void LocationBar::setRootIndex()
296 setIndex( QModelIndex() );
299 void LocationBar::invoke( int i_id )
301 QModelIndex index = model->index( i_id, 0 );
302 emit invoked ( index );
305 void LocationBar::layOut( const QSize& size )
310 int count = buttons.count();
312 for( int i = 0; i < count; i++ )
314 int w = buttons[i]->sizeHint().width();
317 if( totalWidth > size.width() ) break;
321 int shown = widths.count();
323 if( totalWidth > size.width() && count > 1 )
325 QSize sz = btnMore->sizeHint();
326 btnMore->setGeometry( 0, 0, sz.width(), size.height() );
335 for( int i = count - 1; i >= 0; i-- )
337 if( totalWidth <= size.width() || i == 0)
339 buttons[i]->setGeometry( x, 0, qMin( size.width() - x, widths[i] ), size.height() );
342 totalWidth -= widths[i];
346 menuMore->addAction( actions[i] );
348 if( i < shown ) totalWidth -= widths[i];
353 void LocationBar::resizeEvent ( QResizeEvent * event )
355 layOut( event->size() );
358 QSize LocationBar::sizeHint() const
360 return btnMore->sizeHint();
363 LocationButton::LocationButton( const QString &text, bool bold,
364 bool arrow, QWidget * parent )
365 : b_arrow( arrow ), QPushButton( parent )
368 font.setBold( bold );
375 void LocationButton::paintEvent ( QPaintEvent * event )
377 QStyleOptionButton option;
378 option.initFrom( this );
379 option.state |= QStyle::State_Enabled;
385 p.setRenderHint( QPainter::Antialiasing, true );
386 QColor c = palette().color( QPalette::Highlight );
388 p.setBrush( c.lighter( 150 ) );
390 p.drawRoundedRect( option.rect.adjusted( 0, 2, 0, -2 ), 5, 5 );
394 QRect r = option.rect.adjusted( PADDING, 0, -PADDING - (b_arrow ? 10 : 0), 0 );
396 QString str( text() );
397 /* This check is absurd, but either it is not done properly inside elidedText(),
398 or boundingRect() is wrong */
399 if( r.width() < fontMetrics().boundingRect( text() ).width() )
400 str = fontMetrics().elidedText( text(), Qt::ElideRight, r.width() );
401 p.drawText( r, Qt::AlignVCenter | Qt::AlignLeft, str );
405 option.rect.setWidth( 10 );
406 option.rect.moveRight( rect().right() );
407 style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
411 QSize LocationButton::sizeHint() const
413 QSize s( fontMetrics().boundingRect( text() ).size() );
414 /* Add two pixels to width: font metrics are buggy, if you pass text through elidation
415 with exactly the width of its bounding rect, sometimes it still elides */
416 s.setWidth( s.width() + ( 2 * PADDING ) + ( b_arrow ? 10 : 0 ) + 2 );
417 s.setHeight( s.height() + 2 * PADDING );
424 QSplitterHandle *PlaylistWidget::createHandle()
426 return new SplitterHandle( orientation(), this );
429 SplitterHandle::SplitterHandle( Qt::Orientation orientation, QSplitter * parent )
430 : QSplitterHandle( orientation, parent)
434 QSize SplitterHandle::sizeHint() const
436 return (orientation() == Qt::Horizontal) ? QSize( 1, height() ) : QSize( width(), 1 );
439 void SplitterHandle::paintEvent(QPaintEvent *event)
441 QPainter painter( this );
442 painter.fillRect( event->rect(), QColor(81, 81, 81) );
444 #endif /* __APPLE__ */