d466dec18f5ff5b75077e37caf5e50fd86e88e95
[vlc.git] / 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
43 #include <assert.h>
44 #include <vlc_keys.h>
45 #include <vlc_vout.h>
46
47 #ifdef WIN32
48     #define PREF_W 410
49     #define PREF_H 121
50 #else
51     #define PREF_W 450
52     #define PREF_H 125
53 #endif
54
55 #define VISIBLE(i) (i && i->isVisible())
56
57 #define SET_WIDTH(i,j) i->widgetSize.setWidth(j)
58 #define SET_HEIGHT(i,j) i->widgetSize.setHeight(j)
59 #define SET_WH( i,j,k) i->widgetSize.setWidth(j); i->widgetSize.setHeight(k);
60
61 #define DS(i) i.width(),i.height()
62
63 /* Callback prototypes */
64 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
65                         vlc_value_t old_val, vlc_value_t new_val, void *param );
66 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
67                        vlc_value_t old_val, vlc_value_t new_val, void *param );
68 static int InteractCallback( vlc_object_t *, const char *, vlc_value_t,
69                              vlc_value_t, void *);
70 /* Video handling */
71 static void *DoRequest( intf_thread_t *p_intf, vout_thread_t *p_vout,
72                         int *pi1, int *pi2, unsigned int*pi3,unsigned int*pi4)
73 {
74     return p_intf->p_sys->p_mi->requestVideo( p_vout, pi1, pi2, pi3, pi4 );
75 }
76 static void DoRelease( intf_thread_t *p_intf, void *p_win )
77 {
78     return p_intf->p_sys->p_mi->releaseVideo( p_win );
79 }
80 static int DoControl( intf_thread_t *p_intf, void *p_win, int i_q, va_list a )
81 {
82     return p_intf->p_sys->p_mi->controlVideo( p_win, i_q, a );
83 }
84
85 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
86 {
87     /* Configuration */
88     settings = new QSettings( "VideoLAN", "VLC" );
89     settings->beginGroup( "MainWindow" );
90
91     setWindowIcon( QApplication::windowIcon() );
92
93     need_components_update = false;
94     bgWidget = NULL; videoWidget = NULL; playlistWidget = NULL;
95     embeddedPlaylistWasActive = videoIsActive = false;
96
97     videoEmbeddedFlag = false;
98     if( config_GetInt( p_intf, "embedded-video" ) ) videoEmbeddedFlag = true;
99
100     alwaysVideoFlag = false;
101     if( videoEmbeddedFlag && config_GetInt( p_intf, "qt-always-video" ))
102         alwaysVideoFlag = true;
103
104     playlistEmbeddedFlag = settings->value("playlist-embedded", true).toBool();
105     advControlsEnabled= settings->value( "adv-controls", false ).toBool();
106     visualSelectorEnabled= settings->value( "visual-selector", false ).toBool();
107
108     /* UI */
109     setVLCWindowsTitle();
110     handleMainUi( settings );
111     QVLCMenu::createMenuBar( this, p_intf, playlistEmbeddedFlag,
112                              advControlsEnabled, visualSelectorEnabled );
113     timeLabel = new QLabel( 0 );
114     nameLabel = new QLabel( 0 );
115     statusBar()->addWidget( nameLabel, 4 );
116     statusBar()->addPermanentWidget( timeLabel, 1 );
117
118     setFocusPolicy( Qt::StrongFocus );
119     setAcceptDrops(true);
120
121     /* Init input manager */
122     MainInputManager::getInstance( p_intf );
123     ON_TIMEOUT( updateOnTimer() );
124
125     /* Volume control */
126     CONNECT( ui.volumeSlider, valueChanged(int), this, updateVolume(int) );
127
128     /* Connect the input manager to the GUI elements it manages */
129     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
130              slider, setPosition( float,int, int ) );
131     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
132              this, setDisplay( float, int, int ) );
133
134     /* Naming in the controller */
135     CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
136              setName( QString ) );
137     if( config_GetInt( p_intf, "qt-name-in-title" ) )
138     {
139         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
140              setVLCWindowsTitle( QString ) );
141     }
142
143     /* PLAY_STATUS */
144     CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
145     CONNECT( THEMIM->getIM(), navigationChanged( int ),
146              this, setNavigation(int) );
147     CONNECT( slider, sliderDragged( float ),
148              THEMIM->getIM(), sliderUpdate( float ) );
149
150     /* Buttons */
151     CONNECT( ui.prevSectionButton, clicked(), THEMIM->getIM(),
152              sectionPrev() );
153     CONNECT( ui.nextSectionButton, clicked(), THEMIM->getIM(),
154              sectionNext() );
155     CONNECT( ui.menuButton, clicked(), THEMIM->getIM(),
156              sectionMenu() );
157
158     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
159     var_AddCallback( p_intf, "interaction", InteractCallback, this );
160     p_intf->b_interaction = VLC_TRUE;
161
162     /* Register callback for the intf-popupmenu variable */
163     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
164                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
165     if( p_playlist != NULL )
166     {
167         var_AddCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
168         var_AddCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
169         vlc_object_release( p_playlist );
170     }
171     if( QSystemTrayIcon::isSystemTrayAvailable() &&
172                         ( config_GetInt( p_intf, "qt-start-mininimized") == 1))
173     {
174         hide();
175         createSystrayMenu();
176     }
177     if( QSystemTrayIcon::isSystemTrayAvailable() &&
178                               ( config_GetInt( p_intf, "qt-system-tray") == 1))
179             createSystrayMenu();
180
181 }
182
183 MainInterface::~MainInterface()
184 {
185     /* Unregister callback for the intf-popupmenu variable */
186     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
187                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
188     if( p_playlist != NULL )
189     {
190         var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
191         var_DelCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
192         vlc_object_release( p_playlist );
193     }
194
195     settings->setValue( "playlist-embedded", playlistEmbeddedFlag );
196     settings->setValue( "adv-controls", advControlsEnabled );
197     settings->setValue( "pos", pos() );
198     settings->endGroup();
199     delete settings;
200     p_intf->b_interaction = VLC_FALSE;
201     var_DelCallback( p_intf, "interaction", InteractCallback, this );
202
203     p_intf->pf_request_window = NULL;
204     p_intf->pf_release_window = NULL;
205     p_intf->pf_control_window = NULL;
206 }
207
208 void MainInterface::setVLCWindowsTitle( QString aTitle )
209 {
210     if( aTitle.isEmpty() )
211     {
212         this->setWindowTitle( qtr( "VLC media player" ) );
213     }
214     else
215     {
216         this->setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
217     }
218 }
219
220 void MainInterface::handleMainUi( QSettings *settings )
221 {
222     QWidget *main = new QWidget( this );
223     setCentralWidget( main );
224     ui.setupUi( centralWidget() );
225
226     slider = new InputSlider( Qt::Horizontal, NULL );
227     ui.vboxLayout->insertWidget( 0, slider );
228     ui.discFrame->hide();
229     BUTTON_SET_IMG( ui.prevSectionButton, "", previous.png, "" );
230     BUTTON_SET_IMG( ui.nextSectionButton, "", next.png, "" );
231     BUTTON_SET_IMG( ui.menuButton, "", previous.png, "" );
232
233     BUTTON_SET_ACT_I( ui.prevButton, "" , previous.png,
234                       qtr("Previous"), prev() );
235     BUTTON_SET_ACT_I( ui.nextButton, "", next.png, qtr("Next"), next() );
236     BUTTON_SET_ACT_I( ui.playButton, "", play.png, qtr("Play"), play() );
237     BUTTON_SET_ACT_I( ui.stopButton, "", stop.png, qtr("Stop"), stop() );
238
239     /* Volume */
240     ui.volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
241     ui.volumeSlider->setMaximum( 100 );
242     ui.volMuteLabel->setToolTip( qtr( "Mute" ) );
243     VolumeClickHandler *h = new VolumeClickHandler( p_intf, this );
244     ui.volMuteLabel->installEventFilter(h);
245     ui.volumeSlider->setFocusPolicy( Qt::NoFocus );
246
247     BUTTON_SET_IMG( ui.playlistButton, "" , playlist_icon.png,
248                         playlistEmbeddedFlag ?  qtr( "Show playlist" ) :
249                                                 qtr( "Open playlist" ) );
250     BUTTONACT( ui.playlistButton, playlist() );
251
252     /* Set initial size */
253     resize ( PREF_W, PREF_H );
254
255     addSize = QSize( ui.vboxLayout->margin() * 2, PREF_H );
256
257     advControls = new ControlsWidget( p_intf );
258     ui.vboxLayout->insertWidget( 0, advControls );
259     advControls->updateGeometry();
260     if( !advControlsEnabled ) advControls->hide();
261     need_components_update = true;
262
263     visualSelector = new VisualSelector( p_intf );
264     ui.vboxLayout->insertWidget( 0, visualSelector );
265     visualSelector->hide();
266
267     if( alwaysVideoFlag )
268     {
269         bgWidget = new BackgroundWidget( p_intf );
270         bgWidget->widgetSize = settings->value( "backgroundSize",
271                                                 QSize( 200, 200 ) ).toSize();
272         bgWidget->resize( bgWidget->widgetSize );
273         bgWidget->updateGeometry();
274         ui.vboxLayout->insertWidget( 0, bgWidget );
275     }
276
277     if( videoEmbeddedFlag )
278     {
279         videoWidget = new VideoWidget( p_intf );
280         videoWidget->widgetSize = QSize( 1, 1 );
281         videoWidget->resize( videoWidget->widgetSize );
282         ui.vboxLayout->insertWidget( 0, videoWidget );
283
284         p_intf->pf_request_window  = ::DoRequest;
285         p_intf->pf_release_window  = ::DoRelease;
286         p_intf->pf_control_window  = ::DoControl;
287     }
288     setMinimumSize( PREF_W, addSize.height() );
289 }
290
291 void MainInterface::createSystrayMenu()
292 {
293     QIcon iconVLC =  QIcon( QPixmap( ":/vlc128.png" ) );
294     sysTray = new QSystemTrayIcon( iconVLC, this );
295     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
296     systrayMenu->setIcon( iconVLC );
297     QVLCMenu::updateSystrayMenu( this, p_intf, true );
298     sysTray->show();
299     CONNECT( sysTray, activated(  QSystemTrayIcon::ActivationReason ),
300             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
301 }
302
303 void MainInterface::updateSystrayMenu( int status )
304 {
305     QVLCMenu::updateSystrayMenu( this, p_intf ) ;
306 }
307
308 void MainInterface::toggleUpdateSystrayMenu()
309 {
310     QVLCMenu::updateSystrayMenu( this, p_intf );
311     toggleVisible();
312 }
313
314 void MainInterface::handleSystrayClick( QSystemTrayIcon::ActivationReason reason )
315 {
316     switch( reason )
317     {
318         case QSystemTrayIcon::Trigger:
319             this->toggleVisible(); break;
320         case QSystemTrayIcon::MiddleClick:
321             sysTray->showMessage( qtr( "VLC media player" ),
322                     qtr( "Control menu for the player" )
323                     + nameLabel->text() ,
324                     QSystemTrayIcon::Information, 4000 );
325             break;
326     }
327 }
328
329 /**********************************************************************
330  * Handling of the components
331  **********************************************************************/
332 void MainInterface::calculateInterfaceSize()
333 {
334     int width = 0, height = 0;
335     if( VISIBLE( bgWidget ) )
336     {
337         width = bgWidget->widgetSize.width();
338         height = bgWidget->widgetSize.height();
339         assert( !(playlistWidget && playlistWidget->isVisible() ) );
340     }
341     else if( VISIBLE( playlistWidget ) )
342     {
343         width = playlistWidget->widgetSize.width();
344         height = playlistWidget->widgetSize.height();
345     }
346     else if( videoIsActive )
347     {
348         width =  videoWidget->widgetSize.width() ;
349         height = videoWidget->widgetSize.height();
350     }
351     else
352     {
353         width = PREF_W - addSize.width();
354         height = PREF_H - addSize.height();
355     }
356     if( VISIBLE( visualSelector ) )
357         height += visualSelector->height();
358     if( VISIBLE( advControls) )
359     {
360         height += advControls->sizeHint().height();
361     }
362     mainSize = QSize( width + addSize.width(), height + addSize.height() );
363 }
364
365 void MainInterface::resizeEvent( QResizeEvent *e )
366 {
367     videoWidget->widgetSize.setWidth(  e->size().width() - addSize.width() );
368     if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
369     {
370         SET_WH( videoWidget, e->size().width() - addSize.width(),
371                              e->size().height()  - addSize.height() );
372         videoWidget->updateGeometry();
373     }
374     if( VISIBLE( playlistWidget ) )
375     {
376         SET_WH( playlistWidget , e->size().width() - addSize.width(),
377                                  e->size().height() - addSize.height() );
378         playlistWidget->updateGeometry();
379     }
380 }
381
382 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
383                                    int *pi_y, unsigned int *pi_width,
384                                    unsigned int *pi_height )
385 {
386     void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
387     if( ret )
388     {
389         videoIsActive = true;
390         if( VISIBLE( playlistWidget ) )
391         {
392             embeddedPlaylistWasActive = true;
393 //            playlistWidget->hide();
394         }
395         bool bgWasVisible = false;
396         if( VISIBLE( bgWidget) )
397         {
398             bgWasVisible = true;
399 //            bgWidget->hide();
400         }
401         if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
402         {
403             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
404         }
405         else /* Background widget available, use its size */
406         {
407             /* Ok, our visualizations are bad, so don't do this for the moment
408              * use the requested size anyway */
409             // videoWidget->widgetSize = bgWidget->widgeTSize;
410             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
411         }
412 //        videoWidget->updateGeometry(); /// FIXME: Needed ?
413         need_components_update = true;
414     }
415     return ret;
416 }
417
418 void MainInterface::releaseVideo( void *p_win )
419 {
420     videoWidget->release( p_win );
421     videoWidget->widgetSize = QSize( 0, 0 );
422     videoWidget->resize( videoWidget->widgetSize );
423
424     if( embeddedPlaylistWasActive )
425         ;//playlistWidget->show();
426     else if( bgWidget )
427         ;//bgWidget->show();
428
429     videoIsActive = false;
430     need_components_update = true;
431 }
432
433 class SetVideoOnTopQtEvent : public QEvent
434 {
435 public:
436     SetVideoOnTopQtEvent( bool _onTop ) :
437       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
438     {
439     }
440
441     bool OnTop() const
442     {
443         return onTop;
444     }
445
446 private:
447     bool onTop;
448 };
449
450 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
451 {
452     int i_ret = VLC_EGENERIC;
453     switch( i_query )
454     {
455         case VOUT_GET_SIZE:
456         {
457             unsigned int *pi_width  = va_arg( args, unsigned int * );
458             unsigned int *pi_height = va_arg( args, unsigned int * );
459             *pi_width = videoWidget->widgetSize.width();
460             *pi_height = videoWidget->widgetSize.height();
461             i_ret = VLC_SUCCESS;
462             break;
463         }
464         case VOUT_SET_SIZE:
465         {
466             unsigned int i_width  = va_arg( args, unsigned int );
467             unsigned int i_height = va_arg( args, unsigned int );
468             videoWidget->widgetSize = QSize( i_width, i_height );
469             // videoWidget->updateGeometry();
470             need_components_update = true;
471             i_ret = VLC_SUCCESS;
472             break;
473         }
474         case VOUT_SET_STAY_ON_TOP:
475         {
476             int i_arg = va_arg( args, int );
477             QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
478             i_ret = VLC_SUCCESS;
479             break;
480         }
481         default:
482             msg_Warn( p_intf, "unsupported control query" );
483             break;
484     }
485     return i_ret;
486 }
487
488 void MainInterface::advanced()
489 {
490     if( !VISIBLE( advControls ) )
491     {
492         advControls->show();
493         advControlsEnabled = true;
494     }
495     else
496     {
497         advControls->hide();
498         advControlsEnabled = false;
499     }
500     doComponentsUpdate();
501 }
502
503 void MainInterface::visual()
504 {
505     if( !VISIBLE( visualSelector) )
506     {
507         visualSelector->show();
508         if( !THEMIM->getIM()->hasVideo() )
509         {
510             /* Show the background widget */
511         }
512         visualSelectorEnabled = true;
513     }
514     else
515     {
516         /* Stop any currently running visualization */
517         visualSelector->hide();
518         visualSelectorEnabled = false;
519     }
520     doComponentsUpdate();
521 }
522
523 void MainInterface::playlist()
524 {
525     // Toggle the playlist dialog
526     if( !playlistEmbeddedFlag )
527     {
528         if( playlistWidget )
529         {
530             /// \todo Destroy it
531         }
532         THEDP->playlistDialog();
533         return;
534     }
535
536     if( !playlistWidget )
537     {
538         PlaylistDialog::killInstance();
539         playlistWidget = new PlaylistWidget( p_intf );
540         ui.vboxLayout->insertWidget( 0, playlistWidget );
541         playlistWidget->widgetSize = settings->value( "playlistSize",
542                                                QSize( 650, 310 ) ).toSize();
543         playlistWidget->hide();
544     }
545     if( VISIBLE( playlistWidget ) )
546     {
547         playlistWidget->hide();
548         if( videoIsActive )
549         {
550             videoWidget->widgetSize = savedVideoSize;
551             videoWidget->resize( videoWidget->widgetSize );
552             videoWidget->updateGeometry();
553         }
554     }
555     else
556     {
557         playlistWidget->show();
558         if( videoIsActive )
559         {
560             savedVideoSize = videoWidget->widgetSize;
561             videoWidget->widgetSize.setHeight( 0 );
562             videoWidget->resize( videoWidget->widgetSize );
563             videoWidget->updateGeometry();
564         }
565         if( VISIBLE( bgWidget ) ) bgWidget->hide();
566     }
567     doComponentsUpdate();
568 }
569
570 /* Video widget cannot do this synchronously as it runs in another thread */
571 /* Well, could it, actually ? Probably dangerous ... */
572 void MainInterface::doComponentsUpdate()
573 {
574     calculateInterfaceSize();
575     resize( mainSize );
576 }
577
578 void MainInterface::undockPlaylist()
579 {
580     if( playlistWidget )
581     {
582         playlistWidget->hide();
583         playlistWidget->deleteLater();
584         ui.vboxLayout->removeWidget( playlistWidget );
585         playlistWidget = NULL;
586         playlistEmbeddedFlag = false;
587
588         menuBar()->clear();
589         QVLCMenu::createMenuBar( this, p_intf, false, advControlsEnabled,
590                                  visualSelectorEnabled);
591
592         if( videoIsActive )
593         {
594             videoWidget->widgetSize = savedVideoSize;
595             videoWidget->resize( videoWidget->widgetSize );
596             videoWidget->updateGeometry();
597         }
598
599         doComponentsUpdate();
600         THEDP->playlistDialog();
601     }
602 }
603
604 void MainInterface::customEvent( QEvent *event )
605 {
606     if( event->type() == PLDockEvent_Type )
607     {
608         PlaylistDialog::killInstance();
609         playlistEmbeddedFlag = true;
610         menuBar()->clear();
611         QVLCMenu::createMenuBar(this, p_intf, true, advControlsEnabled,
612                                 visualSelectorEnabled);
613         playlist();
614     }
615     else if ( event->type() == SetVideoOnTopEvent_Type )
616     {
617         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
618         if( p_event->OnTop() )
619             setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
620         else
621             setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
622         show(); /* necessary to apply window flags?? */
623     }
624 }
625
626
627 /************************************************************************
628  * D&D
629  ************************************************************************/
630 void MainInterface::dropEvent(QDropEvent *event)
631 {
632      const QMimeData *mimeData = event->mimeData();
633
634      /* D&D of a subtitles file, add it on the fly */
635      if( mimeData->urls().size() == 1 )
636      {
637         if( THEMIM->getIM()->hasInput() )
638         {
639             if( input_AddSubtitles( THEMIM->getInput(),
640                                     qtu( mimeData->urls()[0].toString() ),
641                                     VLC_TRUE ) )
642             {
643                 event->acceptProposedAction();
644                 return;
645             }
646         }
647      }
648      bool first = true;
649      foreach( QUrl url, mimeData->urls() ) {
650         QString s = url.toString();
651         if( s.length() > 0 ) {
652             playlist_Add( THEPL, qtu(s), NULL,
653                           PLAYLIST_APPEND | (first ? PLAYLIST_GO:0),
654                           PLAYLIST_END, VLC_TRUE, VLC_FALSE );
655             first = false;
656         }
657      }
658      event->acceptProposedAction();
659 }
660 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
661 {
662      event->acceptProposedAction();
663 }
664 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
665 {
666      event->acceptProposedAction();
667 }
668 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
669 {
670      event->accept();
671 }
672
673 /************************************************************************
674  * Other stuff
675  ************************************************************************/
676 void MainInterface::keyPressEvent( QKeyEvent *e )
677 {
678     int i_vlck = qtEventToVLCKey( e );
679     if( i_vlck >= 0 )
680     {
681         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
682         e->accept();
683     }
684     else
685         e->ignore();
686 }
687
688 void MainInterface::wheelEvent( QWheelEvent *e )
689 {
690     int i_vlckey = qtWheelEventToVLCKey( e );
691     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
692     e->accept();
693 }
694
695 void MainInterface::stop()
696 {
697     THEMIM->stop();
698 }
699
700 void MainInterface::play()
701 {
702     if( playlist_IsEmpty(THEPL) )
703     {
704         /* The playlist is empty, open a file requester */
705         THEDP->openFileDialog();
706         setStatus( 0 );
707         return;
708     }
709     THEMIM->togglePlayPause();
710 }
711
712 void MainInterface::prev()
713 {
714     THEMIM->prev();
715 }
716
717 void MainInterface::next()
718 {
719     THEMIM->next();
720 }
721
722 void MainInterface::setDisplay( float pos, int time, int length )
723 {
724     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
725     secstotimestr( psz_length, length );
726     secstotimestr( psz_time, time );
727     QString title;
728     title.sprintf( "%s/%s", psz_time, psz_length );
729     timeLabel->setText( " "+title+" " );
730 }
731
732 void MainInterface::setName( QString name )
733 {
734     nameLabel->setText( " " + name+" " );
735
736 }
737
738 void MainInterface::setStatus( int status )
739 {
740     if( status == 1 ) // Playing
741         ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
742     else
743         ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
744     updateSystrayMenu( status );
745 }
746
747 #define HELP_MENU N_("Menu")
748 #define HELP_PCH N_("Previous chapter")
749 #define HELP_NCH N_("Next chapter")
750 #define HELP_PTR N_("Previous track")
751 #define HELP_NTR N_("Next track")
752
753 void MainInterface::setNavigation( int navigation )
754 {
755     // 1 = chapter, 2 = title, 0 = no
756     if( navigation == 0 )
757     {
758         ui.discFrame->hide();
759     } else if( navigation == 1 ) {
760         ui.prevSectionButton->show();
761         ui.prevSectionButton->setToolTip( qfu(HELP_PCH) );
762         ui.nextSectionButton->show();
763         ui.nextSectionButton->setToolTip( qfu(HELP_NCH) );
764         ui.menuButton->show();
765         ui.discFrame->show();
766     } else {
767         ui.prevSectionButton->show();
768         ui.prevSectionButton->setToolTip( qfu(HELP_PCH) );
769         ui.nextSectionButton->show();
770         ui.nextSectionButton->setToolTip( qfu(HELP_NCH) );
771         ui.menuButton->hide();
772         ui.discFrame->show();
773     }
774 }
775
776 static bool b_my_volume;
777
778 void MainInterface::updateOnTimer()
779 {
780     /* \todo Make this event-driven */
781     advControls->enableInput( THEMIM->getIM()->hasInput() );
782     advControls->enableVideo( THEMIM->getIM()->hasVideo() );
783
784     if( intf_ShouldDie( p_intf ) )
785     {
786         QApplication::closeAllWindows();
787         QApplication::quit();
788     }
789     if( need_components_update )
790     {
791         doComponentsUpdate();
792         need_components_update = false;
793     }
794
795     audio_volume_t i_volume;
796     aout_VolumeGet( p_intf, &i_volume );
797     i_volume = (i_volume *  200 )/ AOUT_VOLUME_MAX ;
798     int i_gauge = ui.volumeSlider->value();
799     b_my_volume = false;
800     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
801     {
802         b_my_volume = true;
803         ui.volumeSlider->setValue( i_volume );
804         b_my_volume = false;
805     }
806 }
807
808 void MainInterface::closeEvent( QCloseEvent *e )
809 {
810     hide();
811     p_intf->b_die = VLC_TRUE;
812 }
813
814 void MainInterface::updateVolume( int sliderVolume )
815 {
816     if( !b_my_volume )
817     {
818         int i_res = sliderVolume * AOUT_VOLUME_MAX /
819                             (2*ui.volumeSlider->maximum() );
820         aout_VolumeSet( p_intf, i_res );
821     }
822 }
823
824 static int InteractCallback( vlc_object_t *p_this,
825                              const char *psz_var, vlc_value_t old_val,
826                              vlc_value_t new_val, void *param )
827 {
828     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
829     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
830     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
831     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
832     return VLC_SUCCESS;
833 }
834
835 /*****************************************************************************
836  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
837  *  We don't show the menu directly here because we don't want the
838  *  caller to block for a too long time.
839  *****************************************************************************/
840 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
841                         vlc_value_t old_val, vlc_value_t new_val, void *param )
842 {
843     intf_thread_t *p_intf = (intf_thread_t *)param;
844
845     if( p_intf->pf_show_dialog )
846     {
847         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
848                                 new_val.b_bool, 0 );
849     }
850
851     return VLC_SUCCESS;
852 }
853
854 /*****************************************************************************
855  * IntfShowCB: callback triggered by the intf-show playlist variable.
856  *****************************************************************************/
857 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
858                        vlc_value_t old_val, vlc_value_t new_val, void *param )
859 {
860     intf_thread_t *p_intf = (intf_thread_t *)param;
861     //p_intf->p_sys->b_intf_show = VLC_TRUE;
862
863     return VLC_SUCCESS;
864 }