1 /*****************************************************************************
2 * main_inteface.cpp : Main interface
3 ****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
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.
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.
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. *****************************************************************************/
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>
32 #include <QPushButton>
46 static int InteractCallback( vlc_object_t *, const char *, vlc_value_t,
49 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
52 QWidget *main = new QWidget( this );
53 setCentralWidget( main );
54 setWindowTitle( QString::fromUtf8( _("VLC media player") ) );
55 ui.setupUi( centralWidget() );
57 setFocusPolicy( Qt::StrongFocus );
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" ) );
75 VolumeClickHandler *h = new VolumeClickHandler( this );
76 ui.volLowLabel->installEventFilter(h);
77 ui.volHighLabel->installEventFilter(h);
79 ui.volumeSlider->setFocusPolicy( Qt::NoFocus );
81 QVLCMenu::createMenuBar( menuBar(), p_intf );
83 timeLabel = new QLabel( 0 );
84 nameLabel = new QLabel( 0 );
85 statusBar()->addWidget( nameLabel, 4 );
86 statusBar()->addPermanentWidget( timeLabel, 1 );
88 resize ( PREF_W, PREF_H );
89 if( config_GetInt( p_intf, "embedded" ) )
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" ) )
95 QSettings settings( "VideoLAN", "VLC" );
96 settings.beginGroup( "MainWindow" );
97 videoSize = settings.value( "videoSize", QSize( 200, 200 ) ).
101 videoSize = QSize( 1,1 );
102 videoWidget->resize( videoSize );
103 ui.vboxLayout->insertWidget( 0, videoWidget );
105 readSettings( "MainWindow" );
107 addSize = QSize( ui.vboxLayout->margin() * 2, PREF_H );
108 mainSize.setWidth( videoSize.width() + addSize.width() );
109 mainSize.setHeight( videoSize.height() + addSize.height() );
113 setMinimumSize( PREF_W, addSize.height() );
115 /* Init input manager */
116 MainInputManager::getInstance( p_intf );
117 ON_TIMEOUT( updateOnTimer() );
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 ) );
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() );
138 var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
139 var_AddCallback( p_intf, "interaction", InteractCallback, this );
140 p_intf->b_interaction = VLC_TRUE;
143 MainInterface::~MainInterface()
145 writeSettings( "MainWindow" );
146 if( config_GetInt( p_intf, "qt-always-video" ) )
148 QSettings s("VideoLAN", "VLC" );
149 s.beginGroup( "MainWindow" );
150 s.setValue( "videoSize", videoSize );
153 p_intf->b_interaction = VLC_FALSE;
154 var_DelCallback( p_intf, "interaction", InteractCallback, this );
157 void MainInterface::resizeEvent( QResizeEvent *e )
159 videoSize.setHeight( e->size().height() - addSize.height() );
160 videoSize.setWidth( e->size().width() - addSize.width() );
161 p_intf->p_sys->p_video->updateGeometry() ;
164 void MainInterface::keyPressEvent( QKeyEvent *e )
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;
173 fprintf( stderr, "After modifiers %x\n", i_vlck );
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
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 );
207 fprintf( stderr, "After keys %x\n", i_vlck );
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 )
219 var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
226 void MainInterface::stop()
228 playlist_Stop( THEPL );
230 void MainInterface::play()
232 if( !THEPL->i_size || !THEPL->i_enabled )
234 /* The playlist is empty, open a file requester */
239 THEMIM->togglePlayPause();
241 void MainInterface::prev()
243 playlist_Prev( THEPL );
245 void MainInterface::next()
247 playlist_Next( THEPL );
250 void MainInterface::setDisplay( float pos, int time, int length )
252 char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
253 secstotimestr( psz_length, length );
254 secstotimestr( psz_time, time );
256 title.sprintf( "%s/%s", psz_time, psz_length );
257 timeLabel->setText( " "+title+" " );
260 void MainInterface::setName( QString name )
262 nameLabel->setText( " " + name+" " );
265 void MainInterface::setStatus( int status )
267 if( status == 1 ) // Playing
268 ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
270 ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
273 static bool b_my_volume;
275 void MainInterface::updateOnTimer()
279 QApplication::quit();
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();
286 if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
289 ui.volumeSlider->setValue( i_volume );
294 void MainInterface::closeEvent( QCloseEvent *e )
297 p_intf->b_die = VLC_TRUE;
300 void MainInterface::updateVolume( int sliderVolume )
304 int i_res = sliderVolume * AOUT_VOLUME_MAX /
305 (2*ui.volumeSlider->maximum() );
306 aout_VolumeSet( p_intf, i_res );
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 )
314 intf_dialog_args_t *p_arg = new intf_dialog_args_t;
315 p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
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) );