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