qt-material-widgets/components/qtmaterialslider.cpp

411 lines
8.5 KiB
C++
Raw Normal View History

2017-09-29 13:53:18 +00:00
#include "qtmaterialslider.h"
#include "qtmaterialslider_p.h"
#include <QtWidgets/QApplication>
#include <QMouseEvent>
#include "qtmaterialslider_internal.h"
#include "lib/qtmaterialstyle.h"
#include "lib/qtmaterialstatetransitionevent.h"
2022-02-17 02:51:42 +00:00
namespace md
{
2017-09-29 13:53:18 +00:00
/*!
* \class QtMaterialSliderPrivate
* \internal
*/
2022-02-17 02:51:42 +00:00
SliderPrivate::SliderPrivate(Slider *q)
2017-09-29 13:53:18 +00:00
: q_ptr(q)
{
}
2022-02-17 02:51:42 +00:00
SliderPrivate::~SliderPrivate()
2017-09-29 13:53:18 +00:00
{
}
2022-02-17 02:51:42 +00:00
void SliderPrivate::init()
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_Q(Slider);
2017-09-29 13:53:18 +00:00
2022-02-17 02:51:42 +00:00
thumb = new SliderThumb(q);
track = new SliderTrack(thumb, q);
stateMachine = new SliderStateMachine(q, thumb, track);
2017-09-29 13:53:18 +00:00
stepTo = 0;
oldValue = q->value();
trackWidth = 2;
hoverTrack = false;
hoverThumb = false;
hover = false;
step = false;
pageStepMode = true;
useThemeColors = true;
q->setMouseTracking(true);
q->setFocusPolicy(Qt::StrongFocus);
q->setPageStep(1);
QSizePolicy sp(QSizePolicy::Expanding,
QSizePolicy::Fixed);
if (q->orientation() == Qt::Vertical) {
sp.transpose();
}
q->setSizePolicy(sp);
q->setAttribute(Qt::WA_WState_OwnSizePolicy, false);
stateMachine->start();
QCoreApplication::processEvents();
}
2022-02-17 02:51:42 +00:00
QRectF SliderPrivate::trackBoundingRect() const
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_Q(const Slider);
2017-09-29 13:53:18 +00:00
qreal hw = static_cast<qreal>(trackWidth)/2;
return Qt::Horizontal == q->orientation()
? QRectF(QT_MATERIAL_SLIDER_MARGIN, q->height()/2 - hw,
q->width() - QT_MATERIAL_SLIDER_MARGIN*2, hw*2)
: QRectF(q->width()/2 - hw, QT_MATERIAL_SLIDER_MARGIN, hw*2,
q->height() - QT_MATERIAL_SLIDER_MARGIN*2);
}
2022-02-17 02:51:42 +00:00
QRectF SliderPrivate::thumbBoundingRect() const
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_Q(const Slider);
2017-09-29 13:53:18 +00:00
return Qt::Horizontal == q->orientation()
? QRectF(thumb->offset(), q->height()/2 - QT_MATERIAL_SLIDER_MARGIN,
QT_MATERIAL_SLIDER_MARGIN*2, QT_MATERIAL_SLIDER_MARGIN*2)
: QRectF(q->width()/2 - QT_MATERIAL_SLIDER_MARGIN, thumb->offset(),
QT_MATERIAL_SLIDER_MARGIN*2, QT_MATERIAL_SLIDER_MARGIN*2);
}
2022-02-17 02:51:42 +00:00
int SliderPrivate::valueFromPosition(const QPoint &pos) const
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_Q(const Slider);
2017-09-29 13:53:18 +00:00
const int position = Qt::Horizontal == q->orientation() ? pos.x() : pos.y();
const int span = Qt::Horizontal == q->orientation()
? q->width() - QT_MATERIAL_SLIDER_MARGIN*2
: q->height() - QT_MATERIAL_SLIDER_MARGIN*2;
2022-02-17 02:51:42 +00:00
return Style::sliderValueFromPosition(
2017-09-29 13:53:18 +00:00
q->minimum(),
q->maximum(),
position - QT_MATERIAL_SLIDER_MARGIN,
span,
q->invertedAppearance());
}
2022-02-17 02:51:42 +00:00
void SliderPrivate::setHovered(bool status)
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_Q(Slider);
2017-09-29 13:53:18 +00:00
if (hover == status) {
return;
}
hover = status;
if (!q->hasFocus()) {
if (status) {
2022-02-17 02:51:42 +00:00
stateMachine->postEvent(new StateTransitionEvent(SliderNoFocusMouseEnter));
2017-09-29 13:53:18 +00:00
} else {
2022-02-17 02:51:42 +00:00
stateMachine->postEvent(new StateTransitionEvent(SliderNoFocusMouseLeave));
2017-09-29 13:53:18 +00:00
}
}
q->update();
}
/*!
* \class QtMaterialSlider
*/
2022-02-17 02:51:42 +00:00
Slider::Slider(QWidget *parent)
2017-09-29 13:53:18 +00:00
: QAbstractSlider(parent),
2022-02-17 02:51:42 +00:00
d_ptr(new SliderPrivate(this))
2017-09-29 13:53:18 +00:00
{
d_func()->init();
}
2022-02-17 02:51:42 +00:00
Slider::~Slider()
2017-09-29 13:53:18 +00:00
{
}
2022-02-17 02:51:42 +00:00
void Slider::setUseThemeColors(bool value)
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_D(Slider);
2017-09-29 13:53:18 +00:00
2017-09-29 18:09:22 +00:00
if (d->useThemeColors == value) {
return;
}
2017-09-29 13:53:18 +00:00
d->useThemeColors = value;
d->stateMachine->setupProperties();
}
2022-02-17 02:51:42 +00:00
bool Slider::useThemeColors() const
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_D(const Slider);
2017-09-29 13:53:18 +00:00
return d->useThemeColors;
}
2022-02-17 02:51:42 +00:00
void Slider::setThumbColor(const QColor &color)
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_D(Slider);
2017-09-29 13:53:18 +00:00
d->thumbColor = color;
2017-09-29 18:09:22 +00:00
MATERIAL_DISABLE_THEME_COLORS
d->stateMachine->setupProperties();
update();
2017-09-29 13:53:18 +00:00
}
2022-02-17 02:51:42 +00:00
QColor Slider::thumbColor() const
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_D(const Slider);
2017-09-29 13:53:18 +00:00
if (d->useThemeColors || !d->thumbColor.isValid()) {
2022-02-17 02:51:42 +00:00
return Style::instance().themeColor("primary1");
2017-09-29 13:53:18 +00:00
} else {
return d->thumbColor;
}
}
2022-02-17 02:51:42 +00:00
void Slider::setTrackColor(const QColor &color)
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_D(Slider);
2017-09-29 13:53:18 +00:00
d->trackColor = color;
2017-09-29 18:09:22 +00:00
MATERIAL_DISABLE_THEME_COLORS
d->stateMachine->setupProperties();
update();
2017-09-29 13:53:18 +00:00
}
2022-02-17 02:51:42 +00:00
QColor Slider::trackColor() const
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_D(const Slider);
2017-09-29 13:53:18 +00:00
if (d->useThemeColors || !d->trackColor.isValid()) {
2022-02-17 02:51:42 +00:00
return Style::instance().themeColor("accent3");
2017-09-29 13:53:18 +00:00
} else {
return d->trackColor;
}
}
2022-02-17 02:51:42 +00:00
void Slider::setDisabledColor(const QColor &color)
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_D(Slider);
2017-09-29 13:53:18 +00:00
d->disabledColor = color;
2017-09-29 18:09:22 +00:00
MATERIAL_DISABLE_THEME_COLORS
d->stateMachine->setupProperties();
update();
2017-09-29 13:53:18 +00:00
}
2022-02-17 02:51:42 +00:00
QColor Slider::disabledColor() const
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_D(const Slider);
2017-09-29 13:53:18 +00:00
if (d->useThemeColors || !d->disabledColor.isValid()) {
2022-02-17 02:51:42 +00:00
return Style::instance().themeColor("disabled");
2017-09-29 13:53:18 +00:00
} else {
return d->disabledColor;
}
}
2022-02-17 02:51:42 +00:00
void Slider::setPageStepMode(bool pageStep)
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_D(Slider);
2017-09-29 13:53:18 +00:00
d->pageStepMode = pageStep;
}
2022-02-17 02:51:42 +00:00
bool Slider::pageStepMode() const
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_D(const Slider);
2017-09-29 13:53:18 +00:00
return d->pageStepMode;
}
/*!
* \remip
*/
2022-02-17 02:51:42 +00:00
QSize Slider::minimumSizeHint() const
2017-09-29 13:53:18 +00:00
{
return Qt::Horizontal == orientation()
? QSize(130, 34)
: QSize(34, 130);
}
2022-02-17 02:51:42 +00:00
void Slider::setInvertedAppearance(bool value)
2017-09-29 13:53:18 +00:00
{
QAbstractSlider::setInvertedAppearance(value);
updateThumbOffset();
}
/*!
* \remip
*/
2022-02-17 02:51:42 +00:00
void Slider::sliderChange(SliderChange change)
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_D(Slider);
2017-09-29 13:53:18 +00:00
if (SliderOrientationChange == change)
{
QSizePolicy sp(QSizePolicy::Expanding, QSizePolicy::Fixed);
if (orientation() == Qt::Vertical) {
sp.transpose();
}
setSizePolicy(sp);
}
else if (SliderValueChange == change)
{
if (minimum() == value()) {
triggerAction(SliderToMinimum);
2022-02-17 02:51:42 +00:00
d->stateMachine->postEvent(new StateTransitionEvent(SliderChangedToMinimum));
2017-09-29 13:53:18 +00:00
} else if (maximum() == value()) {
triggerAction(SliderToMaximum);
}
if (minimum() == d->oldValue) {
2022-02-17 02:51:42 +00:00
d->stateMachine->postEvent(new StateTransitionEvent(SliderChangedFromMinimum));
2017-09-29 13:53:18 +00:00
}
d->oldValue = value();
}
updateThumbOffset();
QAbstractSlider::sliderChange(change);
}
/*!
* \remip
*/
2022-02-17 02:51:42 +00:00
void Slider::mouseMoveEvent(QMouseEvent *event)
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_D(Slider);
2017-09-29 13:53:18 +00:00
if (isSliderDown())
{
setSliderPosition(d->valueFromPosition(event->pos()));
}
else
{
QRectF track(d->trackBoundingRect().adjusted(-2, -2, 2, 2));
if (track.contains(event->pos()) != d->hoverTrack) {
d->hoverTrack = !d->hoverTrack;
update();
}
QRectF thumb(0, 0, 16, 16);
thumb.moveCenter(d->thumbBoundingRect().center());
if (thumb.contains(event->pos()) != d->hoverThumb) {
d->hoverThumb = !d->hoverThumb;
update();
}
d->setHovered(d->hoverTrack || d->hoverThumb);
}
QAbstractSlider::mouseMoveEvent(event);
}
/*!
* \remip
*/
2022-02-17 02:51:42 +00:00
void Slider::mousePressEvent(QMouseEvent *event)
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_D(Slider);
2017-09-29 13:53:18 +00:00
const QPoint pos = event->pos();
QRectF thumb(0, 0, 16, 16);
thumb.moveCenter(d->thumbBoundingRect().center());
if (thumb.contains(pos)) {
setSliderDown(true);
return;
}
if (!d->pageStepMode) {
setSliderPosition(d->valueFromPosition(event->pos()));
d->thumb->setHaloSize(0);
setSliderDown(true);
return;
}
d->step = true;
d->stepTo = d->valueFromPosition(pos);
SliderAction action = d->stepTo > sliderPosition()
? SliderPageStepAdd
: SliderPageStepSub;
triggerAction(action);
setRepeatAction(action, 400, 8);
}
/*!
* \remip
*/
2022-02-17 02:51:42 +00:00
void Slider::mouseReleaseEvent(QMouseEvent *event)
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_D(Slider);
2017-09-29 13:53:18 +00:00
if (isSliderDown()) {
setSliderDown(false);
} else if (d->step) {
d->step = false;
setRepeatAction(SliderNoAction, 0);
}
QAbstractSlider::mouseReleaseEvent(event);
}
/*!
* \remip
*/
2022-02-17 02:51:42 +00:00
void Slider::leaveEvent(QEvent *event)
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_D(Slider);
2017-09-29 13:53:18 +00:00
if (d->hoverTrack) {
d->hoverTrack = false;
update();
}
if (d->hoverThumb) {
d->hoverThumb = false;
update();
}
d->setHovered(false);
QAbstractSlider::leaveEvent(event);
}
2022-02-17 02:51:42 +00:00
void Slider::updateThumbOffset()
2017-09-29 13:53:18 +00:00
{
2022-02-17 02:51:42 +00:00
Q_D(Slider);
2017-09-29 13:53:18 +00:00
2022-02-17 02:51:42 +00:00
const int offset = Style::sliderPositionFromValue(
2017-09-29 13:53:18 +00:00
minimum(),
maximum(),
sliderPosition(),
Qt::Horizontal == orientation()
? width() - QT_MATERIAL_SLIDER_MARGIN*2
: height() - QT_MATERIAL_SLIDER_MARGIN*2,
invertedAppearance());
d->thumb->setOffset(offset);
}
2022-02-17 02:51:42 +00:00
}