Qt: Fix the bug where the volume was losing 1% at each start
[vlc/vlc-skelet.git] / modules / gui / qt4 / components / controller_widget.cpp
blobdb295423b69f3765db64d7a4ba4eff79ca71cb45
1 /*****************************************************************************
2 * Controller_widget.cpp : Controller Widget for the controllers
3 ****************************************************************************
4 * Copyright (C) 2006-2008 the VideoLAN team
5 * $Id$
7 * Authors: Jean-Baptiste Kempf <jb@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.
22 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include "controller_widget.hpp"
29 #include "controller.hpp"
31 #include "input_manager.hpp" /* Get notification of Volume Change */
32 #include "util/input_slider.hpp" /* SoundSlider */
34 #include <vlc_aout.h> /* Volume functions */
36 #include <QLabel>
37 #include <QHBoxLayout>
38 #include <QSpinBox>
39 #include <QMenu>
40 #include <QWidgetAction>
41 #include <QMouseEvent>
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_my_volume( false )
48 /* We need a layout for this widget */
49 QHBoxLayout *layout = new QHBoxLayout( this );
50 layout->setSpacing( 0 ); layout->setMargin( 0 );
52 /* We need a Label for the pix */
53 volMuteLabel = new QLabel;
54 volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-medium" ) );
56 /* We might need a subLayout too */
57 QVBoxLayout *subLayout;
59 volMuteLabel->installEventFilter( this );
61 /* Normal View, click on icon mutes */
62 if( !b_special )
64 volumeMenu = NULL; subLayout = NULL;
65 volumeControlWidget = NULL;
67 else
69 /* Special view, click on button shows the slider */
70 b_shiny = false;
72 volumeControlWidget = new QFrame;
73 subLayout = new QVBoxLayout( volumeControlWidget );
74 subLayout->setLayoutMargins( 4, 4, 4, 4, 4 );
75 volumeMenu = new QMenu( this );
77 QWidgetAction *widgetAction = new QWidgetAction( volumeControlWidget );
78 widgetAction->setDefaultWidget( volumeControlWidget );
79 volumeMenu->addAction( widgetAction );
82 /* And add the label */
83 layout->addWidget( volMuteLabel );
85 /* Slider creation: shiny or clean */
86 if( b_shiny )
88 volumeSlider = new SoundSlider( this,
89 config_GetInt( p_intf, "volume-step" ),
90 config_GetInt( p_intf, "qt-volume-complete" ),
91 config_GetPsz( p_intf, "qt-slider-colours" ) );
93 else
95 volumeSlider = new QSlider( NULL );
96 volumeSlider->setOrientation( b_special ? Qt::Vertical
97 : Qt::Horizontal );
98 volumeSlider->setMaximum( config_GetInt( p_intf, "qt-volume-complete" )
99 ? 400 : 200 );
101 if( volumeSlider->orientation() == Qt::Horizontal )
103 volumeSlider->setMaximumSize( QSize( 200, 40 ) );
104 volumeSlider->setMinimumSize( QSize( 85, 30 ) );
107 volumeSlider->setFocusPolicy( Qt::NoFocus );
108 if( b_special )
109 subLayout->addWidget( volumeSlider );
110 else
111 layout->addWidget( volumeSlider, 0, Qt::AlignBottom );
113 /* Set the volume from the config */
114 volumeSlider->setValue( qRound( ( (qreal)config_GetInt( p_intf, "volume" ) ) *
115 VOLUME_MAX / (AOUT_VOLUME_MAX/2) ) );
117 /* Force the update at build time in order to have a muted icon if needed */
118 updateVolume( volumeSlider->value() );
120 /* Volume control connection */
121 CONNECT( volumeSlider, valueChanged( int ), this, updateVolume( int ) );
122 CONNECT( THEMIM, volumeChanged( void ), this, updateVolume( void ) );
125 SoundWidget::~SoundWidget()
127 delete volumeSlider;
128 delete volumeControlWidget;
131 void SoundWidget::updateVolume( int i_sliderVolume )
133 if( !b_my_volume )
135 int i_res = i_sliderVolume * (AOUT_VOLUME_MAX / 2) / VOLUME_MAX;
136 playlist_t *p_playlist = pl_Hold( p_intf );
137 aout_VolumeSet( p_playlist, i_res );
138 pl_Release( p_intf );
140 if( i_sliderVolume == 0 )
142 volMuteLabel->setPixmap( QPixmap(":/toolbar/volume-muted" ) );
143 volMuteLabel->setToolTip( qtr( "Unmute" ) );
144 return;
147 if( i_sliderVolume < VOLUME_MAX / 3 )
148 volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-low" ) );
149 else if( i_sliderVolume > (VOLUME_MAX * 2 / 3 ) )
150 volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-high" ) );
151 else volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-medium" ) );
152 volMuteLabel->setToolTip( qtr( "Mute" ) );
155 void SoundWidget::updateVolume()
157 /* Audio part */
158 audio_volume_t i_volume;
159 playlist_t *p_playlist = pl_Hold( p_intf );
161 aout_VolumeGet( p_playlist, &i_volume );
162 pl_Release( p_intf );
163 i_volume = ( ( i_volume + 1 ) * VOLUME_MAX )/ (AOUT_VOLUME_MAX/2);
164 int i_gauge = volumeSlider->value();
165 b_my_volume = false;
166 if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
168 b_my_volume = true;
169 volumeSlider->setValue( i_volume );
170 b_my_volume = false;
174 void SoundWidget::showVolumeMenu( QPoint pos )
176 volumeMenu->exec( QCursor::pos() - pos - QPoint( 0, volumeMenu->height()/2 )
177 + QPoint( width(), height() /2) );
180 bool SoundWidget::eventFilter( QObject *obj, QEvent *e )
182 VLC_UNUSED( obj );
183 if (e->type() == QEvent::MouseButtonPress )
185 if( volumeSlider->orientation() == Qt::Vertical )
187 QMouseEvent *event = static_cast<QMouseEvent*>(e);
188 showVolumeMenu( event->pos() );
190 else
192 playlist_t *p_playlist = pl_Hold( p_intf );
194 aout_ToggleMute( p_playlist, NULL );
195 pl_Release( p_intf );
197 e->accept();
198 return true;
200 else
202 e->ignore();
203 return false;
208 * Play Button
210 void PlayButton::updateButton( bool b_playing )
212 setIcon( b_playing ? QIcon( ":/toolbar/pause_b" ) : QIcon( ":/toolbar/play_b" ) );
213 setToolTip( b_playing ? qtr( "Pause the playback" )
214 : qtr( I_PLAY_TOOLTIP ) );
217 void AtoB_Button::setIcons( bool timeA, bool timeB )
219 if( !timeA && !timeB)
221 setIcon( QIcon( ":/toolbar/atob_nob" ) );
222 setToolTip( qtr( "Loop from point A to point B continuously\n"
223 "Click to set point A" ) );
225 else if( timeA && !timeB )
227 setIcon( QIcon( ":/toolbar/atob_noa" ) );
228 setToolTip( qtr( "Click to set point B" ) );
230 else if( timeA && timeB )
232 setIcon( QIcon( ":/toolbar/atob" ) );
233 setToolTip( qtr( "Stop the A to B loop" ) );