]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
c80ce1219042856b7dc8599115d2db54ce8ed81c
[vlc] / modules / gui / qt4 / main_interface.cpp
1 /*****************************************************************************
2  * main_inteface.cpp : Main interface
3  ****************************************************************************
4  * Copyright (C) 2006 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 #include "qt4.hpp"
24 #include "main_interface.hpp"
25 #include "input_manager.hpp"
26 #include "util/input_slider.hpp"
27 #include "util/qvlcframe.hpp"
28 #include "dialogs_provider.hpp"
29 #include "components/interface_widgets.hpp"
30 #include "dialogs/playlist.hpp"
31 #include "menus.hpp"
32
33 #include <QCloseEvent>
34 #include <QPushButton>
35 #include <QStatusBar>
36 #include <QKeyEvent>
37
38 #include <assert.h>
39 #include <vlc_keys.h>
40
41 #ifdef WIN32
42     #define PREF_W 410
43     #define PREF_H 121
44 #else
45     #define PREF_W 450
46     #define PREF_H 125
47 #endif
48
49 static int InteractCallback( vlc_object_t *, const char *, vlc_value_t,
50                              vlc_value_t, void *);
51
52 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
53 {
54     settings = new QSettings( "VideoLAN", "VLC" );
55     settings->beginGroup( "MainWindow" );
56
57     setWindowTitle( QString::fromUtf8( _("VLC media player") ) );
58     handleMainUi( settings );
59
60     QVLCMenu::createMenuBar( menuBar(), p_intf );
61     /* Status bar */
62     timeLabel = new QLabel( 0 );
63     nameLabel = new QLabel( 0 );
64     statusBar()->addWidget( nameLabel, 4 );
65     statusBar()->addPermanentWidget( timeLabel, 1 );
66
67     setFocusPolicy( Qt::StrongFocus );
68
69     /* Init input manager */
70     MainInputManager::getInstance( p_intf );
71     ON_TIMEOUT( updateOnTimer() );
72
73     /* Volume control */
74     CONNECT( ui.volumeSlider, valueChanged(int), this, updateVolume(int) );
75     /* Connect the input manager to the GUI elements it manages */
76     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
77              slider, setPosition( float,int, int ) );
78     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
79              this, setDisplay( float, int, int ) );
80     CONNECT( THEMIM->getIM(), nameChanged( QString ), this,setName( QString ) );
81     CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
82     CONNECT( slider, sliderDragged( float ),
83              THEMIM->getIM(), sliderUpdate( float ) );
84
85     /* Actions */
86     BUTTONACT( ui.playButton, play() );
87     BUTTONACT( ui.stopButton, stop() );
88     BUTTONACT( ui.nextButton, next() );
89     BUTTONACT( ui.prevButton, prev() );
90     BUTTONACT( ui.playlistButton, playlist() );
91
92     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
93     var_AddCallback( p_intf, "interaction", InteractCallback, this );
94     p_intf->b_interaction = VLC_TRUE;
95 }
96
97 MainInterface::~MainInterface()
98 {
99     /// \todo Save everything
100     p_intf->b_interaction = VLC_FALSE;
101     var_DelCallback( p_intf, "interaction", InteractCallback, this );
102 }
103
104 void MainInterface::handleMainUi( QSettings *settings )
105 {
106     QWidget *main = new QWidget( this );
107     setCentralWidget( main );
108     ui.setupUi( centralWidget() );
109
110     slider = new InputSlider( Qt::Horizontal, NULL );
111     ui.hboxLayout->insertWidget( 0, slider );
112
113     ui.prevButton->setText( "" );
114     ui.nextButton->setText( "" );
115     ui.playButton->setText( "" );
116     ui.stopButton->setText( "" );
117     ui.prevButton->setIcon( QIcon( ":/pixmaps/previous.png" ) );
118     ui.nextButton->setIcon( QIcon( ":/pixmaps/next.png" ) );
119     ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
120     ui.stopButton->setIcon( QIcon( ":/pixmaps/stop.png" ) );
121
122     /* Volume */
123     ui.volLowLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
124     ui.volHighLabel->setPixmap( QPixmap( ":/pixmaps/volume-high.png" ) );
125     ui.volumeSlider->setMaximum( 100 );
126
127     VolumeClickHandler *h = new VolumeClickHandler( this );
128     ui.volLowLabel->installEventFilter(h);
129     ui.volHighLabel->installEventFilter(h);
130     ui.volumeSlider->setFocusPolicy( Qt::NoFocus );
131
132     ui.playlistButton->setText( "" );
133     ui.playlistButton->setIcon( QIcon( ":/pixmaps/volume-low.png" ) );
134
135     /* Fetch configuration from settings and vlc config */
136     videoEmbeddedFlag = false;
137     if( config_GetInt( p_intf, "embedded-video" ) )
138         videoEmbeddedFlag = true;
139
140     playlistEmbeddedFlag = true;
141     /// \todo fetch playlist settings
142
143     resize ( PREF_W, PREF_H );
144
145     if( videoEmbeddedFlag )
146     {
147         videoWidget = new VideoWidget( p_intf );
148         videoWidget->widgetSize = QSize( 1, 1 );
149         videoWidget->resize( videoWidget->widgetSize );
150         ui.vboxLayout->insertWidget( 0, videoWidget );
151
152         if( config_GetInt( p_intf, "qt-always-video" ))
153         {
154             bgWidget = new BackgroundWidget( p_intf );
155             bgWidget->widgetSize = settings->value( "backgroundSize",
156                                                 QSize( 200, 200 ) ).toSize();
157             ui.vboxLayout->insertWidget( 0, bgWidget ); 
158             bgWidget->hide();
159         }
160     }
161
162     // Size for fixed elements
163     addSize = QSize( ui.vboxLayout->margin() * 2, PREF_H );
164
165     calculateInterfaceSize();
166     resize( mainSize );
167     /// \bug still needed ?
168     mainSize = size();
169
170     setMinimumSize( PREF_W, addSize.height() );
171 }
172
173 void MainInterface::calculateInterfaceSize()
174 {
175     int width = 0, height = 0;
176     if( bgWidget->isVisible() )
177     {
178         width += bgWidget->widgetSize.width();
179         height += bgWidget->widgetSize.height();
180         assert( !playlistWidget->isVisible() );
181
182     }
183     if( playlistWidget->isVisible() )
184     {
185         width += playlistWidget->widgetSize.width();
186         height += playlistWidget->widgetSize.height();
187     }
188     mainSize.setWidth( width + videoWidget->widgetSize.width() + 
189                                                addSize.width() );
190     mainSize.setHeight( height + videoWidget->widgetSize.height() +
191                                                  addSize.height() );
192 }
193
194 void MainInterface::resizeEvent( QResizeEvent *e )
195 {
196     videoWidget->widgetSize.setHeight( e->size().height() - addSize.height() );
197     videoWidget->widgetSize.setWidth( e->size().width() - addSize.width() );
198     p_intf->p_sys->p_video->updateGeometry() ;
199 }
200
201 void MainInterface::keyPressEvent( QKeyEvent *e )
202 {
203     int i_vlck = 0;
204     /* Handle modifiers */
205     if( e->modifiers()& Qt::ShiftModifier ) i_vlck |= KEY_MODIFIER_SHIFT;
206     if( e->modifiers()& Qt::AltModifier ) i_vlck |= KEY_MODIFIER_ALT;
207     if( e->modifiers()& Qt::ControlModifier ) i_vlck |= KEY_MODIFIER_CTRL;
208     if( e->modifiers()& Qt::MetaModifier ) i_vlck |= KEY_MODIFIER_META;
209
210     bool found = false;
211     /* Look for some special keys */
212 #define HANDLE( qt, vk ) case Qt::qt : i_vlck |= vk; found = true;break
213     switch( e->key() )
214     {
215         HANDLE( Key_Left, KEY_LEFT );
216         HANDLE( Key_Right, KEY_RIGHT );
217         HANDLE( Key_Up, KEY_UP );
218         HANDLE( Key_Down, KEY_DOWN );
219         HANDLE( Key_Space, KEY_SPACE );
220         HANDLE( Key_Escape, KEY_ESC );
221         HANDLE( Key_Enter, KEY_ENTER );
222         HANDLE( Key_F1, KEY_F1 );
223         HANDLE( Key_F2, KEY_F2 );
224         HANDLE( Key_F3, KEY_F3 );
225         HANDLE( Key_F4, KEY_F4 );
226         HANDLE( Key_F5, KEY_F5 );
227         HANDLE( Key_F6, KEY_F6 );
228         HANDLE( Key_F7, KEY_F7 );
229         HANDLE( Key_F8, KEY_F8 );
230         HANDLE( Key_F9, KEY_F9 );
231         HANDLE( Key_F10, KEY_F10 );
232         HANDLE( Key_F11, KEY_F11 );
233         HANDLE( Key_F12, KEY_F12 );
234         HANDLE( Key_PageUp, KEY_PAGEUP );
235         HANDLE( Key_PageDown, KEY_PAGEDOWN );
236         HANDLE( Key_Home, KEY_HOME );
237         HANDLE( Key_End, KEY_END );
238         HANDLE( Key_Insert, KEY_INSERT );
239         HANDLE( Key_Delete, KEY_DELETE );
240
241     }
242     if( !found )
243     {
244         /* Force lowercase */
245         if( e->key() >= Qt::Key_A && e->key() <= Qt::Key_Z )
246             i_vlck += e->key() + 32;
247         /* Rest of the ascii range */
248         else if( e->key() >= Qt::Key_Space && e->key() <= Qt::Key_AsciiTilde )
249             i_vlck += e->key();
250     }
251     if( i_vlck >= 0 )
252     {
253         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
254         e->accept();
255     }
256     else
257         e->ignore();
258 }
259
260 void MainInterface::stop()
261 {
262     playlist_Stop( THEPL );
263 }
264 void MainInterface::play()
265 {
266     if( !THEPL->i_size || !THEPL->i_enabled )
267     {
268         /* The playlist is empty, open a file requester */
269         THEDP->openDialog();
270         setStatus( 0 );
271         return;
272     }
273     THEMIM->togglePlayPause();
274 }
275 void MainInterface::prev()
276 {
277     playlist_Prev( THEPL );
278 }
279 void MainInterface::next()
280 {
281     playlist_Next( THEPL );
282 }
283
284 void MainInterface::playlist()
285 {
286     // Toggle the playlist dialog
287     if( !playlistEmbeddedFlag )
288     {
289         if( playlistWidget )
290         {
291             /// \todo Destroy it 
292
293         }
294         THEDP->playlistDialog();
295         return;
296     }
297
298     if( !playlistWidget )
299     {
300         PlaylistDialog::killInstance();
301         playlistWidget = new PlaylistWidget( p_intf );
302         ui.vboxLayout->insertWidget( 0, playlistWidget );
303         playlistWidget->widgetSize = settings->value( "playlistSize",
304                                                QSize( 600, 300 ) ).toSize();
305     }
306     /// Todo, reset its size ?
307     if( playlistWidget->isVisible() ) playlistWidget->show();
308     else playlistWidget->hide();
309
310     calculateInterfaceSize();
311     resize( mainSize );
312 }
313
314 void MainInterface::setDisplay( float pos, int time, int length )
315 {
316     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
317     secstotimestr( psz_length, length );
318     secstotimestr( psz_time, time );
319     QString title;
320     title.sprintf( "%s/%s", psz_time, psz_length );
321     timeLabel->setText( " "+title+" " );
322 }
323
324 void MainInterface::setName( QString name )
325 {
326     nameLabel->setText( " " + name+" " );
327 }
328
329 void MainInterface::setStatus( int status )
330 {
331     if( status == 1 ) // Playing
332         ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
333     else
334         ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
335 }
336
337 static bool b_my_volume;
338
339 void MainInterface::updateOnTimer()
340 {
341     if( p_intf->b_die )
342     {
343         QApplication::closeAllWindows();
344         DialogsProvider::killInstance();
345         QApplication::quit();
346     }
347     audio_volume_t i_volume;
348     aout_VolumeGet( p_intf, &i_volume );
349     i_volume = (i_volume *  200 )/ AOUT_VOLUME_MAX ;
350     int i_gauge = ui.volumeSlider->value();
351     b_my_volume = false;
352     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
353     {
354         b_my_volume = true;
355         ui.volumeSlider->setValue( i_volume );
356         b_my_volume = false;
357     }
358 }
359
360 void MainInterface::closeEvent( QCloseEvent *e )
361 {
362     hide();
363     p_intf->b_die = VLC_TRUE;
364 }
365
366 void MainInterface::updateVolume( int sliderVolume )
367 {
368     if( !b_my_volume )
369     {
370         int i_res = sliderVolume * AOUT_VOLUME_MAX /
371                             (2*ui.volumeSlider->maximum() );
372         aout_VolumeSet( p_intf, i_res );
373     }
374 }
375
376 static int InteractCallback( vlc_object_t *p_this,
377                              const char *psz_var, vlc_value_t old_val,
378                              vlc_value_t new_val, void *param )
379 {
380     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
381     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
382
383     MainInterface *p_interface = (MainInterface*)param;
384     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
385     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
386     return VLC_SUCCESS;
387 }