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