2010-04-08 23:15:33 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-04-08 23:15:33 +02:00
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tracksliderslider.h"
|
|
|
|
|
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QStyle>
|
2010-12-27 17:34:00 +01:00
|
|
|
#include <QStyleOptionSlider>
|
2015-11-09 00:40:15 +01:00
|
|
|
#include <QWheelEvent>
|
2020-09-18 16:15:19 +02:00
|
|
|
#include <QtDebug>
|
|
|
|
|
|
|
|
#include "core/timeconstants.h"
|
|
|
|
#include "core/utilities.h"
|
|
|
|
#include "tracksliderpopup.h"
|
2010-04-08 23:15:33 +02:00
|
|
|
|
|
|
|
TrackSliderSlider::TrackSliderSlider(QWidget* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QSlider(parent),
|
|
|
|
popup_(new TrackSliderPopup(window())),
|
|
|
|
mouse_hover_seconds_(0) {
|
2010-12-27 17:34:00 +01:00
|
|
|
setMouseTracking(true);
|
2011-02-21 23:25:04 +01:00
|
|
|
|
2016-10-11 00:10:56 +02:00
|
|
|
popup_->hide();
|
2011-02-21 23:25:04 +01:00
|
|
|
connect(this, SIGNAL(valueChanged(int)), SLOT(UpdateDeltaTime()));
|
2010-04-08 23:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrackSliderSlider::mousePressEvent(QMouseEvent* e) {
|
|
|
|
// QSlider asks QStyle which mouse button should do what (absolute move or
|
|
|
|
// page step). We force our own behaviour here because it makes more sense
|
|
|
|
// for a music player IMO.
|
|
|
|
|
|
|
|
Qt::MouseButton new_button = e->button();
|
|
|
|
if (e->button() == Qt::LeftButton) {
|
|
|
|
int abs_buttons = style()->styleHint(QStyle::SH_Slider_AbsoluteSetButtons);
|
|
|
|
if (abs_buttons & Qt::LeftButton)
|
|
|
|
new_button = Qt::LeftButton;
|
|
|
|
else if (abs_buttons & Qt::MidButton)
|
|
|
|
new_button = Qt::MidButton;
|
|
|
|
else if (abs_buttons & Qt::RightButton)
|
|
|
|
new_button = Qt::RightButton;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QMouseEvent new_event(e->type(), e->pos(), new_button, new_button,
|
|
|
|
e->modifiers());
|
2010-04-08 23:15:33 +02:00
|
|
|
QSlider::mousePressEvent(&new_event);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (new_event.isAccepted()) e->accept();
|
2010-04-08 23:15:33 +02:00
|
|
|
}
|
2010-12-27 17:34:00 +01:00
|
|
|
|
|
|
|
void TrackSliderSlider::mouseReleaseEvent(QMouseEvent* e) {
|
|
|
|
QSlider::mouseReleaseEvent(e);
|
2018-08-12 17:54:07 +02:00
|
|
|
if (e->button() == Qt::XButton1) {
|
|
|
|
emit Previous();
|
|
|
|
} else if (e->button() == Qt::XButton2) {
|
|
|
|
emit Next();
|
|
|
|
}
|
|
|
|
e->accept();
|
2010-12-27 17:34:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrackSliderSlider::mouseMoveEvent(QMouseEvent* e) {
|
|
|
|
QSlider::mouseMoveEvent(e);
|
|
|
|
|
|
|
|
// Borrowed from QSliderPrivate::pixelPosToRangeValue
|
|
|
|
QStyleOptionSlider opt;
|
|
|
|
initStyleOption(&opt);
|
2014-02-07 16:34:20 +01:00
|
|
|
QRect gr = style()->subControlRect(QStyle::CC_Slider, &opt,
|
|
|
|
QStyle::SC_SliderGroove, this);
|
|
|
|
QRect sr = style()->subControlRect(QStyle::CC_Slider, &opt,
|
|
|
|
QStyle::SC_SliderHandle, this);
|
2010-12-27 17:34:00 +01:00
|
|
|
|
|
|
|
int slider_length = sr.width();
|
|
|
|
int slider_min = gr.x();
|
|
|
|
int slider_max = gr.right() - slider_length + 1;
|
|
|
|
|
2011-02-21 23:25:04 +01:00
|
|
|
mouse_hover_seconds_ = QStyle::sliderValueFromPosition(
|
2014-05-19 17:51:40 +02:00
|
|
|
minimum() / kMsecPerSec, maximum() / kMsecPerSec,
|
|
|
|
e->x() - slider_length / 2 - slider_min + 1, slider_max - slider_min);
|
2010-12-27 17:34:00 +01:00
|
|
|
|
2011-02-21 23:25:04 +01:00
|
|
|
popup_->SetText(Utilities::PrettyTime(mouse_hover_seconds_));
|
|
|
|
UpdateDeltaTime();
|
2014-02-07 16:34:20 +01:00
|
|
|
popup_->SetPopupPosition(
|
|
|
|
mapTo(window(), QPoint(e->x(), rect().center().y())));
|
2010-12-27 17:34:00 +01:00
|
|
|
}
|
|
|
|
|
2020-09-18 16:15:19 +02:00
|
|
|
void TrackSliderSlider::wheelEvent(QWheelEvent* e) {
|
2015-11-09 00:40:15 +01:00
|
|
|
if (e->delta() < 0) {
|
|
|
|
emit SeekBackward();
|
|
|
|
} else {
|
|
|
|
emit SeekForward();
|
|
|
|
}
|
|
|
|
e->accept();
|
|
|
|
}
|
|
|
|
|
2010-12-27 17:34:00 +01:00
|
|
|
void TrackSliderSlider::enterEvent(QEvent* e) {
|
|
|
|
QSlider::enterEvent(e);
|
2010-12-29 15:32:16 +01:00
|
|
|
if (isEnabled()) {
|
2010-12-29 15:49:39 +01:00
|
|
|
popup_->show();
|
2010-12-29 15:32:16 +01:00
|
|
|
}
|
2010-12-27 17:34:00 +01:00
|
|
|
}
|
|
|
|
|
2018-02-24 23:35:27 +01:00
|
|
|
void TrackSliderSlider::leaveEvent(QEvent* e) {
|
|
|
|
QSlider::leaveEvent(e);
|
2018-02-24 21:23:41 +01:00
|
|
|
// On some (but not all) systems, displaying the TrackSliderPopup
|
|
|
|
// generates a leaveEvent. Ensure that this leaveEvent is genuine.
|
|
|
|
if (!geometry().contains(mapFromGlobal(QCursor::pos()))) {
|
|
|
|
popup_->hide();
|
|
|
|
}
|
2018-02-24 23:35:27 +01:00
|
|
|
}
|
|
|
|
|
2015-07-02 13:54:11 +02:00
|
|
|
void TrackSliderSlider::keyPressEvent(QKeyEvent* event) {
|
|
|
|
if (event->key() == Qt::Key_Left || event->key() == Qt::Key_Down) {
|
2015-07-02 14:59:30 +02:00
|
|
|
emit SeekBackward();
|
2015-07-02 13:54:11 +02:00
|
|
|
event->accept();
|
|
|
|
} else if (event->key() == Qt::Key_Right || event->key() == Qt::Key_Up) {
|
2015-07-02 14:59:30 +02:00
|
|
|
emit SeekForward();
|
2015-07-02 13:54:11 +02:00
|
|
|
event->accept();
|
|
|
|
} else {
|
|
|
|
QSlider::keyPressEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-21 23:25:04 +01:00
|
|
|
void TrackSliderSlider::UpdateDeltaTime() {
|
|
|
|
if (popup_->isVisible()) {
|
2014-05-19 17:51:40 +02:00
|
|
|
int delta_seconds = mouse_hover_seconds_ - (value() / kMsecPerSec);
|
2011-02-21 23:25:04 +01:00
|
|
|
popup_->SetSmallText(Utilities::PrettyTimeDelta(delta_seconds));
|
|
|
|
}
|
|
|
|
}
|