]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/controller_widget.cpp
Qt: respect font sizes
[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_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             var_InheritInteger( p_intf, "qt-volume-complete" ),
91             var_InheritString( 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( var_InheritBool( 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     libUpdateVolume();
115     /* Force the update at build time in order to have a muted icon if needed */
116     updateMuteStatus();
117
118     /* Volume control connection */
119     CONNECT( volumeSlider, valueChanged( int ), this, refreshLabels( void ) );
120     CONNECT( volumeSlider, sliderMoved( int ), this, userUpdateVolume( int ) );
121     CONNECT( THEMIM, volumeChanged( void ), this, libUpdateVolume( void ) );
122     CONNECT( THEMIM, soundMuteChanged( void ), this, updateMuteStatus( void ) );
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     int i_res = i_sliderVolume  * (AOUT_VOLUME_MAX / 2) / VOLUME_MAX;
157     aout_VolumeSet( p_playlist, i_res );
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     aout_VolumeGet( p_playlist, &i_volume );
168     i_volume = ( ( i_volume + 1 ) *  VOLUME_MAX )/ (AOUT_VOLUME_MAX/2);
169     int i_gauge = volumeSlider->value();
170     if ( !b_is_muted && /* do not show mute effect on volume (set to 0) */
171         ( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
172     )
173     {
174         volumeSlider->setValue( i_volume );
175     }
176 }
177
178 /* libvlc mute/unmute event slot */
179 void SoundWidget::updateMuteStatus()
180 {
181     playlist_t *p_playlist = pl_Get( p_intf );
182     b_is_muted = aout_IsMuted( VLC_OBJECT(p_playlist) );
183
184     SoundSlider *soundSlider = qobject_cast<SoundSlider *>(volumeSlider);
185     if( soundSlider )
186         soundSlider->setMuted( b_is_muted );
187     refreshLabels();
188 }
189
190 void SoundWidget::showVolumeMenu( QPoint pos )
191 {
192     volumeMenu->setFixedHeight( volumeMenu->sizeHint().height() );
193     volumeMenu->exec( QCursor::pos() - pos - QPoint( 0, volumeMenu->height()/2 )
194                           + QPoint( width(), height() /2) );
195 }
196
197 void SoundWidget::setMuted( bool mute )
198 {
199     b_is_muted = mute;
200     playlist_t *p_playlist = pl_Get( p_intf );
201     aout_SetMute( VLC_OBJECT(p_playlist), NULL, mute );
202 }
203
204 bool SoundWidget::eventFilter( QObject *obj, QEvent *e )
205 {
206     VLC_UNUSED( obj );
207     if (e->type() == QEvent::MouseButtonPress  )
208     {
209         if( volumeSlider->orientation() ==  Qt::Vertical )
210         {
211             QMouseEvent *event = static_cast<QMouseEvent*>(e);
212             showVolumeMenu( event->pos() );
213         }
214         else
215         {
216             setMuted( !b_is_muted );
217         }
218         e->accept();
219         return true;
220     }
221     else
222     {
223         e->ignore();
224         return false;
225     }
226 }
227
228 /**
229  * Play Button
230  **/
231 void PlayButton::updateButton( bool b_playing )
232 {
233     setIcon( b_playing ? QIcon( ":/toolbar/pause_b" ) : QIcon( ":/toolbar/play_b" ) );
234     setToolTip( b_playing ? qtr( "Pause the playback" )
235                           : qtr( I_PLAY_TOOLTIP ) );
236 }
237
238 void AtoB_Button::setIcons( bool timeA, bool timeB )
239 {
240     if( !timeA && !timeB)
241     {
242         setIcon( QIcon( ":/toolbar/atob_nob" ) );
243         setToolTip( qtr( "Loop from point A to point B continuously\n"
244                          "Click to set point A" ) );
245     }
246     else if( timeA && !timeB )
247     {
248         setIcon( QIcon( ":/toolbar/atob_noa" ) );
249         setToolTip( qtr( "Click to set point B" ) );
250     }
251     else if( timeA && timeB )
252     {
253         setIcon( QIcon( ":/toolbar/atob" ) );
254         setToolTip( qtr( "Stop the A to B loop" ) );
255     }
256 }
257
258 void LoopButton::updateIcons( int value )
259 {
260     setChecked( value != NORMAL );
261     setIcon( ( value == REPEAT_ALL ) ? QIcon( ":/buttons/playlist/repeat_all" )
262                                      : QIcon( ":/buttons/playlist/repeat_one" ) );
263 }