]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
2de523d87053785121fb1b343ea5576992f9f138
[vlc] / modules / gui / qt4 / main_interface.cpp
1 /*****************************************************************************
2  * main_interface.cpp : Main interface
3  ****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include "qt4.hpp"
25 #include "main_interface.hpp"
26 #include "input_manager.hpp"
27 #include "util/input_slider.hpp"
28 #include "util/qvlcframe.hpp"
29 #include "util/customwidgets.hpp"
30 #include "dialogs_provider.hpp"
31 #include "components/interface_widgets.hpp"
32 #include "dialogs/playlist.hpp"
33 #include "menus.hpp"
34
35 #include <QMenuBar>
36 #include <QCloseEvent>
37 #include <QPushButton>
38 #include <QStatusBar>
39 #include <QKeyEvent>
40 #include <QUrl>
41 #include <QSystemTrayIcon>
42 #include <QSize>
43 #include <QMenu>
44
45 #include <assert.h>
46 #include <vlc_keys.h>
47 #include <vlc_vout.h>
48
49 #ifdef WIN32
50     #define PREF_W 410
51     #define PREF_H 121
52 #else
53     #define PREF_W 450
54     #define PREF_H 125
55 #endif
56
57 #define VISIBLE(i) (i && i->isVisible())
58
59 #define SET_WIDTH(i,j) i->widgetSize.setWidth(j)
60 #define SET_HEIGHT(i,j) i->widgetSize.setHeight(j)
61 #define SET_WH( i,j,k) i->widgetSize.setWidth(j); i->widgetSize.setHeight(k);
62
63 #define DS(i) i.width(),i.height()
64
65 #define DEBUG_COLOR 0
66
67 /* Callback prototypes */
68 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
69                         vlc_value_t old_val, vlc_value_t new_val, void *param );
70 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
71                        vlc_value_t old_val, vlc_value_t new_val, void *param );
72 static int InteractCallback( vlc_object_t *, const char *, vlc_value_t,
73                              vlc_value_t, void *);
74 /* Video handling */
75 static void *DoRequest( intf_thread_t *p_intf, vout_thread_t *p_vout,
76                         int *pi1, int *pi2, unsigned int*pi3,unsigned int*pi4)
77 {
78     return p_intf->p_sys->p_mi->requestVideo( p_vout, pi1, pi2, pi3, pi4 );
79 }
80 static void DoRelease( intf_thread_t *p_intf, void *p_win )
81 {
82     return p_intf->p_sys->p_mi->releaseVideo( p_win );
83 }
84 static int DoControl( intf_thread_t *p_intf, void *p_win, int i_q, va_list a )
85 {
86     return p_intf->p_sys->p_mi->controlVideo( p_win, i_q, a );
87 }
88
89 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
90 {
91     /* Variables initialisation */
92     need_components_update = false;
93     bgWidget = NULL; videoWidget = NULL; playlistWidget = NULL;
94     embeddedPlaylistWasActive = videoIsActive = false;
95
96     bool b_createSystray = false;
97     input_name = "";
98
99     /**
100      *  Configuration and settings
101      **/
102     settings = new QSettings( "VideoLAN", "VLC" );
103     settings->beginGroup( "MainWindow" );
104
105     /* Main settings */
106     setFocusPolicy( Qt::StrongFocus );
107     setAcceptDrops(true);
108     setWindowIcon( QApplication::windowIcon() );
109
110     /* Set The Video In emebedded Mode or not */
111     videoEmbeddedFlag = false;
112     if( config_GetInt( p_intf, "embedded-video" ) )
113         videoEmbeddedFlag = true;
114
115     alwaysVideoFlag = false;
116     if( videoEmbeddedFlag && config_GetInt( p_intf, "qt-always-video" ) )
117         alwaysVideoFlag = true;
118
119     /* Set the other interface settings */
120     playlistEmbeddedFlag = settings->value("playlist-embedded", true).toBool();
121     advControlsEnabled= settings->value( "adv-controls", false ).toBool();
122     visualSelectorEnabled= settings->value( "visual-selector", false ).toBool();
123
124 #if DEBUG_COLOR
125     QPalette palette2;
126     palette2.setColor(this->backgroundRole(), Qt::magenta);
127     setPalette(palette2);
128 #endif
129
130     /**
131      *  UI design
132      **/
133     setVLCWindowsTitle();
134     handleMainUi( settings );
135
136     /* Menu Bar */
137     QVLCMenu::createMenuBar( this, p_intf, playlistEmbeddedFlag,
138                              advControlsEnabled, visualSelectorEnabled );
139
140     /* Status Bar */
141     timeLabel = new QLabel;
142     nameLabel = new QLabel;
143     speedLabel = new QLabel( "1.0x" );
144     timeLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
145     speedLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
146     statusBar()->addWidget( nameLabel, 8 );
147     statusBar()->addPermanentWidget( speedLabel, 0 );
148     statusBar()->addPermanentWidget( timeLabel, 2 );
149
150     /* Systray */
151     sysTray = NULL;
152     if( config_GetInt( p_intf, "qt-start-minimized") )
153     {
154         hide();
155         b_createSystray = true;
156     }
157     if( config_GetInt( p_intf, "qt-system-tray") )
158         b_createSystray = true;
159     if (QSystemTrayIcon::isSystemTrayAvailable() && b_createSystray )
160             createSystray();
161
162     /* Init input manager */
163     MainInputManager::getInstance( p_intf );
164     ON_TIMEOUT( updateOnTimer() );
165
166     /**
167      * CONNECTs
168      **/
169
170     /* Volume control */
171     CONNECT( ui.volumeSlider, valueChanged(int), this, updateVolume(int) );
172
173     /* Connect the input manager to the GUI elements it manages */
174     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
175              slider, setPosition( float,int, int ) );
176     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
177              this, setDisplay( float, int, int ) );
178
179     /* Naming in the controller */
180     CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
181              setName( QString ) );
182     if( config_GetInt( p_intf, "qt-system-tray" ) && sysTray )
183     {
184         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
185                  updateSystrayTooltipName( QString ) );
186     }
187     if( config_GetInt( p_intf, "qt-name-in-title" ) )
188     {
189         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
190              setVLCWindowsTitle( QString ) );
191     }
192
193     /* PLAY_STATUS */
194     CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
195     CONNECT( THEMIM->getIM(), navigationChanged( int ),
196              this, setNavigation(int) );
197     if( config_GetInt( p_intf, "qt-system-tray" ) && sysTray )
198     {
199         CONNECT( THEMIM->getIM(), statusChanged( int ), this,
200                  updateSystrayTooltipStatus( int ) );
201     }
202     CONNECT( slider, sliderDragged( float ),
203              THEMIM->getIM(), sliderUpdate( float ) );
204
205     /* Buttons */
206     CONNECT( ui.prevSectionButton, clicked(), THEMIM->getIM(),
207              sectionPrev() );
208     CONNECT( ui.nextSectionButton, clicked(), THEMIM->getIM(),
209              sectionNext() );
210     CONNECT( ui.menuButton, clicked(), THEMIM->getIM(),
211              sectionMenu() );
212
213     /**
214      * Callbacks
215      **/
216     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
217     var_AddCallback( p_intf, "interaction", InteractCallback, this );
218     p_intf->b_interaction = VLC_TRUE;
219
220     /* Register callback for the intf-popupmenu variable */
221     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
222                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
223     if( p_playlist != NULL )
224     {
225         var_AddCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
226         var_AddCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
227         vlc_object_release( p_playlist );
228     }
229 }
230
231 MainInterface::~MainInterface()
232 {
233     /* Unregister callback for the intf-popupmenu variable */
234     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
235                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
236     if( p_playlist != NULL )
237     {
238         var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
239         var_DelCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
240         vlc_object_release( p_playlist );
241     }
242
243     settings->setValue( "playlist-embedded", playlistEmbeddedFlag );
244     settings->setValue( "adv-controls", advControlsEnabled );
245     settings->setValue( "pos", pos() );
246     settings->endGroup();
247     delete settings;
248     p_intf->b_interaction = VLC_FALSE;
249     var_DelCallback( p_intf, "interaction", InteractCallback, this );
250
251     p_intf->pf_request_window = NULL;
252     p_intf->pf_release_window = NULL;
253     p_intf->pf_control_window = NULL;
254 }
255
256 /*****************************
257  *   Main UI handling        *
258  *****************************/
259
260 /**
261  * Give the decorations of the Main Window a correct Name.
262  * If nothing is given, set it to VLC...
263  **/
264 void MainInterface::setVLCWindowsTitle( QString aTitle )
265 {
266     if( aTitle.isEmpty() )
267     {
268         setWindowTitle( qtr( "VLC media player" ) );
269     }
270     else
271     {
272         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
273     }
274 }
275
276 void MainInterface::handleMainUi( QSettings *settings )
277 {
278     QWidget *main = new QWidget( this );
279     QVBoxLayout *mainLayout = new QVBoxLayout( main );
280     setCentralWidget( main );
281
282     /* Margins, spacing */
283     main->setContentsMargins( 0, 0, 0, 0 );
284     mainLayout->setMargin( 0 );
285
286     /* CONTROLS */
287     QWidget *controls = new QWidget;
288     ui.setupUi( controls );
289
290     /* Configure the UI */
291     slider = new InputSlider( Qt::Horizontal, NULL );
292     ui.vboxLayout->insertWidget( 0, slider );
293     ui.discFrame->hide();
294
295     BUTTON_SET_IMG( ui.prevSectionButton, "", previous.png, "" );
296     BUTTON_SET_IMG( ui.nextSectionButton, "", next.png, "" );
297     BUTTON_SET_IMG( ui.menuButton, "", previous.png, "" );
298
299     BUTTON_SET_ACT_I( ui.prevButton, "" , previous.png,
300                       qtr("Previous"), prev() );
301     BUTTON_SET_ACT_I( ui.nextButton, "", next.png, qtr("Next"), next() );
302     BUTTON_SET_ACT_I( ui.playButton, "", play.png, qtr("Play"), play() );
303     BUTTON_SET_ACT_I( ui.stopButton, "", stop.png, qtr("Stop"), stop() );
304
305     /* Volume */
306     ui.volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
307     ui.volumeSlider->setMaximum( 100 );
308     ui.volMuteLabel->setToolTip( qtr( "Mute" ) );
309     VolumeClickHandler *h = new VolumeClickHandler( p_intf, this );
310     ui.volMuteLabel->installEventFilter(h);
311     ui.volumeSlider->setFocusPolicy( Qt::NoFocus );
312
313     BUTTON_SET_IMG( ui.playlistButton, "" , playlist_icon.png,
314                         playlistEmbeddedFlag ?  qtr( "Show playlist" ) :
315                                                 qtr( "Open playlist" ) );
316     BUTTONACT( ui.playlistButton, playlist() );
317
318 #if DEBUG_COLOR
319     QPalette palette;
320     palette.setColor(main->backgroundRole(), Qt::green);
321     main->setPalette(palette);
322 #endif
323
324     /* Add the controls Widget */
325     mainLayout->addWidget( controls );
326
327     /* Set initial size */
328     resize ( PREF_W, PREF_H );
329
330     addSize = QSize( mainLayout->margin() * 2, PREF_H );
331
332     /* advanced Controls handling */
333     advControls = new ControlsWidget( p_intf );
334     mainLayout->insertWidget( 0, advControls );
335     advControls->updateGeometry();
336     if( !advControlsEnabled ) advControls->hide();
337     need_components_update = true;
338
339     /* Visualisation */
340     visualSelector = new VisualSelector( p_intf );
341     mainLayout->insertWidget( 0, visualSelector );
342     visualSelector->hide();
343
344     /* And video Outputs */
345     if( alwaysVideoFlag )
346     {
347         bgWidget = new BackgroundWidget( p_intf );
348         bgWidget->widgetSize = settings->value( "backgroundSize",
349                                            QSize( 200, 200 ) ).toSize();
350         bgWidget->resize( bgWidget->widgetSize );
351         bgWidget->updateGeometry();
352         mainLayout->insertWidget( 0, bgWidget );
353     }
354
355     if( videoEmbeddedFlag )
356     {
357         videoWidget = new VideoWidget( p_intf );
358         videoWidget->widgetSize = QSize( 1, 1 );
359         //videoWidget->resize( videoWidget->widgetSize );
360         mainLayout->insertWidget( 0, videoWidget );
361
362         p_intf->pf_request_window  = ::DoRequest;
363         p_intf->pf_release_window  = ::DoRelease;
364         p_intf->pf_control_window  = ::DoControl;
365     }
366
367     /* Finish the sizing */
368     setMinimumSize( PREF_W, addSize.height() );
369 }
370
371 /**********************************************************************
372  * Handling of the components
373  **********************************************************************/
374 void MainInterface::calculateInterfaceSize()
375 {
376     int width = 0, height = 0;
377     if( VISIBLE( bgWidget ) )
378     {
379         width = bgWidget->widgetSize.width();
380         height = bgWidget->widgetSize.height();
381         assert( !(playlistWidget && playlistWidget->isVisible() ) );
382     }
383     else if( VISIBLE( playlistWidget ) )
384     {
385         width = playlistWidget->widgetSize.width();
386         height = playlistWidget->widgetSize.height();
387     }
388     else if( videoIsActive )
389     {
390         width =  videoWidget->widgetSize.width() ;
391         height = videoWidget->widgetSize.height();
392     }
393     else
394     {
395         width = PREF_W - addSize.width();
396         height = PREF_H - addSize.height();
397     }
398     if( VISIBLE( visualSelector ) )
399         height += visualSelector->height();
400     if( VISIBLE( advControls) )
401     {
402         height += advControls->sizeHint().height();
403     }
404     mainSize = QSize( width + addSize.width(), height + addSize.height() );
405 }
406
407 void MainInterface::resizeEvent( QResizeEvent *e )
408 {
409     videoWidget->widgetSize.setWidth(  e->size().width() - addSize.width() );
410     if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
411     {
412         SET_WH( videoWidget, e->size().width() - addSize.width(),
413                              e->size().height()  - addSize.height() );
414         videoWidget->updateGeometry();
415     }
416     if( VISIBLE( playlistWidget ) )
417     {
418         SET_WH( playlistWidget , e->size().width() - addSize.width(),
419                                  e->size().height() - addSize.height() );
420         playlistWidget->updateGeometry();
421     }
422 }
423
424 /*****************************************************************************
425  * Systray Icon and Systray Menu
426  *****************************************************************************/
427
428 /**
429  * Create a SystemTray icon and a menu that would go with it.
430  * Connects to a click handler on the icon.
431  **/
432 void MainInterface::createSystray()
433 {
434     QIcon iconVLC =  QIcon( QPixmap( ":/vlc128.png" ) );
435     sysTray = new QSystemTrayIcon( iconVLC, this );
436     sysTray->setToolTip( qtr( "VLC media player" ));
437
438     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
439     systrayMenu->setIcon( iconVLC );
440
441     QVLCMenu::updateSystrayMenu( this, p_intf, true );
442     sysTray->show();
443
444     CONNECT( sysTray, activated(  QSystemTrayIcon::ActivationReason ),
445             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
446 }
447
448 /**
449  * Update the menu of the Systray Icon.
450  * May be unneedded, since it just calls QVLCMenu::update
451  * FIXME !!!
452  **/
453 void MainInterface::updateSystrayMenu( int status )
454 {
455     QVLCMenu::updateSystrayMenu( this, p_intf ) ;
456 }
457
458 /**
459  * Updates the Systray Icon's menu and toggle the main interface
460  */
461 void MainInterface::toggleUpdateSystrayMenu()
462 {
463     toggleVisible();
464     QVLCMenu::updateSystrayMenu( this, p_intf );
465 }
466
467 void MainInterface::handleSystrayClick(
468                                     QSystemTrayIcon::ActivationReason reason )
469 {
470     switch( reason )
471     {
472         case QSystemTrayIcon::Trigger:
473             toggleUpdateSystrayMenu();
474             break;
475         case QSystemTrayIcon::MiddleClick:
476             sysTray->showMessage( qtr( "VLC media player" ),
477                     qtr( "Control menu for the player" ),
478                     QSystemTrayIcon::Information, 4000 );
479             break;
480     }
481 }
482
483 /**
484  * Updates the name of the systray Icon tooltip.
485  * Doesn't check if the systray exists, check before you call it.
486  * FIXME !!! Fusion with next function ?
487  **/
488 void MainInterface::updateSystrayTooltipName( QString name )
489 {
490     if( name.isEmpty() )
491     {
492         sysTray->setToolTip( qtr( "VLC media player" ) );
493     }
494     else
495     {
496         sysTray->setToolTip( name );
497     }
498 }
499
500 /**
501  * Updates the status of the systray Icon tooltip.
502  * Doesn't check if the systray exists, check before you call it.
503  **/
504 void MainInterface::updateSystrayTooltipStatus( int i_status )
505 {
506     switch( i_status )
507     {
508         case  0:
509             {
510                 sysTray->setToolTip( qtr( "VLC media player" ) );
511                 break;
512             }
513         case PLAYING_S:
514             {
515                 sysTray->setToolTip( input_name );
516                 //+ " - " + qtr( "Playing" ) );
517                 break;
518             }
519         case PAUSE_S:
520             {
521                 sysTray->setToolTip( input_name + " - "
522                         + qtr( "Paused") );
523                 break;
524             }
525     }
526 }
527 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
528                                    int *pi_y, unsigned int *pi_width,
529                                    unsigned int *pi_height )
530 {
531     void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
532     if( ret )
533     {
534         videoIsActive = true;
535         if( VISIBLE( playlistWidget ) )
536         {
537             embeddedPlaylistWasActive = true;
538 //            playlistWidget->hide();
539         }
540         bool bgWasVisible = false;
541         if( VISIBLE( bgWidget) )
542         {
543             bgWasVisible = true;
544 //            bgWidget->hide();
545         }
546         if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
547         {
548             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
549         }
550         else /* Background widget available, use its size */
551         {
552             /* Ok, our visualizations are bad, so don't do this for the moment
553              * use the requested size anyway */
554             // videoWidget->widgetSize = bgWidget->widgeTSize;
555             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
556         }
557 //        videoWidget->updateGeometry(); /// FIXME: Needed ?
558         need_components_update = true;
559     }
560     return ret;
561 }
562
563 void MainInterface::releaseVideo( void *p_win )
564 {
565     videoWidget->release( p_win );
566     videoWidget->widgetSize = QSize( 0, 0 );
567     videoWidget->resize( videoWidget->widgetSize );
568
569     if( embeddedPlaylistWasActive )
570         ;//playlistWidget->show();
571     else if( bgWidget )
572         ;//bgWidget->show();
573
574     videoIsActive = false;
575     need_components_update = true;
576 }
577
578 class SetVideoOnTopQtEvent : public QEvent
579 {
580 public:
581     SetVideoOnTopQtEvent( bool _onTop ) :
582       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
583     {
584     }
585
586     bool OnTop() const
587     {
588         return onTop;
589     }
590
591 private:
592     bool onTop;
593 };
594
595 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
596 {
597     int i_ret = VLC_EGENERIC;
598     switch( i_query )
599     {
600         case VOUT_GET_SIZE:
601         {
602             unsigned int *pi_width  = va_arg( args, unsigned int * );
603             unsigned int *pi_height = va_arg( args, unsigned int * );
604             *pi_width = videoWidget->widgetSize.width();
605             *pi_height = videoWidget->widgetSize.height();
606             i_ret = VLC_SUCCESS;
607             break;
608         }
609         case VOUT_SET_SIZE:
610         {
611             unsigned int i_width  = va_arg( args, unsigned int );
612             unsigned int i_height = va_arg( args, unsigned int );
613             videoWidget->widgetSize = QSize( i_width, i_height );
614             // videoWidget->updateGeometry();
615             need_components_update = true;
616             i_ret = VLC_SUCCESS;
617             break;
618         }
619         case VOUT_SET_STAY_ON_TOP:
620         {
621             int i_arg = va_arg( args, int );
622             QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
623             i_ret = VLC_SUCCESS;
624             break;
625         }
626         default:
627             msg_Warn( p_intf, "unsupported control query" );
628             break;
629     }
630     return i_ret;
631 }
632
633 void MainInterface::advanced()
634 {
635     if( !VISIBLE( advControls ) )
636     {
637         advControls->show();
638         advControlsEnabled = true;
639     }
640     else
641     {
642         advControls->hide();
643         advControlsEnabled = false;
644     }
645     doComponentsUpdate();
646 }
647
648 void MainInterface::visual()
649 {
650     if( !VISIBLE( visualSelector) )
651     {
652         visualSelector->show();
653         if( !THEMIM->getIM()->hasVideo() )
654         {
655             /* Show the background widget */
656         }
657         visualSelectorEnabled = true;
658     }
659     else
660     {
661         /* Stop any currently running visualization */
662         visualSelector->hide();
663         visualSelectorEnabled = false;
664     }
665     doComponentsUpdate();
666 }
667
668 void MainInterface::playlist()
669 {
670     // Toggle the playlist dialog
671     if( !playlistEmbeddedFlag )
672     {
673         if( playlistWidget )
674         {
675             /// \todo Destroy it
676         }
677         THEDP->playlistDialog();
678         return;
679     }
680
681     if( !playlistWidget )
682     {
683         PlaylistDialog::killInstance();
684         playlistWidget = new PlaylistWidget( p_intf );
685         ui.vboxLayout->insertWidget( 0, playlistWidget );
686         playlistWidget->widgetSize = settings->value( "playlistSize",
687                                                QSize( 650, 310 ) ).toSize();
688         playlistWidget->hide();
689     }
690     if( VISIBLE( playlistWidget ) )
691     {
692         playlistWidget->hide();
693         if( videoIsActive )
694         {
695             videoWidget->widgetSize = savedVideoSize;
696             videoWidget->resize( videoWidget->widgetSize );
697             videoWidget->updateGeometry();
698         }
699         if( bgWidget ) bgWidget->show();
700     }
701     else
702     {
703         playlistWidget->show();
704         if( videoIsActive )
705         {
706             savedVideoSize = videoWidget->widgetSize;
707             videoWidget->widgetSize.setHeight( 0 );
708             videoWidget->resize( videoWidget->widgetSize );
709             videoWidget->updateGeometry();
710         }
711         if( VISIBLE( bgWidget ) ) bgWidget->hide();
712     }
713     doComponentsUpdate();
714 }
715
716 /* Video widget cannot do this synchronously as it runs in another thread */
717 /* Well, could it, actually ? Probably dangerous ... */
718 void MainInterface::doComponentsUpdate()
719 {
720     calculateInterfaceSize();
721     resize( mainSize );
722 }
723
724 void MainInterface::undockPlaylist()
725 {
726     if( playlistWidget )
727     {
728         playlistWidget->hide();
729         playlistWidget->deleteLater();
730         ui.vboxLayout->removeWidget( playlistWidget );
731         playlistWidget = NULL;
732         playlistEmbeddedFlag = false;
733
734         menuBar()->clear();
735         QVLCMenu::createMenuBar( this, p_intf, false, advControlsEnabled,
736                                  visualSelectorEnabled);
737
738         if( videoIsActive )
739         {
740             videoWidget->widgetSize = savedVideoSize;
741             videoWidget->resize( videoWidget->widgetSize );
742             videoWidget->updateGeometry();
743         }
744
745         doComponentsUpdate();
746         THEDP->playlistDialog();
747     }
748 }
749
750 void MainInterface::toggleMenus()
751 {
752     if( menuBar()->isVisible() ) menuBar()->hide();
753         else menuBar()->show();
754     msg_Dbg( p_intf, "I was there: \\_o<~~ coin coin" );
755 }
756
757 void MainInterface::customEvent( QEvent *event )
758 {
759     if( event->type() == PLDockEvent_Type )
760     {
761         PlaylistDialog::killInstance();
762         playlistEmbeddedFlag = true;
763         menuBar()->clear();
764         QVLCMenu::createMenuBar(this, p_intf, true, advControlsEnabled,
765                                 visualSelectorEnabled);
766         playlist();
767     }
768     else if ( event->type() == SetVideoOnTopEvent_Type )
769     {
770         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
771         if( p_event->OnTop() )
772             setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
773         else
774             setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
775         show(); /* necessary to apply window flags?? */
776     }
777 }
778
779
780 /************************************************************************
781  * D&D
782  ************************************************************************/
783 void MainInterface::dropEvent(QDropEvent *event)
784 {
785      const QMimeData *mimeData = event->mimeData();
786
787      /* D&D of a subtitles file, add it on the fly */
788      if( mimeData->urls().size() == 1 )
789      {
790         if( THEMIM->getIM()->hasInput() )
791         {
792             if( input_AddSubtitles( THEMIM->getInput(),
793                                     qtu( mimeData->urls()[0].toString() ),
794                                     VLC_TRUE ) )
795             {
796                 event->acceptProposedAction();
797                 return;
798             }
799         }
800      }
801      bool first = true;
802      foreach( QUrl url, mimeData->urls() ) {
803         QString s = url.toString();
804         if( s.length() > 0 ) {
805             playlist_Add( THEPL, qtu(s), NULL,
806                           PLAYLIST_APPEND | (first ? PLAYLIST_GO:0),
807                           PLAYLIST_END, VLC_TRUE, VLC_FALSE );
808             first = false;
809         }
810      }
811      event->acceptProposedAction();
812 }
813 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
814 {
815      event->acceptProposedAction();
816 }
817 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
818 {
819      event->acceptProposedAction();
820 }
821 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
822 {
823      event->accept();
824 }
825
826 /************************************************************************
827  * Other stuff
828  ************************************************************************/
829 void MainInterface::keyPressEvent( QKeyEvent *e )
830 {
831     int i_vlck = qtEventToVLCKey( e );
832     if( i_vlck >= 0 )
833     {
834         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
835         e->accept();
836     }
837     else
838         e->ignore();
839 }
840
841 void MainInterface::wheelEvent( QWheelEvent *e )
842 {
843     int i_vlckey = qtWheelEventToVLCKey( e );
844     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
845     e->accept();
846 }
847
848 void MainInterface::stop()
849 {
850     THEMIM->stop();
851 }
852
853 void MainInterface::play()
854 {
855     if( playlist_IsEmpty(THEPL) )
856     {
857         /* The playlist is empty, open a file requester */
858         THEDP->openFileDialog();
859         setStatus( 0 );
860         return;
861     }
862     THEMIM->togglePlayPause();
863 }
864
865 void MainInterface::prev()
866 {
867     THEMIM->prev();
868 }
869
870 void MainInterface::next()
871 {
872     THEMIM->next();
873 }
874
875 void MainInterface::setDisplay( float pos, int time, int length )
876 {
877     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
878     secstotimestr( psz_length, length );
879     secstotimestr( psz_time, time );
880     QString title;
881     title.sprintf( "%s/%s", psz_time, psz_length );
882     timeLabel->setText( " "+title+" " );
883 }
884
885 void MainInterface::setName( QString name )
886 {
887     input_name = name;
888     nameLabel->setText( " " + name+" " );
889 }
890
891 void MainInterface::setStatus( int status )
892 {
893     if( status == 1 ) // Playing
894         ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
895     else
896         ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
897     if( systrayMenu )
898         updateSystrayMenu( status );
899 }
900
901 #define HELP_MENU N_("Menu")
902 #define HELP_PCH N_("Previous chapter")
903 #define HELP_NCH N_("Next chapter")
904 #define HELP_PTR N_("Previous track")
905 #define HELP_NTR N_("Next track")
906
907 void MainInterface::setNavigation( int navigation )
908 {
909     // 1 = chapter, 2 = title, 0 = no
910     if( navigation == 0 )
911     {
912         ui.discFrame->hide();
913     } else if( navigation == 1 ) {
914         ui.prevSectionButton->show();
915         ui.prevSectionButton->setToolTip( qfu(HELP_PCH) );
916         ui.nextSectionButton->show();
917         ui.nextSectionButton->setToolTip( qfu(HELP_NCH) );
918         ui.menuButton->show();
919         ui.discFrame->show();
920     } else {
921         ui.prevSectionButton->show();
922         ui.prevSectionButton->setToolTip( qfu(HELP_PCH) );
923         ui.nextSectionButton->show();
924         ui.nextSectionButton->setToolTip( qfu(HELP_NCH) );
925         ui.menuButton->hide();
926         ui.discFrame->show();
927     }
928 }
929
930 static bool b_my_volume;
931
932 void MainInterface::updateOnTimer()
933 {
934     /* \todo Make this event-driven */
935     advControls->enableInput( THEMIM->getIM()->hasInput() );
936     advControls->enableVideo( THEMIM->getIM()->hasVideo() );
937
938     if( intf_ShouldDie( p_intf ) )
939     {
940         QApplication::closeAllWindows();
941         QApplication::quit();
942     }
943     if( need_components_update )
944     {
945         doComponentsUpdate();
946         need_components_update = false;
947     }
948
949     audio_volume_t i_volume;
950     aout_VolumeGet( p_intf, &i_volume );
951     i_volume = (i_volume *  200 )/ AOUT_VOLUME_MAX ;
952     int i_gauge = ui.volumeSlider->value();
953     b_my_volume = false;
954     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
955     {
956         b_my_volume = true;
957         ui.volumeSlider->setValue( i_volume );
958         b_my_volume = false;
959     }
960 }
961
962 void MainInterface::closeEvent( QCloseEvent *e )
963 {
964     hide();
965     vlc_object_kill( p_intf );
966 }
967
968 void MainInterface::updateVolume( int sliderVolume )
969 {
970     if( !b_my_volume )
971     {
972         int i_res = sliderVolume * AOUT_VOLUME_MAX /
973                             (2*ui.volumeSlider->maximum() );
974         aout_VolumeSet( p_intf, i_res );
975     }
976 }
977
978 static int InteractCallback( vlc_object_t *p_this,
979                              const char *psz_var, vlc_value_t old_val,
980                              vlc_value_t new_val, void *param )
981 {
982     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
983     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
984     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
985     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
986     return VLC_SUCCESS;
987 }
988
989 /*****************************************************************************
990  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
991  *  We don't show the menu directly here because we don't want the
992  *  caller to block for a too long time.
993  *****************************************************************************/
994 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
995                         vlc_value_t old_val, vlc_value_t new_val, void *param )
996 {
997     intf_thread_t *p_intf = (intf_thread_t *)param;
998
999     if( p_intf->pf_show_dialog )
1000     {
1001         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1002                                 new_val.b_bool, 0 );
1003     }
1004
1005     return VLC_SUCCESS;
1006 }
1007
1008 /*****************************************************************************
1009  * IntfShowCB: callback triggered by the intf-show playlist variable.
1010  *****************************************************************************/
1011 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1012                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1013 {
1014     intf_thread_t *p_intf = (intf_thread_t *)param;
1015     //p_intf->p_sys->b_intf_show = VLC_TRUE;
1016
1017     return VLC_SUCCESS;
1018 }