Phonon

volumeslider.cpp
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#ifndef QT_NO_PHONON_VOLUMESLIDER
31
32namespace Phonon
33{
35 : QWidget(parent),
36 k_ptr(new VolumeSliderPrivate(this))
37{
38 P_D(VolumeSlider);
39#ifndef QT_NO_TOOLTIP
40 setToolTip(tr("Volume: %1%").arg(100));
41#endif
42#ifndef QT_NO_WHATSTHIS
43 setWhatsThis(tr("Use this slider to adjust the volume. The leftmost position is 0%, the rightmost is %1%").arg(100));
44#endif
45
46 connect(&d->slider, SIGNAL(valueChanged(int)), SLOT(_k_sliderChanged(int)));
47 connect(&d->slider, SIGNAL(sliderPressed()), this, SLOT(_k_sliderPressed()));
48 connect(&d->slider, SIGNAL(sliderReleased()), this, SLOT(_k_sliderReleased()));
49 connect(&d->slider, SIGNAL(scrollStart()), this, SLOT(_k_sliderPressed()));
50 connect(&d->slider, SIGNAL(scrollEnd()), this, SLOT(_k_sliderReleased()));
51 connect(&d->muteButton, SIGNAL(clicked()), SLOT(_k_buttonClicked()));
52
53 setFocusProxy(&d->slider);
54}
55
57 : QWidget(parent),
58 k_ptr(new VolumeSliderPrivate(this))
59{
60 P_D(VolumeSlider);
61#ifndef QT_NO_TOOLTIP
62 setToolTip(tr("Volume: %1%").arg(100));
63#endif
64#ifndef QT_NO_WHATSTHIS
65 setWhatsThis(tr("Use this slider to adjust the volume. The leftmost position is 0%, the rightmost is %1%").arg(100));
66#endif
67
68 connect(&d->slider, SIGNAL(valueChanged(int)), SLOT(_k_sliderChanged(int)));
69 connect(&d->slider, SIGNAL(sliderPressed()), this, SLOT(_k_sliderPressed()));
70 connect(&d->slider, SIGNAL(sliderReleased()), this, SLOT(_k_sliderReleased()));
71 connect(&d->slider, SIGNAL(scrollStart()), this, SLOT(_k_sliderPressed()));
72 connect(&d->slider, SIGNAL(scrollEnd()), this, SLOT(_k_sliderReleased()));
73 connect(&d->muteButton, SIGNAL(clicked()), SLOT(_k_buttonClicked()));
74
75 if (output) {
76 d->output = output;
77 d->slider.setValue(qRound(100 * output->volume()));
78 d->slider.setEnabled(true);
79 d->muteButton.setEnabled(true);
80 connect(output, SIGNAL(volumeChanged(qreal)), SLOT(_k_volumeChanged(qreal)));
81 connect(output, SIGNAL(mutedChanged(bool)), SLOT(_k_mutedChanged(bool)));
82 }
83
84 setFocusProxy(&d->slider);
85}
86
87VolumeSlider::~VolumeSlider()
88{
89 delete k_ptr;
90}
91
92bool VolumeSlider::isMuteVisible() const
93{
94 return !k_ptr->muteButton.isHidden();
95}
96
97void VolumeSlider::setMuteVisible(bool visible)
98{
99 k_ptr->muteButton.setVisible(visible);
100}
101
103{
104 return k_ptr->muteButton.iconSize();
105}
106
107void VolumeSlider::setIconSize(const QSize &iconSize)
108{
109 pDebug() << Q_FUNC_INFO << iconSize;
110 k_ptr->muteButton.setIconSize(iconSize);
111}
112
113qreal VolumeSlider::maximumVolume() const
114{
115 return k_ptr->slider.maximum() * 0.01;
116}
117
118void VolumeSlider::setMaximumVolume(qreal volume)
119{
120 int max = static_cast<int>(volume * 100);
121 k_ptr->slider.setMaximum(max);
122#ifndef QT_NO_WHATSTHIS
123 setWhatsThis(tr("Use this slider to adjust the volume. The leftmost position is 0%, the rightmost is %1%")
124 .arg(max));
125#endif
126}
127
129{
130 return k_ptr->slider.orientation();
131}
132
133void VolumeSlider::setOrientation(Qt::Orientation o)
134{
135 P_D(VolumeSlider);
137 d->layout.setAlignment(&d->muteButton, align);
138 d->layout.setAlignment(&d->slider, align);
139 d->layout.setDirection(o == Qt::Horizontal ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom);
140 d->slider.setOrientation(o);
141}
142
143AudioOutput *VolumeSlider::audioOutput() const
144{
145 P_D(const VolumeSlider);
146 return d->output;
147}
148
150{
151 P_D(VolumeSlider);
152 if (d->output) {
153 disconnect(d->output, nullptr, this, nullptr);
154 }
155 d->output = output;
156 if (output) {
157 d->slider.setValue(qRound(100 * output->volume()));
158 d->slider.setEnabled(true);
159 d->muteButton.setEnabled(true);
160
161 d->_k_volumeChanged(output->volume());
162 d->_k_mutedChanged(output->isMuted());
163
164 connect(output, SIGNAL(volumeChanged(qreal)), SLOT(_k_volumeChanged(qreal)));
165 connect(output, SIGNAL(mutedChanged(bool)), SLOT(_k_mutedChanged(bool)));
166 } else {
167 d->slider.setValue(100);
168 d->slider.setEnabled(false);
169 d->muteButton.setEnabled(false);
170 }
171}
172
173void VolumeSliderPrivate::_k_buttonClicked()
174{
175 if (output) {
176 output->setMuted(!output->isMuted());
177 } else {
178 slider.setEnabled(false);
179 muteButton.setEnabled(false);
180 }
181}
182
183void VolumeSliderPrivate::_k_sliderPressed()
184{
185 sliderPressed = true;
186}
187
188void VolumeSliderPrivate::_k_sliderReleased()
189{
190 sliderPressed = false;
191 if (output) {
192 _k_volumeChanged(output->volume());
193 }
194}
195
196void VolumeSliderPrivate::_k_mutedChanged(bool muted)
197{
198#ifndef QT_NO_TOOLTIP
199 P_Q(VolumeSlider);
200#endif
201 if (muted) {
202#ifndef QT_NO_TOOLTIP
203 q->setToolTip(VolumeSlider::tr("Muted"));
204#endif
205 muteButton.setIcon(mutedIcon);
206 } else {
207#ifndef QT_NO_TOOLTIP
208 q->setToolTip(VolumeSlider::tr("Volume: %1%").arg(static_cast<int>(output->volume() * 100.0)));
209#endif
210 muteButton.setIcon(volumeIcon);
211 }
212}
213
214void VolumeSliderPrivate::_k_sliderChanged(int value)
215{
216#ifndef QT_NO_TOOLTIP
217 P_Q(VolumeSlider);
218#endif
219
220 if (output) {
221#ifndef QT_NO_TOOLTIP
222 if (!output->isMuted()) {
223 q->setToolTip(VolumeSlider::tr("Volume: %1%").arg(value));
224 }
225#endif
226
227 qreal newvolume = (static_cast<qreal>(value)) * 0.01;
228 if (!ignoreVolumeChangeObserve && output->volume() != newvolume) {
229 ignoreVolumeChangeAction = true;
230 output->setVolume(newvolume);
231 }
232 } else {
233 slider.setEnabled(false);
234 muteButton.setEnabled(false);
235 }
236
237 ignoreVolumeChangeObserve = false;
238}
239
240void VolumeSliderPrivate::_k_volumeChanged(qreal value)
241{
242 if (sliderPressed) {
243 return;
244 }
245
246 int newslidervalue = qRound(100 * value);
247 if (!ignoreVolumeChangeAction && slider.value() != newslidervalue) {
248 ignoreVolumeChangeObserve = true;
249 slider.setValue(newslidervalue);
250 }
251
252 ignoreVolumeChangeAction = false;
253}
254
255bool VolumeSlider::hasTracking() const
256{
257 return k_ptr->slider.hasTracking();
258}
259
260void VolumeSlider::setTracking(bool tracking)
261{
262 k_ptr->slider.setTracking(tracking);
263}
264
265int VolumeSlider::pageStep() const
266{
267 return k_ptr->slider.pageStep();
268}
269
270void VolumeSlider::setPageStep(int milliseconds)
271{
272 k_ptr->slider.setPageStep(milliseconds);
273}
274
275int VolumeSlider::singleStep() const
276{
277 return k_ptr->slider.singleStep();
278}
279
280void VolumeSlider::setSingleStep(int milliseconds)
281{
282 k_ptr->slider.setSingleStep(milliseconds);
283}
284
285} // namespace Phonon
286
287#endif //QT_NO_PHONON_VOLUMESLIDER
288
289#include "moc_volumeslider.cpp"
290
291// vim: sw=4 et
Class for audio output to the soundcard.
Definition audiooutput.h:49
qreal volume
This is the current loudness of the output (it is using Stevens' law to calculate the change in volta...
Definition audiooutput.h:67
Widget providing a slider to control the volume of an AudioOutput.
bool tracking
This property holds whether slider tracking is enabled.
void setAudioOutput(Phonon::AudioOutput *)
Sets the audio output object to be controlled by this slider.
Qt::Orientation orientation
This property holds the orientation of the slider.
qreal maximumVolume
This property holds the maximum volume that can be set with this slider.
int pageStep
This property holds the page step.
QSize iconSize
the icon size used for the mute button/icon.
int singleStep
This property holds the single step.
VolumeSlider(QWidget *parent=nullptr)
Constructs a new volume slider with a parent.
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
QString tr(const char *sourceText, const char *disambiguation, int n)
typedef Alignment
Orientation
void setFocusProxy(QWidget *w)
void setToolTip(const QString &)
void setWhatsThis(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:18:23 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.