File indexing completed on 2021-12-21 13:28:00

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 <QMouseEvent>
0026 #include <QWheelEvent>
0027 #include <QFocusEvent>
0028 #include <QBoxLayout>
0029 
0030 #include "volumepopupbutton.h"
0031 #include "slider.h"
0032 #include "playermanager.h"
0033 #include "juk.h"
0034 #include "juk_debug.h"
0035 
0036 TrackPositionAction::TrackPositionAction(const QString &text, QObject *parent) :
0037     QWidgetAction(parent)
0038 {
0039     this->setText(text);
0040 }
0041 
0042 QWidget *TrackPositionAction::createWidget(QWidget *parent)
0043 {
0044     TimeSlider *slider = new TimeSlider(parent);
0045     slider->setObjectName(QLatin1String("timeSlider"));
0046 
0047     PlayerManager *player = JuK::JuKInstance()->playerManager();
0048 
0049     // When user starts dragging slider we may have tick events in event loop
0050     // that will set the position back so stop updating until the user is done.
0051     connect(slider, &TimeSlider::sliderPressed, this, [player, slider]() {
0052             QObject::disconnect(player, &PlayerManager::tick, slider, nullptr);
0053             });
0054 
0055     connect(slider, &TimeSlider::sliderReleased, this, [player, slider]() {
0056             const auto newValue = slider->value();
0057             player->seek(newValue);
0058             slider->setValue(newValue);
0059 
0060             connect(player, &PlayerManager::tick, slider, &TimeSlider::setValue);
0061             });
0062 
0063     // TODO only connect the tick signal when actually visible
0064     connect(player, &PlayerManager::tick, slider, &TimeSlider::setValue);
0065 
0066     connect(slider, &TimeSlider::actionTriggered, this, [player, slider](int action) {
0067                 if(action == QAbstractSlider::SliderMove) {
0068                     return;
0069                 }
0070 
0071                 // Set the new position before the PlayerManager overwrites it
0072                 // again
0073                 player->seek(slider->sliderPosition());
0074             });
0075 
0076     connect(player, &PlayerManager::seekableChanged, slider, &Slider::setEnabled);
0077     connect(player, &PlayerManager::seekableChanged, slider, [slider](bool seekable) {
0078         static const QString noSeekMsg =
0079             i18n("Seeking is not supported in this file with your audio settings.");
0080         slider->setToolTip(seekable ? QString() : noSeekMsg);
0081     });
0082     connect(player, &PlayerManager::totalTimeChanged, slider, [slider](int newLength) {
0083             slider->setMaximum(newLength);
0084             slider->setPageStep(newLength / 10);
0085             slider->setSingleStep(qMin(5000, newLength / 20));
0086             });
0087 
0088     return slider;
0089 }
0090 
0091 VolumeAction::VolumeAction(const QString &text, QObject *parent)
0092     : QWidgetAction(parent)
0093 {
0094     this->setText(text);
0095 }
0096 
0097 QWidget *VolumeAction::createWidget(QWidget *parent)
0098 {
0099     return new VolumePopupButton(parent);
0100 }
0101 
0102 // vim: set et sw=4 tw=0 sta: