]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist.cpp
Qt, locationBar: translate Input->Name
[vlc] / modules / gui / qt4 / components / playlist / playlist.cpp
1 /*****************************************************************************
2  * playlist.cpp : Custom widgets for the playlist
3  ****************************************************************************
4  * Copyright © 2007-2010 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
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 */
34
35 #include "util/searchlineedit.hpp"
36
37 #include "input_manager.hpp"                      /* art signal */
38 #include "main_interface.hpp"                     /* DropEvent TODO remove this*/
39
40 #include <QMenu>
41 #include <QSignalMapper>
42
43 /**********************************************************************
44  * Playlist Widget. The embedded playlist
45  **********************************************************************/
46
47 PlaylistWidget::PlaylistWidget( intf_thread_t *_p_i, QWidget *_par )
48                : QWidget( _par ), p_intf ( _p_i )
49 {
50
51     setContentsMargins( 0, 3, 0, 3 );
52
53     QGridLayout *layout = new QGridLayout( this );
54     layout->setMargin( 0 ); layout->setSpacing( 0 );
55
56     /*******************
57      * Left            *
58      *******************/
59     /* We use a QSplitter for the left part */
60     leftSplitter = new QSplitter( Qt::Vertical, this );
61
62     /* Source Selector */
63     PLSelector *selector = new PLSelector( this, p_intf );
64     leftSplitter->addWidget( selector);
65
66     /* Create a Container for the Art Label
67        in order to have a beautiful resizing for the selector above it */
68     QWidget *artContainer = new QWidget;
69     QHBoxLayout *artContLay = new QHBoxLayout( artContainer );
70     artContLay->setMargin( 0 );
71     artContLay->setSpacing( 0 );
72     artContainer->setMaximumHeight( 128 );
73
74     /* Art label */
75     CoverArtLabel *art = new CoverArtLabel( artContainer, p_intf );
76     art->setToolTip( qtr( "Double click to get media information" ) );
77     artContLay->addWidget( art, 1 );
78
79     CONNECT( THEMIM->getIM(), artChanged( QString ),
80              art, showArtUpdate( const QString& ) );
81
82     leftSplitter->addWidget( artContainer );
83
84     /*******************
85      * Right           *
86      *******************/
87     /* Initialisation of the playlist */
88     playlist_t * p_playlist = THEPL;
89     PL_LOCK;
90     playlist_item_t *p_root = THEPL->p_playing;
91     PL_UNLOCK;
92
93     setMinimumWidth( 400 );
94
95     PLModel *model = new PLModel( p_playlist, p_intf, p_root, this );
96     mainView = new StandardPLPanel( this, p_intf, p_root, selector, model );
97
98     /* Location Bar */
99     locationBar = new LocationBar( model );
100     locationBar->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Preferred );
101     layout->addWidget( locationBar, 0, 0, 1, 2 );
102     layout->setColumnStretch( 0, 5 );
103     CONNECT( locationBar, invoked( const QModelIndex & ),
104              mainView, browseInto( const QModelIndex & ) );
105
106     /* Button to switch views */
107     QToolButton *viewButton = new QToolButton( this );
108     viewButton->setIcon( style()->standardIcon( QStyle::SP_FileDialogDetailedView ) );
109     viewButton->setToolTip( qtr("Change playlistview") );
110     layout->addWidget( viewButton, 0, 2 );
111
112     /* View selection menu */
113     QSignalMapper *viewSelectionMapper = new QSignalMapper( this );
114     CONNECT( viewSelectionMapper, mapped( int ), mainView, showView( int ) );
115
116     QActionGroup *actionGroup = new QActionGroup( this );
117
118     for( int i = 0; i < StandardPLPanel::VIEW_COUNT; i++ )
119     {
120         viewActions[i] = actionGroup->addAction( viewNames[i] );
121         viewActions[i]->setCheckable( true );
122         viewSelectionMapper->setMapping( viewActions[i], i );
123         CONNECT( viewActions[i], triggered(), viewSelectionMapper, map() );
124     }
125     viewActions[0]->setChecked( true );
126
127     QMenu *viewMenu = new QMenu( viewButton );
128     viewMenu->addActions( actionGroup->actions() );
129     viewButton->setMenu( viewMenu );
130     CONNECT( viewButton, clicked(), mainView, cycleViews() );
131
132     /* Search */
133     searchEdit = new SearchLineEdit( this );
134     searchEdit->setMaximumWidth( 250 );
135     searchEdit->setMinimumWidth( 80 );
136     layout->addWidget( searchEdit, 0, 3 );
137     CONNECT( searchEdit, textEdited( const QString& ),
138              mainView, search( const QString& ) );
139     CONNECT( searchEdit, searchDelayedChanged( const QString& ),
140              mainView, searchDelayed( const QString & ) );
141
142     CONNECT( mainView, viewChanged( const QModelIndex& ),
143              this, changeView( const QModelIndex &) );
144     layout->setColumnStretch( 3, 3 );
145
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 * ) );
149     mainView->setRoot( p_root );
150
151
152     split = new PlaylistSplitter( this );
153
154     /* Add the two sides of the QSplitter */
155     split->addWidget( leftSplitter );
156     split->addWidget( mainView );
157
158     QList<int> sizeList;
159     sizeList << 180 << 420 ;
160     split->setSizes( sizeList );
161     split->setStretchFactor( 0, 0 );
162     split->setStretchFactor( 1, 3 );
163     split->setCollapsible( 1, false );
164     leftSplitter->setMaximumWidth( 250 );
165
166     /* In case we want to keep the splitter information */
167     // components shall never write there setting to a fixed location, may infer
168     // with other uses of the same component...
169     getSettings()->beginGroup("Playlist");
170     split->restoreState( getSettings()->value("splitterSizes").toByteArray());
171     leftSplitter->restoreState( getSettings()->value("leftSplitterGeometry").toByteArray() );
172     getSettings()->endGroup();
173
174     layout->addWidget( split, 1, 0, 1, -1 );
175     setAcceptDrops( true );
176     setWindowTitle( qtr( "Playlist" ) );
177     setWindowRole( "vlc-playlist" );
178     setWindowIcon( QApplication::windowIcon() );
179 }
180
181 PlaylistWidget::~PlaylistWidget()
182 {
183     getSettings()->beginGroup("Playlist");
184     getSettings()->setValue( "splitterSizes", split->saveState() );
185     getSettings()->setValue( "leftSplitterGeometry", leftSplitter->saveState() );
186     getSettings()->endGroup();
187     msg_Dbg( p_intf, "Playlist Destroyed" );
188 }
189
190 void PlaylistWidget::dropEvent( QDropEvent *event )
191 {
192     if( p_intf->p_sys->p_mi )
193         p_intf->p_sys->p_mi->dropEventPlay( event, false );
194 }
195 void PlaylistWidget::dragEnterEvent( QDragEnterEvent *event )
196 {
197     event->acceptProposedAction();
198 }
199
200 void PlaylistWidget::closeEvent( QCloseEvent *event )
201 {
202     if( THEDP->isDying() )
203     {
204         p_intf->p_sys->p_mi->playlistVisible = true;
205         event->accept();
206     }
207     else
208     {
209         p_intf->p_sys->p_mi->playlistVisible = false;
210         hide();
211         event->ignore();
212     }
213 }
214
215 void PlaylistWidget::forceHide()
216 {
217     leftSplitter->hide();
218     mainView->hide();
219     updateGeometry();
220 }
221
222 void PlaylistWidget::forceShow()
223 {
224     leftSplitter->show();
225     mainView->show();
226     updateGeometry();
227 }
228
229 void PlaylistWidget::changeView( const QModelIndex& index )
230 {
231     searchEdit->clear();
232     locationBar->setIndex( index );
233     int i = mainView->currentViewIndex();
234     viewActions[i]->setChecked(true);
235 }
236
237 #include <QSignalMapper>
238 #include <QMenu>
239 #include <QPainter>
240 LocationBar::LocationBar( PLModel *m )
241 {
242     model = m;
243     mapper = new QSignalMapper( this );
244     CONNECT( mapper, mapped( int ), this, invoke( int ) );
245
246     btnMore = new LocationButton( "...", false, true, this );
247     menuMore = new QMenu( this );
248     btnMore->setMenu( menuMore );
249 }
250
251 void LocationBar::setIndex( const QModelIndex &index )
252 {
253     qDeleteAll( buttons );
254     buttons.clear();
255     qDeleteAll( actions );
256     actions.clear();
257
258     QModelIndex i = index;
259     bool first = true;
260
261     while( true )
262     {
263         PLItem *item = model->getItem( i );
264         QString text;
265
266         char *fb_name = input_item_GetTitle( item->inputItem() );
267         if( !EMPTY_STR( fb_name ) )
268              text = qfu(fb_name);
269         else
270         {
271             fb_name = input_item_GetName( item->inputItem() );
272             text = qtr(fb_name);
273         }
274         free(fb_name);
275
276         QAbstractButton *btn = new LocationButton( text, first, !first, this );
277         btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
278         buttons.append( btn );
279
280         QAction *action = new QAction( text, this );
281         actions.append( action );
282         CONNECT( btn, clicked(), action, trigger() );
283
284         mapper->setMapping( action, item->id() );
285         CONNECT( action, triggered(), mapper, map() );
286
287         first = false;
288
289         if( i.isValid() ) i = i.parent();
290         else break;
291     }
292
293     QString prefix;
294     for( int a = actions.count() - 1; a >= 0 ; a-- )
295     {
296         actions[a]->setText( prefix + actions[a]->text() );
297         prefix += QString("  ");
298     }
299
300     if( isVisible() ) layOut( size() );
301 }
302
303 void LocationBar::setRootIndex()
304 {
305     setIndex( QModelIndex() );
306 }
307
308 void LocationBar::invoke( int i_id )
309 {
310     QModelIndex index = model->index( i_id, 0 );
311     emit invoked ( index );
312 }
313
314 void LocationBar::layOut( const QSize& size )
315 {
316     menuMore->clear();
317     widths.clear();
318
319     int count = buttons.count();
320     int totalWidth = 0;
321     for( int i = 0; i < count; i++ )
322     {
323         int w = buttons[i]->sizeHint().width();
324         widths.append( w );
325         totalWidth += w;
326         if( totalWidth > size.width() ) break;
327     }
328
329     int x = 0;
330     int shown = widths.count();
331
332     if( totalWidth > size.width() && count > 1 )
333     {
334         QSize sz = btnMore->sizeHint();
335         btnMore->setGeometry( 0, 0, sz.width(), size.height() );
336         btnMore->show();
337         x = sz.width();
338         totalWidth += x;
339     }
340     else
341     {
342         btnMore->hide();
343     }
344     for( int i = count - 1; i >= 0; i-- )
345     {
346         if( totalWidth <= size.width() || i == 0)
347         {
348             buttons[i]->setGeometry( x, 0, qMin( size.width() - x, widths[i] ), size.height() );
349             buttons[i]->show();
350             x += widths[i];
351             totalWidth -= widths[i];
352         }
353         else
354         {
355             menuMore->addAction( actions[i] );
356             buttons[i]->hide();
357             if( i < shown ) totalWidth -= widths[i];
358         }
359     }
360 }
361
362 void LocationBar::resizeEvent ( QResizeEvent * event )
363 {
364     layOut( event->size() );
365 }
366
367 QSize LocationBar::sizeHint() const
368 {
369     return btnMore->sizeHint();
370 }
371
372 LocationButton::LocationButton( const QString &text, bool bold,
373                                 bool arrow, QWidget * parent )
374   : b_arrow( arrow ), QPushButton( parent )
375 {
376     QFont font;
377     font.setBold( bold );
378     setFont( font );
379     setText( text );
380 }
381
382 #define PADDING 4
383
384 void LocationButton::paintEvent ( QPaintEvent * event )
385 {
386     QStyleOptionButton option;
387     option.initFrom( this );
388     option.state |= QStyle::State_Enabled;
389     QPainter p( this );
390
391     if( underMouse() )
392     {
393         p.save();
394         p.setRenderHint( QPainter::Antialiasing, true );
395         QColor c = palette().color( QPalette::Highlight );
396         p.setPen( c );
397         p.setBrush( c.lighter( 150 ) );
398         p.setOpacity( 0.2 );
399         p.drawRoundedRect( option.rect.adjusted( 0, 2, 0, -2 ), 5, 5 );
400         p.restore();
401     }
402
403     QRect r = option.rect.adjusted( PADDING, 0, -PADDING - (b_arrow ? 10 : 0), 0 );
404
405     QString str( text() );
406     /* This check is absurd, but either it is not done properly inside elidedText(),
407        or boundingRect() is wrong */
408     if( r.width() < fontMetrics().boundingRect( text() ).width() )
409         str = fontMetrics().elidedText( text(), Qt::ElideRight, r.width() );
410     p.drawText( r, Qt::AlignVCenter | Qt::AlignLeft, str );
411
412     if( b_arrow )
413     {
414         option.rect.setWidth( 10 );
415         option.rect.moveRight( rect().right() );
416         style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
417     }
418 }
419
420 QSize LocationButton::sizeHint() const
421 {
422     QSize s( fontMetrics().boundingRect( text() ).size() );
423     /* Add two pixels to width: font metrics are buggy, if you pass text through elidation
424        with exactly the width of its bounding rect, sometimes it still elides */
425     s.setWidth( s.width() + ( 2 * PADDING ) + ( b_arrow ? 10 : 0 ) + 2 );
426     s.setHeight( s.height() + 2 * PADDING );
427     return s;
428 }
429
430 #undef PADDING
431
432 #ifdef Q_WS_MAC
433 QSplitterHandle *PlaylistSplitter::createHandle()
434 {
435     return new SplitterHandle( orientation(), this );
436 }
437
438 SplitterHandle::SplitterHandle( Qt::Orientation orientation, QSplitter * parent )
439                : QSplitterHandle( orientation, parent)
440 {
441 };
442
443 QSize SplitterHandle::sizeHint() const
444 {
445     return (orientation() == Qt::Horizontal) ? QSize( 1, height() ) : QSize( width(), 1 );
446 }
447
448 void SplitterHandle::paintEvent(QPaintEvent *event)
449 {
450     QPainter painter( this );
451     painter.fillRect( event->rect(), QColor(81, 81, 81) );
452 }
453 #endif /* __APPLE__ */