File indexing completed on 2023-05-30 11:30:51

0001 /**
0002  * Copyright (C) 2002-2004 Scott Wheeler <wheeler@kde.org>
0003  * Copyright (C) 2021 Michael Pyne <mpyne@kde.org>
0004  *
0005  * This program is free software; you can redistribute it and/or modify it under
0006  * the terms of the GNU General Public License as published by the Free Software
0007  * Foundation; either version 2 of the License, or (at your option) any later
0008  * version.
0009  *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License along with
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.
0016  */
0017 
0018 #include "slideraction.h"
0019 
0020 #include <ktoolbar.h>
0021 #include <kiconloader.h>
0022 #include <kactioncollection.h>
0023 #include <KLocalizedString>
0024 
0025 #include <QWheelEvent>
0026 
0027 #include "volumepopupbutton.h"
0028 #include "slider.h"
0029 #include "playermanager.h"
0030 #include "juk.h"
0031 #include "juk_debug.h"
0032 
0033 TrackPositionAction::TrackPositionAction(const QString &text, QObject *parent) :
0034     QWidgetAction(parent)
0035 {
0036     this->setText(text);
0037 }
0038 
0039 QWidget *TrackPositionAction::createWidget(QWidget *parent)
0040 {
0041     TimeSlider *slider = new TimeSlider(parent);
0042     slider->setObjectName(QLatin1String("timeSlider"));
0043 
0044     PlayerManager *player = JuK::JuKInstance()->playerManager();
0045 
0046     // When user starts dragging slider we may have tick events in event loop
0047     // that will set the position back so stop updating until the user is done.
0048     connect(slider, &TimeSlider::sliderPressed, this, [player, slider]() {
0049             QObject::disconnect(player, &PlayerManager::tick, slider, nullptr);
0050             });
0051 
0052     connect(slider, &TimeSlider::sliderReleased, this, [player, slider]() {
0053             const auto newValue = slider->value();
0054             player->seek(newValue);
0055             slider->setValue(newValue);
0056 
0057             connect(player, &PlayerManager::tick, slider, &TimeSlider::setValue);
0058             });
0059 
0060     // TODO only connect the tick signal when actually visible
0061     connect(player, &PlayerManager::tick, slider, &TimeSlider::setValue);
0062 
0063     connect(slider, &TimeSlider::actionTriggered, this, [player, slider](int action) {
0064                 if(action == QAbstractSlider::SliderMove) {
0065                     return;
0066                 }
0067 
0068                 // Set the new position before the PlayerManager overwrites it
0069                 // again
0070                 player->seek(slider->sliderPosition());
0071             });
0072 
0073     connect(player, &PlayerManager::seekableChanged, slider, &Slider::setEnabled);
0074     connect(player, &PlayerManager::seekableChanged, slider, [slider](bool seekable) {
0075         static const QString noSeekMsg =
0076             i18n("Seeking is not supported in this file with your audio settings.");
0077         slider->setToolTip(seekable ? QString() : noSeekMsg);
0078     });
0079     connect(player, &PlayerManager::totalTimeChanged, slider, [slider](int newLength) {
0080             slider->setMaximum(newLength);
0081             slider->setPageStep(newLength / 10);
0082             slider->setSingleStep(qMin(5000, newLength / 20));
0083             });
0084 
0085     return slider;
0086 }
0087 
0088 VolumeAction::VolumeAction(const QString &text, QObject *parent)
0089     : QWidgetAction(parent)
0090 {
0091     this->setText(text);
0092 }
0093 
0094 QWidget *VolumeAction::createWidget(QWidget *parent)
0095 {
0096     return new VolumePopupButton(parent);
0097 }
0098 
0099 // vim: set et sw=4 tw=0 sta: