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 );
119 connect( ui.volumeSlider, SIGNAL( valueChanged(int) ),
120 this, SLOT( updateVolume(int) ) );
122 /* Get timer updates */
123 connect( THEDP->fixed_timer, SIGNAL( timeout() ),
124 this, SLOT(updateOnTimer() ) );
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 ) ) );
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() ) );
144 connect( ui.playlistButton, SIGNAL(clicked()),
145 THEDP, SLOT( playlistDialog() ) );
147 var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
148 var_AddCallback( p_intf, "interaction", InteractCallback, this );
149 p_intf->b_interaction = VLC_TRUE;
152 MainInterface::~MainInterface()
154 writeSettings( "MainWindow" );
155 if( config_GetInt( p_intf, "qt-always-video" ) )
157 QSettings s("VideoLAN", "VLC" );
158 s.beginGroup( "MainWindow" );
159 s.setValue( "videoSize", videoSize );
162 p_intf->b_interaction = VLC_FALSE;
163 var_DelCallback( p_intf, "interaction", InteractCallback, this );
166 void MainInterface::resizeEvent( QResizeEvent *e )
168 videoSize.setHeight( e->size().height() - addSize.height() );
169 videoSize.setWidth( e->size().width() - addSize.width() );
170 p_intf->p_sys->p_video->updateGeometry() ;
173 void MainInterface::keyPressEvent( QKeyEvent *e )
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;
182 fprintf( stderr, "After modifiers %x\n", i_vlck );
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
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 );
216 fprintf( stderr, "After keys %x\n", i_vlck );
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 )
228 var_SetInteger( p_intf->p_vlc, "key-pressed", i_vlck );
235 void MainInterface::stop()
237 playlist_Stop( THEPL );
239 void MainInterface::play()
241 if( !THEPL->i_size || !THEPL->i_enabled )
243 /* The playlist is empty, open a file requester */
248 THEMIM->togglePlayPause();
250 void MainInterface::prev()
252 playlist_Prev( THEPL );
254 void MainInterface::next()
256 playlist_Next( THEPL );
259 void MainInterface::setDisplay( float pos, int time, int length )
261 char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
262 secstotimestr( psz_length, length );
263 secstotimestr( psz_time, time );
265 title.sprintf( "%s/%s", psz_time, psz_length );
266 timeLabel->setText( " "+title+" " );
269 void MainInterface::setName( QString name )
271 nameLabel->setText( " " + name+" " );
274 void MainInterface::setStatus( int status )
276 if( status == 1 ) // Playing
277 ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
279 ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
282 static bool b_my_volume;
284 void MainInterface::updateOnTimer()
288 QApplication::quit();
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();
295 if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
298 ui.volumeSlider->setValue( i_volume );
303 void MainInterface::closeEvent( QCloseEvent *e )
306 p_intf->b_die = VLC_TRUE;
309 void MainInterface::updateVolume( int sliderVolume )
313 int i_res = sliderVolume * AOUT_VOLUME_MAX /
314 (2*ui.volumeSlider->maximum() );
315 aout_VolumeSet( p_intf, i_res );
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 )
323 intf_dialog_args_t *p_arg = new intf_dialog_args_t;
324 p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
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) );