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/standardpanel.hpp"
30 #include "components/playlist/selector.hpp"
31 #include "components/playlist/playlist.hpp"
32 #include "components/playlist/playlist_model.hpp"
34 #include "input_manager.hpp" /* art signal */
35 #include "main_interface.hpp" /* DropEvent TODO remove this*/
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 )
48 setContentsMargins( 3, 3, 3, 3 );
53 /* We use a QSplitter for the left part */
54 leftSplitter = new QSplitter( Qt::Vertical, this );
57 selector = new PLSelector( this, p_intf );
58 leftSplitter->addWidget( selector);
60 /* Create a Container for the Art Label
61 in order to have a beautiful resizing for the selector above it */
62 QWidget *artContainer = new QWidget;
63 QHBoxLayout *artContLay = new QHBoxLayout( artContainer );
64 artContLay->setMargin( 0 );
65 artContLay->setSpacing( 0 );
66 artContainer->setMaximumHeight( 128 );
69 art = new ArtLabel( artContainer, p_intf );
70 art->setToolTip( qtr( "Double click to get media information" ) );
71 artContLay->addWidget( art, 1 );
73 CONNECT( THEMIM->getIM(), artChanged( QString ),
74 art, showArtUpdate( const QString& ) );
76 leftSplitter->addWidget( artContainer );
81 /* Initialisation of the playlist */
82 playlist_t * p_playlist = THEPL;
84 playlist_item_t *p_root = THEPL->p_playing;
87 QWidget *rightPanel = new QWidget( this );
88 QGridLayout *layout = new QGridLayout( rightPanel );
89 layout->setSpacing( 0 ); layout->setMargin( 0 );
90 setMinimumWidth( 300 );
92 PLModel *model = new PLModel( p_playlist, p_intf, p_root, this );
93 mainView = new StandardPLPanel( this, p_intf, THEPL, p_root, selector, model );
96 locationBar = new LocationBar( model );
97 locationBar->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Preferred );
98 layout->addWidget( locationBar, 0, 0 );
99 layout->setColumnStretch( 0, 5 );
100 CONNECT( locationBar, invoked( const QModelIndex & ),
101 mainView, browseInto( const QModelIndex & ) );
103 /* Button to switch views */
104 QToolButton *viewButton = new QToolButton( this );
105 viewButton->setIcon( style()->standardIcon( QStyle::SP_FileDialogDetailedView ) );
106 viewButton->setToolTip( qtr("Change playlistview") );
107 layout->addWidget( viewButton, 0, 1 );
109 /* View selection menu */
110 viewSelectionMapper = new QSignalMapper( this );
111 CONNECT( viewSelectionMapper, mapped( int ), mainView, showView( int ) );
113 QActionGroup *actionGroup = new QActionGroup( this );
114 for( int i = 0; i < StandardPLPanel::VIEW_COUNT; i++ )
116 viewActions[i] = actionGroup->addAction( viewNames[i] );
117 viewActions[i]->setCheckable( true );
118 viewSelectionMapper->setMapping( viewActions[i], i );
119 CONNECT( viewActions[i], triggered(), viewSelectionMapper, map() );
122 CONNECT( viewButton, clicked(), mainView, cycleViews() );
123 QMenu *viewMenu = new QMenu( this );
124 viewMenu->addActions( actionGroup->actions() );
125 viewButton->setMenu( viewMenu );
128 searchEdit = new SearchLineEdit( this );
129 searchEdit->setMaximumWidth( 250 );
130 searchEdit->setMinimumWidth( 80 );
131 layout->addWidget( searchEdit, 0, 2 );
132 CONNECT( searchEdit, textEdited( const QString& ),
133 mainView, search( const QString& ) );
134 CONNECT( searchEdit, searchDelayedChanged( const QString& ),
135 mainView, searchDelayed( const QString & ) );
136 CONNECT( mainView, viewChanged( const QModelIndex& ),
137 this, changeView( const QModelIndex &) );
138 layout->setColumnStretch( 2, 3 );
140 /* Connect the activation of the selector to a redefining of the PL */
141 DCONNECT( selector, activated( playlist_item_t * ),
142 mainView, setRoot( playlist_item_t * ) );
144 mainView->setRoot( p_root );
145 layout->addWidget( mainView, 1, 0, 1, -1 );
147 /* Add the two sides of the QSplitter */
148 addWidget( leftSplitter );
149 addWidget( rightPanel );
152 sizeList << 180 << 420 ;
153 setSizes( sizeList );
154 //setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Expanding );
155 setStretchFactor( 0, 0 );
156 setStretchFactor( 1, 3 );
157 leftSplitter->setMaximumWidth( 250 );
158 setCollapsible( 1, false );
160 /* In case we want to keep the splitter information */
161 // components shall never write there setting to a fixed location, may infer
162 // with other uses of the same component...
163 getSettings()->beginGroup("Playlist");
164 restoreState( getSettings()->value("splitterSizes").toByteArray());
165 leftSplitter->restoreState( getSettings()->value("leftSplitterGeometry").toByteArray() );
166 getSettings()->endGroup();
168 setAcceptDrops( true );
169 setWindowTitle( qtr( "Playlist" ) );
170 setWindowRole( "vlc-playlist" );
171 setWindowIcon( QApplication::windowIcon() );
174 PlaylistWidget::~PlaylistWidget()
176 getSettings()->beginGroup("Playlist");
177 getSettings()->setValue( "splitterSizes", saveState() );
178 getSettings()->setValue( "leftSplitterGeometry", leftSplitter->saveState() );
179 getSettings()->endGroup();
180 msg_Dbg( p_intf, "Playlist Destroyed" );
183 void PlaylistWidget::dropEvent( QDropEvent *event )
185 if( p_intf->p_sys->p_mi )
186 p_intf->p_sys->p_mi->dropEventPlay( event, false );
188 void PlaylistWidget::dragEnterEvent( QDragEnterEvent *event )
190 event->acceptProposedAction();
193 void PlaylistWidget::closeEvent( QCloseEvent *event )
195 if( THEDP->isDying() )
197 p_intf->p_sys->p_mi->playlistVisible = true;
202 p_intf->p_sys->p_mi->playlistVisible = false;
208 void PlaylistWidget::forceHide()
210 leftSplitter->hide();
215 void PlaylistWidget::forceShow()
217 leftSplitter->show();
222 void PlaylistWidget::changeView( const QModelIndex& index )
225 locationBar->setIndex( index );
229 #include <QSignalMapper>
232 LocationBar::LocationBar( PLModel *m )
235 mapper = new QSignalMapper( this );
236 CONNECT( mapper, mapped( int ), this, invoke( int ) );
238 btnMore = new LocationButton( "...", false, true, this );
239 menuMore = new QMenu( this );
240 btnMore->setMenu( menuMore );
243 void LocationBar::setIndex( const QModelIndex &index )
245 qDeleteAll( buttons );
247 qDeleteAll( actions );
250 QModelIndex i = index;
255 PLItem *item = model->getItem( i );
257 char *fb_name = input_item_GetTitleFbName( item->inputItem() );
258 QString text = qfu(fb_name);
261 QAbstractButton *btn = new LocationButton( text, first, !first, this );
262 btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
263 buttons.append( btn );
265 QAction *action = new QAction( text, this );
266 actions.append( action );
267 CONNECT( btn, clicked(), action, trigger() );
269 mapper->setMapping( action, item->id() );
270 CONNECT( action, triggered(), mapper, map() );
274 if( i.isValid() ) i = i.parent();
279 for( int a = actions.count() - 1; a >= 0 ; a-- )
281 actions[a]->setText( prefix + actions[a]->text() );
282 prefix += QString(" ");
285 if( isVisible() ) layOut( size() );
288 void LocationBar::setRootIndex()
290 setIndex( QModelIndex() );
293 void LocationBar::invoke( int i_id )
295 QModelIndex index = model->index( i_id, 0 );
296 emit invoked ( index );
299 void LocationBar::layOut( const QSize& size )
304 int count = buttons.count();
306 for( int i = 0; i < count; i++ )
308 int w = buttons[i]->sizeHint().width();
311 if( totalWidth > size.width() ) break;
315 int shown = widths.count();
317 if( totalWidth > size.width() && count > 1 )
319 QSize sz = btnMore->sizeHint();
320 btnMore->setGeometry( 0, 0, sz.width(), size.height() );
329 for( int i = count - 1; i >= 0; i-- )
331 if( totalWidth <= size.width() || i == 0)
333 buttons[i]->setGeometry( x, 0, qMin( size.width() - x, widths[i] ), size.height() );
336 totalWidth -= widths[i];
340 menuMore->addAction( actions[i] );
342 if( i < shown ) totalWidth -= widths[i];
347 void LocationBar::resizeEvent ( QResizeEvent * event )
349 layOut( event->size() );
352 QSize LocationBar::sizeHint() const
354 return btnMore->sizeHint();
357 LocationButton::LocationButton( const QString &text, bool bold,
358 bool arrow, QWidget * parent )
359 : b_arrow( arrow ), QPushButton( parent )
362 font.setBold( bold );
369 void LocationButton::paintEvent ( QPaintEvent * event )
371 QStyleOptionButton option;
372 option.initFrom( this );
373 option.state |= QStyle::State_Enabled;
379 p.setRenderHint( QPainter::Antialiasing, true );
380 QColor c = palette().color( QPalette::Highlight );
382 p.setBrush( c.lighter( 150 ) );
384 p.drawRoundedRect( option.rect.adjusted( 0, 2, 0, -2 ), 5, 5 );
388 QRect r = option.rect.adjusted( PADDING, 0, -PADDING - (b_arrow ? 10 : 0), 0 );
390 QString str( text() );
391 /* This check is absurd, but either it is not done properly inside elidedText(),
392 or boundingRect() is wrong */
393 if( r.width() < fontMetrics().boundingRect( text() ).width() )
394 str = fontMetrics().elidedText( text(), Qt::ElideRight, r.width() );
395 p.drawText( r, Qt::AlignVCenter | Qt::AlignLeft, str );
399 option.rect.setWidth( 10 );
400 option.rect.moveRight( rect().right() );
401 style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
405 QSize LocationButton::sizeHint() const
407 QSize s( fontMetrics().boundingRect( text() ).size() );
408 /* Add two pixels to width: font metrics are buggy, if you pass text through elidation
409 with exactly the width of its bounding rect, sometimes it still elides */
410 s.setWidth( s.width() + ( 2 * PADDING ) + ( b_arrow ? 10 : 0 ) + 2 );
411 s.setHeight( s.height() + 2 * PADDING );