File indexing completed on 2025-02-09 05:31:55
0001 /* This file is part of the KDE project 0002 Copyright (C) 2006-2007 Matthias Kretz <kretz@kde.org> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Lesser General Public 0006 License as published by the Free Software Foundation; either 0007 version 2.1 of the License, or (at your option) version 3, or any 0008 later version accepted by the membership of KDE e.V. (or its 0009 successor approved by the membership of KDE e.V.), Nokia Corporation 0010 (or its successors, if any) and the KDE Free Qt Foundation, which shall 0011 act as a proxy defined in Section 6 of version 3 of the license. 0012 0013 This library is distributed in the hope that it will be useful, 0014 but WITHOUT ANY WARRANTY; without even the implied warranty of 0015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0016 Lesser General Public License for more details. 0017 0018 You should have received a copy of the GNU Lesser General Public 0019 License along with this library. If not, see <http://www.gnu.org/licenses/>. 0020 0021 */ 0022 0023 #include "volumeslider.h" 0024 #include "volumeslider_p.h" 0025 #include "audiooutput.h" 0026 #include "phonondefs_p.h" 0027 #include "phononnamespace_p.h" 0028 #include "factory_p.h" 0029 0030 #ifndef QT_NO_PHONON_VOLUMESLIDER 0031 0032 namespace Phonon 0033 { 0034 VolumeSlider::VolumeSlider(QWidget *parent) 0035 : QWidget(parent), 0036 k_ptr(new VolumeSliderPrivate(this)) 0037 { 0038 P_D(VolumeSlider); 0039 #ifndef QT_NO_TOOLTIP 0040 setToolTip(tr("Volume: %1%").arg(100)); 0041 #endif 0042 #ifndef QT_NO_WHATSTHIS 0043 setWhatsThis(tr("Use this slider to adjust the volume. The leftmost position is 0%, the rightmost is %1%").arg(100)); 0044 #endif 0045 0046 connect(&d->slider, SIGNAL(valueChanged(int)), SLOT(_k_sliderChanged(int))); 0047 connect(&d->slider, SIGNAL(sliderPressed()), this, SLOT(_k_sliderPressed())); 0048 connect(&d->slider, SIGNAL(sliderReleased()), this, SLOT(_k_sliderReleased())); 0049 connect(&d->slider, SIGNAL(scrollStart()), this, SLOT(_k_sliderPressed())); 0050 connect(&d->slider, SIGNAL(scrollEnd()), this, SLOT(_k_sliderReleased())); 0051 connect(&d->muteButton, SIGNAL(clicked()), SLOT(_k_buttonClicked())); 0052 0053 setFocusProxy(&d->slider); 0054 } 0055 0056 VolumeSlider::VolumeSlider(AudioOutput *output, QWidget *parent) 0057 : QWidget(parent), 0058 k_ptr(new VolumeSliderPrivate(this)) 0059 { 0060 P_D(VolumeSlider); 0061 #ifndef QT_NO_TOOLTIP 0062 setToolTip(tr("Volume: %1%").arg(100)); 0063 #endif 0064 #ifndef QT_NO_WHATSTHIS 0065 setWhatsThis(tr("Use this slider to adjust the volume. The leftmost position is 0%, the rightmost is %1%").arg(100)); 0066 #endif 0067 0068 connect(&d->slider, SIGNAL(valueChanged(int)), SLOT(_k_sliderChanged(int))); 0069 connect(&d->slider, SIGNAL(sliderPressed()), this, SLOT(_k_sliderPressed())); 0070 connect(&d->slider, SIGNAL(sliderReleased()), this, SLOT(_k_sliderReleased())); 0071 connect(&d->slider, SIGNAL(scrollStart()), this, SLOT(_k_sliderPressed())); 0072 connect(&d->slider, SIGNAL(scrollEnd()), this, SLOT(_k_sliderReleased())); 0073 connect(&d->muteButton, SIGNAL(clicked()), SLOT(_k_buttonClicked())); 0074 0075 if (output) { 0076 d->output = output; 0077 d->slider.setValue(qRound(100 * output->volume())); 0078 d->slider.setEnabled(true); 0079 d->muteButton.setEnabled(true); 0080 connect(output, SIGNAL(volumeChanged(qreal)), SLOT(_k_volumeChanged(qreal))); 0081 connect(output, SIGNAL(mutedChanged(bool)), SLOT(_k_mutedChanged(bool))); 0082 } 0083 0084 setFocusProxy(&d->slider); 0085 } 0086 0087 VolumeSlider::~VolumeSlider() 0088 { 0089 delete k_ptr; 0090 } 0091 0092 bool VolumeSlider::isMuteVisible() const 0093 { 0094 return !k_ptr->muteButton.isHidden(); 0095 } 0096 0097 void VolumeSlider::setMuteVisible(bool visible) 0098 { 0099 k_ptr->muteButton.setVisible(visible); 0100 } 0101 0102 QSize VolumeSlider::iconSize() const 0103 { 0104 return k_ptr->muteButton.iconSize(); 0105 } 0106 0107 void VolumeSlider::setIconSize(const QSize &iconSize) 0108 { 0109 pDebug() << Q_FUNC_INFO << iconSize; 0110 k_ptr->muteButton.setIconSize(iconSize); 0111 } 0112 0113 qreal VolumeSlider::maximumVolume() const 0114 { 0115 return k_ptr->slider.maximum() * 0.01; 0116 } 0117 0118 void VolumeSlider::setMaximumVolume(qreal volume) 0119 { 0120 int max = static_cast<int>(volume * 100); 0121 k_ptr->slider.setMaximum(max); 0122 #ifndef QT_NO_WHATSTHIS 0123 setWhatsThis(tr("Use this slider to adjust the volume. The leftmost position is 0%, the rightmost is %1%") 0124 .arg(max)); 0125 #endif 0126 } 0127 0128 Qt::Orientation VolumeSlider::orientation() const 0129 { 0130 return k_ptr->slider.orientation(); 0131 } 0132 0133 void VolumeSlider::setOrientation(Qt::Orientation o) 0134 { 0135 P_D(VolumeSlider); 0136 Qt::Alignment align = (o == Qt::Horizontal ? Qt::AlignVCenter : Qt::AlignHCenter); 0137 d->layout.setAlignment(&d->muteButton, align); 0138 d->layout.setAlignment(&d->slider, align); 0139 d->layout.setDirection(o == Qt::Horizontal ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom); 0140 d->slider.setOrientation(o); 0141 } 0142 0143 AudioOutput *VolumeSlider::audioOutput() const 0144 { 0145 P_D(const VolumeSlider); 0146 return d->output; 0147 } 0148 0149 void VolumeSlider::setAudioOutput(AudioOutput *output) 0150 { 0151 P_D(VolumeSlider); 0152 if (d->output) { 0153 disconnect(d->output, nullptr, this, nullptr); 0154 } 0155 d->output = output; 0156 if (output) { 0157 d->slider.setValue(qRound(100 * output->volume())); 0158 d->slider.setEnabled(true); 0159 d->muteButton.setEnabled(true); 0160 0161 d->_k_volumeChanged(output->volume()); 0162 d->_k_mutedChanged(output->isMuted()); 0163 0164 connect(output, SIGNAL(volumeChanged(qreal)), SLOT(_k_volumeChanged(qreal))); 0165 connect(output, SIGNAL(mutedChanged(bool)), SLOT(_k_mutedChanged(bool))); 0166 } else { 0167 d->slider.setValue(100); 0168 d->slider.setEnabled(false); 0169 d->muteButton.setEnabled(false); 0170 } 0171 } 0172 0173 void VolumeSliderPrivate::_k_buttonClicked() 0174 { 0175 if (output) { 0176 output->setMuted(!output->isMuted()); 0177 } else { 0178 slider.setEnabled(false); 0179 muteButton.setEnabled(false); 0180 } 0181 } 0182 0183 void VolumeSliderPrivate::_k_sliderPressed() 0184 { 0185 sliderPressed = true; 0186 } 0187 0188 void VolumeSliderPrivate::_k_sliderReleased() 0189 { 0190 sliderPressed = false; 0191 if (output) { 0192 _k_volumeChanged(output->volume()); 0193 } 0194 } 0195 0196 void VolumeSliderPrivate::_k_mutedChanged(bool muted) 0197 { 0198 #ifndef QT_NO_TOOLTIP 0199 P_Q(VolumeSlider); 0200 #endif 0201 if (muted) { 0202 #ifndef QT_NO_TOOLTIP 0203 q->setToolTip(VolumeSlider::tr("Muted")); 0204 #endif 0205 muteButton.setIcon(mutedIcon); 0206 } else { 0207 #ifndef QT_NO_TOOLTIP 0208 q->setToolTip(VolumeSlider::tr("Volume: %1%").arg(static_cast<int>(output->volume() * 100.0))); 0209 #endif 0210 muteButton.setIcon(volumeIcon); 0211 } 0212 } 0213 0214 void VolumeSliderPrivate::_k_sliderChanged(int value) 0215 { 0216 #ifndef QT_NO_TOOLTIP 0217 P_Q(VolumeSlider); 0218 #endif 0219 0220 if (output) { 0221 #ifndef QT_NO_TOOLTIP 0222 if (!output->isMuted()) { 0223 q->setToolTip(VolumeSlider::tr("Volume: %1%").arg(value)); 0224 } 0225 #endif 0226 0227 qreal newvolume = (static_cast<qreal>(value)) * 0.01; 0228 if (!ignoreVolumeChangeObserve && output->volume() != newvolume) { 0229 ignoreVolumeChangeAction = true; 0230 output->setVolume(newvolume); 0231 } 0232 } else { 0233 slider.setEnabled(false); 0234 muteButton.setEnabled(false); 0235 } 0236 0237 ignoreVolumeChangeObserve = false; 0238 } 0239 0240 void VolumeSliderPrivate::_k_volumeChanged(qreal value) 0241 { 0242 if (sliderPressed) { 0243 return; 0244 } 0245 0246 int newslidervalue = qRound(100 * value); 0247 if (!ignoreVolumeChangeAction && slider.value() != newslidervalue) { 0248 ignoreVolumeChangeObserve = true; 0249 slider.setValue(newslidervalue); 0250 } 0251 0252 ignoreVolumeChangeAction = false; 0253 } 0254 0255 bool VolumeSlider::hasTracking() const 0256 { 0257 return k_ptr->slider.hasTracking(); 0258 } 0259 0260 void VolumeSlider::setTracking(bool tracking) 0261 { 0262 k_ptr->slider.setTracking(tracking); 0263 } 0264 0265 int VolumeSlider::pageStep() const 0266 { 0267 return k_ptr->slider.pageStep(); 0268 } 0269 0270 void VolumeSlider::setPageStep(int milliseconds) 0271 { 0272 k_ptr->slider.setPageStep(milliseconds); 0273 } 0274 0275 int VolumeSlider::singleStep() const 0276 { 0277 return k_ptr->slider.singleStep(); 0278 } 0279 0280 void VolumeSlider::setSingleStep(int milliseconds) 0281 { 0282 k_ptr->slider.setSingleStep(milliseconds); 0283 } 0284 0285 } // namespace Phonon 0286 0287 #endif //QT_NO_PHONON_VOLUMESLIDER 0288 0289 #include "moc_volumeslider.cpp" 0290 0291 // vim: sw=4 et