1 /*****************************************************************************
2  * controller_widget.cpp : Controller Widget for the controllers
3  ****************************************************************************
4  * Copyright (C) 2006-2008 the VideoLAN team
5  * $Id: 45ce4219fca86ebe06226901d34ea9747aea1168 $
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 #include "util/imagehelper.hpp"
34 
35 #include <math.h>
36 
37 #include <QLabel>
38 #include <QHBoxLayout>
39 #include <QMenu>
40 #include <QWidgetAction>
41 #include <QMouseEvent>
42 
43 #define VOLUME_MAX 125
44 
SoundWidget(QWidget * _parent,intf_thread_t * _p_intf,bool b_shiny,bool b_special)45 SoundWidget::SoundWidget( QWidget *_parent, intf_thread_t * _p_intf,
46                           bool b_shiny, bool b_special )
47                          : QWidget( _parent ), p_intf( _p_intf),
48                            b_is_muted( false ), b_ignore_valuechanged( false )
49 {
50     /* We need a layout for this widget */
51     QHBoxLayout *layout = new QHBoxLayout( this );
52     layout->setSpacing( 0 ); layout->setMargin( 0 );
53 
54     /* We need a Label for the pix */
55     volMuteLabel = new QLabel;
56     volMuteLabel->setPixmap( ImageHelper::loadSvgToPixmap( ":/toolbar/volume-medium.svg", 16, 16 ) );
57 
58     /* We might need a subLayout too */
59     QVBoxLayout *subLayout;
60 
61     volMuteLabel->installEventFilter( this );
62 
63     /* Normal View, click on icon mutes */
64     if( !b_special )
65     {
66         volumeMenu = NULL; subLayout = NULL;
67         volumeControlWidget = NULL;
68 
69         /* And add the label */
70         layout->addWidget( volMuteLabel, 0, b_shiny? Qt::AlignBottom : Qt::AlignCenter );
71     }
72     else
73     {
74         /* Special view, click on button shows the slider */
75         b_shiny = false;
76 
77         volumeControlWidget = new QFrame( this );
78         subLayout = new QVBoxLayout( volumeControlWidget );
79         subLayout->setContentsMargins( 4, 4, 4, 4 );
80         volumeMenu = new QMenu( this );
81 
82         QWidgetAction *widgetAction = new QWidgetAction( volumeControlWidget );
83         widgetAction->setDefaultWidget( volumeControlWidget );
84         volumeMenu->addAction( widgetAction );
85 
86         /* And add the label */
87         layout->addWidget( volMuteLabel );
88     }
89 
90     /* Slider creation: shiny or clean */
91     if( b_shiny )
92     {
93         volumeSlider = new SoundSlider( this,
94             config_GetFloat( p_intf, "volume-step" ),
95             var_InheritString( p_intf, "qt-slider-colours" ),
96             var_InheritInteger( p_intf, "qt-max-volume") );
97     }
98     else
99     {
100         volumeSlider = new QSlider( NULL );
101         volumeSlider->setAttribute( Qt::WA_MacSmallSize);
102         volumeSlider->setOrientation( b_special ? Qt::Vertical
103                                                 : Qt::Horizontal );
104         volumeSlider->setMaximum( 200 );
105     }
106 
107     volumeSlider->setFocusPolicy( Qt::NoFocus );
108     if( b_special )
109         subLayout->addWidget( volumeSlider );
110     else
111         layout->addWidget( volumeSlider, 0, b_shiny? Qt::AlignBottom : Qt::AlignCenter );
112 
113     /* Set the volume from the config */
114     float volume = playlist_VolumeGet( THEPL );
115     libUpdateVolume( (volume >= 0.f) ? volume : 1.f );
116     /* Sync mute status */
117     if( playlist_MuteGet( THEPL ) > 0 )
118         updateMuteStatus( true );
119 
120     /* Volume control connection */
121     volumeSlider->setTracking( true );
122     CONNECT( volumeSlider, valueChanged( int ), this, valueChangedFilter( int ) );
123     CONNECT( this, valueReallyChanged( int ), this, userUpdateVolume( int ) );
124     CONNECT( THEMIM, volumeChanged( float ), this, libUpdateVolume( float ) );
125     CONNECT( THEMIM, soundMuteChanged( bool ), this, updateMuteStatus( bool ) );
126 }
127 
refreshLabels()128 void SoundWidget::refreshLabels()
129 {
130     int i_sliderVolume = volumeSlider->value();
131     const char *psz_icon = ":/toolbar/volume-muted.svg";
132 
133     if( b_is_muted )
134     {
135         volMuteLabel->setPixmap( ImageHelper::loadSvgToPixmap( psz_icon, 16, 16 ) );
136         volMuteLabel->setToolTip(qfu(vlc_pgettext("Tooltip|Unmute", "Unmute")));
137         return;
138     }
139 
140     if( i_sliderVolume < VOLUME_MAX / 3 )
141         psz_icon = ":/toolbar/volume-low.svg";
142     else if( i_sliderVolume > (VOLUME_MAX * 2 / 3 ) )
143         psz_icon = ":/toolbar/volume-high.svg";
144     else
145         psz_icon = ":/toolbar/volume-medium.svg";
146 
147     volMuteLabel->setPixmap( ImageHelper::loadSvgToPixmap( psz_icon, 16, 16 ) );
148     volMuteLabel->setToolTip( qfu(vlc_pgettext("Tooltip|Mute", "Mute")) );
149 }
150 
151 /* volumeSlider changed value event slot */
userUpdateVolume(int i_sliderVolume)152 void SoundWidget::userUpdateVolume( int i_sliderVolume )
153 {
154     /* Only if volume is set by user action on slider */
155     setMuted( false );
156     playlist_VolumeSet( THEPL, i_sliderVolume / 100.f );
157     refreshLabels();
158 }
159 
160 /* libvlc changed value event slot */
libUpdateVolume(float volume)161 void SoundWidget::libUpdateVolume( float volume )
162 {
163     long i_volume = lroundf(volume * 100.f);
164     if( i_volume != volumeSlider->value()  )
165     {
166         b_ignore_valuechanged = true;
167         volumeSlider->setValue( i_volume );
168         b_ignore_valuechanged = false;
169     }
170     refreshLabels();
171 }
172 
valueChangedFilter(int i_val)173 void SoundWidget::valueChangedFilter( int i_val )
174 {
175     /* valueChanged is also emitted when the lib setValue() */
176     if ( !b_ignore_valuechanged ) emit valueReallyChanged( i_val );
177 }
178 
179 /* libvlc mute/unmute event slot */
updateMuteStatus(bool mute)180 void SoundWidget::updateMuteStatus( bool mute )
181 {
182     b_is_muted = mute;
183 
184     SoundSlider *soundSlider = qobject_cast<SoundSlider *>(volumeSlider);
185     if( soundSlider )
186         soundSlider->setMuted( mute );
187     refreshLabels();
188 }
189 
showVolumeMenu(QPoint pos)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 
setMuted(bool mute)197 void SoundWidget::setMuted( bool mute )
198 {
199     b_is_muted = mute;
200     playlist_t *p_playlist = THEPL;
201     playlist_MuteSet( p_playlist, mute );
202 }
203 
eventFilter(QObject * obj,QEvent * e)204 bool SoundWidget::eventFilter( QObject *obj, QEvent *e )
205 {
206     VLC_UNUSED( obj );
207     if( e->type() == QEvent::MouseButtonPress )
208     {
209         QMouseEvent *event = static_cast<QMouseEvent*>(e);
210         if( event->button() == Qt::LeftButton )
211         {
212             if( volumeSlider->orientation() ==  Qt::Vertical )
213             {
214                 showVolumeMenu( event->pos() );
215             }
216             else
217             {
218                 setMuted( !b_is_muted );
219             }
220             e->accept();
221             return true;
222         }
223     }
224     e->ignore();
225     return false;
226 }
227 
228 /**
229  * Play Button
230  **/
updateButtonIcons(bool b_playing)231 void PlayButton::updateButtonIcons( bool b_playing )
232 {
233     setIcon( b_playing ? QIcon( ":/toolbar/pause_b.svg" ) : QIcon( ":/toolbar/play_b.svg" ) );
234     setToolTip( b_playing ? qtr( "Pause the playback" )
235                           : qtr( I_PLAY_TOOLTIP ) );
236 }
237 
updateButtonIcons(bool timeA,bool timeB)238 void AtoB_Button::updateButtonIcons( bool timeA, bool timeB )
239 {
240     if( !timeA && !timeB)
241     {
242         setIcon( QIcon( ":/toolbar/atob_nob.svg" ) );
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.svg" ) );
249         setToolTip( qtr( "Click to set point B" ) );
250     }
251     else if( timeA && timeB )
252     {
253         setIcon( QIcon( ":/toolbar/atob.svg" ) );
254         setToolTip( qtr( "Stop the A to B loop" ) );
255     }
256 }
257 
updateButtonIcons(int value)258 void LoopButton::updateButtonIcons( int value )
259 {
260     setChecked( value != NORMAL );
261     setIcon( ( value == REPEAT_ONE ) ? QIcon( ":/buttons/playlist/repeat_one.svg" )
262                                      : QIcon( ":/buttons/playlist/repeat_all.svg" ) );
263 }
264 
updateRatios()265 void AspectRatioComboBox::updateRatios()
266 {
267     /* Clear the list before updating */
268     clear();
269     vlc_value_t val_list, text_list;
270     vout_thread_t* p_vout = THEMIM->getVout();
271 
272     /* Disable if there is no vout */
273     if( p_vout == NULL )
274     {
275         addItem( qtr("Aspect Ratio") );
276         setDisabled( true );
277         return;
278     }
279 
280     var_Change( p_vout, "aspect-ratio", VLC_VAR_GETCHOICES, &val_list, &text_list );
281     for( int i = 0; i < val_list.p_list->i_count; i++ )
282         addItem( qfu( text_list.p_list->p_values[i].psz_string ),
283                  QString( val_list.p_list->p_values[i].psz_string ) );
284     setEnabled( true );
285     var_FreeList( &val_list, &text_list );
286     vlc_object_release( p_vout );
287 }
288 
updateAspectRatio(int x)289 void AspectRatioComboBox::updateAspectRatio( int x )
290 {
291     vout_thread_t* p_vout = THEMIM->getVout();
292     if( p_vout && x >= 0 )
293     {
294         var_SetString( p_vout, "aspect-ratio", qtu( itemData(x).toString() ) );
295     }
296     if( p_vout )
297         vlc_object_release( p_vout );
298 }
299 
300