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 | |
32 | namespace Phonon |
33 | { |
34 | VolumeSlider::VolumeSlider(QWidget *parent) |
35 | : QWidget(parent), |
36 | k_ptr(new VolumeSliderPrivate(this)) |
37 | { |
38 | P_D(VolumeSlider); |
39 | #ifndef QT_NO_TOOLTIP |
40 | setToolTip(tr(s: "Volume: %1%" ).arg(a: 100)); |
41 | #endif |
42 | #ifndef QT_NO_WHATSTHIS |
43 | setWhatsThis(tr(s: "Use this slider to adjust the volume. The leftmost position is 0%, the rightmost is %1%" ).arg(a: 100)); |
44 | #endif |
45 | |
46 | connect(asender: &d->slider, SIGNAL(valueChanged(int)), SLOT(_k_sliderChanged(int))); |
47 | connect(sender: &d->slider, SIGNAL(sliderPressed()), receiver: this, SLOT(_k_sliderPressed())); |
48 | connect(sender: &d->slider, SIGNAL(sliderReleased()), receiver: this, SLOT(_k_sliderReleased())); |
49 | connect(sender: &d->slider, SIGNAL(scrollStart()), receiver: this, SLOT(_k_sliderPressed())); |
50 | connect(sender: &d->slider, SIGNAL(scrollEnd()), receiver: this, SLOT(_k_sliderReleased())); |
51 | connect(asender: &d->muteButton, SIGNAL(clicked()), SLOT(_k_buttonClicked())); |
52 | |
53 | setFocusProxy(&d->slider); |
54 | } |
55 | |
56 | VolumeSlider::VolumeSlider(AudioOutput *output, QWidget *parent) |
57 | : QWidget(parent), |
58 | k_ptr(new VolumeSliderPrivate(this)) |
59 | { |
60 | P_D(VolumeSlider); |
61 | #ifndef QT_NO_TOOLTIP |
62 | setToolTip(tr(s: "Volume: %1%" ).arg(a: 100)); |
63 | #endif |
64 | #ifndef QT_NO_WHATSTHIS |
65 | setWhatsThis(tr(s: "Use this slider to adjust the volume. The leftmost position is 0%, the rightmost is %1%" ).arg(a: 100)); |
66 | #endif |
67 | |
68 | connect(asender: &d->slider, SIGNAL(valueChanged(int)), SLOT(_k_sliderChanged(int))); |
69 | connect(sender: &d->slider, SIGNAL(sliderPressed()), receiver: this, SLOT(_k_sliderPressed())); |
70 | connect(sender: &d->slider, SIGNAL(sliderReleased()), receiver: this, SLOT(_k_sliderReleased())); |
71 | connect(sender: &d->slider, SIGNAL(scrollStart()), receiver: this, SLOT(_k_sliderPressed())); |
72 | connect(sender: &d->slider, SIGNAL(scrollEnd()), receiver: this, SLOT(_k_sliderReleased())); |
73 | connect(asender: &d->muteButton, SIGNAL(clicked()), SLOT(_k_buttonClicked())); |
74 | |
75 | if (output) { |
76 | d->output = output; |
77 | d->slider.setValue(qRound(d: 100 * output->volume())); |
78 | d->slider.setEnabled(true); |
79 | d->muteButton.setEnabled(true); |
80 | connect(asender: output, SIGNAL(volumeChanged(qreal)), SLOT(_k_volumeChanged(qreal))); |
81 | connect(asender: output, SIGNAL(mutedChanged(bool)), SLOT(_k_mutedChanged(bool))); |
82 | } |
83 | |
84 | setFocusProxy(&d->slider); |
85 | } |
86 | |
87 | VolumeSlider::~VolumeSlider() |
88 | { |
89 | delete k_ptr; |
90 | } |
91 | |
92 | bool VolumeSlider::isMuteVisible() const |
93 | { |
94 | return !k_ptr->muteButton.isHidden(); |
95 | } |
96 | |
97 | void VolumeSlider::setMuteVisible(bool visible) |
98 | { |
99 | k_ptr->muteButton.setVisible(visible); |
100 | } |
101 | |
102 | QSize VolumeSlider::iconSize() const |
103 | { |
104 | return k_ptr->muteButton.iconSize(); |
105 | } |
106 | |
107 | void VolumeSlider::setIconSize(const QSize &iconSize) |
108 | { |
109 | pDebug() << Q_FUNC_INFO << iconSize; |
110 | k_ptr->muteButton.setIconSize(iconSize); |
111 | } |
112 | |
113 | qreal VolumeSlider::maximumVolume() const |
114 | { |
115 | return k_ptr->slider.maximum() * 0.01; |
116 | } |
117 | |
118 | void 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(s: "Use this slider to adjust the volume. The leftmost position is 0%, the rightmost is %1%" ) |
124 | .arg(a: max)); |
125 | #endif |
126 | } |
127 | |
128 | Qt::Orientation VolumeSlider::orientation() const |
129 | { |
130 | return k_ptr->slider.orientation(); |
131 | } |
132 | |
133 | void VolumeSlider::setOrientation(Qt::Orientation o) |
134 | { |
135 | P_D(VolumeSlider); |
136 | Qt::Alignment align = (o == Qt::Horizontal ? Qt::AlignVCenter : Qt::AlignHCenter); |
137 | d->layout.setAlignment(w: &d->muteButton, alignment: align); |
138 | d->layout.setAlignment(w: &d->slider, alignment: align); |
139 | d->layout.setDirection(o == Qt::Horizontal ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom); |
140 | d->slider.setOrientation(o); |
141 | } |
142 | |
143 | AudioOutput *VolumeSlider::audioOutput() const |
144 | { |
145 | P_D(const VolumeSlider); |
146 | return d->output; |
147 | } |
148 | |
149 | void VolumeSlider::setAudioOutput(AudioOutput *output) |
150 | { |
151 | P_D(VolumeSlider); |
152 | if (d->output) { |
153 | disconnect(sender: d->output, signal: nullptr, receiver: this, member: nullptr); |
154 | } |
155 | d->output = output; |
156 | if (output) { |
157 | d->slider.setValue(qRound(d: 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(asender: output, SIGNAL(volumeChanged(qreal)), SLOT(_k_volumeChanged(qreal))); |
165 | connect(asender: 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 | |
173 | void 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 | |
183 | void VolumeSliderPrivate::_k_sliderPressed() |
184 | { |
185 | sliderPressed = true; |
186 | } |
187 | |
188 | void VolumeSliderPrivate::_k_sliderReleased() |
189 | { |
190 | sliderPressed = false; |
191 | if (output) { |
192 | _k_volumeChanged(output->volume()); |
193 | } |
194 | } |
195 | |
196 | void 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(s: "Muted" )); |
204 | #endif |
205 | muteButton.setIcon(mutedIcon); |
206 | } else { |
207 | #ifndef QT_NO_TOOLTIP |
208 | q->setToolTip(VolumeSlider::tr(s: "Volume: %1%" ).arg(a: static_cast<int>(output->volume() * 100.0))); |
209 | #endif |
210 | muteButton.setIcon(volumeIcon); |
211 | } |
212 | } |
213 | |
214 | void 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(s: "Volume: %1%" ).arg(a: 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 | |
240 | void VolumeSliderPrivate::_k_volumeChanged(qreal value) |
241 | { |
242 | if (sliderPressed) { |
243 | return; |
244 | } |
245 | |
246 | int newslidervalue = qRound(d: 100 * value); |
247 | if (!ignoreVolumeChangeAction && slider.value() != newslidervalue) { |
248 | ignoreVolumeChangeObserve = true; |
249 | slider.setValue(newslidervalue); |
250 | } |
251 | |
252 | ignoreVolumeChangeAction = false; |
253 | } |
254 | |
255 | bool VolumeSlider::hasTracking() const |
256 | { |
257 | return k_ptr->slider.hasTracking(); |
258 | } |
259 | |
260 | void VolumeSlider::setTracking(bool tracking) |
261 | { |
262 | k_ptr->slider.setTracking(tracking); |
263 | } |
264 | |
265 | int VolumeSlider::pageStep() const |
266 | { |
267 | return k_ptr->slider.pageStep(); |
268 | } |
269 | |
270 | void VolumeSlider::setPageStep(int milliseconds) |
271 | { |
272 | k_ptr->slider.setPageStep(milliseconds); |
273 | } |
274 | |
275 | int VolumeSlider::singleStep() const |
276 | { |
277 | return k_ptr->slider.singleStep(); |
278 | } |
279 | |
280 | void 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 |
292 | |