1 /*****************************************************************************
2 * controller_widget.cpp : Controller Widget for the controllers
3 ****************************************************************************
4 * Copyright (C) 2006-2008 the VideoLAN team
7 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
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.
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.
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 *****************************************************************************/
28 #include "controller_widget.hpp"
29 #include "controller.hpp"
31 #include "input_manager.hpp" /* Get notification of Volume Change */
32 #include "util/input_slider.hpp" /* SoundSlider */
37 #include <QHBoxLayout>
39 #include <QWidgetAction>
40 #include <QMouseEvent>
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 )
47 /* We need a layout for this widget */
48 QHBoxLayout *layout = new QHBoxLayout( this );
49 layout->setSpacing( 0 ); layout->setMargin( 0 );
51 /* We need a Label for the pix */
52 volMuteLabel = new QLabel;
53 volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-medium" ) );
55 /* We might need a subLayout too */
56 QVBoxLayout *subLayout;
58 volMuteLabel->installEventFilter( this );
60 /* Normal View, click on icon mutes */
63 volumeMenu = NULL; subLayout = NULL;
64 volumeControlWidget = NULL;
66 /* And add the label */
67 layout->addWidget( volMuteLabel, 0, Qt::AlignBottom );
71 /* Special view, click on button shows the slider */
74 volumeControlWidget = new QFrame;
75 subLayout = new QVBoxLayout( volumeControlWidget );
76 subLayout->setContentsMargins( 4, 4, 4, 4 );
77 volumeMenu = new QMenu( this );
79 QWidgetAction *widgetAction = new QWidgetAction( volumeControlWidget );
80 widgetAction->setDefaultWidget( volumeControlWidget );
81 volumeMenu->addAction( widgetAction );
83 /* And add the label */
84 layout->addWidget( volMuteLabel );
87 /* Slider creation: shiny or clean */
90 volumeSlider = new SoundSlider( this,
91 config_GetInt( p_intf, "volume-step" ),
92 var_InheritString( p_intf, "qt-slider-colours" ),
93 var_InheritInteger( p_intf, "qt-max-volume") );
97 volumeSlider = new QSlider( NULL );
98 volumeSlider->setAttribute( Qt::WA_MacSmallSize);
99 volumeSlider->setOrientation( b_special ? Qt::Vertical
101 volumeSlider->setMaximum( 200 );
104 volumeSlider->setFocusPolicy( Qt::NoFocus );
106 subLayout->addWidget( volumeSlider );
108 layout->addWidget( volumeSlider, 0, Qt::AlignBottom );
110 /* Set the volume from the config */
111 float volume = playlist_VolumeGet( THEPL );
112 libUpdateVolume( (volume >= 0.f) ? volume : 1.f );
113 /* Sync mute status */
114 if( playlist_MuteGet( THEPL ) > 0 )
115 updateMuteStatus( true );
117 /* Volume control connection */
118 volumeSlider->setTracking( true );
119 CONNECT( volumeSlider, valueChanged( int ), this, valueChangedFilter( int ) );
120 CONNECT( this, valueReallyChanged( int ), this, userUpdateVolume( int ) );
121 CONNECT( THEMIM, volumeChanged( float ), this, libUpdateVolume( float ) );
122 CONNECT( THEMIM, soundMuteChanged( bool ), this, updateMuteStatus( bool ) );
125 SoundWidget::~SoundWidget()
128 delete volumeControlWidget;
131 void SoundWidget::refreshLabels()
133 int i_sliderVolume = volumeSlider->value();
137 volMuteLabel->setPixmap( QPixmap(":/toolbar/volume-muted" ) );
138 volMuteLabel->setToolTip(qfu(vlc_pgettext("Tooltip|Unmute", "Unmute")));
142 if( i_sliderVolume < VOLUME_MAX / 3 )
143 volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-low" ) );
144 else if( i_sliderVolume > (VOLUME_MAX * 2 / 3 ) )
145 volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-high" ) );
146 else volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-medium" ) );
147 volMuteLabel->setToolTip( qfu(vlc_pgettext("Tooltip|Mute", "Mute")) );
150 /* volumeSlider changed value event slot */
151 void SoundWidget::userUpdateVolume( int i_sliderVolume )
153 /* Only if volume is set by user action on slider */
155 playlist_VolumeSet( THEPL, i_sliderVolume / 100.f );
159 /* libvlc changed value event slot */
160 void SoundWidget::libUpdateVolume( float volume )
162 long i_volume = lroundf(volume * 100.f);
163 if( i_volume != volumeSlider->value() )
165 b_ignore_valuechanged = true;
166 volumeSlider->setValue( i_volume );
167 b_ignore_valuechanged = false;
172 void SoundWidget::valueChangedFilter( int i_val )
174 /* valueChanged is also emitted when the lib setValue() */
175 if ( !b_ignore_valuechanged ) emit valueReallyChanged( i_val );
178 /* libvlc mute/unmute event slot */
179 void SoundWidget::updateMuteStatus( bool mute )
183 SoundSlider *soundSlider = qobject_cast<SoundSlider *>(volumeSlider);
185 soundSlider->setMuted( mute );
189 void SoundWidget::showVolumeMenu( QPoint pos )
191 volumeMenu->setFixedHeight( volumeMenu->sizeHint().height() );
192 volumeMenu->exec( QCursor::pos() - pos - QPoint( 0, volumeMenu->height()/2 )
193 + QPoint( width(), height() /2) );
196 void SoundWidget::setMuted( bool mute )
199 playlist_t *p_playlist = pl_Get( p_intf );
200 playlist_MuteSet( p_playlist, mute );
203 bool SoundWidget::eventFilter( QObject *obj, QEvent *e )
206 if( e->type() == QEvent::MouseButtonPress )
208 QMouseEvent *event = static_cast<QMouseEvent*>(e);
209 if( event->button() == Qt::LeftButton )
211 if( volumeSlider->orientation() == Qt::Vertical )
213 showVolumeMenu( event->pos() );
217 setMuted( !b_is_muted );
230 void PlayButton::updateButtonIcons( bool b_playing )
232 setIcon( b_playing ? QIcon( ":/toolbar/pause_b" ) : QIcon( ":/toolbar/play_b" ) );
233 setToolTip( b_playing ? qtr( "Pause the playback" )
234 : qtr( I_PLAY_TOOLTIP ) );
237 void AtoB_Button::updateButtonIcons( bool timeA, bool timeB )
239 if( !timeA && !timeB)
241 setIcon( QIcon( ":/toolbar/atob_nob" ) );
242 setToolTip( qtr( "Loop from point A to point B continuously\n"
243 "Click to set point A" ) );
245 else if( timeA && !timeB )
247 setIcon( QIcon( ":/toolbar/atob_noa" ) );
248 setToolTip( qtr( "Click to set point B" ) );
250 else if( timeA && timeB )
252 setIcon( QIcon( ":/toolbar/atob" ) );
253 setToolTip( qtr( "Stop the A to B loop" ) );
257 void LoopButton::updateButtonIcons( int value )
259 setChecked( value != NORMAL );
260 setIcon( ( value == REPEAT_ONE ) ? QIcon( ":/buttons/playlist/repeat_one" )
261 : QIcon( ":/buttons/playlist/repeat_all" ) );
264 void AspectRatioComboBox::updateRatios()
266 /* Clear the list before updating */
268 vlc_value_t val_list, text_list;
269 vout_thread_t* p_vout = THEMIM->getVout();
271 /* Disable if there is no vout */
274 addItem( qtr("Aspect Ratio") );
279 var_Change( p_vout, "aspect-ratio", VLC_VAR_GETLIST, &val_list, &text_list );
280 for( int i = 0; i < val_list.p_list->i_count; i++ )
281 addItem( qfu( text_list.p_list->p_values[i].psz_string ),
282 QString( val_list.p_list->p_values[i].psz_string ) );
284 var_FreeList( &val_list, &text_list );
285 vlc_object_release( p_vout );
288 void AspectRatioComboBox::updateAspectRatio( int x )
290 vout_thread_t* p_vout = THEMIM->getVout();
291 if( p_vout && x >= 0 )
293 var_SetString( p_vout, "aspect-ratio", qtu( itemData(x).toString() ) );
296 vlc_object_release( p_vout );