Qt4: Make playlist window drag&drop to current selected PLSelItem
[vlc.git] / modules / gui / qt4 / components / playlist / playlist.cpp
blob32b0ea92548815556cd7d45892ae2b3062ce3d3d
1 /*****************************************************************************
2 * playlist.cpp : Custom widgets for the playlist
3 ****************************************************************************
4 * Copyright © 2007-2010 the VideoLAN team
5 * $Id$
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 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
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*/
41 #include <QMenu>
42 #include <QSignalMapper>
43 #include <QSlider>
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 );
59 /*******************
60 * Left *
61 *******************/
62 /* We use a QSplitter for the left part */
63 leftSplitter = new QSplitter( Qt::Vertical, this );
65 /* Source Selector */
66 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 );
74 /* Art label */
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 );
84 /*******************
85 * Right *
86 *******************/
87 /* Initialisation of the playlist */
88 playlist_t * p_playlist = THEPL;
89 PL_LOCK;
90 playlist_item_t *p_root = p_playlist->p_playing;
91 PL_UNLOCK;
93 setMinimumWidth( 400 );
95 PLModel *model = PLModel::getPLModel( p_intf );
96 #ifdef MEDIA_LIBRARY
97 MLModel *mlmodel = new MLModel( p_intf, this );
98 mainView = new StandardPLPanel( this, p_intf, p_root, selector, model, mlmodel );
99 #else
100 mainView = new StandardPLPanel( this, p_intf, p_root, selector, model, NULL );
101 #endif
103 /* Location Bar */
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 clear playlist */
116 QToolButton *clearPlaylistButton = new QToolButton( this );
117 clearPlaylistButton->setIcon( style()->standardIcon( QStyle::SP_TrashIcon ) );
118 clearPlaylistButton->setToolTip( qtr("Clear playlist") );
119 topbarLayout->addWidget( clearPlaylistButton );
120 CONNECT( clearPlaylistButton, clicked(), this, clearPlaylist() );
122 /* Button to switch views */
123 QToolButton *viewButton = new QToolButton( this );
124 viewButton->setIcon( style()->standardIcon( QStyle::SP_FileDialogDetailedView ) );
125 viewButton->setToolTip( qtr("Change playlistview") );
126 topbarLayout->addWidget( viewButton );
128 /* View selection menu */
129 QSignalMapper *viewSelectionMapper = new QSignalMapper( this );
130 CONNECT( viewSelectionMapper, mapped( int ), mainView, showView( int ) );
132 QActionGroup *actionGroup = new QActionGroup( this );
134 #ifndef NDEBUG
135 # define MAX_VIEW StandardPLPanel::VIEW_COUNT
136 #else
137 # define MAX_VIEW StandardPLPanel::VIEW_COUNT - 1
138 #endif
139 for( int i = 0; i < MAX_VIEW; i++ )
141 viewActions[i] = actionGroup->addAction( viewNames[i] );
142 viewActions[i]->setCheckable( true );
143 viewSelectionMapper->setMapping( viewActions[i], i );
144 CONNECT( viewActions[i], triggered(), viewSelectionMapper, map() );
146 viewActions[0]->setChecked( true );
148 QMenu *viewMenu = new QMenu( viewButton );
149 viewMenu->addActions( actionGroup->actions() );
150 viewButton->setMenu( viewMenu );
151 CONNECT( viewButton, clicked(), mainView, cycleViews() );
153 /* Search */
154 searchEdit = new SearchLineEdit( this );
155 searchEdit->setMaximumWidth( 250 );
156 searchEdit->setMinimumWidth( 80 );
157 searchEdit->setToolTip( qtr("Search the playlist") );
158 topbarLayout->addWidget( searchEdit );
159 CONNECT( searchEdit, textChanged( const QString& ),
160 mainView, search( const QString& ) );
161 CONNECT( searchEdit, searchDelayedChanged( const QString& ),
162 mainView, searchDelayed( const QString & ) );
164 CONNECT( mainView, viewChanged( const QModelIndex& ),
165 this, changeView( const QModelIndex &) );
167 /* Connect the activation of the selector to a redefining of the PL */
168 DCONNECT( selector, categoryActivated( playlist_item_t *, bool ),
169 mainView, setRootItem( playlist_item_t *, bool ) );
170 mainView->setRootItem( p_root, false );
172 /* */
173 split = new PlaylistSplitter( this );
175 /* Add the two sides of the QSplitter */
176 split->addWidget( leftSplitter );
177 split->addWidget( mainView );
179 QList<int> sizeList;
180 sizeList << 180 << 420 ;
181 split->setSizes( sizeList );
182 split->setStretchFactor( 0, 0 );
183 split->setStretchFactor( 1, 3 );
184 split->setCollapsible( 1, false );
185 leftSplitter->setMaximumWidth( 250 );
187 /* In case we want to keep the splitter information */
188 // components shall never write there setting to a fixed location, may infer
189 // with other uses of the same component...
190 getSettings()->beginGroup("Playlist");
191 split->restoreState( getSettings()->value("splitterSizes").toByteArray());
192 leftSplitter->restoreState( getSettings()->value("leftSplitterGeometry").toByteArray() );
193 getSettings()->endGroup();
195 layout->addWidget( split, 1, 0, 1, -1 );
197 setAcceptDrops( true );
198 setWindowTitle( qtr( "Playlist" ) );
199 setWindowRole( "vlc-playlist" );
200 setWindowIcon( QApplication::windowIcon() );
203 PlaylistWidget::~PlaylistWidget()
205 getSettings()->beginGroup("Playlist");
206 getSettings()->setValue( "splitterSizes", split->saveState() );
207 getSettings()->setValue( "leftSplitterGeometry", leftSplitter->saveState() );
208 getSettings()->endGroup();
209 msg_Dbg( p_intf, "Playlist Destroyed" );
212 void PlaylistWidget::dropEvent( QDropEvent *event )
214 if( !( selector->getCurrentItemCategory() == IS_PL ||
215 selector->getCurrentItemCategory() == IS_ML ) ) return;
217 if( p_intf->p_sys->p_mi )
218 p_intf->p_sys->p_mi->dropEventPlay( event, false,
219 (selector->getCurrentItemCategory() == IS_PL) );
221 void PlaylistWidget::dragEnterEvent( QDragEnterEvent *event )
223 event->acceptProposedAction();
226 void PlaylistWidget::closeEvent( QCloseEvent *event )
228 if( THEDP->isDying() )
230 p_intf->p_sys->p_mi->playlistVisible = true;
231 event->accept();
233 else
235 p_intf->p_sys->p_mi->playlistVisible = false;
236 hide();
237 event->ignore();
241 void PlaylistWidget::forceHide()
243 leftSplitter->hide();
244 mainView->hide();
245 updateGeometry();
248 void PlaylistWidget::forceShow()
250 leftSplitter->show();
251 mainView->show();
252 updateGeometry();
255 void PlaylistWidget::changeView( const QModelIndex& index )
257 searchEdit->clear();
258 locationBar->setIndex( index );
259 int i = mainView->currentViewIndex();
260 viewActions[i]->setChecked(true);
263 void PlaylistWidget::clearPlaylist()
265 PLModel *model = PLModel::getPLModel( p_intf );
266 if( model->rowCount() < 1 ) return;
268 QModelIndexList* l = new QModelIndexList();
269 for( int i = 0; i < model->rowCount(); i++)
271 QModelIndex indexrecord = model->index( i, 0, QModelIndex() );
272 l->append( indexrecord );
274 model->doDelete(*l);
276 #include <QSignalMapper>
277 #include <QMenu>
278 #include <QPainter>
279 LocationBar::LocationBar( PLModel *m )
281 model = m;
282 mapper = new QSignalMapper( this );
283 CONNECT( mapper, mapped( int ), this, invoke( int ) );
285 btnMore = new LocationButton( "...", false, true, this );
286 menuMore = new QMenu( this );
287 btnMore->setMenu( menuMore );
290 void LocationBar::setIndex( const QModelIndex &index )
292 qDeleteAll( buttons );
293 buttons.clear();
294 qDeleteAll( actions );
295 actions.clear();
297 QModelIndex i = index;
298 bool first = true;
300 while( true )
302 PLItem *item = model->getItem( i );
303 QString text;
305 char *fb_name = input_item_GetTitle( item->inputItem() );
306 if( EMPTY_STR( fb_name ) )
308 free( fb_name );
309 fb_name = input_item_GetName( item->inputItem() );
311 text = qfu(fb_name);
312 free(fb_name);
314 QAbstractButton *btn = new LocationButton( text, first, !first, this );
315 btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
316 buttons.append( btn );
318 QAction *action = new QAction( text, this );
319 actions.append( action );
320 CONNECT( btn, clicked(), action, trigger() );
322 mapper->setMapping( action, item->id() );
323 CONNECT( action, triggered(), mapper, map() );
325 first = false;
327 if( i.isValid() ) i = i.parent();
328 else break;
331 QString prefix;
332 for( int a = actions.count() - 1; a >= 0 ; a-- )
334 actions[a]->setText( prefix + actions[a]->text() );
335 prefix += QString(" ");
338 if( isVisible() ) layOut( size() );
341 void LocationBar::setRootIndex()
343 setIndex( QModelIndex() );
346 void LocationBar::invoke( int i_id )
348 QModelIndex index = model->index( i_id, 0 );
349 emit invoked ( index );
352 void LocationBar::layOut( const QSize& size )
354 menuMore->clear();
355 widths.clear();
357 int count = buttons.count();
358 int totalWidth = 0;
359 for( int i = 0; i < count; i++ )
361 int w = buttons[i]->sizeHint().width();
362 widths.append( w );
363 totalWidth += w;
364 if( totalWidth > size.width() ) break;
367 int x = 0;
368 int shown = widths.count();
370 if( totalWidth > size.width() && count > 1 )
372 QSize sz = btnMore->sizeHint();
373 btnMore->setGeometry( 0, 0, sz.width(), size.height() );
374 btnMore->show();
375 x = sz.width();
376 totalWidth += x;
378 else
380 btnMore->hide();
382 for( int i = count - 1; i >= 0; i-- )
384 if( totalWidth <= size.width() || i == 0)
386 buttons[i]->setGeometry( x, 0, qMin( size.width() - x, widths[i] ), size.height() );
387 buttons[i]->show();
388 x += widths[i];
389 totalWidth -= widths[i];
391 else
393 menuMore->addAction( actions[i] );
394 buttons[i]->hide();
395 if( i < shown ) totalWidth -= widths[i];
400 void LocationBar::resizeEvent ( QResizeEvent * event )
402 layOut( event->size() );
405 QSize LocationBar::sizeHint() const
407 return btnMore->sizeHint();
410 LocationButton::LocationButton( const QString &text, bool bold,
411 bool arrow, QWidget * parent )
412 : QPushButton( parent ), b_arrow( arrow )
414 QFont font;
415 font.setBold( bold );
416 setFont( font );
417 setText( text );
420 #define PADDING 4
422 void LocationButton::paintEvent ( QPaintEvent * )
424 QStyleOptionButton option;
425 option.initFrom( this );
426 option.state |= QStyle::State_Enabled;
427 QPainter p( this );
429 if( underMouse() )
431 p.save();
432 p.setRenderHint( QPainter::Antialiasing, true );
433 QColor c = palette().color( QPalette::Highlight );
434 p.setPen( c );
435 p.setBrush( c.lighter( 150 ) );
436 p.setOpacity( 0.2 );
437 p.drawRoundedRect( option.rect.adjusted( 0, 2, 0, -2 ), 5, 5 );
438 p.restore();
441 QRect r = option.rect.adjusted( PADDING, 0, -PADDING - (b_arrow ? 10 : 0), 0 );
443 QString str( text() );
444 /* This check is absurd, but either it is not done properly inside elidedText(),
445 or boundingRect() is wrong */
446 if( r.width() < fontMetrics().boundingRect( text() ).width() )
447 str = fontMetrics().elidedText( text(), Qt::ElideRight, r.width() );
448 p.drawText( r, Qt::AlignVCenter | Qt::AlignLeft, str );
450 if( b_arrow )
452 option.rect.setWidth( 10 );
453 option.rect.moveRight( rect().right() );
454 style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
458 QSize LocationButton::sizeHint() const
460 QSize s( fontMetrics().boundingRect( text() ).size() );
461 /* Add two pixels to width: font metrics are buggy, if you pass text through elidation
462 with exactly the width of its bounding rect, sometimes it still elides */
463 s.setWidth( s.width() + ( 2 * PADDING ) + ( b_arrow ? 10 : 0 ) + 2 );
464 s.setHeight( s.height() + 2 * PADDING );
465 return s;
468 #undef PADDING
470 #ifdef Q_WS_MAC
471 QSplitterHandle *PlaylistSplitter::createHandle()
473 return new SplitterHandle( orientation(), this );
476 SplitterHandle::SplitterHandle( Qt::Orientation orientation, QSplitter * parent )
477 : QSplitterHandle( orientation, parent)
481 QSize SplitterHandle::sizeHint() const
483 return (orientation() == Qt::Horizontal) ? QSize( 1, height() ) : QSize( width(), 1 );
486 void SplitterHandle::paintEvent(QPaintEvent *event)
488 QPainter painter( this );
489 painter.fillRect( event->rect(), QColor(81, 81, 81) );
491 #endif /* __APPLE__ */