]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/controller_widget.cpp
Qt4: Add aspect ratio combobox to toolbar editor
[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_intf.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             false,
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( 200 );
102     }
103
104     volumeSlider->setFocusPolicy( Qt::NoFocus );
105     if( b_special )
106         subLayout->addWidget( volumeSlider );
107     else
108         layout->addWidget( volumeSlider, 0, Qt::AlignBottom  );
109
110     /* Set the volume from the config */
111     libUpdateVolume();
112     /* Force the update at build time in order to have a muted icon if needed */
113     updateMuteStatus();
114
115     /* Volume control connection */
116     volumeSlider->setTracking( true );
117     CONNECT( volumeSlider, valueChanged( int ), this, valueChangedFilter( int ) );
118     CONNECT( this, valueReallyChanged( int ), this, userUpdateVolume( int ) );
119     CONNECT( THEMIM, volumeChanged( void ), this, libUpdateVolume( void ) );
120     CONNECT( THEMIM, soundMuteChanged( void ), this, updateMuteStatus( void ) );
121 }
122
123 SoundWidget::~SoundWidget()
124 {
125     delete volumeSlider;
126     delete volumeControlWidget;
127 }
128
129 void SoundWidget::refreshLabels()
130 {
131     int i_sliderVolume = volumeSlider->value();
132
133     if( b_is_muted )
134     {
135         volMuteLabel->setPixmap( QPixmap(":/toolbar/volume-muted" ) );
136         volMuteLabel->setToolTip(qfu(vlc_pgettext("Tooltip|Unmute", "Unmute")));
137         return;
138     }
139
140     if( i_sliderVolume < VOLUME_MAX / 3 )
141         volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-low" ) );
142     else if( i_sliderVolume > (VOLUME_MAX * 2 / 3 ) )
143         volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-high" ) );
144     else volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-medium" ) );
145     volMuteLabel->setToolTip( qfu(vlc_pgettext("Tooltip|Mute", "Mute")) );
146 }
147
148 /* volumeSlider changed value event slot */
149 void SoundWidget::userUpdateVolume( int i_sliderVolume )
150 {
151     /* Only if volume is set by user action on slider */
152     setMuted( false );
153     playlist_t *p_playlist = pl_Get( p_intf );
154     int i_res = i_sliderVolume * (AOUT_VOLUME_DEFAULT * 2) / VOLUME_MAX;
155     aout_VolumeSet( p_playlist, i_res );
156     refreshLabels();
157 }
158
159 /* libvlc changed value event slot */
160 void SoundWidget::libUpdateVolume()
161 {
162     /* Audio part */
163     audio_volume_t i_volume;
164     playlist_t *p_playlist = pl_Get( p_intf );
165
166     i_volume = aout_VolumeGet( p_playlist );
167     i_volume = (i_volume * VOLUME_MAX ) / (AOUT_VOLUME_DEFAULT * 2);
168
169     if ( i_volume - volumeSlider->value() != 0 )
170     {
171         b_ignore_valuechanged = true;
172         volumeSlider->setValue( i_volume );
173         b_ignore_valuechanged = false;
174     }
175     refreshLabels();
176 }
177
178 void SoundWidget::valueChangedFilter( int i_val )
179 {
180     /* valueChanged is also emitted when the lib setValue() */
181     if ( !b_ignore_valuechanged ) emit valueReallyChanged( i_val );
182 }
183
184 /* libvlc mute/unmute event slot */
185 void SoundWidget::updateMuteStatus()
186 {
187     playlist_t *p_playlist = pl_Get( p_intf );
188     b_is_muted = aout_IsMuted( VLC_OBJECT(p_playlist) );
189
190     SoundSlider *soundSlider = qobject_cast<SoundSlider *>(volumeSlider);
191     if( soundSlider )
192         soundSlider->setMuted( b_is_muted );
193     refreshLabels();
194 }
195
196 void SoundWidget::showVolumeMenu( QPoint pos )
197 {
198     volumeMenu->setFixedHeight( volumeMenu->sizeHint().height() );
199     volumeMenu->exec( QCursor::pos() - pos - QPoint( 0, volumeMenu->height()/2 )
200                           + QPoint( width(), height() /2) );
201 }
202
203 void SoundWidget::setMuted( bool mute )
204 {
205     b_is_muted = mute;
206     playlist_t *p_playlist = pl_Get( p_intf );
207     aout_SetMute( VLC_OBJECT(p_playlist), NULL, mute );
208 }
209
210 bool SoundWidget::eventFilter( QObject *obj, QEvent *e )
211 {
212     VLC_UNUSED( obj );
213     if( e->type() == QEvent::MouseButtonPress )
214     {
215         QMouseEvent *event = static_cast<QMouseEvent*>(e);
216         if( event->button() != Qt::RightButton )
217         {
218             if( volumeSlider->orientation() ==  Qt::Vertical )
219             {
220                 showVolumeMenu( event->pos() );
221             }
222             else
223             {
224                 setMuted( !b_is_muted );
225             }
226             e->accept();
227             return true;
228         }
229     }
230     e->ignore();
231     return false;
232 }
233
234 /**
235  * Play Button
236  **/
237 void PlayButton::updateButtonIcons( bool b_playing )
238 {
239     setIcon( b_playing ? QIcon( ":/toolbar/pause_b" ) : QIcon( ":/toolbar/play_b" ) );
240     setToolTip( b_playing ? qtr( "Pause the playback" )
241                           : qtr( I_PLAY_TOOLTIP ) );
242 }
243
244 void AtoB_Button::updateButtonIcons( bool timeA, bool timeB )
245 {
246     if( !timeA && !timeB)
247     {
248         setIcon( QIcon( ":/toolbar/atob_nob" ) );
249         setToolTip( qtr( "Loop from point A to point B continuously\n"
250                          "Click to set point A" ) );
251     }
252     else if( timeA && !timeB )
253     {
254         setIcon( QIcon( ":/toolbar/atob_noa" ) );
255         setToolTip( qtr( "Click to set point B" ) );
256     }
257     else if( timeA && timeB )
258     {
259         setIcon( QIcon( ":/toolbar/atob" ) );
260         setToolTip( qtr( "Stop the A to B loop" ) );
261     }
262 }
263
264 void LoopButton::updateButtonIcons( int value )
265 {
266     setChecked( value != NORMAL );
267     setIcon( ( value == REPEAT_ONE ) ? QIcon( ":/buttons/playlist/repeat_one" )
268                                      : QIcon( ":/buttons/playlist/repeat_all" ) );
269 }
270
271 void AspectRatioComboBox::updateRatios() {
272     /* Clear the list before updating */
273     this->clear();
274     vlc_value_t val_list, text_list;
275     vout_thread_t* p_vout = THEMIM->getVout();
276     /* Disable if there is no vout */
277     if( p_vout == NULL ) {
278         addItem("Aspect Ratio");
279         setDisabled( true );
280         return;
281     }
282     var_Change( p_vout, "aspect-ratio", VLC_VAR_GETLIST, &val_list, &text_list );
283     for( int i = 0; i < val_list.p_list->i_count; i++ )
284         addItem( QString( text_list.p_list->p_values[i].psz_string ), QString( val_list.p_list->p_values[i].psz_string ) );
285     setEnabled( true );
286     var_FreeList( &val_list, &text_list );
287 }
288
289 void AspectRatioComboBox::updateAspectRatio( int x ) {
290     vout_thread_t* p_vout = THEMIM->getVout();
291     if( p_vout && x >= 0 ) var_SetString( p_vout, "aspect-ratio", qtu( itemData(x).toString() ) );
292 }