]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/controller_widget.cpp
Qt: kill warnings and probably a crash on Win32
[vlc] / modules / gui / qt4 / components / controller_widget.cpp
1 /*****************************************************************************
2  * Controller_widget.cpp : Controller Widget for the controllers
3  ****************************************************************************
4  * Copyright (C) 2006-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Baptiste Kempf <jb@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
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "controller_widget.hpp"
29 #include "controller.hpp"
30
31 #include "input_manager.hpp"         /* Get notification of Volume Change */
32 #include "util/input_slider.hpp"     /* SoundSlider */
33
34 #include <vlc_aout.h>                /* Volume functions */
35
36 #include <QLabel>
37 #include <QHBoxLayout>
38 #include <QMenu>
39 #include <QWidgetAction>
40 #include <QMouseEvent>
41
42 SoundWidget::SoundWidget( QWidget *_parent, intf_thread_t * _p_intf,
43                           bool b_shiny, bool b_special )
44                          : QWidget( _parent ), p_intf( _p_intf),
45                            b_is_muted( false ), b_ignore_valuechanged( false )
46 {
47     /* We need a layout for this widget */
48     QHBoxLayout *layout = new QHBoxLayout( this );
49     layout->setSpacing( 0 ); layout->setMargin( 0 );
50
51     /* We need a Label for the pix */
52     volMuteLabel = new QLabel;
53     volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-medium" ) );
54
55     /* We might need a subLayout too */
56     QVBoxLayout *subLayout;
57
58     volMuteLabel->installEventFilter( this );
59
60     /* Normal View, click on icon mutes */
61     if( !b_special )
62     {
63         volumeMenu = NULL; subLayout = NULL;
64         volumeControlWidget = NULL;
65
66         /* And add the label */
67         layout->addWidget( volMuteLabel, 0, Qt::AlignBottom );
68     }
69     else
70     {
71         /* Special view, click on button shows the slider */
72         b_shiny = false;
73
74         volumeControlWidget = new QFrame;
75         subLayout = new QVBoxLayout( volumeControlWidget );
76         subLayout->setContentsMargins( 4, 4, 4, 4 );
77         volumeMenu = new QMenu( this );
78
79         QWidgetAction *widgetAction = new QWidgetAction( volumeControlWidget );
80         widgetAction->setDefaultWidget( volumeControlWidget );
81         volumeMenu->addAction( widgetAction );
82
83         /* And add the label */
84         layout->addWidget( volMuteLabel );
85     }
86
87     /* Slider creation: shiny or clean */
88     if( b_shiny )
89     {
90         volumeSlider = new SoundSlider( this,
91             config_GetInt( p_intf, "volume-step" ),
92             var_InheritBool( p_intf, "qt-volume-complete" ),
93             var_InheritString( p_intf, "qt-slider-colours" ) );
94     }
95     else
96     {
97         volumeSlider = new QSlider( NULL );
98         volumeSlider->setAttribute( Qt::WA_MacSmallSize);
99         volumeSlider->setOrientation( b_special ? Qt::Vertical
100                                                 : Qt::Horizontal );
101         volumeSlider->setMaximum( var_InheritBool( p_intf, "qt-volume-complete" )
102                                   ? 400 : 200 );
103     }
104
105     volumeSlider->setFocusPolicy( Qt::NoFocus );
106     if( b_special )
107         subLayout->addWidget( volumeSlider );
108     else
109         layout->addWidget( volumeSlider, 0, Qt::AlignBottom  );
110
111     /* Set the volume from the config */
112     libUpdateVolume();
113     /* Force the update at build time in order to have a muted icon if needed */
114     updateMuteStatus();
115
116     /* Volume control connection */
117     volumeSlider->setTracking( true );
118     CONNECT( volumeSlider, valueChanged( int ), this, valueChangedFilter( int ) );
119     CONNECT( this, valueReallyChanged( int ), this, userUpdateVolume( int ) );
120     CONNECT( THEMIM, volumeChanged( void ), this, libUpdateVolume( void ) );
121     CONNECT( THEMIM, soundMuteChanged( void ), this, updateMuteStatus( void ) );
122 }
123
124 SoundWidget::~SoundWidget()
125 {
126     delete volumeSlider;
127     delete volumeControlWidget;
128 }
129
130 void SoundWidget::refreshLabels()
131 {
132     int i_sliderVolume = volumeSlider->value();
133
134     if( b_is_muted )
135     {
136         volMuteLabel->setPixmap( QPixmap(":/toolbar/volume-muted" ) );
137         volMuteLabel->setToolTip(qfu(vlc_pgettext("Tooltip|Unmute", "Unmute")));
138         return;
139     }
140
141     if( i_sliderVolume < VOLUME_MAX / 3 )
142         volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-low" ) );
143     else if( i_sliderVolume > (VOLUME_MAX * 2 / 3 ) )
144         volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-high" ) );
145     else volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-medium" ) );
146     volMuteLabel->setToolTip( qfu(vlc_pgettext("Tooltip|Mute", "Mute")) );
147 }
148
149 /* volumeSlider changed value event slot */
150 void SoundWidget::userUpdateVolume( int i_sliderVolume )
151 {
152     /* Only if volume is set by user action on slider */
153     setMuted( false );
154     playlist_t *p_playlist = pl_Get( p_intf );
155     int i_res = i_sliderVolume  * (AOUT_VOLUME_MAX / 2) / VOLUME_MAX;
156     aout_VolumeSet( p_playlist, i_res );
157     refreshLabels();
158 }
159
160 /* libvlc changed value event slot */
161 void SoundWidget::libUpdateVolume()
162 {
163     /* Audio part */
164     audio_volume_t i_volume;
165     playlist_t *p_playlist = pl_Get( p_intf );
166
167     i_volume = aout_VolumeGet( p_playlist );
168     i_volume = ( ( i_volume + 1 ) *  VOLUME_MAX )/ (AOUT_VOLUME_MAX/2);
169
170     if ( i_volume - volumeSlider->value() != 0 )
171     {
172         b_ignore_valuechanged = true;
173         volumeSlider->setValue( i_volume );
174         b_ignore_valuechanged = false;
175     }
176     refreshLabels();
177 }
178
179 void SoundWidget::valueChangedFilter( int i_val )
180 {
181     /* valueChanged is also emitted when the lib setValue() */
182     if ( !b_ignore_valuechanged ) emit valueReallyChanged( i_val );
183 }
184
185 /* libvlc mute/unmute event slot */
186 void SoundWidget::updateMuteStatus()
187 {
188     playlist_t *p_playlist = pl_Get( p_intf );
189     b_is_muted = aout_IsMuted( VLC_OBJECT(p_playlist) );
190
191     SoundSlider *soundSlider = qobject_cast<SoundSlider *>(volumeSlider);
192     if( soundSlider )
193         soundSlider->setMuted( b_is_muted );
194     refreshLabels();
195 }
196
197 void SoundWidget::showVolumeMenu( QPoint pos )
198 {
199     volumeMenu->setFixedHeight( volumeMenu->sizeHint().height() );
200     volumeMenu->exec( QCursor::pos() - pos - QPoint( 0, volumeMenu->height()/2 )
201                           + QPoint( width(), height() /2) );
202 }
203
204 void SoundWidget::setMuted( bool mute )
205 {
206     b_is_muted = mute;
207     playlist_t *p_playlist = pl_Get( p_intf );
208     aout_SetMute( VLC_OBJECT(p_playlist), NULL, mute );
209 }
210
211 bool SoundWidget::eventFilter( QObject *obj, QEvent *e )
212 {
213     VLC_UNUSED( obj );
214     if (e->type() == QEvent::MouseButtonPress  )
215     {
216         if( volumeSlider->orientation() ==  Qt::Vertical )
217         {
218             QMouseEvent *event = static_cast<QMouseEvent*>(e);
219             showVolumeMenu( event->pos() );
220         }
221         else
222         {
223             setMuted( !b_is_muted );
224         }
225         e->accept();
226         return true;
227     }
228     else
229     {
230         e->ignore();
231         return false;
232     }
233 }
234
235 /**
236  * Play Button
237  **/
238 void PlayButton::updateButtonIcons( bool b_playing )
239 {
240     setIcon( b_playing ? QIcon( ":/toolbar/pause_b" ) : QIcon( ":/toolbar/play_b" ) );
241     setToolTip( b_playing ? qtr( "Pause the playback" )
242                           : qtr( I_PLAY_TOOLTIP ) );
243 }
244
245 void AtoB_Button::updateButtonIcons( bool timeA, bool timeB )
246 {
247     if( !timeA && !timeB)
248     {
249         setIcon( QIcon( ":/toolbar/atob_nob" ) );
250         setToolTip( qtr( "Loop from point A to point B continuously\n"
251                          "Click to set point A" ) );
252     }
253     else if( timeA && !timeB )
254     {
255         setIcon( QIcon( ":/toolbar/atob_noa" ) );
256         setToolTip( qtr( "Click to set point B" ) );
257     }
258     else if( timeA && timeB )
259     {
260         setIcon( QIcon( ":/toolbar/atob" ) );
261         setToolTip( qtr( "Stop the A to B loop" ) );
262     }
263 }
264
265 void LoopButton::updateButtonIcons( int value )
266 {
267     setChecked( value != NORMAL );
268     setIcon( ( value == REPEAT_ONE ) ? QIcon( ":/buttons/playlist/repeat_one" )
269                                      : QIcon( ":/buttons/playlist/repeat_all" ) );
270 }