]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/controller_widget.cpp
2e584a44b72a80019a1a649ecc11507a261bdd6d
[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 <QSpinBox>
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_my_volume( false ), b_is_muted( 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     else
68     {
69         /* Special view, click on button shows the slider */
70         b_shiny = false;
71
72         volumeControlWidget = new QFrame;
73         subLayout = new QVBoxLayout( volumeControlWidget );
74         subLayout->setContentsMargins( 4, 4, 4, 4 );
75         volumeMenu = new QMenu( this );
76
77         QWidgetAction *widgetAction = new QWidgetAction( volumeControlWidget );
78         widgetAction->setDefaultWidget( volumeControlWidget );
79         volumeMenu->addAction( widgetAction );
80     }
81
82     /* And add the label */
83     layout->addWidget( volMuteLabel );
84
85     /* Slider creation: shiny or clean */
86     if( b_shiny )
87     {
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" ) );
92     }
93     else
94     {
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 );
100     }
101     if( volumeSlider->orientation() ==  Qt::Horizontal )
102     {
103         volumeSlider->setMaximumSize( QSize( 200, 40 ) );
104         volumeSlider->setMinimumSize( QSize( 85, 30 ) );
105     }
106
107     volumeSlider->setFocusPolicy( Qt::NoFocus );
108     if( b_special )
109         subLayout->addWidget( volumeSlider );
110     else
111         layout->addWidget( volumeSlider, 0, Qt::AlignBottom  );
112
113     /* Set the volume from the config */
114     volumeSlider->setValue( qRound( ( (qreal)config_GetInt( p_intf, "volume" ) ) *
115                               VOLUME_MAX / (AOUT_VOLUME_MAX/2) ) );
116
117     /* Force the update at build time in order to have a muted icon if needed */
118     updateVolume( volumeSlider->value() );
119
120     /* Volume control connection */
121     CONNECT( volumeSlider, valueChanged( int ), this, updateVolume( int ) );
122     CONNECT( THEMIM, volumeChanged( void ), this, updateVolume( void ) );
123     CONNECT( THEMIM, soundMuteChanged( void ), this, updateMuteStatus( void ) );
124 }
125
126 SoundWidget::~SoundWidget()
127 {
128     delete volumeSlider;
129     delete volumeControlWidget;
130 }
131
132 void SoundWidget::refreshLabels()
133 {
134     int i_sliderVolume = volumeSlider->value();
135
136     if( b_is_muted )
137     {
138         volMuteLabel->setPixmap( QPixmap(":/toolbar/volume-muted" ) );
139         volMuteLabel->setToolTip(qfu(vlc_pgettext("Tooltip|Unmute", "Unmute")));
140         return;
141     }
142
143     if( i_sliderVolume < VOLUME_MAX / 3 )
144         volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-low" ) );
145     else if( i_sliderVolume > (VOLUME_MAX * 2 / 3 ) )
146         volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-high" ) );
147     else volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-medium" ) );
148     volMuteLabel->setToolTip( qfu(vlc_pgettext("Tooltip|Mute", "Mute")) );
149 }
150
151 /* volumeSlider changed value event slot */
152 void SoundWidget::updateVolume( int i_sliderVolume )
153 {
154     if( !b_my_volume ) /* Only if volume is set by user action on slider */
155     {
156         setMuted( false );
157         playlist_t *p_playlist = pl_Hold( p_intf );
158         int i_res = i_sliderVolume  * (AOUT_VOLUME_MAX / 2) / VOLUME_MAX;
159         aout_VolumeSet( p_playlist, i_res );
160         pl_Release( p_intf );
161     }
162     refreshLabels();
163 }
164
165 /* libvlc changed value event slot */
166 void SoundWidget::updateVolume()
167 {
168     /* Audio part */
169     audio_volume_t i_volume;
170     playlist_t *p_playlist = pl_Hold( p_intf );
171
172     aout_VolumeGet( p_playlist, &i_volume );
173     pl_Release( p_intf );
174     i_volume = ( ( i_volume + 1 ) *  VOLUME_MAX )/ (AOUT_VOLUME_MAX/2);
175     int i_gauge = volumeSlider->value();
176     b_my_volume = false;
177     if ( !b_is_muted && /* do not show mute effect on volume (set to 0) */
178         ( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
179     )
180     {
181         b_my_volume = true;
182         volumeSlider->setValue( i_volume );
183         b_my_volume = false;
184     }
185 }
186
187 /* libvlc mute/unmute event slot */
188 void SoundWidget::updateMuteStatus()
189 {
190     playlist_t *p_playlist = pl_Hold( p_intf );
191     b_is_muted = aout_IsMuted( VLC_OBJECT(p_playlist) );
192     pl_Release( p_intf );
193     (qobject_cast<SoundSlider *>(volumeSlider))->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_Hold( p_intf );
208     aout_SetMute( VLC_OBJECT(p_playlist), NULL, mute );
209     pl_Release( p_intf );
210 }
211
212 bool SoundWidget::eventFilter( QObject *obj, QEvent *e )
213 {
214     VLC_UNUSED( obj );
215     if (e->type() == QEvent::MouseButtonPress  )
216     {
217         if( volumeSlider->orientation() ==  Qt::Vertical )
218         {
219             QMouseEvent *event = static_cast<QMouseEvent*>(e);
220             showVolumeMenu( event->pos() );
221         }
222         else
223         {
224             setMuted( !b_is_muted );
225         }
226         e->accept();
227         return true;
228     }
229     else
230     {
231         e->ignore();
232         return false;
233     }
234 }
235
236 /**
237  * Play Button
238  **/
239 void PlayButton::updateButton( bool b_playing )
240 {
241     setIcon( b_playing ? QIcon( ":/toolbar/pause_b" ) : QIcon( ":/toolbar/play_b" ) );
242     setToolTip( b_playing ? qtr( "Pause the playback" )
243                           : qtr( I_PLAY_TOOLTIP ) );
244 }
245
246 void AtoB_Button::setIcons( bool timeA, bool timeB )
247 {
248     if( !timeA && !timeB)
249     {
250         setIcon( QIcon( ":/toolbar/atob_nob" ) );
251         setToolTip( qtr( "Loop from point A to point B continuously\n"
252                          "Click to set point A" ) );
253     }
254     else if( timeA && !timeB )
255     {
256         setIcon( QIcon( ":/toolbar/atob_noa" ) );
257         setToolTip( qtr( "Click to set point B" ) );
258     }
259     else if( timeA && timeB )
260     {
261         setIcon( QIcon( ":/toolbar/atob" ) );
262         setToolTip( qtr( "Stop the A to B loop" ) );
263     }
264 }
265
266 void LoopButton::updateIcons( int value )
267 {
268     setChecked( value != NORMAL );
269     setIcon( ( value == REPEAT_ALL ) ? QIcon( ":/buttons/playlist/repeat_all" )
270                                      : QIcon( ":/buttons/playlist/repeat_one" ) );
271 }