]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
Add ability to put some background when there is no video
[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 "menus.hpp"
35
36 #define PREF_W 480
37 #define PREF_H 125
38
39 static int InteractCallback( vlc_object_t *, const char *, vlc_value_t,
40                              vlc_value_t, void *);
41
42 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
43 {
44     /* All UI stuff */
45     QWidget *main = new QWidget( this );
46     setCentralWidget( main );
47     setWindowTitle( QString::fromUtf8( _("VLC media player") ) );
48     ui.setupUi( centralWidget() );
49
50     slider = new InputSlider( Qt::Horizontal, NULL );
51     ui.hboxLayout->insertWidget( 0, slider );
52     ui.prevButton->setText( "" );
53     ui.nextButton->setText( "" );
54     ui.playButton->setText( "" );
55     ui.stopButton->setText( "" );
56     ui.prevButton->setIcon( QIcon( ":/pixmaps/previous.png" ) );
57     ui.nextButton->setIcon( QIcon( ":/pixmaps/next.png" ) );
58     ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
59     ui.stopButton->setIcon( QIcon( ":/pixmaps/stop.png" ) );
60     ui.volLowLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
61     ui.volHighLabel->setPixmap( QPixmap( ":/pixmaps/volume-high.png" ) );
62     ui.volumeSlider->setMaximum( 100 );
63     ui.playlistButton->setIcon( QIcon( ":/pixmaps/volume-low.png" ) );
64
65
66     VolumeClickHandler *h = new VolumeClickHandler( this );
67     ui.volLowLabel->installEventFilter(h);
68     ui.volHighLabel->installEventFilter(h);
69
70     QVLCMenu::createMenuBar( menuBar(), p_intf );
71
72     timeLabel = new QLabel( 0 );
73     nameLabel = new QLabel( 0 );
74     statusBar()->addWidget( nameLabel, 4 );
75     statusBar()->addPermanentWidget( timeLabel, 1 );
76
77     resize ( PREF_W, PREF_H );
78 //    if( config_GetInt( p_intf, "embedded" ) )
79
80     {
81         videoWidget = new VideoWidget( p_intf, config_GetInt( p_intf, "qt-always-video" ) ? true:false );
82         if( config_GetInt( p_intf, "qt-always-video" ) )
83         {
84             QSettings settings( "VideoLAN", "VLC" );
85             settings.beginGroup( "MainWindow" );
86             videoSize = settings.value( "videoSize", QSize( 200, 200 ) ).
87                                                 toSize();
88         }
89         else
90             videoSize = QSize( 1,1 );
91         videoWidget->resize( videoSize );
92         ui.vboxLayout->insertWidget( 0, videoWidget );
93     }
94     readSettings( "MainWindow" );
95
96     addSize = QSize( ui.vboxLayout->margin() * 2, PREF_H );
97 //    if( config_GetInt( p_intf, "qt-always-video" ) )
98         mainSize.setWidth( videoSize.width() + addSize.width() );
99         mainSize.setHeight( videoSize.height() + addSize.height() );
100 //    else
101 //        mainSize = QSize( PREF_W, PREF_H );
102     fprintf( stderr, "Resulting size %ix%i", mainSize.width(), mainSize.height() );
103     resize( mainSize );
104     mainSize = size();
105     fprintf( stderr, "After size %ix%i", mainSize.width(), mainSize.height() );
106
107     setMinimumSize( PREF_W, addSize.height() );
108
109     /* Init input manager */
110     MainInputManager::getInstance( p_intf );
111
112     /* Volume control */
113     connect( ui.volumeSlider, SIGNAL( valueChanged(int) ),
114              this, SLOT( updateVolume(int) ) );
115
116     /* Get timer updates */
117     connect( THEDP->fixed_timer, SIGNAL( timeout() ),
118              this, SLOT(updateOnTimer() ) );
119
120     /* Connect the input manager to the GUI elements it manages */
121     connect( THEMIM->getIM(),SIGNAL(positionUpdated( float, int, int ) ),
122              slider, SLOT( setPosition( float,int, int ) ) );
123     connect( THEMIM->getIM(), SIGNAL( positionUpdated( float, int, int ) ),
124              this, SLOT( setDisplay( float, int, int ) ) );
125     connect( THEMIM->getIM(), SIGNAL( nameChanged( QString ) ),
126              this, SLOT( setName( QString ) ) );
127     connect( THEMIM->getIM(), SIGNAL( statusChanged( int ) ),
128              this, SLOT( setStatus( int ) ) );
129     connect( slider, SIGNAL( sliderDragged( float ) ),
130              THEMIM->getIM(),SLOT( sliderUpdate( float ) ) );
131
132     /* Actions */
133     connect( ui.playButton, SIGNAL( clicked() ), this, SLOT( play() ) );
134     connect( ui.stopButton, SIGNAL( clicked() ), this, SLOT( stop() ) );
135     connect( ui.nextButton, SIGNAL( clicked() ), this, SLOT( next() ) );
136     connect( ui.prevButton, SIGNAL( clicked() ), this, SLOT( prev() ) );
137
138     connect( ui.playlistButton, SIGNAL(clicked()),
139              THEDP, SLOT( playlistDialog() ) );
140
141     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
142     var_AddCallback( p_intf, "interaction", InteractCallback, this );
143     p_intf->b_interaction = VLC_TRUE;
144 }
145
146 MainInterface::~MainInterface()
147 {
148     writeSettings( "MainWindow" );
149     if( config_GetInt( p_intf, "qt-always-video" ) )
150     {
151         QSettings s("VideoLAN", "VLC" );
152         s.beginGroup( "MainWindow" );
153         s.setValue( "videoSize", videoSize );
154         s.endGroup();
155     }
156     p_intf->b_interaction = VLC_FALSE;
157     var_DelCallback( p_intf, "interaction", InteractCallback, this );
158 }
159
160 void MainInterface::resizeEvent( QResizeEvent *e )
161 {
162     videoSize.setHeight( e->size().height() - addSize.height() );
163     videoSize.setWidth( e->size().width() - addSize.width() );
164     p_intf->p_sys->p_video->updateGeometry() ;
165 }
166
167 void MainInterface::stop()
168 {
169     playlist_Stop( THEPL );
170 }
171 void MainInterface::play()
172 {
173     if( !THEPL->i_size || !THEPL->i_enabled )
174     {
175         /* The playlist is empty, open a file requester */
176         THEDP->openDialog();
177         setStatus( 0 );
178         return;
179     }
180     THEMIM->togglePlayPause();
181 }
182 void MainInterface::prev()
183 {
184     playlist_Prev( THEPL );
185 }
186 void MainInterface::next()
187 {
188     playlist_Next( THEPL );
189 }
190
191 void MainInterface::setDisplay( float pos, int time, int length )
192 {
193     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
194     secstotimestr( psz_length, length );
195     secstotimestr( psz_time, time );
196     QString title;
197     title.sprintf( "%s/%s", psz_time, psz_length );
198     timeLabel->setText( " "+title+" " );
199 }
200
201 void MainInterface::setName( QString name )
202 {
203     nameLabel->setText( " " + name+" " );
204 }
205
206 void MainInterface::setStatus( int status )
207 {
208     if( status == 1 ) // Playing
209         ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
210     else
211         ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
212 }
213
214 static bool b_my_volume;
215
216 void MainInterface::updateOnTimer()
217 {
218     if( p_intf->b_die )
219     {
220         QApplication::quit();
221     }
222     audio_volume_t i_volume;
223     aout_VolumeGet( p_intf, &i_volume );
224     i_volume = (i_volume *  200 )/ AOUT_VOLUME_MAX ;
225     int i_gauge = ui.volumeSlider->value();
226     b_my_volume = false;
227     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
228     {
229         b_my_volume = true;
230         ui.volumeSlider->setValue( i_volume );
231         b_my_volume = false;
232     }
233 }
234
235 void MainInterface::closeEvent( QCloseEvent *e )
236 {
237     hide();
238     p_intf->b_die = VLC_TRUE;
239 }
240
241 void MainInterface::updateVolume( int sliderVolume )
242 {
243     if( !b_my_volume )
244     {
245         int i_res = sliderVolume * AOUT_VOLUME_MAX /
246                             (2*ui.volumeSlider->maximum() );
247         aout_VolumeSet( p_intf, i_res );
248     }
249 }
250
251 static int InteractCallback( vlc_object_t *p_this,
252                              const char *psz_var, vlc_value_t old_val,
253                              vlc_value_t new_val, void *param )
254 {
255     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
256     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
257
258     MainInterface *p_interface = (MainInterface*)param;
259     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
260     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
261     return VLC_SUCCESS;
262 }