2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
|
|
|
*
|
|
|
|
* Strawberry is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Strawberry is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-09 18:39:44 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
2020-02-12 00:07:05 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QWidget>
|
|
|
|
#include <QVariant>
|
|
|
|
#include <QString>
|
|
|
|
#include <QSize>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QSettings>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QEvent>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2022-12-28 03:12:00 +01:00
|
|
|
#include "utilities/timeutils.h"
|
|
|
|
#include "utilities/timeconstants.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "trackslider.h"
|
|
|
|
#include "ui_trackslider.h"
|
|
|
|
#include "clickablelabel.h"
|
|
|
|
#include "tracksliderslider.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-04-18 15:03:01 +02:00
|
|
|
#ifdef HAVE_MOODBAR
|
|
|
|
# include "moodbar/moodbarproxystyle.h"
|
|
|
|
#endif
|
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
const char *TrackSlider::kSettingsGroup = "MainWindow";
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
TrackSlider::TrackSlider(QWidget *parent)
|
2018-02-27 18:06:05 +01:00
|
|
|
: QWidget(parent),
|
|
|
|
ui_(new Ui_TrackSlider),
|
2019-04-18 15:03:01 +02:00
|
|
|
#ifdef HAVE_MOODBAR
|
|
|
|
moodbar_style_(nullptr),
|
|
|
|
#endif
|
2018-02-27 18:06:05 +01:00
|
|
|
setting_value_(false),
|
|
|
|
show_remaining_time_(true),
|
2021-01-26 16:48:04 +01:00
|
|
|
slider_maximum_value_(0) {
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
ui_->setupUi(this);
|
|
|
|
|
|
|
|
UpdateLabelWidth();
|
|
|
|
|
|
|
|
// Load settings
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
show_remaining_time_ = s.value("show_remaining_time").toBool();
|
2021-01-26 16:48:04 +01:00
|
|
|
s.endGroup();
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::connect(ui_->slider, &TrackSliderSlider::sliderMoved, this, &TrackSlider::ValueChanged);
|
|
|
|
QObject::connect(ui_->slider, &TrackSliderSlider::valueChanged, this, &TrackSlider::ValueMaybeChanged);
|
|
|
|
QObject::connect(ui_->remaining, &ClickableLabel::Clicked, this, &TrackSlider::ToggleTimeDisplay);
|
|
|
|
QObject::connect(ui_->slider, &TrackSliderSlider::SeekForward, this, &TrackSlider::SeekForward);
|
|
|
|
QObject::connect(ui_->slider, &TrackSliderSlider::SeekBackward, this, &TrackSlider::SeekBackward);
|
|
|
|
QObject::connect(ui_->slider, &TrackSliderSlider::Previous, this, &TrackSlider::Previous);
|
|
|
|
QObject::connect(ui_->slider, &TrackSliderSlider::Next, this, &TrackSlider::Next);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TrackSlider::~TrackSlider() {
|
2021-08-23 21:21:08 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
delete ui_;
|
2019-07-22 20:53:05 +02:00
|
|
|
#ifdef HAVE_MOODBAR
|
|
|
|
if (moodbar_style_) moodbar_style_->deleteLater();
|
|
|
|
#endif
|
2021-08-23 21:21:08 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
void TrackSlider::SetApplication(Application *app) {
|
2021-08-23 21:21:08 +02:00
|
|
|
|
2019-04-18 15:03:01 +02:00
|
|
|
#ifdef HAVE_MOODBAR
|
2019-07-22 20:53:05 +02:00
|
|
|
if (!moodbar_style_) moodbar_style_ = new MoodbarProxyStyle(app, ui_->slider);
|
2019-10-03 23:29:52 +02:00
|
|
|
#else
|
|
|
|
Q_UNUSED(app);
|
2019-04-18 15:03:01 +02:00
|
|
|
#endif
|
2021-08-23 21:21:08 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrackSlider::UpdateLabelWidth() {
|
2021-08-23 21:21:08 +02:00
|
|
|
|
2022-08-28 02:44:37 +02:00
|
|
|
// We set the label's minimum size, so it won't resize itself when the user is dragging the slider.
|
2018-02-27 18:06:05 +01:00
|
|
|
UpdateLabelWidth(ui_->elapsed, "0:00:00");
|
|
|
|
UpdateLabelWidth(ui_->remaining, "-0:00:00");
|
2021-08-23 21:21:08 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
void TrackSlider::UpdateLabelWidth(QLabel *label, const QString &text) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
QString old_text = label->text();
|
|
|
|
label->setText(text);
|
|
|
|
label->setMinimumWidth(0);
|
|
|
|
int width = label->sizeHint().width();
|
|
|
|
label->setText(old_text);
|
|
|
|
|
|
|
|
label->setMinimumWidth(width);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize TrackSlider::sizeHint() const {
|
|
|
|
|
|
|
|
int width = 500;
|
|
|
|
width += ui_->elapsed->sizeHint().width();
|
|
|
|
width += ui_->remaining->sizeHint().width();
|
|
|
|
|
|
|
|
int height = qMax(ui_->slider->sizeHint().height(), ui_->elapsed->sizeHint().height());
|
|
|
|
|
|
|
|
return QSize(width, height);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
void TrackSlider::SetValue(const int elapsed, const int total) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-07-11 09:49:38 +02:00
|
|
|
setting_value_ = true; // This is so we don't emit from QAbstractSlider::valueChanged
|
2018-02-27 18:06:05 +01:00
|
|
|
ui_->slider->setMaximum(total);
|
2018-11-17 03:25:21 +01:00
|
|
|
if (!ui_->slider->isSliderDown()) {
|
|
|
|
ui_->slider->setValue(elapsed);
|
|
|
|
}
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
setting_value_ = false;
|
|
|
|
|
2021-03-21 18:53:02 +01:00
|
|
|
UpdateTimes(static_cast<int>(elapsed / kMsecPerSec));
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
void TrackSlider::UpdateTimes(const int elapsed) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
ui_->elapsed->setText(Utilities::PrettyTime(elapsed));
|
2018-05-01 00:41:33 +02:00
|
|
|
// Update normally if showing remaining time
|
2018-02-27 18:06:05 +01:00
|
|
|
if (show_remaining_time_) {
|
2022-11-10 19:19:33 +01:00
|
|
|
ui_->remaining->setText("-" + Utilities::PrettyTime(static_cast<int>(ui_->slider->maximum() / kMsecPerSec) - elapsed));
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
else {
|
2018-05-01 00:41:33 +02:00
|
|
|
// Check if slider maximum value is changed before updating
|
2018-11-17 03:25:21 +01:00
|
|
|
if (slider_maximum_value_ != ui_->slider->maximum() || !ui_->slider->isEnabled()) {
|
2018-02-27 18:06:05 +01:00
|
|
|
slider_maximum_value_ = ui_->slider->maximum();
|
2022-11-10 19:19:33 +01:00
|
|
|
ui_->remaining->setText(Utilities::PrettyTime(static_cast<int>(ui_->slider->maximum() / kMsecPerSec)));
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
setEnabled(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackSlider::SetStopped() {
|
|
|
|
|
|
|
|
setEnabled(false);
|
|
|
|
ui_->elapsed->setText("0:00:00");
|
|
|
|
ui_->remaining->setText("0:00:00");
|
|
|
|
|
|
|
|
setting_value_ = true;
|
|
|
|
ui_->slider->setValue(0);
|
|
|
|
slider_maximum_value_ = 0;
|
|
|
|
setting_value_ = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
void TrackSlider::SetCanSeek(const bool can_seek) {
|
2018-02-27 18:06:05 +01:00
|
|
|
ui_->slider->setEnabled(can_seek);
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
void TrackSlider::Seek(const int gap) {
|
2021-08-23 21:21:08 +02:00
|
|
|
|
|
|
|
if (ui_->slider->isEnabled()) {
|
2021-03-21 18:53:02 +01:00
|
|
|
ui_->slider->setValue(static_cast<int>(ui_->slider->value() + gap * kMsecPerSec));
|
2021-08-23 21:21:08 +02:00
|
|
|
}
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
void TrackSlider::ValueMaybeChanged(const int value) {
|
2021-08-23 21:21:08 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
if (setting_value_) return;
|
|
|
|
|
2021-03-21 18:53:02 +01:00
|
|
|
UpdateTimes(static_cast<int>(value / kMsecPerSec));
|
2022-09-12 23:18:54 +02:00
|
|
|
emit ValueChangedSeconds(static_cast<quint64>(value / kMsecPerSec));
|
2021-08-23 21:21:08 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
bool TrackSlider::event(QEvent *e) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
switch (e->type()) {
|
|
|
|
case QEvent::ApplicationFontChange:
|
|
|
|
case QEvent::StyleChange:
|
|
|
|
UpdateLabelWidth();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-06-20 19:04:08 +02:00
|
|
|
|
|
|
|
return QWidget::event(e);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackSlider::ToggleTimeDisplay() {
|
|
|
|
|
|
|
|
show_remaining_time_ = !show_remaining_time_;
|
|
|
|
if (!show_remaining_time_) {
|
2018-05-01 00:41:33 +02:00
|
|
|
// We set the value to -1 because the label must be updated
|
2018-02-27 18:06:05 +01:00
|
|
|
slider_maximum_value_ = -1;
|
|
|
|
}
|
2021-03-21 18:53:02 +01:00
|
|
|
UpdateTimes(static_cast<int>(ui_->slider->value() / kMsecPerSec));
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
// Save this setting
|
2018-02-27 18:06:05 +01:00
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
s.setValue("show_remaining_time", show_remaining_time_);
|
|
|
|
|
|
|
|
}
|