]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
Qt4 - Cleaning trailing spaces...
[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  *          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 #include "qt4.hpp"
26 #include "main_interface.hpp"
27 #include "input_manager.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/extended.hpp"
33 #include "dialogs/playlist.hpp"
34 #include "menus.hpp"
35
36 #include <QMenuBar>
37 #include <QCloseEvent>
38 #include <QPushButton>
39 #include <QStatusBar>
40 #include <QKeyEvent>
41 #include <QUrl>
42 #include <QSystemTrayIcon>
43 #include <QSize>
44 #include <QMenu>
45 #include <QLabel>
46 #include <QSlider>
47 #include <QWidgetAction>
48 #include <QDockWidget>
49 #include <QToolBar>
50 #include <QGroupBox>
51
52 #include <assert.h>
53 #include <vlc_keys.h>
54 #include <vlc_vout.h>
55
56 #define SET_WIDTH(i,j) i->widgetSize.setWidth(j)
57 #define SET_HEIGHT(i,j) i->widgetSize.setHeight(j)
58 #define SET_WH( i,j,k) i->widgetSize.setWidth(j); i->widgetSize.setHeight(k);
59
60 #define DS(i) i.width(),i.height()
61
62 /* Callback prototypes */
63 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
64                         vlc_value_t old_val, vlc_value_t new_val, void *param );
65 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
66                        vlc_value_t old_val, vlc_value_t new_val, void *param );
67 static int InteractCallback( vlc_object_t *, const char *, vlc_value_t,
68                              vlc_value_t, void *);
69 /* Video handling */
70 static void *DoRequest( intf_thread_t *p_intf, vout_thread_t *p_vout,
71                         int *pi1, int *pi2, unsigned int*pi3,unsigned int*pi4)
72 {
73     return p_intf->p_sys->p_mi->requestVideo( p_vout, pi1, pi2, pi3, pi4 );
74 }
75 static void DoRelease( intf_thread_t *p_intf, void *p_win )
76 {
77     return p_intf->p_sys->p_mi->releaseVideo( p_win );
78 }
79 static int DoControl( intf_thread_t *p_intf, void *p_win, int i_q, va_list a )
80 {
81     return p_intf->p_sys->p_mi->controlVideo( p_win, i_q, a );
82 }
83
84 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
85 {
86     /* Variables initialisation */
87     need_components_update = false;
88     bgWidget = NULL; videoWidget = NULL; playlistWidget = NULL;
89     embeddedPlaylistWasActive = videoIsActive = false;
90     input_name = "";
91
92     /**
93      * Ask for the network policy on FIRST STARTUP
94      **/
95     if( config_GetInt( p_intf, "privacy-ask") )
96     {
97         QList<ConfigControl *> controls;
98         privacyDialog( controls );
99
100         QList<ConfigControl *>::Iterator i;
101         for(  i = controls.begin() ; i != controls.end() ; i++ )
102         {
103             ConfigControl *c = qobject_cast<ConfigControl *>(*i);
104             c->doApply( p_intf );
105         }
106
107         config_PutInt( p_intf,  "privacy-ask" , 0 );
108         config_SaveConfigFile( p_intf, NULL );
109     }
110
111     /**
112      *  Configuration and settings
113      **/
114     settings = new QSettings( "vlc", "vlc-qt-interface" );
115     settings->beginGroup( "MainWindow" );
116
117     /* Main settings */
118     setFocusPolicy( Qt::StrongFocus );
119     setAcceptDrops( true );
120     setWindowIcon( QApplication::windowIcon() );
121     setWindowOpacity( config_GetFloat( p_intf, "qt-opacity" ) );
122
123     /* Set The Video In emebedded Mode or not */
124     videoEmbeddedFlag = false;
125     if( config_GetInt( p_intf, "embedded-video" ) )
126         videoEmbeddedFlag = true;
127
128     alwaysVideoFlag = false;
129     if( videoEmbeddedFlag && config_GetInt( p_intf, "qt-always-video" ) )
130         alwaysVideoFlag = true;
131
132     /* Set the other interface settings */
133     visualSelectorEnabled = settings->value( "visual-selector", false ).toBool();
134     notificationEnabled = config_GetInt( p_intf, "qt-notification" )
135                           ? true : false;
136
137     /**************************
138      *  UI and Widgets design
139      **************************/
140     setVLCWindowsTitle();
141     handleMainUi( settings );
142
143     /* Create a Dock to get the playlist */
144     dockPL = new QDockWidget( qtr("Playlist"), this );
145     dockPL->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::MinimumExpanding );
146     dockPL->setFeatures( QDockWidget::AllDockWidgetFeatures );
147     dockPL->setAllowedAreas( Qt::LeftDockWidgetArea
148                            | Qt::RightDockWidgetArea
149                            | Qt::BottomDockWidgetArea );
150
151     /************
152      * Menu Bar
153      ************/
154     QVLCMenu::createMenuBar( this, p_intf, visualSelectorEnabled );
155
156     /****************
157      *  Status Bar  *
158      ****************/
159
160     /* Widgets Creation*/
161     b_remainingTime = false;
162     timeLabel = new TimeLabel;
163     nameLabel = new QLabel;
164     nameLabel->setTextInteractionFlags( Qt::TextSelectableByMouse
165                                       | Qt::TextSelectableByKeyboard );
166     speedLabel = new QLabel( "1.00x" );
167     speedLabel->setContextMenuPolicy ( Qt::CustomContextMenu );
168
169     /* Styling those labels */
170     timeLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
171     speedLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
172     nameLabel->setFrameStyle( QFrame::Sunken | QFrame::StyledPanel);
173
174     /* and adding those */
175     statusBar()->addWidget( nameLabel, 8 );
176     statusBar()->addPermanentWidget( speedLabel, 0 );
177     statusBar()->addPermanentWidget( timeLabel, 2 );
178
179     /* timeLabel behaviour:
180        - double clicking opens the goto time dialog
181        - right-clicking and clicking just toggle between remaining and
182          elapsed time.*/
183     CONNECT( timeLabel, timeLabelClicked(), this, toggleTimeDisplay() );
184     CONNECT( timeLabel, timeLabelDoubleClicked(), THEDP, gotoTimeDialog() );
185     CONNECT( timeLabel, timeLabelDoubleClicked(), this, toggleTimeDisplay() );
186
187     /* Speed Label behaviour:
188        - right click gives the vertical speed slider */
189     CONNECT( speedLabel, customContextMenuRequested( QPoint ),
190              this, showSpeedMenu( QPoint ) );
191
192     /**********************
193      * Systray Management *
194      **********************/
195     sysTray = NULL;
196     bool b_createSystray = false;
197     bool b_systrayAvailable = QSystemTrayIcon::isSystemTrayAvailable();
198     if( config_GetInt( p_intf, "qt-start-minimized") )
199     {
200         if( b_systrayAvailable ){
201             b_createSystray = true;
202             hide(); //FIXME BUG HERE
203         }
204         else msg_Warn( p_intf, "You can't minize if you haven't a system "
205                 "tray bar" );
206     }
207     if( config_GetInt( p_intf, "qt-system-tray") )
208         b_createSystray = true;
209
210     if( b_systrayAvailable && b_createSystray )
211             createSystray();
212
213     if( config_GetInt( p_intf, "qt-minimal-view" ) )
214         toggleMinimalView();
215
216     /* Init input manager */
217     MainInputManager::getInstance( p_intf );
218     ON_TIMEOUT( updateOnTimer() );
219     //ON_TIMEOUT( debug() );
220
221     /********************
222      * Various CONNECTs *
223      ********************/
224
225     /* Connect the input manager to the GUI elements it manages */
226     /* It is also connected to the control->slider, see the ControlsWidget */
227     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
228              this, setDisplayPosition( float, int, int ) );
229
230     CONNECT( THEMIM->getIM(), rateChanged( int ), this, setRate( int ) );
231
232     /**
233      * Connects on nameChanged()
234      */
235     /* Naming in the controller statusbar */
236     CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
237              setName( QString ) );
238     /* and in the systray */
239     if( sysTray )
240     {
241         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
242                  updateSystrayTooltipName( QString ) );
243     }
244     /* and in the title of the controller */
245     if( config_GetInt( p_intf, "qt-name-in-title" ) )
246     {
247         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
248              setVLCWindowsTitle( QString ) );
249     }
250
251     /** CONNECTS on PLAY_STATUS **/
252     /* Status on the main controller */
253     CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
254     /* and in the systray */
255     if( sysTray )
256     {
257         CONNECT( THEMIM->getIM(), statusChanged( int ), this,
258                  updateSystrayTooltipStatus( int ) );
259     }
260
261     /**
262      * Callbacks
263      **/
264     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
265     var_AddCallback( p_intf, "interaction", InteractCallback, this );
266     p_intf->b_interaction = VLC_TRUE;
267
268     /* Register callback for the intf-popupmenu variable */
269     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
270                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
271     if( p_playlist != NULL )
272     {
273         var_AddCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
274         var_AddCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
275         vlc_object_release( p_playlist );
276     }
277
278     CONNECT( this, askReleaseVideo( void * ), this, releaseVideoSlot( void * ) );
279
280     CONNECT( dockPL, topLevelChanged( bool ), this, doComponentsUpdate() );
281     // DEBUG FIXME
282     hide();
283
284
285     updateGeometry();
286     settings->endGroup();
287 }
288
289 MainInterface::~MainInterface()
290 {
291     if( playlistWidget ) playlistWidget->savingSettings( settings );
292     if( ExtendedDialog::exists() )
293         ExtendedDialog::getInstance( p_intf )->savingSettings();
294
295     settings->beginGroup( "MainWindow" );
296     settings->setValue( "playlist-floats", dockPL->isFloating() );
297     settings->setValue( "adv-controls", getControlsVisibilityStatus() & CONTROLS_ADVANCED );
298     settings->setValue( "pos", pos() );
299
300     settings->endGroup();
301     delete settings;
302
303     /* Unregister callback for the intf-popupmenu variable */
304     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
305                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
306     if( p_playlist != NULL )
307     {
308         var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
309         var_DelCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
310         vlc_object_release( p_playlist );
311     }
312
313     p_intf->b_interaction = VLC_FALSE;
314     var_DelCallback( p_intf, "interaction", InteractCallback, this );
315
316     p_intf->pf_request_window = NULL;
317     p_intf->pf_release_window = NULL;
318     p_intf->pf_control_window = NULL;
319 }
320
321 /*****************************
322  *   Main UI handling        *
323  *****************************/
324
325 /**
326  * Give the decorations of the Main Window a correct Name.
327  * If nothing is given, set it to VLC...
328  **/
329 void MainInterface::setVLCWindowsTitle( QString aTitle )
330 {
331     if( aTitle.isEmpty() )
332     {
333         setWindowTitle( qtr( "VLC media player" ) );
334     }
335     else
336     {
337         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
338     }
339 }
340
341 void MainInterface::handleMainUi( QSettings *settings )
342 {
343     /* Create the main Widget and the mainLayout */
344     QWidget *main = new QWidget( this );
345     mainLayout = new QVBoxLayout( main );
346     setCentralWidget( main );
347
348     /* Margins, spacing */
349     main->setContentsMargins( 0, 0, 0, 0 );
350     mainLayout->setMargin( 0 );
351
352     /* Create the CONTROLS Widget */
353     bool b_shiny = config_GetInt( p_intf, "qt-blingbling" );
354     controls = new ControlsWidget( p_intf,
355                    settings->value( "adv-controls", false ).toBool(),
356                    b_shiny );
357
358     /* Configure the Controls, the playlist button doesn't trigger THEDP
359        but the toggle from this MainInterface */
360     BUTTONACT( controls->playlistButton, togglePlaylist() );
361
362     /* Add the controls Widget to the main Widget */
363     mainLayout->addWidget( controls );
364
365     /* Create the Speed Control Widget */
366     speedControl = new SpeedControlWidget( p_intf );
367     speedControlMenu = new QMenu( this );
368     QWidgetAction *widgetAction = new QWidgetAction( this );
369     widgetAction->setDefaultWidget( speedControl );
370     speedControlMenu->addAction( widgetAction );
371
372     /* Visualisation */
373     /* Disabled for now, they SUCK */
374     #if 0
375     visualSelector = new VisualSelector( p_intf );
376     mainLayout->insertWidget( 0, visualSelector );
377     visualSelector->hide();
378     #endif
379
380     /* And video Outputs */
381     if( alwaysVideoFlag )
382     {
383         bgWidget = new BackgroundWidget( p_intf );
384         bgWidget->widgetSize = settings->value( "backgroundSize",
385                                            QSize( 300, 300 ) ).toSize();
386         bgWidget->resize( bgWidget->widgetSize );
387         bgWidget->updateGeometry();
388         mainLayout->insertWidget( 0, bgWidget );
389         CONNECT( this, askBgWidgetToToggle(), bgWidget, toggle() );
390     }
391
392     if( videoEmbeddedFlag )
393     {
394         videoWidget = new VideoWidget( p_intf );
395         videoWidget->widgetSize = QSize( 1, 1 );
396         //videoWidget->resize( videoWidget->widgetSize );
397         mainLayout->insertWidget( 0, videoWidget );
398
399         p_intf->pf_request_window  = ::DoRequest;
400         p_intf->pf_release_window  = ::DoRelease;
401         p_intf->pf_control_window  = ::DoControl;
402     }
403
404     /* Finish the sizing */
405     updateGeometry();
406 }
407
408 void MainInterface::privacyDialog( QList<ConfigControl *> controls )
409 {
410     QDialog *privacy = new QDialog( this );
411
412     privacy->setWindowTitle( qtr( "Privacy and Network policies" ) );
413
414     QGridLayout *gLayout = new QGridLayout( privacy );
415
416     QGroupBox *blabla = new QGroupBox( qtr( "Privacy and Network Warning" ) );
417     QGridLayout *blablaLayout = new QGridLayout( blabla );
418     QLabel *text = new QLabel( qtr(
419         "<p>The <i>VideoLAN Team</i> doesn't like when an application goes "
420         "online without authorisation.</p>\n "
421         "<p><i>VLC media player</i> can request limited information on "
422         "Internet, espically to get CD Covers and songs metadata or to know "
423         "if updates are available.</p>\n"
424         "<p><i>VLC media player</i> <b>DOES NOT</b> send or collect <b>ANY</b> "
425         "information, even anonymously about your "
426         "usage.</p>\n"
427         "<p>Therefore please check the following options, the default being "
428         "almost no access on the web.</p>\n") );
429     text->setWordWrap( true );
430     text->setTextFormat( Qt::RichText );
431
432     blablaLayout->addWidget( text, 0, 0 ) ;
433
434     QGroupBox *options = new QGroupBox;
435     QGridLayout *optionsLayout = new QGridLayout( options );
436
437     gLayout->addWidget( blabla, 0, 0, 1, 3 );
438     gLayout->addWidget( options, 1, 0, 1, 3 );
439     module_config_t *p_config;
440     ConfigControl *control;
441     int line = 0;
442
443 #define CONFIG_GENERIC( option, type )                            \
444     p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
445     if( p_config )                                                \
446     {                                                             \
447         control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
448                 p_config, options, false, optionsLayout, line );  \
449         controls.append( control );                               \
450     }
451
452 #define CONFIG_GENERIC_NOBOOL( option, type )                     \
453     p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
454     if( p_config )                                                \
455     {                                                             \
456         control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
457                 p_config, options, optionsLayout, line );  \
458         controls.append( control );                               \
459     }
460
461     CONFIG_GENERIC( "album-art", IntegerList ); line++;
462     CONFIG_GENERIC_NOBOOL( "fetch-meta", Bool ); line++;
463     CONFIG_GENERIC_NOBOOL( "qt-updates-notif", Bool );
464
465     QPushButton *ok = new QPushButton( qtr( "Ok" ) );
466
467     gLayout->addWidget( ok, 2, 2 );
468
469     CONNECT( ok, clicked(), privacy, accept() );
470     privacy->exec();
471 }
472
473 //FIXME remove me at the end...
474 void MainInterface::debug()
475 {
476     msg_Dbg( p_intf, "size: %i - %i", controls->size().height(), controls->size().width() );
477     msg_Dbg( p_intf, "sizeHint: %i - %i", controls->sizeHint().height(), controls->sizeHint().width() );
478 }
479
480 /**********************************************************************
481  * Handling of sizing of the components
482  **********************************************************************/
483
484 /* This function is probably wrong, but we don't have many many choices...
485    Since we can't know from the playlist Widget if we are inside a dock or not,
486    because the playlist Widget can be called by THEDP, as a separate windows for
487    the skins.
488    Maybe the other solution is to redefine the sizeHint() of the playlist and
489    ask _parent->isFloating()...
490    If you think this would be better, please FIX it...
491 */
492 QSize MainInterface::sizeHint() const
493 {
494     QSize tempSize = controls->sizeHint() +
495         QSize( 100, menuBar()->size().height() + statusBar()->size().height() );
496
497     if( VISIBLE( bgWidget ) )
498         tempSize += bgWidget->sizeHint();
499     else if( videoIsActive )
500         tempSize += videoWidget->size();
501
502     if( !dockPL->isFloating() && dockPL->widget() )
503         tempSize += dockPL->widget()->size();
504
505     return tempSize;
506 }
507
508 #if 0
509 /* This is dead code and need to be removed AT THE END */
510 void MainInterface::resizeEvent( QResizeEvent *e )
511 {
512     if( videoWidget )
513         videoWidget->widgetSize.setWidth( e->size().width() - addSize.width() );
514     if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
515     {
516         SET_WH( videoWidget, e->size().width() - addSize.width(),
517                              e->size().height()  - addSize.height() );
518         videoWidget->updateGeometry();
519     }
520     if( VISIBLE( playlistWidget ) )
521     {
522         //FIXME
523 //        SET_WH( playlistWidget , e->size().width() - addSize.width(),
524               //                   e->size().height() - addSize.height() );
525         playlistWidget->updateGeometry();
526     }
527 }
528 #endif
529
530 /****************************************************************************
531  * Small right-click menu for rate control
532  ****************************************************************************/
533 void MainInterface::showSpeedMenu( QPoint pos )
534 {
535     speedControlMenu->exec( QCursor::pos() - pos
536             + QPoint( 0, speedLabel->height() ) );
537 }
538
539 /****************************************************************************
540  * Video Handling
541  ****************************************************************************/
542 class SetVideoOnTopQtEvent : public QEvent
543 {
544 public:
545     SetVideoOnTopQtEvent( bool _onTop ) :
546       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
547     {}
548
549     bool OnTop() const
550     {
551         return onTop;
552     }
553
554 private:
555     bool onTop;
556 };
557
558 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
559                                    int *pi_y, unsigned int *pi_width,
560                                    unsigned int *pi_height )
561 {
562     void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
563     if( ret )
564     {
565         videoIsActive = true;
566         bool bgWasVisible = false;
567
568         /* Did we have a bg ? */
569         if( VISIBLE( bgWidget) )
570         {
571             bgWasVisible = true;
572             emit askBgWidgetToToggle();
573         }
574
575         if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
576         {
577             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
578         }
579         else /* Background widget available, use its size */
580         {
581             /* Ok, our visualizations are bad, so don't do this for the moment
582              * use the requested size anyway */
583             // videoWidget->widgetSize = bgWidget->widgeTSize;
584             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
585         }
586         videoWidget->updateGeometry(); // Needed for deinterlace
587         updateGeometry();
588     }
589     return ret;
590 }
591
592 void MainInterface::releaseVideo( void *p_win )
593 {
594     emit askReleaseVideo( p_win );
595 }
596
597 void MainInterface::releaseVideoSlot( void *p_win )
598 {
599     videoWidget->release( p_win );
600     videoWidget->hide();
601
602     if( bgWidget )
603         bgWidget->show();
604
605     videoIsActive = false;
606     updateGeometry();
607 }
608
609 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
610 {
611     int i_ret = VLC_EGENERIC;
612     switch( i_query )
613     {
614         case VOUT_GET_SIZE:
615         {
616             unsigned int *pi_width  = va_arg( args, unsigned int * );
617             unsigned int *pi_height = va_arg( args, unsigned int * );
618             *pi_width = videoWidget->widgetSize.width();
619             *pi_height = videoWidget->widgetSize.height();
620             i_ret = VLC_SUCCESS;
621             break;
622         }
623         case VOUT_SET_SIZE:
624         {
625             unsigned int i_width  = va_arg( args, unsigned int );
626             unsigned int i_height = va_arg( args, unsigned int );
627             videoWidget->widgetSize = QSize( i_width, i_height );
628             videoWidget->updateGeometry();
629             updateGeometry();
630             i_ret = VLC_SUCCESS;
631             break;
632         }
633         case VOUT_SET_STAY_ON_TOP:
634         {
635             int i_arg = va_arg( args, int );
636             QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
637             i_ret = VLC_SUCCESS;
638             break;
639         }
640         default:
641             msg_Warn( p_intf, "unsupported control query" );
642             break;
643     }
644     return i_ret;
645 }
646
647 /*****************************************************************************
648  * Playlist, Visualisation and Menus handling
649  *****************************************************************************/
650 /**
651  * Toggle the playlist widget or dialog
652  **/
653 void MainInterface::togglePlaylist()
654 {
655     /* CREATION
656     If no playlist exist, then create one and attach it to the DockPL*/
657     if( !playlistWidget )
658     {
659         msg_Dbg( p_intf, "Creating a new playlist" );
660         playlistWidget = new PlaylistWidget( p_intf, settings );
661         if( bgWidget )
662             CONNECT( playlistWidget, artSet( QString ),
663                      bgWidget, setArt(QString) );
664
665         /* Add it to the parent DockWidget */
666         dockPL->setWidget( playlistWidget );
667
668         /* Add the dock to the main Interface */
669         addDockWidget( Qt::BottomDockWidgetArea, dockPL );
670
671         /* Make the playlist floating is requested. Default is not. */
672         if( settings->value( "playlist-floats", false ).toBool() );
673         {
674             msg_Dbg( p_intf, "we don't want it inside");
675             dockPL->setFloating( true );
676         }
677     }
678     else
679     {
680     /* toggle the visibility of the playlist */
681        TOGGLEV( dockPL );
682      //resize(sizeHint());
683     }
684 #if 0
685     doComponentsUpdate();
686 #endif
687     updateGeometry();
688 }
689
690 /* Function called from the menu to undock the playlist */
691 void MainInterface::undockPlaylist()
692 {
693     dockPL->setFloating( true );
694     updateGeometry();
695 }
696
697 void MainInterface::toggleMinimalView()
698 {
699     TOGGLEV( menuBar() );
700     TOGGLEV( controls );
701     TOGGLEV( statusBar() );
702     updateGeometry();
703 }
704
705 /* Video widget cannot do this synchronously as it runs in another thread */
706 /* Well, could it, actually ? Probably dangerous ... */
707 void MainInterface::doComponentsUpdate()
708 {
709     updateGeometry();
710 }
711
712 /* toggling advanced controls buttons */
713 void MainInterface::toggleAdvanced()
714 {
715     controls->toggleAdvanced();
716 }
717
718 /* Get the visibility status of the controls (hidden or not, advanced or not) */
719 int MainInterface::getControlsVisibilityStatus()
720 {
721     return( (controls->isVisible() ? CONTROLS_VISIBLE : CONTROLS_HIDDEN )
722                 + CONTROLS_ADVANCED * controls->b_advancedVisible );
723 }
724
725 #if 0
726 void MainInterface::visual()
727 {
728     if( !VISIBLE( visualSelector) )
729     {
730         visualSelector->show();
731         if( !THEMIM->getIM()->hasVideo() )
732         {
733             /* Show the background widget */
734         }
735         visualSelectorEnabled = true;
736     }
737     else
738     {
739         /* Stop any currently running visualization */
740         visualSelector->hide();
741         visualSelectorEnabled = false;
742     }
743     doComponentsUpdate();
744 }
745 #endif
746
747 /************************************************************************
748  * Other stuff
749  ************************************************************************/
750 void MainInterface::setDisplayPosition( float pos, int time, int length )
751 {
752     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
753     secstotimestr( psz_length, length );
754     secstotimestr( psz_time, ( b_remainingTime && length ) ? length - time
755                                                            : time );
756
757     QString title;
758     title.sprintf( "%s/%s", psz_time,
759                             ( !length && time ) ? "--:--" : psz_length );
760
761     /* Add a minus to remaining time*/
762     if( b_remainingTime && length ) timeLabel->setText( " -"+title+" " );
763     else timeLabel->setText( " "+title+" " );
764 }
765
766 void MainInterface::toggleTimeDisplay()
767 {
768     b_remainingTime = ( b_remainingTime ? false : true );
769 }
770
771 void MainInterface::setName( QString name )
772 {
773     input_name = name; /* store it for the QSystray use */
774     /* Display it in the status bar, but also as a Tooltip in case it doesn't
775        fit in the label */
776     nameLabel->setText( " " + name + " " );
777     nameLabel->setToolTip( " " + name +" " );
778 }
779
780 void MainInterface::setStatus( int status )
781 {
782     /* Forward the status to the controls to toggle Play/Pause */
783     controls->setStatus( status );
784     /* And in the systray for the menu */
785     if( sysTray )
786         QVLCMenu::updateSystrayMenu( this, p_intf );
787 }
788
789 void MainInterface::setRate( int rate )
790 {
791     QString str;
792     str.setNum( ( 1000 / (double)rate), 'f', 2 );
793     str.append( "x" );
794     speedLabel->setText( str );
795     speedControl->updateControls( rate );
796 }
797
798 void MainInterface::updateOnTimer()
799 {
800     /* \todo Make this event-driven */
801     if( intf_ShouldDie( p_intf ) )
802     {
803         QApplication::closeAllWindows();
804         QApplication::quit();
805     }
806 #if 0
807     if( need_components_update )
808     {
809         doComponentsUpdate();
810         need_components_update = false;
811     }
812 #endif
813
814     controls->updateOnTimer();
815 }
816
817 /*****************************************************************************
818  * Systray Icon and Systray Menu
819  *****************************************************************************/
820
821 /**
822  * Create a SystemTray icon and a menu that would go with it.
823  * Connects to a click handler on the icon.
824  **/
825 void MainInterface::createSystray()
826 {
827     QIcon iconVLC =  QIcon( QPixmap( ":/vlc128.png" ) );
828     sysTray = new QSystemTrayIcon( iconVLC, this );
829     sysTray->setToolTip( qtr( "VLC media player" ));
830
831     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
832     systrayMenu->setIcon( iconVLC );
833
834     QVLCMenu::updateSystrayMenu( this, p_intf, true );
835     sysTray->show();
836
837     CONNECT( sysTray, activated( QSystemTrayIcon::ActivationReason ),
838             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
839 }
840
841 /**
842  * Updates the Systray Icon's menu and toggle the main interface
843  */
844 void MainInterface::toggleUpdateSystrayMenu()
845 {
846     if( isHidden() )
847     {
848         show();
849         activateWindow();
850     }
851     else if( isMinimized() )
852     {
853         showNormal();
854         activateWindow();
855     }
856     else
857     {
858 #ifdef WIN32
859         /* check if any visible window is above vlc in the z-order,
860          * but ignore the ones always on top */
861         WINDOWINFO wi;
862         HWND hwnd;
863         wi.cbSize = sizeof( WINDOWINFO );
864         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
865                 hwnd && !IsWindowVisible( hwnd );
866                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
867         if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
868                 (wi.dwExStyle&WS_EX_TOPMOST) )
869 #else
870         if( isActiveWindow() )
871 #endif
872         {
873             hide();
874         }
875         else
876         {
877             activateWindow();
878         }
879     }
880     QVLCMenu::updateSystrayMenu( this, p_intf );
881 }
882
883 void MainInterface::handleSystrayClick(
884                                     QSystemTrayIcon::ActivationReason reason )
885 {
886     switch( reason )
887     {
888         case QSystemTrayIcon::Trigger:
889             toggleUpdateSystrayMenu();
890             break;
891         case QSystemTrayIcon::MiddleClick:
892             sysTray->showMessage( qtr( "VLC media player" ),
893                     qtr( "Control menu for the player" ),
894                     QSystemTrayIcon::Information, 4000 );
895             break;
896     }
897 }
898
899 /**
900  * Updates the name of the systray Icon tooltip.
901  * Doesn't check if the systray exists, check before you call it.
902  **/
903 void MainInterface::updateSystrayTooltipName( QString name )
904 {
905     if( name.isEmpty() )
906     {
907         sysTray->setToolTip( qtr( "VLC media player" ) );
908     }
909     else
910     {
911         sysTray->setToolTip( name );
912         if( notificationEnabled && ( isHidden() || isMinimized() ) )
913         {
914             sysTray->showMessage( qtr( "VLC media player" ), name,
915                     QSystemTrayIcon::NoIcon, 4000 );
916         }
917     }
918 }
919
920 /**
921  * Updates the status of the systray Icon tooltip.
922  * Doesn't check if the systray exists, check before you call it.
923  **/
924 void MainInterface::updateSystrayTooltipStatus( int i_status )
925 {
926     switch( i_status )
927     {
928         case  0:
929             {
930                 sysTray->setToolTip( qtr( "VLC media player" ) );
931                 break;
932             }
933         case PLAYING_S:
934             {
935                 sysTray->setToolTip( input_name );
936                 break;
937             }
938         case PAUSE_S:
939             {
940                 sysTray->setToolTip( input_name + " - "
941                         + qtr( "Paused") );
942                 break;
943             }
944     }
945 }
946
947 /************************************************************************
948  * D&D Events
949  ************************************************************************/
950 void MainInterface::dropEvent(QDropEvent *event)
951 {
952      const QMimeData *mimeData = event->mimeData();
953
954      /* D&D of a subtitles file, add it on the fly */
955      if( mimeData->urls().size() == 1 )
956      {
957         if( THEMIM->getIM()->hasInput() )
958         {
959             if( input_AddSubtitles( THEMIM->getInput(),
960                                     qtu( mimeData->urls()[0].toString() ),
961                                     VLC_TRUE ) )
962             {
963                 event->acceptProposedAction();
964                 return;
965             }
966         }
967      }
968      bool first = true;
969      foreach( QUrl url, mimeData->urls() ) {
970         QString s = url.toString();
971         if( s.length() > 0 ) {
972             playlist_Add( THEPL, qtu(s), NULL,
973                           PLAYLIST_APPEND | (first ? PLAYLIST_GO:0),
974                           PLAYLIST_END, VLC_TRUE, VLC_FALSE );
975             first = false;
976         }
977      }
978      event->acceptProposedAction();
979 }
980 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
981 {
982      event->acceptProposedAction();
983 }
984 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
985 {
986      event->acceptProposedAction();
987 }
988 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
989 {
990      event->accept();
991 }
992
993 /************************************************************************
994  * Events stuff
995  ************************************************************************/
996 void MainInterface::customEvent( QEvent *event )
997 {
998 #if 0
999     if( event->type() == PLDockEvent_Type )
1000     {
1001         PlaylistDialog::killInstance();
1002         playlistEmbeddedFlag = true;
1003         menuBar()->clear();
1004         QVLCMenu::createMenuBar(this, p_intf, true, visualSelectorEnabled);
1005         togglePlaylist();
1006     }
1007 #endif
1008     /*else */
1009     if ( event->type() == SetVideoOnTopEvent_Type )
1010     {
1011         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
1012         if( p_event->OnTop() )
1013             setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
1014         else
1015             setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
1016         show(); /* necessary to apply window flags?? */
1017     }
1018 }
1019
1020 void MainInterface::keyPressEvent( QKeyEvent *e )
1021 {
1022     if( ( e->modifiers() &  Qt::ControlModifier ) && ( e->key() & Qt::Key_H )
1023           && menuBar()->isHidden() )
1024     {
1025         toggleMinimalView();
1026         e->accept();
1027     }
1028
1029     int i_vlck = qtEventToVLCKey( e );
1030     if( i_vlck > 0 )
1031     {
1032         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1033         e->accept();
1034     }
1035     else
1036         e->ignore();
1037 }
1038
1039 void MainInterface::wheelEvent( QWheelEvent *e )
1040 {
1041     int i_vlckey = qtWheelEventToVLCKey( e );
1042     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
1043     e->accept();
1044 }
1045
1046 void MainInterface::closeEvent( QCloseEvent *e )
1047 {
1048     hide();
1049     vlc_object_kill( p_intf );
1050 }
1051
1052 /*****************************************************************************
1053  * Callbacks
1054  *****************************************************************************/
1055 static int InteractCallback( vlc_object_t *p_this,
1056                              const char *psz_var, vlc_value_t old_val,
1057                              vlc_value_t new_val, void *param )
1058 {
1059     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
1060     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
1061     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
1062     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
1063     return VLC_SUCCESS;
1064 }
1065
1066 /*****************************************************************************
1067  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
1068  *  We don't show the menu directly here because we don't want the
1069  *  caller to block for a too long time.
1070  *****************************************************************************/
1071 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
1072                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1073 {
1074     intf_thread_t *p_intf = (intf_thread_t *)param;
1075
1076     if( p_intf->pf_show_dialog )
1077     {
1078         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1079                                 new_val.b_bool, 0 );
1080     }
1081
1082     return VLC_SUCCESS;
1083 }
1084
1085 /*****************************************************************************
1086  * IntfShowCB: callback triggered by the intf-show playlist variable.
1087  *****************************************************************************/
1088 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1089                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1090 {
1091     intf_thread_t *p_intf = (intf_thread_t *)param;
1092     //p_intf->p_sys->b_intf_show = VLC_TRUE;
1093
1094     return VLC_SUCCESS;
1095 }