2017-09-18 14:33:44 +08:00
|
|
|
/*
|
2018-02-07 11:52:47 +08:00
|
|
|
* Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
|
2017-09-18 14:33:44 +08:00
|
|
|
*
|
|
|
|
* Author: sbw <sbw@sbw.so>
|
|
|
|
*
|
|
|
|
* Maintainer: sbw <sbw@sbw.so>
|
|
|
|
*
|
|
|
|
* This program 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
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-08-02 20:14:45 +08:00
|
|
|
#include "volumeslider.h"
|
|
|
|
|
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
VolumeSlider::VolumeSlider(QWidget *parent)
|
2020-10-12 14:03:41 +08:00
|
|
|
: DSlider(Qt::Horizontal, parent),
|
2017-05-27 13:53:54 +08:00
|
|
|
m_pressed(false),
|
|
|
|
m_timer(new QTimer(this))
|
2016-08-02 20:14:45 +08:00
|
|
|
{
|
|
|
|
setPageStep(50);
|
2017-05-27 13:53:54 +08:00
|
|
|
m_timer->setInterval(100);
|
|
|
|
|
|
|
|
connect(m_timer, &QTimer::timeout, this, &VolumeSlider::onTimeout);
|
2016-08-02 20:14:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeSlider::setValue(const int value)
|
|
|
|
{
|
|
|
|
if (m_pressed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
blockSignals(true);
|
2020-10-12 14:03:41 +08:00
|
|
|
DSlider::setValue(value);
|
2016-08-02 20:14:45 +08:00
|
|
|
blockSignals(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeSlider::mousePressEvent(QMouseEvent *e)
|
|
|
|
{
|
|
|
|
if (e->button() == Qt::LeftButton)
|
|
|
|
{
|
|
|
|
if (!rect().contains(e->pos()))
|
|
|
|
return;
|
|
|
|
m_pressed = true;
|
2020-10-12 14:03:41 +08:00
|
|
|
DSlider::setValue(maximum() * e->x() / rect().width());
|
2016-08-02 20:14:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeSlider::mouseMoveEvent(QMouseEvent *e)
|
|
|
|
{
|
|
|
|
const int value = minimum() + (double((maximum()) - minimum()) * e->x() / rect().width());
|
2018-04-06 22:11:43 +08:00
|
|
|
const int normalized = std::max(std::min(maximum(), value), 0);
|
2016-08-02 20:14:45 +08:00
|
|
|
|
2020-10-12 14:03:41 +08:00
|
|
|
DSlider::setValue(normalized);
|
2018-01-31 19:36:32 +08:00
|
|
|
|
2020-09-19 20:15:22 +08:00
|
|
|
blockSignals(true);
|
2018-01-31 19:36:32 +08:00
|
|
|
emit valueChanged(normalized);
|
2020-09-19 20:15:22 +08:00
|
|
|
blockSignals(false);
|
2016-08-02 20:14:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeSlider::mouseReleaseEvent(QMouseEvent *e)
|
|
|
|
{
|
2022-11-04 06:29:03 +00:00
|
|
|
if (e->button() == Qt::LeftButton) {
|
2016-08-02 20:14:45 +08:00
|
|
|
m_pressed = false;
|
2017-05-27 13:53:54 +08:00
|
|
|
emit requestPlaySoundEffect();
|
|
|
|
}
|
2016-08-16 11:24:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeSlider::wheelEvent(QWheelEvent *e)
|
|
|
|
{
|
2016-09-23 15:28:28 +08:00
|
|
|
e->accept();
|
2017-02-17 14:23:22 +08:00
|
|
|
|
2017-05-27 13:53:54 +08:00
|
|
|
m_timer->start();
|
|
|
|
|
2022-11-04 06:29:03 +00:00
|
|
|
DSlider::setValue(value() + (e->angleDelta().y() > 0 ? 2 : -2));
|
2016-08-02 20:14:45 +08:00
|
|
|
}
|
2017-05-27 13:53:54 +08:00
|
|
|
|
|
|
|
void VolumeSlider::onTimeout()
|
|
|
|
{
|
|
|
|
m_timer->stop();
|
|
|
|
emit requestPlaySoundEffect();
|
|
|
|
}
|