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