]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/controller_widget.cpp
1b907ffadfe352fc5364eea06f252e2556174042
[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
30 #include "input_manager.hpp"         /* Get notification of Volume Change */
31 #include "util/input_slider.hpp"     /* SoundSlider */
32
33 #include <vlc_aout.h>                /* Volume functions */
34
35 #include <QLabel>
36 #include <QHBoxLayout>
37 #include <QSpinBox>
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 ), b_my_volume( false ),
45                            p_intf( _p_intf)
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( ":/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     }
65     else
66     {
67         /* Special view, click on button shows the slider */
68         b_shiny = false;
69
70         QFrame *volumeControlWidget = new QFrame;
71         subLayout = new QVBoxLayout( volumeControlWidget );
72         subLayout->setLayoutMargins( 4, 4, 4, 4, 4 );
73         volumeMenu = new QMenu( this );
74
75         QWidgetAction *widgetAction = new QWidgetAction( volumeControlWidget );
76         widgetAction->setDefaultWidget( volumeControlWidget );
77         volumeMenu->addAction( widgetAction );
78     }
79
80     /* And add the label */
81     layout->addWidget( volMuteLabel );
82
83     /* Slider creation: shiny or clean */
84     if( b_shiny )
85     {
86         volumeSlider = new SoundSlider( this,
87             config_GetInt( p_intf, "volume-step" ),
88             config_GetInt( p_intf, "qt-volume-complete" ),
89             config_GetPsz( p_intf, "qt-slider-colours" ) );
90     }
91     else
92     {
93         volumeSlider = new QSlider( NULL );
94         volumeSlider->setOrientation( b_special ? Qt::Vertical
95                                                 : Qt::Horizontal );
96         volumeSlider->setMaximum( config_GetInt( p_intf, "qt-volume-complete" )
97                                   ? 400 : 200 );
98     }
99     if( volumeSlider->orientation() ==  Qt::Horizontal )
100     {
101         volumeSlider->setMaximumSize( QSize( 200, 40 ) );
102         volumeSlider->setMinimumSize( QSize( 85, 30 ) );
103     }
104
105     volumeSlider->setFocusPolicy( Qt::NoFocus );
106     if( b_special )
107         subLayout->addWidget( volumeSlider );
108     else
109         layout->addWidget( volumeSlider );
110
111     /* Set the volume from the config */
112     volumeSlider->setValue( ( config_GetInt( p_intf, "volume" ) ) *
113                               VOLUME_MAX / (AOUT_VOLUME_MAX/2) );
114
115     /* Force the update at build time in order to have a muted icon if needed */
116     updateVolume( volumeSlider->value() );
117
118     /* Volume control connection */
119     CONNECT( volumeSlider, valueChanged( int ), this, updateVolume( int ) );
120     CONNECT( THEMIM, volumeChanged( void ), this, updateVolume( void ) );
121 }
122
123 void SoundWidget::updateVolume( int i_sliderVolume )
124 {
125     if( !b_my_volume )
126     {
127         int i_res = i_sliderVolume  * (AOUT_VOLUME_MAX / 2) / VOLUME_MAX;
128         aout_VolumeSet( p_intf, i_res );
129     }
130     if( i_sliderVolume == 0 )
131     {
132         volMuteLabel->setPixmap( QPixmap(":/volume-muted" ) );
133         volMuteLabel->setToolTip( qtr( "Unmute" ) );
134         return;
135     }
136
137     if( i_sliderVolume < VOLUME_MAX / 3 )
138         volMuteLabel->setPixmap( QPixmap( ":/volume-low" ) );
139     else if( i_sliderVolume > (VOLUME_MAX * 2 / 3 ) )
140         volMuteLabel->setPixmap( QPixmap( ":/volume-high" ) );
141     else volMuteLabel->setPixmap( QPixmap( ":/volume-medium" ) );
142     volMuteLabel->setToolTip( qtr( "Mute" ) );
143 }
144
145 void SoundWidget::updateVolume()
146 {
147     /* Audio part */
148     audio_volume_t i_volume;
149     aout_VolumeGet( p_intf, &i_volume );
150     i_volume = ( ( i_volume + 1 ) *  VOLUME_MAX )/ (AOUT_VOLUME_MAX/2);
151     int i_gauge = volumeSlider->value();
152     b_my_volume = false;
153     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
154     {
155         b_my_volume = true;
156         volumeSlider->setValue( i_volume );
157         b_my_volume = false;
158     }
159 }
160
161 void SoundWidget::showVolumeMenu( QPoint pos )
162 {
163     volumeMenu->exec( QCursor::pos() - pos - QPoint( 0, volumeMenu->height()/2 )
164                           + QPoint( width(), height() /2) );
165 }
166
167 bool SoundWidget::eventFilter( QObject *obj, QEvent *e )
168 {
169     VLC_UNUSED( obj );
170     if (e->type() == QEvent::MouseButtonPress  )
171     {
172         if( volumeSlider->orientation() ==  Qt::Vertical )
173         {
174             QMouseEvent *event = static_cast<QMouseEvent*>(e);
175             showVolumeMenu( event->pos() );
176         }
177         else
178         {
179             aout_VolumeMute( p_intf, NULL );
180         }
181         e->accept();
182         return true;
183     }
184     else
185     {
186         e->ignore();
187         return false;
188     }
189 }
190
191 /**
192  * Play Button
193  **/
194 void PlayButton::updateButton( bool b_playing )
195 {
196     setIcon( b_playing ? QIcon( ":/pause_b" ) : QIcon( ":/play_b" ) );
197     setToolTip( b_playing ? qtr( "Pause the playback" )
198                           : qtr( I_PLAY_TOOLTIP ) );
199 }
200
201 void AtoB_Button::setIcons( bool timeA, bool timeB )
202 {
203     if( !timeA && !timeB)
204     {
205         setIcon( QIcon( ":/atob_nob" ) );
206         setToolTip( qtr( "Loop from point A to point B continuously\n"
207                          "Click to set point A" ) );
208     }
209     else if( timeA && !timeB )
210     {
211         setIcon( QIcon( ":/atob_noa" ) );
212         setToolTip( qtr( "Click to set point B" ) );
213     }
214     else if( timeA && timeB )
215     {
216         setIcon( QIcon( ":/atob" ) );
217         setToolTip( qtr( "Stop the A to B loop" ) );
218     }
219 }
220
221