]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/controller_widget.cpp
Qt4: simplify and partly fix mute tracking
[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 <math.h>
35 #include <vlc_aout_intf.h>           /* Volume functions */
36
37 #include <QLabel>
38 #include <QHBoxLayout>
39 #include <QMenu>
40 #include <QWidgetAction>
41 #include <QMouseEvent>
42
43 SoundWidget::SoundWidget( QWidget *_parent, intf_thread_t * _p_intf,
44                           bool b_shiny, bool b_special )
45                          : QWidget( _parent ), p_intf( _p_intf),
46                            b_is_muted( false ), b_ignore_valuechanged( false )
47 {
48     /* We need a layout for this widget */
49     QHBoxLayout *layout = new QHBoxLayout( this );
50     layout->setSpacing( 0 ); layout->setMargin( 0 );
51
52     /* We need a Label for the pix */
53     volMuteLabel = new QLabel;
54     volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-medium" ) );
55
56     /* We might need a subLayout too */
57     QVBoxLayout *subLayout;
58
59     volMuteLabel->installEventFilter( this );
60
61     /* Normal View, click on icon mutes */
62     if( !b_special )
63     {
64         volumeMenu = NULL; subLayout = NULL;
65         volumeControlWidget = NULL;
66
67         /* And add the label */
68         layout->addWidget( volMuteLabel, 0, Qt::AlignBottom );
69     }
70     else
71     {
72         /* Special view, click on button shows the slider */
73         b_shiny = false;
74
75         volumeControlWidget = new QFrame;
76         subLayout = new QVBoxLayout( volumeControlWidget );
77         subLayout->setContentsMargins( 4, 4, 4, 4 );
78         volumeMenu = new QMenu( this );
79
80         QWidgetAction *widgetAction = new QWidgetAction( volumeControlWidget );
81         widgetAction->setDefaultWidget( volumeControlWidget );
82         volumeMenu->addAction( widgetAction );
83
84         /* And add the label */
85         layout->addWidget( volMuteLabel );
86     }
87
88     /* Slider creation: shiny or clean */
89     if( b_shiny )
90     {
91         volumeSlider = new SoundSlider( this,
92             config_GetInt( p_intf, "volume-step" ),
93             false,
94             var_InheritString( p_intf, "qt-slider-colours" ) );
95     }
96     else
97     {
98         volumeSlider = new QSlider( NULL );
99         volumeSlider->setAttribute( Qt::WA_MacSmallSize);
100         volumeSlider->setOrientation( b_special ? Qt::Vertical
101                                                 : Qt::Horizontal );
102         volumeSlider->setMaximum( 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     /* Sync mute status */
114     if( aout_MuteGet( THEPL ) > 0 )
115         updateMuteStatus( true );
116
117     /* Volume control connection */
118     volumeSlider->setTracking( true );
119     CONNECT( volumeSlider, valueChanged( int ), this, valueChangedFilter( int ) );
120     CONNECT( this, valueReallyChanged( int ), this, userUpdateVolume( int ) );
121     CONNECT( THEMIM, volumeChanged( void ), this, libUpdateVolume( void ) );
122     CONNECT( THEMIM, soundMuteChanged( bool ), this, updateMuteStatus( bool ) );
123 }
124
125 SoundWidget::~SoundWidget()
126 {
127     delete volumeSlider;
128     delete volumeControlWidget;
129 }
130
131 void SoundWidget::refreshLabels()
132 {
133     int i_sliderVolume = volumeSlider->value();
134
135     if( b_is_muted )
136     {
137         volMuteLabel->setPixmap( QPixmap(":/toolbar/volume-muted" ) );
138         volMuteLabel->setToolTip(qfu(vlc_pgettext("Tooltip|Unmute", "Unmute")));
139         return;
140     }
141
142     if( i_sliderVolume < VOLUME_MAX / 3 )
143         volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-low" ) );
144     else if( i_sliderVolume > (VOLUME_MAX * 2 / 3 ) )
145         volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-high" ) );
146     else volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-medium" ) );
147     volMuteLabel->setToolTip( qfu(vlc_pgettext("Tooltip|Mute", "Mute")) );
148 }
149
150 /* volumeSlider changed value event slot */
151 void SoundWidget::userUpdateVolume( int i_sliderVolume )
152 {
153     /* Only if volume is set by user action on slider */
154     setMuted( false );
155     playlist_t *p_playlist = pl_Get( p_intf );
156     aout_VolumeSet( p_playlist, i_sliderVolume / 100.f );
157     refreshLabels();
158 }
159
160 /* libvlc changed value event slot */
161 void SoundWidget::libUpdateVolume()
162 {
163     /* Audio part */
164     playlist_t *p_playlist = pl_Get( p_intf );
165     long i_volume = lroundf(aout_VolumeGet( p_playlist ) * 100.f);
166
167     if ( i_volume - volumeSlider->value() != 0 )
168     {
169         b_ignore_valuechanged = true;
170         volumeSlider->setValue( i_volume );
171         b_ignore_valuechanged = false;
172     }
173     refreshLabels();
174 }
175
176 void SoundWidget::valueChangedFilter( int i_val )
177 {
178     /* valueChanged is also emitted when the lib setValue() */
179     if ( !b_ignore_valuechanged ) emit valueReallyChanged( i_val );
180 }
181
182 /* libvlc mute/unmute event slot */
183 void SoundWidget::updateMuteStatus( bool mute )
184 {
185     b_is_muted = mute;
186
187     SoundSlider *soundSlider = qobject_cast<SoundSlider *>(volumeSlider);
188     if( soundSlider )
189         soundSlider->setMuted( mute );
190     refreshLabels();
191 }
192
193 void SoundWidget::showVolumeMenu( QPoint pos )
194 {
195     volumeMenu->setFixedHeight( volumeMenu->sizeHint().height() );
196     volumeMenu->exec( QCursor::pos() - pos - QPoint( 0, volumeMenu->height()/2 )
197                           + QPoint( width(), height() /2) );
198 }
199
200 void SoundWidget::setMuted( bool mute )
201 {
202     b_is_muted = mute;
203     playlist_t *p_playlist = pl_Get( p_intf );
204     aout_MuteSet( VLC_OBJECT(p_playlist), mute );
205 }
206
207 bool SoundWidget::eventFilter( QObject *obj, QEvent *e )
208 {
209     VLC_UNUSED( obj );
210     if( e->type() == QEvent::MouseButtonPress )
211     {
212         QMouseEvent *event = static_cast<QMouseEvent*>(e);
213         if( event->button() == Qt::LeftButton )
214         {
215             if( volumeSlider->orientation() ==  Qt::Vertical )
216             {
217                 showVolumeMenu( event->pos() );
218             }
219             else
220             {
221                 setMuted( !b_is_muted );
222             }
223             e->accept();
224             return true;
225         }
226     }
227     e->ignore();
228     return false;
229 }
230
231 /**
232  * Play Button
233  **/
234 void PlayButton::updateButtonIcons( bool b_playing )
235 {
236     setIcon( b_playing ? QIcon( ":/toolbar/pause_b" ) : QIcon( ":/toolbar/play_b" ) );
237     setToolTip( b_playing ? qtr( "Pause the playback" )
238                           : qtr( I_PLAY_TOOLTIP ) );
239 }
240
241 void AtoB_Button::updateButtonIcons( bool timeA, bool timeB )
242 {
243     if( !timeA && !timeB)
244     {
245         setIcon( QIcon( ":/toolbar/atob_nob" ) );
246         setToolTip( qtr( "Loop from point A to point B continuously\n"
247                          "Click to set point A" ) );
248     }
249     else if( timeA && !timeB )
250     {
251         setIcon( QIcon( ":/toolbar/atob_noa" ) );
252         setToolTip( qtr( "Click to set point B" ) );
253     }
254     else if( timeA && timeB )
255     {
256         setIcon( QIcon( ":/toolbar/atob" ) );
257         setToolTip( qtr( "Stop the A to B loop" ) );
258     }
259 }
260
261 void LoopButton::updateButtonIcons( int value )
262 {
263     setChecked( value != NORMAL );
264     setIcon( ( value == REPEAT_ONE ) ? QIcon( ":/buttons/playlist/repeat_one" )
265                                      : QIcon( ":/buttons/playlist/repeat_all" ) );
266 }
267
268 void AspectRatioComboBox::updateRatios()
269 {
270     /* Clear the list before updating */
271     clear();
272     vlc_value_t val_list, text_list;
273     vout_thread_t* p_vout = THEMIM->getVout();
274
275     /* Disable if there is no vout */
276     if( p_vout == NULL )
277     {
278         addItem( qtr("Aspect Ratio") );
279         setDisabled( true );
280         return;
281     }
282
283     var_Change( p_vout, "aspect-ratio", VLC_VAR_GETLIST, &val_list, &text_list );
284     for( int i = 0; i < val_list.p_list->i_count; i++ )
285         addItem( qfu( text_list.p_list->p_values[i].psz_string ),
286                  QString( val_list.p_list->p_values[i].psz_string ) );
287     setEnabled( true );
288     var_FreeList( &val_list, &text_list );
289     vlc_object_release( p_vout );
290 }
291
292 void AspectRatioComboBox::updateAspectRatio( int x )
293 {
294     vout_thread_t* p_vout = THEMIM->getVout();
295     if( p_vout && x >= 0 )
296     {
297         var_SetString( p_vout, "aspect-ratio", qtu( itemData(x).toString() ) );
298     }
299     if( p_vout )
300         vlc_object_release( p_vout );
301 }
302