1
/*  This file is part of the KDE project
2
    Copyright (C) 2006-2007 Matthias Kretz <kretz@kde.org>
3
4
    This library is free software; you can redistribute it and/or
5
    modify it under the terms of the GNU Lesser General Public
6
    License as published by the Free Software Foundation; either
7
    version 2.1 of the License, or (at your option) version 3, or any
8
    later version accepted by the membership of KDE e.V. (or its
9
    successor approved by the membership of KDE e.V.), Nokia Corporation 
10
    (or its successors, if any) and the KDE Free Qt Foundation, which shall
11
    act as a proxy defined in Section 6 of version 3 of the license.
12
13
    This library is distributed in the hope that it will be useful,
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
    Lesser General Public License for more details.
17
18
    You should have received a copy of the GNU Lesser General Public 
19
    License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
21
*/
22
23
#include "volumeslider.h"
24
#include "volumeslider_p.h"
25
#include "audiooutput.h"
26
#include "phonondefs_p.h"
27
#include "phononnamespace_p.h"
28
#include "factory_p.h"
29
30
QT_BEGIN_NAMESPACE
31
32
#ifndef QT_NO_PHONON_VOLUMESLIDER
33
34
namespace Phonon
35
{
36
VolumeSlider::VolumeSlider(QWidget *parent)
37
    : QWidget(parent),
38
    k_ptr(new VolumeSliderPrivate(this))
39
{
40
    K_D(VolumeSlider);
41
#ifndef QT_NO_TOOLTIP
42
    setToolTip(tr("Volume: %1%").arg(100));
43
#endif
44
#ifndef QT_NO_WHATSTHIS
45
    setWhatsThis(tr("Use this slider to adjust the volume. The leftmost position is 0%. The rightmost is %1%").arg(100));
46
#endif
47
48
    connect(&d->slider, SIGNAL(valueChanged(int)), SLOT(_k_sliderChanged(int)));
49
    connect(&d->muteButton, SIGNAL(clicked()), SLOT(_k_buttonClicked()));
50
51
    setFocusProxy(&d->slider);
52
}
53
54
VolumeSlider::VolumeSlider(AudioOutput *output, QWidget *parent)
55
    : QWidget(parent),
56
    k_ptr(new VolumeSliderPrivate(this))
57
{
58
    K_D(VolumeSlider);
59
#ifndef QT_NO_TOOLTIP
60
    setToolTip(tr("Volume: %1%").arg(100));
61
#endif
62
#ifndef QT_NO_WHATSTHIS
63
    setWhatsThis(tr("Use this slider to adjust the volume. The leftmost position is 0%. The rightmost is %1%").arg(100));
64
#endif
65
66
    connect(&d->slider, SIGNAL(valueChanged(int)), SLOT(_k_sliderChanged(int)));
67
    connect(&d->muteButton, SIGNAL(clicked()), SLOT(_k_buttonClicked()));
68
69
    if (output) {
70
        d->output = output;
71
        d->slider.setValue(qRound(100 * output->volume()));
72
        d->slider.setEnabled(true);
73
        d->muteButton.setEnabled(true);
74
        connect(output, SIGNAL(volumeChanged(qreal)), SLOT(_k_volumeChanged(qreal)));
75
        connect(output, SIGNAL(mutedChanged(bool)), SLOT(_k_mutedChanged(bool)));
76
    }
77
78
    setFocusProxy(&d->slider);
79
}
80
81
VolumeSlider::~VolumeSlider()
82
{
83
    delete k_ptr;
84
}
85
86
bool VolumeSlider::isMuteVisible() const
87
{
88
    return !k_ptr->muteButton.isHidden();
89
}
90
91
void VolumeSlider::setMuteVisible(bool visible)
92
{
93
    k_ptr->muteButton.setVisible(visible);
94
}
95
96
QSize VolumeSlider::iconSize() const
97
{
98
    return k_ptr->muteButton.iconSize();
99
}
100
101
void VolumeSlider::setIconSize(const QSize &iconSize)
102
{
103
    pDebug() << Q_FUNC_INFO << iconSize;
104
    k_ptr->muteButton.setIconSize(iconSize);
105
}
106
107
qreal VolumeSlider::maximumVolume() const
108
{
109
    return k_ptr->slider.maximum() * 0.01;
110
}
111
112
void VolumeSlider::setMaximumVolume(qreal volume)
113
{
114
    int max = static_cast<int>(volume * 100);
115
    k_ptr->slider.setMaximum(max);
116
#ifndef QT_NO_WHATSTHIS
117
    setWhatsThis(tr("Use this slider to adjust the volume. The leftmost position is 0%. The rightmost is %1%")
118
            .arg(max));
119
#endif
120
}
121
122
Qt::Orientation VolumeSlider::orientation() const
123
{
124
    return k_ptr->slider.orientation();
125
}
126
127
void VolumeSlider::setOrientation(Qt::Orientation o)
128
{
129
    K_D(VolumeSlider);
130
    Qt::Alignment align = (o == Qt::Horizontal ? Qt::AlignVCenter : Qt::AlignHCenter);
131
    d->layout.setAlignment(&d->muteButton, align);
132
    d->layout.setAlignment(&d->slider, align);
133
    d->layout.setDirection(o == Qt::Horizontal ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom);
134
    d->slider.setOrientation(o);
135
}
136
137
AudioOutput *VolumeSlider::audioOutput() const
138
{
139
    K_D(const VolumeSlider);
140
    return d->output;
141
}
142
143
void VolumeSlider::setAudioOutput(AudioOutput *output)
144
{
145
    K_D(VolumeSlider);
146
    if (d->output) {
147
        disconnect(d->output, 0, this, 0);
148
    }
149
    d->output = output;
150
    if (output) {
151
        d->slider.setValue(qRound(100 * output->volume()));
152
        d->slider.setEnabled(true);
153
        d->muteButton.setEnabled(true);
154
155
        d->_k_volumeChanged(output->volume());
156
        d->_k_mutedChanged(output->isMuted());
157
158
        connect(output, SIGNAL(volumeChanged(qreal)), SLOT(_k_volumeChanged(qreal)));
159
        connect(output, SIGNAL(mutedChanged(bool)), SLOT(_k_mutedChanged(bool)));
160
    } else {
161
        d->slider.setValue(100);
162
        d->slider.setEnabled(false);
163
        d->muteButton.setEnabled(false);
164
    }
165
}
166
167
void VolumeSliderPrivate::_k_buttonClicked()
168
{
169
    if (output) {
170
        output->setMuted(!output->isMuted());
171
    } else {
172
        slider.setEnabled(false);
173
        muteButton.setEnabled(false);
174
    }
175
}
176
177
void VolumeSliderPrivate::_k_mutedChanged(bool muted)
178
{
179
#ifndef QT_NO_TOOLTIP
180
    Q_Q(VolumeSlider);
181
#endif
182
    if (muted) {
183
#ifndef QT_NO_TOOLTIP
184
        q->setToolTip(VolumeSlider::tr("Muted"));
185
#endif
186
        muteButton.setIcon(mutedIcon);
187
    } else {
188
#ifndef QT_NO_TOOLTIP
189
        q->setToolTip(VolumeSlider::tr("Volume: %1%").arg(static_cast<int>(output->volume() * 100.0)));
190
#endif
191
        muteButton.setIcon(volumeIcon);
192
    }
193
}
194
195
void VolumeSliderPrivate::_k_sliderChanged(int value)
196
{
197
#ifndef QT_NO_TOOLTIP
198
    Q_Q(VolumeSlider);
199
#endif
200
201
    if (output) {
202
#ifndef QT_NO_TOOLTIP
203
        if (!output->isMuted()) {
204
           q->setToolTip(VolumeSlider::tr("Volume: %1%").arg(value));
205
        }
206
#endif
207
208
        ignoreVolumeChange = true;
209
        output->setVolume((static_cast<qreal>(value)) * 0.01);
210
        ignoreVolumeChange = false;
211
    } else {
212
        slider.setEnabled(false);
213
        muteButton.setEnabled(false);
214
    }
215
}
216
217
void VolumeSliderPrivate::_k_volumeChanged(qreal value)
218
{
219
    if (!ignoreVolumeChange) {
220
        slider.setValue(qRound(100 * value));
221
    }
222
}
223
224
bool VolumeSlider::hasTracking() const
225
{
226
    return k_ptr->slider.hasTracking();
227
}
228
229
void VolumeSlider::setTracking(bool tracking)
230
{
231
    k_ptr->slider.setTracking(tracking);
232
}
233
234
int VolumeSlider::pageStep() const
235
{
236
    return k_ptr->slider.pageStep();
237
}
238
239
void VolumeSlider::setPageStep(int milliseconds)
240
{
241
    k_ptr->slider.setPageStep(milliseconds);
242
}
243
244
int VolumeSlider::singleStep() const
245
{
246
    return k_ptr->slider.singleStep();
247
}
248
249
void VolumeSlider::setSingleStep(int milliseconds)
250
{
251
    k_ptr->slider.setSingleStep(milliseconds);
252
}
253
254
} // namespace Phonon
255
256
#endif //QT_NO_PHONON_VOLUMESLIDER
257
258
QT_END_NAMESPACE
259
260
#include "moc_volumeslider.cpp"
261
262
// vim: sw=4 et