controller_widget.cpp 8.35 KB
Newer Older
1
2
3
/*****************************************************************************
 * Controller_widget.cpp : Controller Widget for the controllers
 ****************************************************************************
4
 * Copyright (C) 2006-2008 the VideoLAN team
5
6
 * $Id$
 *
7
 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * ( at your option ) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "controller_widget.hpp"
29
#include "controller.hpp"
30

31
32
33
34
#include "input_manager.hpp"         /* Get notification of Volume Change */
#include "util/input_slider.hpp"     /* SoundSlider */

#include <vlc_aout.h>                /* Volume functions */
35
36
37

#include <QLabel>
#include <QHBoxLayout>
38
#include <QSpinBox>
39
40
#include <QMenu>
#include <QWidgetAction>
41
#include <QMouseEvent>
42
43

SoundWidget::SoundWidget( QWidget *_parent, intf_thread_t * _p_intf,
44
                          bool b_shiny, bool b_special )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
45
                         : QWidget( _parent ), p_intf( _p_intf),
46
                           b_is_muted( false )
47
{
48
    /* We need a layout for this widget */
49
50
51
    QHBoxLayout *layout = new QHBoxLayout( this );
    layout->setSpacing( 0 ); layout->setMargin( 0 );

52
    /* We need a Label for the pix */
53
    volMuteLabel = new QLabel;
54
    volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-medium" ) );
55
56
57
58

    /* We might need a subLayout too */
    QVBoxLayout *subLayout;

59
60
    volMuteLabel->installEventFilter( this );

61
62
63
    /* Normal View, click on icon mutes */
    if( !b_special )
    {
64
        volumeMenu = NULL; subLayout = NULL;
65
        volumeControlWidget = NULL;
66
67
68
69
70
71
    }
    else
    {
        /* Special view, click on button shows the slider */
        b_shiny = false;

72
        volumeControlWidget = new QFrame;
73
        subLayout = new QVBoxLayout( volumeControlWidget );
74
        subLayout->setContentsMargins( 4, 4, 4, 4 );
75
76
77
78
79
80
81
82
        volumeMenu = new QMenu( this );

        QWidgetAction *widgetAction = new QWidgetAction( volumeControlWidget );
        widgetAction->setDefaultWidget( volumeControlWidget );
        volumeMenu->addAction( widgetAction );
    }

    /* And add the label */
83
84
    layout->addWidget( volMuteLabel );

85
    /* Slider creation: shiny or clean */
86
87
88
89
90
91
92
93
94
    if( b_shiny )
    {
        volumeSlider = new SoundSlider( this,
            config_GetInt( p_intf, "volume-step" ),
            config_GetInt( p_intf, "qt-volume-complete" ),
            config_GetPsz( p_intf, "qt-slider-colours" ) );
    }
    else
    {
95
96
97
        volumeSlider = new QSlider( NULL );
        volumeSlider->setOrientation( b_special ? Qt::Vertical
                                                : Qt::Horizontal );
98
99
        volumeSlider->setMaximum( config_GetInt( p_intf, "qt-volume-complete" )
                                  ? 400 : 200 );
100
    }
101
102
103
104
105
106
    if( volumeSlider->orientation() ==  Qt::Horizontal )
    {
        volumeSlider->setMaximumSize( QSize( 200, 40 ) );
        volumeSlider->setMinimumSize( QSize( 85, 30 ) );
    }

107
    volumeSlider->setFocusPolicy( Qt::NoFocus );
108
109
110
    if( b_special )
        subLayout->addWidget( volumeSlider );
    else
111
        layout->addWidget( volumeSlider, 0, Qt::AlignBottom  );
112
113

    /* Set the volume from the config */
114
    libUpdateVolume();
115
    /* Force the update at build time in order to have a muted icon if needed */
116
    updateMuteStatus();
117
118

    /* Volume control connection */
119
    CONNECT( volumeSlider, valueChanged( int ), this, refreshLabels( void ) );
120
121
    CONNECT( volumeSlider, sliderMoved( int ), this, userUpdateVolume( int ) );
    CONNECT( THEMIM, volumeChanged( void ), this, libUpdateVolume( void ) );
122
    CONNECT( THEMIM, soundMuteChanged( void ), this, updateMuteStatus( void ) );
123
124
}

125
126
127
128
129
130
SoundWidget::~SoundWidget()
{
    delete volumeSlider;
    delete volumeControlWidget;
}

131
void SoundWidget::refreshLabels()
132
{
133
134
135
    int i_sliderVolume = volumeSlider->value();

    if( b_is_muted )
136
    {
137
        volMuteLabel->setPixmap( QPixmap(":/toolbar/volume-muted" ) );
138
        volMuteLabel->setToolTip(qfu(vlc_pgettext("Tooltip|Unmute", "Unmute")));
139
140
141
142
        return;
    }

    if( i_sliderVolume < VOLUME_MAX / 3 )
143
        volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-low" ) );
144
    else if( i_sliderVolume > (VOLUME_MAX * 2 / 3 ) )
145
146
        volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-high" ) );
    else volMuteLabel->setPixmap( QPixmap( ":/toolbar/volume-medium" ) );
147
    volMuteLabel->setToolTip( qfu(vlc_pgettext("Tooltip|Mute", "Mute")) );
148
149
}

150
/* volumeSlider changed value event slot */
151
void SoundWidget::userUpdateVolume( int i_sliderVolume )
152
{
153
154
155
156
157
158
    /* 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 );
159
160
161
}

/* libvlc changed value event slot */
162
void SoundWidget::libUpdateVolume()
163
164
165
{
    /* Audio part */
    audio_volume_t i_volume;
166
167
168
169
    playlist_t *p_playlist = pl_Hold( p_intf );

    aout_VolumeGet( p_playlist, &i_volume );
    pl_Release( p_intf );
170
    i_volume = ( ( i_volume + 1 ) *  VOLUME_MAX )/ (AOUT_VOLUME_MAX/2);
171
    int i_gauge = volumeSlider->value();
172
173
174
    if ( !b_is_muted && /* do not show mute effect on volume (set to 0) */
        ( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
    )
175
176
177
178
179
    {
        volumeSlider->setValue( i_volume );
    }
}

180
181
182
183
184
185
/* 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 );
186
187
188
189

    SoundSlider *soundSlider = qobject_cast<SoundSlider *>(volumeSlider);
    if( soundSlider )
        soundSlider->setMuted( b_is_muted );
190
191
192
    refreshLabels();
}

193
194
void SoundWidget::showVolumeMenu( QPoint pos )
{
195
    volumeMenu->setFixedHeight( volumeMenu->sizeHint().height() );
196
197
198
199
    volumeMenu->exec( QCursor::pos() - pos - QPoint( 0, volumeMenu->height()/2 )
                          + QPoint( width(), height() /2) );
}

200
201
202
203
204
205
206
207
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 );
}

208
209
210
211
212
213
214
215
216
217
218
219
bool SoundWidget::eventFilter( QObject *obj, QEvent *e )
{
    VLC_UNUSED( obj );
    if (e->type() == QEvent::MouseButtonPress  )
    {
        if( volumeSlider->orientation() ==  Qt::Vertical )
        {
            QMouseEvent *event = static_cast<QMouseEvent*>(e);
            showVolumeMenu( event->pos() );
        }
        else
        {
220
            setMuted( !b_is_muted );
221
222
223
224
225
226
227
228
229
230
231
232
233
234
        }
        e->accept();
        return true;
    }
    else
    {
        e->ignore();
        return false;
    }
}

/**
 * Play Button
 **/
235
236
void PlayButton::updateButton( bool b_playing )
{
237
    setIcon( b_playing ? QIcon( ":/toolbar/pause_b" ) : QIcon( ":/toolbar/play_b" ) );
238
239
240
241
242
243
244
245
    setToolTip( b_playing ? qtr( "Pause the playback" )
                          : qtr( I_PLAY_TOOLTIP ) );
}

void AtoB_Button::setIcons( bool timeA, bool timeB )
{
    if( !timeA && !timeB)
    {
246
        setIcon( QIcon( ":/toolbar/atob_nob" ) );
247
248
249
250
251
        setToolTip( qtr( "Loop from point A to point B continuously\n"
                         "Click to set point A" ) );
    }
    else if( timeA && !timeB )
    {
252
        setIcon( QIcon( ":/toolbar/atob_noa" ) );
253
254
255
256
        setToolTip( qtr( "Click to set point B" ) );
    }
    else if( timeA && timeB )
    {
257
        setIcon( QIcon( ":/toolbar/atob" ) );
258
259
260
261
        setToolTip( qtr( "Stop the A to B loop" ) );
    }
}

262
void LoopButton::updateIcons( int value )
263
{
264
265
266
    setChecked( value != NORMAL );
    setIcon( ( value == REPEAT_ALL ) ? QIcon( ":/buttons/playlist/repeat_all" )
                                     : QIcon( ":/buttons/playlist/repeat_one" ) );
267
}