]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
For consistency, remove references to vlc from libvlc
[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     ON_TIMEOUT( updateOnTimer() );
118
119     /* Volume control */
120     CONNECT( ui.volumeSlider, valueChanged(int), this, updateVolume(int) );
121     /* Connect the input manager to the GUI elements it manages */
122     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
123              slider, setPosition( float,int, int ) );
124     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
125              this, setDisplay( float, int, int ) );
126     CONNECT( THEMIM->getIM(), nameChanged( QString ), this,setName( QString ) );
127     CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
128     CONNECT( slider, sliderDragged( float ),
129              THEMIM->getIM(), sliderUpdate( float ) );
130
131     /* Actions */
132     BUTTONACT( ui.playButton, play() );
133     BUTTONACT( ui.stopButton, stop() );
134     BUTTONACT( ui.nextButton, next() );
135     BUTTONACT( ui.prevButton, prev() );
136     CONNECT( ui.playlistButton, clicked(), THEDP, playlistDialog() );
137
138     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
139     var_AddCallback( p_intf, "interaction", InteractCallback, this );
140     p_intf->b_interaction = VLC_TRUE;
141 }
142
143 MainInterface::~MainInterface()
144 {
145     writeSettings( "MainWindow" );
146     if( config_GetInt( p_intf, "qt-always-video" ) )
147     {
148         QSettings s("VideoLAN", "VLC" );
149         s.beginGroup( "MainWindow" );
150         s.setValue( "videoSize", videoSize );
151         s.endGroup();
152     }
153     p_intf->b_interaction = VLC_FALSE;
154     var_DelCallback( p_intf, "interaction", InteractCallback, this );
155 }
156
157 void MainInterface::resizeEvent( QResizeEvent *e )
158 {
159     videoSize.setHeight( e->size().height() - addSize.height() );
160     videoSize.setWidth( e->size().width() - addSize.width() );
161     p_intf->p_sys->p_video->updateGeometry() ;
162 }
163
164 void MainInterface::keyPressEvent( QKeyEvent *e )
165 {
166     int i_vlck = 0;
167     /* Handle modifiers */
168     if( e->modifiers()& Qt::ShiftModifier ) i_vlck |= KEY_MODIFIER_SHIFT;
169     if( e->modifiers()& Qt::AltModifier ) i_vlck |= KEY_MODIFIER_ALT;
170     if( e->modifiers()& Qt::ControlModifier ) i_vlck |= KEY_MODIFIER_CTRL;
171     if( e->modifiers()& Qt::MetaModifier ) i_vlck |= KEY_MODIFIER_META;
172
173     fprintf( stderr, "After modifiers %x\n", i_vlck );
174     bool found = false;
175     fprintf( stderr, "Qt %x\n", e->key() );
176     /* Look for some special keys */
177 #define HANDLE( qt, vk ) case Qt::qt : i_vlck |= vk; found = true;break
178     switch( e->key() )
179     {
180         HANDLE( Key_Left, KEY_LEFT );
181         HANDLE( Key_Right, KEY_RIGHT );
182         HANDLE( Key_Up, KEY_UP );
183         HANDLE( Key_Down, KEY_DOWN );
184         HANDLE( Key_Space, KEY_SPACE );
185         HANDLE( Key_Escape, KEY_ESC );
186         HANDLE( Key_Enter, KEY_ENTER );
187         HANDLE( Key_F1, KEY_F1 );
188         HANDLE( Key_F2, KEY_F2 );
189         HANDLE( Key_F3, KEY_F3 );
190         HANDLE( Key_F4, KEY_F4 );
191         HANDLE( Key_F5, KEY_F5 );
192         HANDLE( Key_F6, KEY_F6 );
193         HANDLE( Key_F7, KEY_F7 );
194         HANDLE( Key_F8, KEY_F8 );
195         HANDLE( Key_F9, KEY_F9 );
196         HANDLE( Key_F10, KEY_F10 );
197         HANDLE( Key_F11, KEY_F11 );
198         HANDLE( Key_F12, KEY_F12 );
199         HANDLE( Key_PageUp, KEY_PAGEUP );
200         HANDLE( Key_PageDown, KEY_PAGEDOWN );
201         HANDLE( Key_Home, KEY_HOME );
202         HANDLE( Key_End, KEY_END );
203         HANDLE( Key_Insert, KEY_INSERT );
204         HANDLE( Key_Delete, KEY_DELETE );
205
206     }
207     fprintf( stderr, "After keys %x\n", i_vlck );
208     if( !found )
209     {
210         /* Force lowercase */
211         if( e->key() >= Qt::Key_A && e->key() <= Qt::Key_Z )
212             i_vlck += e->key() + 32;
213         /* Rest of the ascii range */
214         else if( e->key() >= Qt::Key_Space && e->key() <= Qt::Key_AsciiTilde )
215             i_vlck += e->key();
216     }
217     if( i_vlck >= 0 )
218     {
219         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
220         e->accept();
221     }
222     else
223         e->ignore();
224 }
225
226 void MainInterface::stop()
227 {
228     playlist_Stop( THEPL );
229 }
230 void MainInterface::play()
231 {
232     if( !THEPL->i_size || !THEPL->i_enabled )
233     {
234         /* The playlist is empty, open a file requester */
235         THEDP->openDialog();
236         setStatus( 0 );
237         return;
238     }
239     THEMIM->togglePlayPause();
240 }
241 void MainInterface::prev()
242 {
243     playlist_Prev( THEPL );
244 }
245 void MainInterface::next()
246 {
247     playlist_Next( THEPL );
248 }
249
250 void MainInterface::setDisplay( float pos, int time, int length )
251 {
252     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
253     secstotimestr( psz_length, length );
254     secstotimestr( psz_time, time );
255     QString title;
256     title.sprintf( "%s/%s", psz_time, psz_length );
257     timeLabel->setText( " "+title+" " );
258 }
259
260 void MainInterface::setName( QString name )
261 {
262     nameLabel->setText( " " + name+" " );
263 }
264
265 void MainInterface::setStatus( int status )
266 {
267     if( status == 1 ) // Playing
268         ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
269     else
270         ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
271 }
272
273 static bool b_my_volume;
274
275 void MainInterface::updateOnTimer()
276 {
277     if( p_intf->b_die )
278     {
279         QApplication::quit();
280     }
281     audio_volume_t i_volume;
282     aout_VolumeGet( p_intf, &i_volume );
283     i_volume = (i_volume *  200 )/ AOUT_VOLUME_MAX ;
284     int i_gauge = ui.volumeSlider->value();
285     b_my_volume = false;
286     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
287     {
288         b_my_volume = true;
289         ui.volumeSlider->setValue( i_volume );
290         b_my_volume = false;
291     }
292 }
293
294 void MainInterface::closeEvent( QCloseEvent *e )
295 {
296     hide();
297     p_intf->b_die = VLC_TRUE;
298 }
299
300 void MainInterface::updateVolume( int sliderVolume )
301 {
302     if( !b_my_volume )
303     {
304         int i_res = sliderVolume * AOUT_VOLUME_MAX /
305                             (2*ui.volumeSlider->maximum() );
306         aout_VolumeSet( p_intf, i_res );
307     }
308 }
309
310 static int InteractCallback( vlc_object_t *p_this,
311                              const char *psz_var, vlc_value_t old_val,
312                              vlc_value_t new_val, void *param )
313 {
314     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
315     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
316
317     MainInterface *p_interface = (MainInterface*)param;
318     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
319     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
320     return VLC_SUCCESS;
321 }