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*/
40 /**********************************************************************
41 * Playlist Widget. The embedded playlist
42 **********************************************************************/
44 PlaylistWidget::PlaylistWidget( intf_thread_t *_p_i, QWidget *_par )
45 : QSplitter( _par ), p_intf ( _p_i )
47 setContentsMargins( 3, 3, 3, 3 );
49 /* We use a QSplitter for the left part*/
50 leftSplitter = new QSplitter( Qt::Vertical, this );
53 selector = new PLSelector( this, p_intf );
55 leftSplitter->addWidget( selector);
57 /* Create a Container for the Art Label
58 in order to have a beautiful resizing for the selector above it */
59 QWidget *artContainer = new QWidget;
60 QHBoxLayout *artContLay = new QHBoxLayout( artContainer );
61 artContLay->setMargin( 0 );
62 artContLay->setSpacing( 0 );
63 artContainer->setMaximumHeight( 128 );
66 art = new ArtLabel( artContainer, p_intf );
67 art->setToolTip( qtr( "Double click to get media information" ) );
69 CONNECT( THEMIM->getIM(), artChanged( QString ),
70 art, showArtUpdate( const QString& ) );
72 artContLay->addWidget( art, 1 );
74 leftSplitter->addWidget( artContainer );
76 /* Initialisation of the playlist */
77 playlist_t * p_playlist = THEPL;
79 playlist_item_t *p_root = THEPL->p_playing;
82 rightPanel = new StandardPLPanel( this, p_intf, THEPL, p_root, selector );
84 /* Connect the activation of the selector to a redefining of the PL */
85 DCONNECT( selector, activated( playlist_item_t * ),
86 rightPanel, setRoot( playlist_item_t * ) );
88 rightPanel->setRoot( p_root );
90 /* Add the two sides of the QSplitter */
91 addWidget( leftSplitter );
92 addWidget( rightPanel );
95 sizeList << 180 << 420 ;
97 //setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Expanding );
98 setStretchFactor( 0, 0 );
99 setStretchFactor( 1, 3 );
100 leftSplitter->setMaximumWidth( 250 );
101 setCollapsible( 1, false );
103 /* In case we want to keep the splitter information */
104 // components shall never write there setting to a fixed location, may infer
105 // with other uses of the same component...
106 getSettings()->beginGroup("Playlist");
107 restoreState( getSettings()->value("splitterSizes").toByteArray());
108 leftSplitter->restoreState( getSettings()->value("leftSplitterGeometry").toByteArray() );
109 getSettings()->endGroup();
111 setAcceptDrops( true );
112 setWindowTitle( qtr( "Playlist" ) );
113 setWindowRole( "vlc-playlist" );
114 setWindowIcon( QApplication::windowIcon() );
117 PlaylistWidget::~PlaylistWidget()
119 getSettings()->beginGroup("Playlist");
120 getSettings()->setValue( "splitterSizes", saveState() );
121 getSettings()->setValue( "leftSplitterGeometry", leftSplitter->saveState() );
122 getSettings()->endGroup();
123 msg_Dbg( p_intf, "Playlist Destroyed" );
126 void PlaylistWidget::dropEvent( QDropEvent *event )
128 if( p_intf->p_sys->p_mi )
129 p_intf->p_sys->p_mi->dropEventPlay( event, false );
131 void PlaylistWidget::dragEnterEvent( QDragEnterEvent *event )
133 event->acceptProposedAction();
136 void PlaylistWidget::closeEvent( QCloseEvent *event )
138 if( THEDP->isDying() )
140 p_intf->p_sys->p_mi->playlistVisible = true;
145 p_intf->p_sys->p_mi->playlistVisible = false;
151 void PlaylistWidget::forceHide()
153 leftSplitter->hide();
158 void PlaylistWidget::forceShow()
160 leftSplitter->show();
166 #include <QSignalMapper>
169 LocationBar::LocationBar( PLModel *m )
172 mapper = new QSignalMapper( this );
173 CONNECT( mapper, mapped( int ), this, invoke( int ) );
175 btnMore = new LocationButton( "...", false, true, this );
176 menuMore = new QMenu( this );
177 btnMore->setMenu( menuMore );
180 void LocationBar::setIndex( const QModelIndex &index )
182 qDeleteAll( buttons );
184 qDeleteAll( actions );
187 QModelIndex i = index;
192 PLItem *item = model->getItem( i );
194 char *fb_name = input_item_GetTitleFbName( item->inputItem() );
195 QString text = qfu(fb_name);
198 QAbstractButton *btn = new LocationButton( text, first, !first, this );
199 btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
200 buttons.append( btn );
202 QAction *action = new QAction( text, this );
203 actions.append( action );
204 CONNECT( btn, clicked(), action, trigger() );
206 mapper->setMapping( action, item->id() );
207 CONNECT( action, triggered(), mapper, map() );
211 if( i.isValid() ) i = i.parent();
216 for( int a = actions.count() - 1; a >= 0 ; a-- )
218 actions[a]->setText( prefix + actions[a]->text() );
219 prefix += QString(" ");
222 if( isVisible() ) layOut( size() );
225 void LocationBar::setRootIndex()
227 setIndex( QModelIndex() );
230 void LocationBar::invoke( int i_id )
232 QModelIndex index = model->index( i_id, 0 );
233 emit invoked ( index );
236 void LocationBar::layOut( const QSize& size )
241 int count = buttons.count();
243 for( int i = 0; i < count; i++ )
245 int w = buttons[i]->sizeHint().width();
248 if( totalWidth > size.width() ) break;
252 int shown = widths.count();
254 if( totalWidth > size.width() && count > 1 )
256 QSize sz = btnMore->sizeHint();
257 btnMore->setGeometry( 0, 0, sz.width(), size.height() );
266 for( int i = count - 1; i >= 0; i-- )
268 if( totalWidth <= size.width() || i == 0)
270 buttons[i]->setGeometry( x, 0, qMin( size.width() - x, widths[i] ), size.height() );
273 totalWidth -= widths[i];
277 menuMore->addAction( actions[i] );
279 if( i < shown ) totalWidth -= widths[i];
284 void LocationBar::resizeEvent ( QResizeEvent * event )
286 layOut( event->size() );
289 QSize LocationBar::sizeHint() const
291 return btnMore->sizeHint();
294 LocationButton::LocationButton( const QString &text, bool bold,
295 bool arrow, QWidget * parent )
296 : b_arrow( arrow ), QPushButton( parent )
299 font.setBold( bold );
306 void LocationButton::paintEvent ( QPaintEvent * event )
308 QStyleOptionButton option;
309 option.initFrom( this );
310 option.state |= QStyle::State_Enabled;
316 p.setRenderHint( QPainter::Antialiasing, true );
317 QColor c = palette().color( QPalette::Highlight );
319 p.setBrush( c.lighter( 150 ) );
321 p.drawRoundedRect( option.rect.adjusted( 0, 2, 0, -2 ), 5, 5 );
325 QRect r = option.rect.adjusted( PADDING, 0, -PADDING - (b_arrow ? 10 : 0), 0 );
327 QString str( text() );
328 /* This check is absurd, but either it is not done properly inside elidedText(),
329 or boundingRect() is wrong */
330 if( r.width() < fontMetrics().boundingRect( text() ).width() )
331 str = fontMetrics().elidedText( text(), Qt::ElideRight, r.width() );
332 p.drawText( r, Qt::AlignVCenter | Qt::AlignLeft, str );
336 option.rect.setWidth( 10 );
337 option.rect.moveRight( rect().right() );
338 style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
342 QSize LocationButton::sizeHint() const
344 QSize s( fontMetrics().boundingRect( text() ).size() );
345 /* Add two pixels to width: font metrics are buggy, if you pass text through elidation
346 with exactly the width of its bounding rect, sometimes it still elides */
347 s.setWidth( s.width() + ( 2 * PADDING ) + ( b_arrow ? 10 : 0 ) + 2 );
348 s.setHeight( s.height() + 2 * PADDING );