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