]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/playlist.cpp
Qt: change the LocationBar Class file
[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/standardpanel.hpp"
30 #include "components/playlist/selector.hpp"
31 #include "components/playlist/playlist.hpp"
32 #include "components/playlist/playlist_model.hpp"
33
34 #include "input_manager.hpp" /* art signal */
35 #include "main_interface.hpp" /* DropEvent TODO remove this*/
36
37 #include <QGroupBox>
38
39 #include <iostream>
40 /**********************************************************************
41  * Playlist Widget. The embedded playlist
42  **********************************************************************/
43
44 PlaylistWidget::PlaylistWidget( intf_thread_t *_p_i, QWidget *_par )
45                : QSplitter( _par ), p_intf ( _p_i )
46 {
47     setContentsMargins( 3, 3, 3, 3 );
48
49     /* We use a QSplitter for the left part*/
50     leftSplitter = new QSplitter( Qt::Vertical, this );
51
52     /* Source Selector */
53     selector = new PLSelector( this, p_intf );
54
55     leftSplitter->addWidget( selector);
56
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 );
64
65     /* Art label */
66     art = new ArtLabel( artContainer, p_intf );
67     art->setToolTip( qtr( "Double click to get media information" ) );
68
69     CONNECT( THEMIM->getIM(), artChanged( QString ),
70              art, showArtUpdate( const QString& ) );
71
72     artContLay->addWidget( art, 1 );
73
74     leftSplitter->addWidget( artContainer );
75
76     /* Initialisation of the playlist */
77     playlist_t * p_playlist = THEPL;
78     PL_LOCK;
79     playlist_item_t *p_root = THEPL->p_playing;
80     PL_UNLOCK;
81
82     rightPanel = new StandardPLPanel( this, p_intf, THEPL, p_root, selector );
83
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 * ) );
87
88     rightPanel->setRoot( p_root );
89
90     /* Add the two sides of the QSplitter */
91     addWidget( leftSplitter );
92     addWidget( rightPanel );
93
94     QList<int> sizeList;
95     sizeList << 180 << 420 ;
96     setSizes( sizeList );
97     //setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Expanding );
98     setStretchFactor( 0, 0 );
99     setStretchFactor( 1, 3 );
100     leftSplitter->setMaximumWidth( 250 );
101     setCollapsible( 1, false );
102
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();
110
111     setAcceptDrops( true );
112     setWindowTitle( qtr( "Playlist" ) );
113     setWindowRole( "vlc-playlist" );
114     setWindowIcon( QApplication::windowIcon() );
115 }
116
117 PlaylistWidget::~PlaylistWidget()
118 {
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" );
124 }
125
126 void PlaylistWidget::dropEvent( QDropEvent *event )
127 {
128     if( p_intf->p_sys->p_mi )
129         p_intf->p_sys->p_mi->dropEventPlay( event, false );
130 }
131 void PlaylistWidget::dragEnterEvent( QDragEnterEvent *event )
132 {
133     event->acceptProposedAction();
134 }
135
136 void PlaylistWidget::closeEvent( QCloseEvent *event )
137 {
138     if( THEDP->isDying() )
139     {
140         p_intf->p_sys->p_mi->playlistVisible = true;
141         event->accept();
142     }
143     else
144     {
145         p_intf->p_sys->p_mi->playlistVisible = false;
146         hide();
147         event->ignore();
148     }
149 }
150
151 void PlaylistWidget::forceHide()
152 {
153     leftSplitter->hide();
154     rightPanel->hide();
155     updateGeometry();
156 }
157
158 void PlaylistWidget::forceShow()
159 {
160     leftSplitter->show();
161     rightPanel->show();
162     updateGeometry();
163 }
164
165
166 #include <QSignalMapper>
167 #include <QMenu>
168 #include <QPainter>
169 LocationBar::LocationBar( PLModel *m )
170 {
171     model = m;
172     mapper = new QSignalMapper( this );
173     CONNECT( mapper, mapped( int ), this, invoke( int ) );
174
175     btnMore = new LocationButton( "...", false, true, this );
176     menuMore = new QMenu( this );
177     btnMore->setMenu( menuMore );
178 }
179
180 void LocationBar::setIndex( const QModelIndex &index )
181 {
182     qDeleteAll( buttons );
183     buttons.clear();
184     qDeleteAll( actions );
185     actions.clear();
186
187     QModelIndex i = index;
188     bool first = true;
189
190     while( true )
191     {
192         PLItem *item = model->getItem( i );
193
194         char *fb_name = input_item_GetTitleFbName( item->inputItem() );
195         QString text = qfu(fb_name);
196         free(fb_name);
197
198         QAbstractButton *btn = new LocationButton( text, first, !first, this );
199         btn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
200         buttons.append( btn );
201
202         QAction *action = new QAction( text, this );
203         actions.append( action );
204         CONNECT( btn, clicked(), action, trigger() );
205
206         mapper->setMapping( action, item->id() );
207         CONNECT( action, triggered(), mapper, map() );
208
209         first = false;
210
211         if( i.isValid() ) i = i.parent();
212         else break;
213     }
214
215     QString prefix;
216     for( int a = actions.count() - 1; a >= 0 ; a-- )
217     {
218         actions[a]->setText( prefix + actions[a]->text() );
219         prefix += QString("  ");
220     }
221
222     if( isVisible() ) layOut( size() );
223 }
224
225 void LocationBar::setRootIndex()
226 {
227     setIndex( QModelIndex() );
228 }
229
230 void LocationBar::invoke( int i_id )
231 {
232     QModelIndex index = model->index( i_id, 0 );
233     emit invoked ( index );
234 }
235
236 void LocationBar::layOut( const QSize& size )
237 {
238     menuMore->clear();
239     widths.clear();
240
241     int count = buttons.count();
242     int totalWidth = 0;
243     for( int i = 0; i < count; i++ )
244     {
245         int w = buttons[i]->sizeHint().width();
246         widths.append( w );
247         totalWidth += w;
248         if( totalWidth > size.width() ) break;
249     }
250
251     int x = 0;
252     int shown = widths.count();
253
254     if( totalWidth > size.width() && count > 1 )
255     {
256         QSize sz = btnMore->sizeHint();
257         btnMore->setGeometry( 0, 0, sz.width(), size.height() );
258         btnMore->show();
259         x = sz.width();
260         totalWidth += x;
261     }
262     else
263     {
264         btnMore->hide();
265     }
266     for( int i = count - 1; i >= 0; i-- )
267     {
268         if( totalWidth <= size.width() || i == 0)
269         {
270             buttons[i]->setGeometry( x, 0, qMin( size.width() - x, widths[i] ), size.height() );
271             buttons[i]->show();
272             x += widths[i];
273             totalWidth -= widths[i];
274         }
275         else
276         {
277             menuMore->addAction( actions[i] );
278             buttons[i]->hide();
279             if( i < shown ) totalWidth -= widths[i];
280         }
281     }
282 }
283
284 void LocationBar::resizeEvent ( QResizeEvent * event )
285 {
286     layOut( event->size() );
287 }
288
289 QSize LocationBar::sizeHint() const
290 {
291     return btnMore->sizeHint();
292 }
293
294 LocationButton::LocationButton( const QString &text, bool bold,
295                                 bool arrow, QWidget * parent )
296   : b_arrow( arrow ), QPushButton( parent )
297 {
298     QFont font;
299     font.setBold( bold );
300     setFont( font );
301     setText( text );
302 }
303
304 #define PADDING 4
305
306 void LocationButton::paintEvent ( QPaintEvent * event )
307 {
308     QStyleOptionButton option;
309     option.initFrom( this );
310     option.state |= QStyle::State_Enabled;
311     QPainter p( this );
312
313     if( underMouse() )
314     {
315         p.save();
316         p.setRenderHint( QPainter::Antialiasing, true );
317         QColor c = palette().color( QPalette::Highlight );
318         p.setPen( c );
319         p.setBrush( c.lighter( 150 ) );
320         p.setOpacity( 0.2 );
321         p.drawRoundedRect( option.rect.adjusted( 0, 2, 0, -2 ), 5, 5 );
322         p.restore();
323     }
324
325     QRect r = option.rect.adjusted( PADDING, 0, -PADDING - (b_arrow ? 10 : 0), 0 );
326
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 );
333
334     if( b_arrow )
335     {
336         option.rect.setWidth( 10 );
337         option.rect.moveRight( rect().right() );
338         style()->drawPrimitive( QStyle::PE_IndicatorArrowRight, &option, &p );
339     }
340 }
341
342 QSize LocationButton::sizeHint() const
343 {
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 );
349     return s;
350 }
351
352 #undef PADDING