]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/controller_widget.cpp
Qt menus: SD go in the media menu as on Mac.
[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::showVolumeMenu( QPoint pos )
124 {
125     volumeMenu->exec( QCursor::pos() - pos - QPoint( 0, volumeMenu->height()/2 )
126                           + QPoint( width(), height() /2) );
127 }
128
129 bool SoundWidget::eventFilter( QObject *obj, QEvent *e )
130 {
131     VLC_UNUSED( obj );
132     if (e->type() == QEvent::MouseButtonPress  )
133     {
134         if( volumeSlider->orientation() ==  Qt::Vertical )
135         {
136             QMouseEvent *event = static_cast<QMouseEvent*>(e);
137             showVolumeMenu( event->pos() );
138         }
139         else
140         {
141             aout_VolumeMute( p_intf, NULL );
142         }
143         e->accept();
144         return true;
145     }
146     else
147     {
148         e->ignore();
149         return false;
150     }
151 }
152
153 void SoundWidget::updateVolume( int i_sliderVolume )
154 {
155     if( !b_my_volume )
156     {
157         int i_res = i_sliderVolume  * (AOUT_VOLUME_MAX / 2) / VOLUME_MAX;
158         aout_VolumeSet( p_intf, i_res );
159     }
160     if( i_sliderVolume == 0 )
161     {
162         volMuteLabel->setPixmap( QPixmap(":/volume-muted" ) );
163         volMuteLabel->setToolTip( qtr( "Unmute" ) );
164         return;
165     }
166
167     if( i_sliderVolume < VOLUME_MAX / 3 )
168         volMuteLabel->setPixmap( QPixmap( ":/volume-low" ) );
169     else if( i_sliderVolume > (VOLUME_MAX * 2 / 3 ) )
170         volMuteLabel->setPixmap( QPixmap( ":/volume-high" ) );
171     else volMuteLabel->setPixmap( QPixmap( ":/volume-medium" ) );
172     volMuteLabel->setToolTip( qtr( "Mute" ) );
173 }
174
175 void SoundWidget::updateVolume()
176 {
177     /* Audio part */
178     audio_volume_t i_volume;
179     aout_VolumeGet( p_intf, &i_volume );
180     i_volume = ( i_volume *  VOLUME_MAX )/ (AOUT_VOLUME_MAX/2);
181     int i_gauge = volumeSlider->value();
182     b_my_volume = false;
183     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
184     {
185         b_my_volume = true;
186         volumeSlider->setValue( i_volume );
187         b_my_volume = false;
188     }
189 }
190
191 void TeletextController::enableTeletextButtons( bool b_enabled )
192 {
193     telexOn->setChecked( b_enabled );
194     telexTransparent->setEnabled( b_enabled );
195     telexPage->setEnabled( b_enabled );
196 }
197
198 void PlayButton::updateButton( bool b_playing )
199 {
200     setIcon( b_playing ? QIcon( ":/pause_b" ) : QIcon( ":/play_b" ) );
201     setToolTip( b_playing ? qtr( "Pause the playback" )
202                           : qtr( I_PLAY_TOOLTIP ) );
203 }
204
205 void AtoB_Button::setIcons( bool timeA, bool timeB )
206 {
207     if( !timeA && !timeB)
208     {
209         setIcon( QIcon( ":/atob_nob" ) );
210         setToolTip( qtr( "Loop from point A to point B continuously\n"
211                          "Click to set point A" ) );
212     }
213     else if( timeA && !timeB )
214     {
215         setIcon( QIcon( ":/atob_noa" ) );
216         setToolTip( qtr( "Click to set point B" ) );
217     }
218     else if( timeA && timeB )
219     {
220         setIcon( QIcon( ":/atob" ) );
221         setToolTip( qtr( "Stop the A to B loop" ) );
222     }
223 }
224
225