[vlc-devel] [PATCH 3/3] Qt: Audio control widget changes

Francois Cartegnie fcvlcdev at free.fr
Thu Jan 7 20:18:31 CET 2010


---
 modules/gui/qt4/components/controller_widget.cpp |   58 ++++++++++++++++------
 modules/gui/qt4/components/controller_widget.hpp |    5 ++
 modules/gui/qt4/input_manager.cpp                |   17 ++++++
 modules/gui/qt4/input_manager.hpp                |    2 +
 modules/gui/qt4/util/input_slider.cpp            |   51 +++++++++++++++++--
 modules/gui/qt4/util/input_slider.hpp            |    3 +
 6 files changed, 115 insertions(+), 21 deletions(-)

diff --git a/modules/gui/qt4/components/controller_widget.cpp b/modules/gui/qt4/components/controller_widget.cpp
index 9312783..2e584a4 100644
--- a/modules/gui/qt4/components/controller_widget.cpp
+++ b/modules/gui/qt4/components/controller_widget.cpp
@@ -43,7 +43,7 @@
 SoundWidget::SoundWidget( QWidget *_parent, intf_thread_t * _p_intf,
                           bool b_shiny, bool b_special )
                          : QWidget( _parent ), p_intf( _p_intf),
-                           b_my_volume( false )
+                           b_my_volume( false ), b_is_muted( false )
 {
     /* We need a layout for this widget */
     QHBoxLayout *layout = new QHBoxLayout( this );
@@ -120,6 +120,7 @@ SoundWidget::SoundWidget( QWidget *_parent, intf_thread_t * _p_intf,
     /* Volume control connection */
     CONNECT( volumeSlider, valueChanged( int ), this, updateVolume( int ) );
     CONNECT( THEMIM, volumeChanged( void ), this, updateVolume( void ) );
+    CONNECT( THEMIM, soundMuteChanged( void ), this, updateMuteStatus( void ) );
 }
 
 SoundWidget::~SoundWidget()
@@ -128,16 +129,11 @@ SoundWidget::~SoundWidget()
     delete volumeControlWidget;
 }
 
-void SoundWidget::updateVolume( int i_sliderVolume )
+void SoundWidget::refreshLabels()
 {
-    if( !b_my_volume )
-    {
-        int i_res = i_sliderVolume  * (AOUT_VOLUME_MAX / 2) / VOLUME_MAX;
-        playlist_t *p_playlist = pl_Hold( p_intf );
-        aout_VolumeSet( p_playlist, i_res );
-        pl_Release( p_intf );
-    }
-    if( i_sliderVolume == 0 )
+    int i_sliderVolume = volumeSlider->value();
+
+    if( b_is_muted )
     {
         volMuteLabel->setPixmap( QPixmap(":/toolbar/volume-muted" ) );
         volMuteLabel->setToolTip(qfu(vlc_pgettext("Tooltip|Unmute", "Unmute")));
@@ -152,6 +148,21 @@ void SoundWidget::updateVolume( int i_sliderVolume )
     volMuteLabel->setToolTip( qfu(vlc_pgettext("Tooltip|Mute", "Mute")) );
 }
 
+/* volumeSlider changed value event slot */
+void SoundWidget::updateVolume( int i_sliderVolume )
+{
+    if( !b_my_volume ) /* Only if volume is set by user action on slider */
+    {
+        setMuted( false );
+        playlist_t *p_playlist = pl_Hold( p_intf );
+        int i_res = i_sliderVolume  * (AOUT_VOLUME_MAX / 2) / VOLUME_MAX;
+        aout_VolumeSet( p_playlist, i_res );
+        pl_Release( p_intf );
+    }
+    refreshLabels();
+}
+
+/* libvlc changed value event slot */
 void SoundWidget::updateVolume()
 {
     /* Audio part */
@@ -163,7 +174,9 @@ void SoundWidget::updateVolume()
     i_volume = ( ( i_volume + 1 ) *  VOLUME_MAX )/ (AOUT_VOLUME_MAX/2);
     int i_gauge = volumeSlider->value();
     b_my_volume = false;
-    if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
+    if ( !b_is_muted && /* do not show mute effect on volume (set to 0) */
+        ( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
+    )
     {
         b_my_volume = true;
         volumeSlider->setValue( i_volume );
@@ -171,6 +184,16 @@ void SoundWidget::updateVolume()
     }
 }
 
+/* libvlc mute/unmute event slot */
+void SoundWidget::updateMuteStatus()
+{
+    playlist_t *p_playlist = pl_Hold( p_intf );
+    b_is_muted = aout_IsMuted( VLC_OBJECT(p_playlist) );
+    pl_Release( p_intf );
+    (qobject_cast<SoundSlider *>(volumeSlider))->setMuted( b_is_muted );
+    refreshLabels();
+}
+
 void SoundWidget::showVolumeMenu( QPoint pos )
 {
     volumeMenu->setFixedHeight( volumeMenu->sizeHint().height() );
@@ -178,6 +201,14 @@ void SoundWidget::showVolumeMenu( QPoint pos )
                           + QPoint( width(), height() /2) );
 }
 
+void SoundWidget::setMuted( bool mute )
+{
+    b_is_muted = mute;
+    playlist_t *p_playlist = pl_Hold( p_intf );
+    aout_SetMute( VLC_OBJECT(p_playlist), NULL, mute );
+    pl_Release( p_intf );
+}
+
 bool SoundWidget::eventFilter( QObject *obj, QEvent *e )
 {
     VLC_UNUSED( obj );
@@ -190,10 +221,7 @@ bool SoundWidget::eventFilter( QObject *obj, QEvent *e )
         }
         else
         {
-            playlist_t *p_playlist = pl_Hold( p_intf );
-
-            aout_ToggleMute( p_playlist, NULL );
-            pl_Release( p_intf );
+            setMuted( !b_is_muted );
         }
         e->accept();
         return true;
diff --git a/modules/gui/qt4/components/controller_widget.hpp b/modules/gui/qt4/components/controller_widget.hpp
index 9714c05..f68b238 100644
--- a/modules/gui/qt4/components/controller_widget.hpp
+++ b/modules/gui/qt4/components/controller_widget.hpp
@@ -76,6 +76,7 @@ public:
     SoundWidget( QWidget *parent, intf_thread_t  *_p_i, bool,
                  bool b_special = false );
     virtual ~SoundWidget();
+    void setMuted( bool );
 
 private:
     intf_thread_t       *p_intf;
@@ -85,9 +86,13 @@ private:
     bool                 b_my_volume;
     QMenu               *volumeMenu;
     virtual bool eventFilter( QObject *obj, QEvent *e );
+    bool                b_is_muted;
+
 protected slots:
     void updateVolume( int );
     void updateVolume( void );
+    void updateMuteStatus( void );
+    void refreshLabels( void );
     void showVolumeMenu( QPoint pos );
 };
 
diff --git a/modules/gui/qt4/input_manager.cpp b/modules/gui/qt4/input_manager.cpp
index 2317e0c..919f154 100644
--- a/modules/gui/qt4/input_manager.cpp
+++ b/modules/gui/qt4/input_manager.cpp
@@ -45,6 +45,8 @@ static int PLItemRemoved( vlc_object_t *, const char *,
                         vlc_value_t, vlc_value_t, void * );
 static int VolumeChanged( vlc_object_t *, const char *,
                         vlc_value_t, vlc_value_t, void * );
+static int SoundMuteChanged( vlc_object_t *, const char *,
+                        vlc_value_t, vlc_value_t, void * );
 
 static int RandomChanged( vlc_object_t *, const char *,
                         vlc_value_t, vlc_value_t, void * );
@@ -901,6 +903,7 @@ MainInputManager::MainInputManager( intf_thread_t *_p_intf )
     var_AddCallback( THEPL, "loop", LoopChanged, this );
 
     var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
+    var_AddCallback( p_intf->p_libvlc, "volume-muted", SoundMuteChanged, this );
 
     /* Warn our embedded IM about input changes */
     CONNECT( this, inputChanged( input_thread_t * ),
@@ -931,6 +934,7 @@ MainInputManager::~MainInputManager()
     }
 
     var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
+    var_DelCallback( p_intf->p_libvlc, "volume-muted", SoundMuteChanged, this );
 
     var_DelCallback( THEPL, "activity", PLItemChanged, this );
     var_DelCallback( THEPL, "item-change", ItemChanged, im );
@@ -966,6 +970,9 @@ void MainInputManager::customEvent( QEvent *event )
     case VolumeChanged_Type:
         emit volumeChanged();
         return;
+    case SoundMuteChanged_Type:
+        emit soundMuteChanged();
+        return;
     case PLItemAppended_Type:
         plEv = static_cast<PLEvent*>( event );
         emit playlistItemAppended( plEv->i_item, plEv->i_parent );
@@ -1107,6 +1114,16 @@ static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
     return VLC_SUCCESS;
 }
 
+static int SoundMuteChanged( vlc_object_t *p_this, const char *psz_var,
+                        vlc_value_t oldval, vlc_value_t newval, void *param )
+{
+    MainInputManager *mim = (MainInputManager*)param;
+
+    IMEvent *event = new IMEvent( SoundMuteChanged_Type );
+    QApplication::postEvent( mim, event );
+    return VLC_SUCCESS;
+}
+
 static int PLItemAppended
 ( vlc_object_t * obj, const char *var, vlc_value_t old, vlc_value_t cur, void *data )
 {
diff --git a/modules/gui/qt4/input_manager.hpp b/modules/gui/qt4/input_manager.hpp
index 03a7f35..b8f4f5e 100644
--- a/modules/gui/qt4/input_manager.hpp
+++ b/modules/gui/qt4/input_manager.hpp
@@ -44,6 +44,7 @@ enum {
     ItemTitleChanged_Type,
     ItemRateChanged_Type,
     VolumeChanged_Type,
+    SoundMuteChanged_Type,
     ItemEsChanged_Type,
     ItemTeletextChanged_Type,
     InterfaceVoutUpdate_Type,
@@ -278,6 +279,7 @@ public slots:
 signals:
     void inputChanged( input_thread_t * );
     void volumeChanged();
+    void soundMuteChanged();
     void playlistItemAppended( int itemId, int parentId );
     void playlistItemRemoved( int itemId );
     void randomChanged( bool );
diff --git a/modules/gui/qt4/util/input_slider.cpp b/modules/gui/qt4/util/input_slider.cpp
index c2d861c..a91b564 100644
--- a/modules/gui/qt4/util/input_slider.cpp
+++ b/modules/gui/qt4/util/input_slider.cpp
@@ -157,6 +157,7 @@ SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
     setMouseTracking( true );
     b_isSliding = false;
     b_mouseOutside = true;
+    b_isMuted = false;
 
     pixOutside = QPixmap( ":/toolbar/volslide-outside" );
 
@@ -166,9 +167,11 @@ SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
     setMinimumSize( pixOutside.size() );
 
     pixGradient = QPixmap( mask.size() );
+    pixGradient2 = QPixmap( mask.size() );
 
     /* Gradient building from the preferences */
     QLinearGradient gradient( paddingL, 2, WLENGTH + paddingL , 2 );
+    QLinearGradient gradient2( paddingL, 2, WLENGTH + paddingL , 2 );
 
     QStringList colorList = qfu( psz_colors ).split( ";" );
     free( psz_colors );
@@ -178,11 +181,28 @@ SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
         for( int i = colorList.size(); i < 12; i++)
             colorList.append( "255" );
 
+    /* Regular colors */
 #define c(i) colorList.at(i).toInt()
-    gradient.setColorAt( 0.0, QColor( c(0), c(1), c(2) ) );
-    gradient.setColorAt( 0.22, QColor( c(3), c(4), c(5) ) );
-    gradient.setColorAt( 0.5, QColor( c(6), c(7), c(8) ) );
-    gradient.setColorAt( 1.0, QColor( c(9), c(10), c(11) ) );
+#define add_color(gradient, range, c1, c2, c3) \
+    gradient.setColorAt( range, QColor( c(c1), c(c2), c(c3) ) );
+
+    /* Desaturated colors */
+#define desaturate(c) c->setHsvF( c->hueF(), 0.2 , 0.5, 1.0 )
+#define add_desaturated_color(gradient, range, c1, c2, c3) \
+    foo = new QColor( c(c1), c(c2), c(c3) );\
+    desaturate( foo ); gradient.setColorAt( range, *foo );\
+    delete foo;
+
+    /* combine the two helpers */
+#define add_colors( gradient1, gradient2, range, c1, c2, c3 )\
+    add_color( gradient1, range, c1, c2, c3 ); \
+    add_desaturated_color( gradient2, range, c1, c2, c3 );
+
+    QColor * foo;
+    add_colors( gradient, gradient2, 0.0, 0, 1, 2 );
+    add_colors( gradient, gradient2, 0.22, 3, 4, 5 );
+    add_colors( gradient, gradient2, 0.5, 6, 7, 8 );
+    add_colors( gradient, gradient2, 1.0, 9, 10, 11 );
 
     QPainter painter( &pixGradient );
     painter.setPen( Qt::NoPen );
@@ -190,7 +210,14 @@ SoundSlider::SoundSlider( QWidget *_parent, int _i_step, bool b_hard,
     painter.drawRect( pixGradient.rect() );
     painter.end();
 
+    painter.begin( &pixGradient2 );
+    painter.setPen( Qt::NoPen );
+    painter.setBrush( gradient2 );
+    painter.drawRect( pixGradient2.rect() );
+    painter.end();
+
     pixGradient.setMask( mask );
+    pixGradient2.setMask( mask );
 }
 
 void SoundSlider::wheelEvent( QWheelEvent *event )
@@ -259,13 +286,25 @@ void SoundSlider::changeValue( int x )
     setValue( (x * maximum() + 40 ) / WLENGTH );
 }
 
+void SoundSlider::setMuted( bool m )
+{
+    b_isMuted = m;
+    update();
+}
+
 void SoundSlider::paintEvent( QPaintEvent *e )
 {
     QPainter painter( this );
+    QPixmap *pixGradient;
+    if (b_isMuted)
+        pixGradient = &this->pixGradient2;
+    else
+        pixGradient = &this->pixGradient;
+
     const int offset = int( ( WLENGTH * value() + 100 ) / maximum() ) + paddingL;
 
-    const QRectF boundsG( 0, 0, offset , pixGradient.height() );
-    painter.drawPixmap( boundsG, pixGradient, boundsG );
+    const QRectF boundsG( 0, 0, offset , pixGradient->height() );
+    painter.drawPixmap( boundsG, *pixGradient, boundsG );
 
     const QRectF boundsO( 0, 0, pixOutside.width(), pixOutside.height() );
     painter.drawPixmap( boundsO, pixOutside, boundsO );
diff --git a/modules/gui/qt4/util/input_slider.hpp b/modules/gui/qt4/util/input_slider.hpp
index 4925d19..8bdc58f 100644
--- a/modules/gui/qt4/util/input_slider.hpp
+++ b/modules/gui/qt4/util/input_slider.hpp
@@ -73,6 +73,7 @@ class SoundSlider : public QAbstractSlider
 public:
     SoundSlider( QWidget *_parent, int _i_step, bool b_softamp, char * );
     virtual ~SoundSlider() {};
+    void setMuted( bool ); /* Set Mute status */
 
 protected:
     const static int paddingL = 3;
@@ -89,8 +90,10 @@ private:
     bool b_mouseOutside; /* Whether the mouse is outside or inside the Widget */
     int i_oldvalue; /* Store the old Value before changing */
     float f_step; /* How much do we increase each time we wheel */
+    bool b_isMuted;
 
     QPixmap pixGradient; /* Gradient pix storage */
+    QPixmap pixGradient2; /* Muted Gradient pix storage */
     QPixmap pixOutside; /* OutLine pix storage */
 
     void changeValue( int x ); /* Function to modify the value from pixel x() */
-- 
1.6.4.4




More information about the vlc-devel mailing list