123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- #include "qtmaterialslider.h"
- #include "qtmaterialslider_p.h"
- #include <QtWidgets/QApplication>
- #include <QMouseEvent>
- #include "qtmaterialslider_internal.h"
- #include "lib/qtmaterialstyle.h"
- #include "lib/qtmaterialstatetransitionevent.h"
- /*!
- * \class QtMaterialSliderPrivate
- * \internal
- */
- QtMaterialSliderPrivate::QtMaterialSliderPrivate(QtMaterialSlider *q)
- : q_ptr(q)
- {
- }
- QtMaterialSliderPrivate::~QtMaterialSliderPrivate()
- {
- }
- void QtMaterialSliderPrivate::init()
- {
- Q_Q(QtMaterialSlider);
- thumb = new QtMaterialSliderThumb(q);
- track = new QtMaterialSliderTrack(thumb, q);
- stateMachine = new QtMaterialSliderStateMachine(q, thumb, track);
- 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();
- }
- QRectF QtMaterialSliderPrivate::trackBoundingRect() const
- {
- Q_Q(const QtMaterialSlider);
- 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);
- }
- QRectF QtMaterialSliderPrivate::thumbBoundingRect() const
- {
- Q_Q(const QtMaterialSlider);
- 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);
- }
- int QtMaterialSliderPrivate::valueFromPosition(const QPoint &pos) const
- {
- Q_Q(const QtMaterialSlider);
- 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;
- return QtMaterialStyle::sliderValueFromPosition(
- q->minimum(),
- q->maximum(),
- position - QT_MATERIAL_SLIDER_MARGIN,
- span,
- q->invertedAppearance());
- }
- void QtMaterialSliderPrivate::setHovered(bool status)
- {
- Q_Q(QtMaterialSlider);
- if (hover == status) {
- return;
- }
- hover = status;
- if (!q->hasFocus()) {
- if (status) {
- stateMachine->postEvent(new QtMaterialStateTransitionEvent(SliderNoFocusMouseEnter));
- } else {
- stateMachine->postEvent(new QtMaterialStateTransitionEvent(SliderNoFocusMouseLeave));
- }
- }
- q->update();
- }
- /*!
- * \class QtMaterialSlider
- */
- QtMaterialSlider::QtMaterialSlider(QWidget *parent)
- : QAbstractSlider(parent),
- d_ptr(new QtMaterialSliderPrivate(this))
- {
- d_func()->init();
- }
- QtMaterialSlider::~QtMaterialSlider()
- {
- }
- void QtMaterialSlider::setUseThemeColors(bool value)
- {
- Q_D(QtMaterialSlider);
- if (d->useThemeColors == value) {
- return;
- }
- d->useThemeColors = value;
- d->stateMachine->setupProperties();
- }
- bool QtMaterialSlider::useThemeColors() const
- {
- Q_D(const QtMaterialSlider);
- return d->useThemeColors;
- }
- void QtMaterialSlider::setThumbColor(const QColor &color)
- {
- Q_D(QtMaterialSlider);
- d->thumbColor = color;
- MATERIAL_DISABLE_THEME_COLORS
- d->stateMachine->setupProperties();
- update();
- }
- QColor QtMaterialSlider::thumbColor() const
- {
- Q_D(const QtMaterialSlider);
- if (d->useThemeColors || !d->thumbColor.isValid()) {
- return QtMaterialStyle::instance().themeColor("primary1");
- } else {
- return d->thumbColor;
- }
- }
- void QtMaterialSlider::setTrackColor(const QColor &color)
- {
- Q_D(QtMaterialSlider);
- d->trackColor = color;
- MATERIAL_DISABLE_THEME_COLORS
- d->stateMachine->setupProperties();
- update();
- }
- QColor QtMaterialSlider::trackColor() const
- {
- Q_D(const QtMaterialSlider);
- if (d->useThemeColors || !d->trackColor.isValid()) {
- return QtMaterialStyle::instance().themeColor("accent3");
- } else {
- return d->trackColor;
- }
- }
- void QtMaterialSlider::setDisabledColor(const QColor &color)
- {
- Q_D(QtMaterialSlider);
- d->disabledColor = color;
- MATERIAL_DISABLE_THEME_COLORS
- d->stateMachine->setupProperties();
- update();
- }
- QColor QtMaterialSlider::disabledColor() const
- {
- Q_D(const QtMaterialSlider);
- if (d->useThemeColors || !d->disabledColor.isValid()) {
- return QtMaterialStyle::instance().themeColor("disabled");
- } else {
- return d->disabledColor;
- }
- }
- void QtMaterialSlider::setPageStepMode(bool pageStep)
- {
- Q_D(QtMaterialSlider);
- d->pageStepMode = pageStep;
- }
- bool QtMaterialSlider::pageStepMode() const
- {
- Q_D(const QtMaterialSlider);
- return d->pageStepMode;
- }
- /*!
- * \remip
- */
- QSize QtMaterialSlider::minimumSizeHint() const
- {
- return Qt::Horizontal == orientation()
- ? QSize(130, 34)
- : QSize(34, 130);
- }
- void QtMaterialSlider::setInvertedAppearance(bool value)
- {
- QAbstractSlider::setInvertedAppearance(value);
- updateThumbOffset();
- }
- /*!
- * \remip
- */
- void QtMaterialSlider::sliderChange(SliderChange change)
- {
- Q_D(QtMaterialSlider);
- 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);
- d->stateMachine->postEvent(new QtMaterialStateTransitionEvent(SliderChangedToMinimum));
- } else if (maximum() == value()) {
- triggerAction(SliderToMaximum);
- }
- if (minimum() == d->oldValue) {
- d->stateMachine->postEvent(new QtMaterialStateTransitionEvent(SliderChangedFromMinimum));
- }
- d->oldValue = value();
- }
- updateThumbOffset();
- QAbstractSlider::sliderChange(change);
- }
- /*!
- * \remip
- */
- void QtMaterialSlider::mouseMoveEvent(QMouseEvent *event)
- {
- Q_D(QtMaterialSlider);
- 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
- */
- void QtMaterialSlider::mousePressEvent(QMouseEvent *event)
- {
- Q_D(QtMaterialSlider);
- 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
- */
- void QtMaterialSlider::mouseReleaseEvent(QMouseEvent *event)
- {
- Q_D(QtMaterialSlider);
- if (isSliderDown()) {
- setSliderDown(false);
- } else if (d->step) {
- d->step = false;
- setRepeatAction(SliderNoAction, 0);
- }
- QAbstractSlider::mouseReleaseEvent(event);
- }
- /*!
- * \remip
- */
- void QtMaterialSlider::leaveEvent(QEvent *event)
- {
- Q_D(QtMaterialSlider);
- if (d->hoverTrack) {
- d->hoverTrack = false;
- update();
- }
- if (d->hoverThumb) {
- d->hoverThumb = false;
- update();
- }
- d->setHovered(false);
- QAbstractSlider::leaveEvent(event);
- }
- void QtMaterialSlider::updateThumbOffset()
- {
- Q_D(QtMaterialSlider);
- const int offset = QtMaterialStyle::sliderPositionFromValue(
- minimum(),
- maximum(),
- sliderPosition(),
- Qt::Horizontal == orientation()
- ? width() - QT_MATERIAL_SLIDER_MARGIN*2
- : height() - QT_MATERIAL_SLIDER_MARGIN*2,
- invertedAppearance());
- d->thumb->setOffset(offset);
- }
|