107 lines
3.5 KiB
C++
107 lines
3.5 KiB
C++
#include "VolumeWidgets.h"
|
|
|
|
#include <QHBoxLayout>
|
|
#include <QMouseEvent>
|
|
#include <QStyle>
|
|
#include <QStyleOptionSlider>
|
|
|
|
ClickSlider::ClickSlider(Qt::Orientation orientation, QWidget *parent)
|
|
: QSlider(orientation, parent) {}
|
|
|
|
void ClickSlider::mousePressEvent(QMouseEvent *event) {
|
|
QStyleOptionSlider opt;
|
|
initStyleOption(&opt);
|
|
QRect grooveRect =
|
|
style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
|
|
QRect handleRect =
|
|
style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
|
|
|
|
int pos;
|
|
int span;
|
|
if (orientation() == Qt::Horizontal) {
|
|
pos = event->pos().x() - grooveRect.x() - handleRect.width() / 2;
|
|
span = grooveRect.width() - handleRect.width();
|
|
} else {
|
|
pos = event->pos().y() - grooveRect.y() - handleRect.height() / 2;
|
|
span = grooveRect.height() - handleRect.height();
|
|
}
|
|
|
|
if (span > 0) {
|
|
int val;
|
|
if (orientation() == Qt::Horizontal) {
|
|
val = QStyle::sliderValueFromPosition(minimum(), maximum(), pos, span, opt.upsideDown);
|
|
} else {
|
|
val = QStyle::sliderValueFromPosition(minimum(), maximum(), pos, span, !opt.upsideDown);
|
|
}
|
|
setValue(val);
|
|
event->accept();
|
|
}
|
|
|
|
QSlider::mousePressEvent(event);
|
|
}
|
|
|
|
static const char *kSliderStyle =
|
|
"QSlider::groove:horizontal {"
|
|
" background: #1a1a1e; border-radius: 3px; height: 6px; }"
|
|
"QSlider::handle:horizontal {"
|
|
" background: #ecf0f6; border-radius: 5px;"
|
|
" width: 10px; margin: -4px 0; }"
|
|
"QSlider::sub-page:horizontal {"
|
|
" background: #4caf50; border-radius: 3px; }";
|
|
|
|
static const char *kMuteBtnStyle =
|
|
"QToolButton {"
|
|
" background: #2e2e36; color: #ecf0f6; border: 1px solid #3a3a44;"
|
|
" border-radius: 4px; padding: 2px 6px; font-weight: bold; font-size: 11px; }"
|
|
"QToolButton:checked {"
|
|
" background: #b03030; color: #ecf0f6; border: 1px solid #d04040; }"
|
|
"QToolButton:hover { background: #3a3a44; }"
|
|
"QToolButton:checked:hover { background: #c04040; }";
|
|
|
|
NodeVolumeWidget::NodeVolumeWidget(QWidget *parent) : QWidget(parent) {
|
|
setAutoFillBackground(true);
|
|
QPalette pal = palette();
|
|
pal.setColor(QPalette::Window, QColor(0x1a, 0x1a, 0x1e));
|
|
setPalette(pal);
|
|
|
|
m_slider = new ClickSlider(Qt::Horizontal, this);
|
|
m_slider->setRange(0, 100);
|
|
m_slider->setValue(100);
|
|
m_slider->setFixedWidth(100);
|
|
m_slider->setStyleSheet(QString::fromLatin1(kSliderStyle));
|
|
|
|
m_muteBtn = new QToolButton(this);
|
|
m_muteBtn->setText(QStringLiteral("M"));
|
|
m_muteBtn->setCheckable(true);
|
|
m_muteBtn->setFixedSize(22, 22);
|
|
m_muteBtn->setStyleSheet(QString::fromLatin1(kMuteBtnStyle));
|
|
|
|
auto *layout = new QHBoxLayout(this);
|
|
layout->setContentsMargins(4, 2, 4, 2);
|
|
layout->setSpacing(4);
|
|
layout->addWidget(m_slider);
|
|
layout->addWidget(m_muteBtn);
|
|
|
|
connect(m_slider, &QSlider::valueChanged, this,
|
|
&NodeVolumeWidget::volumeChanged);
|
|
connect(m_slider, &QSlider::sliderReleased, this,
|
|
&NodeVolumeWidget::sliderReleased);
|
|
connect(m_muteBtn, &QToolButton::toggled, this,
|
|
&NodeVolumeWidget::muteToggled);
|
|
}
|
|
|
|
int NodeVolumeWidget::volume() const { return m_slider->value(); }
|
|
|
|
bool NodeVolumeWidget::isMuted() const { return m_muteBtn->isChecked(); }
|
|
|
|
bool NodeVolumeWidget::isSliderDown() const { return m_slider->isSliderDown(); }
|
|
|
|
void NodeVolumeWidget::setVolume(int value) {
|
|
QSignalBlocker blocker(m_slider);
|
|
m_slider->setValue(value);
|
|
}
|
|
|
|
void NodeVolumeWidget::setMuted(bool muted) {
|
|
QSignalBlocker blocker(m_muteBtn);
|
|
m_muteBtn->setChecked(muted);
|
|
}
|