move Slider track to separate class
This commit is contained in:
parent
699bc77faa
commit
40f311bdb7
|
@ -49,19 +49,11 @@ bool Slider::pageStepMode() const
|
||||||
return d->pageStepMode;
|
return d->pageStepMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Slider::setTrackWidth(int width)
|
bool Slider::hovered() const
|
||||||
{
|
|
||||||
Q_D(Slider);
|
|
||||||
|
|
||||||
d->trackWidth = width;
|
|
||||||
update();
|
|
||||||
}
|
|
||||||
|
|
||||||
int Slider::trackWidth() const
|
|
||||||
{
|
{
|
||||||
Q_D(const Slider);
|
Q_D(const Slider);
|
||||||
|
|
||||||
return d->trackWidth;
|
return d->hover;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Slider::sliderChange(SliderChange change)
|
void Slider::sliderChange(SliderChange change)
|
||||||
|
@ -107,12 +99,6 @@ void Slider::paintEvent(QPaintEvent *event)
|
||||||
{
|
{
|
||||||
Q_UNUSED(event)
|
Q_UNUSED(event)
|
||||||
|
|
||||||
Q_D(Slider);
|
|
||||||
|
|
||||||
QPainter painter(this);
|
|
||||||
|
|
||||||
d->paintTrack(&painter);
|
|
||||||
|
|
||||||
#ifdef DEBUG_LAYOUT
|
#ifdef DEBUG_LAYOUT
|
||||||
if (hasFocus())
|
if (hasFocus())
|
||||||
painter.drawRect(rect().adjusted(0, 0, -1, -1));
|
painter.drawRect(rect().adjusted(0, 0, -1, -1));
|
||||||
|
|
|
@ -12,8 +12,6 @@ class Slider : public QAbstractSlider
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
Q_PROPERTY(int trackWidth WRITE setTrackWidth READ trackWidth)
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Slider(QWidget *parent = 0);
|
explicit Slider(QWidget *parent = 0);
|
||||||
~Slider();
|
~Slider();
|
||||||
|
@ -25,8 +23,7 @@ public:
|
||||||
void setPageStepMode(bool pageStep);
|
void setPageStepMode(bool pageStep);
|
||||||
bool pageStepMode() const;
|
bool pageStepMode() const;
|
||||||
|
|
||||||
void setTrackWidth(int width);
|
bool hovered() const;
|
||||||
int trackWidth() const;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void sliderChange(SliderChange change) Q_DECL_OVERRIDE;
|
void sliderChange(SliderChange change) Q_DECL_OVERRIDE;
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include "lib/style.h"
|
#include "lib/style.h"
|
||||||
#include "slider.h"
|
#include "slider.h"
|
||||||
|
#include "slidertrack.h"
|
||||||
#include "sliderthumb.h"
|
#include "sliderthumb.h"
|
||||||
#include "sliderstatemachine.h"
|
#include "sliderstatemachine.h"
|
||||||
|
|
||||||
|
@ -20,12 +21,13 @@ public:
|
||||||
QRectF trackBoundingRect() const;
|
QRectF trackBoundingRect() const;
|
||||||
QRectF thumbBoundingRect() const;
|
QRectF thumbBoundingRect() const;
|
||||||
|
|
||||||
void paintTrack(QPainter *painter);
|
//void paintTrack(QPainter *painter);
|
||||||
int valueFromPosition(const QPoint &pos) const;
|
int valueFromPosition(const QPoint &pos) const;
|
||||||
|
|
||||||
void setHovered(bool status);
|
void setHovered(bool status);
|
||||||
|
|
||||||
Slider *const q_ptr;
|
Slider *const q_ptr;
|
||||||
|
SliderTrack *const track;
|
||||||
SliderThumb *const thumb;
|
SliderThumb *const thumb;
|
||||||
SliderStateMachine *const machine;
|
SliderStateMachine *const machine;
|
||||||
bool hoverTrack;
|
bool hoverTrack;
|
||||||
|
@ -40,6 +42,7 @@ public:
|
||||||
|
|
||||||
SliderPrivate::SliderPrivate(Slider *parent)
|
SliderPrivate::SliderPrivate(Slider *parent)
|
||||||
: q_ptr(parent),
|
: q_ptr(parent),
|
||||||
|
track(new SliderTrack(parent)),
|
||||||
thumb(new SliderThumb(parent)),
|
thumb(new SliderThumb(parent)),
|
||||||
machine(new SliderStateMachine(parent, thumb)),
|
machine(new SliderStateMachine(parent, thumb)),
|
||||||
hoverTrack(false),
|
hoverTrack(false),
|
||||||
|
@ -74,10 +77,10 @@ QRectF SliderPrivate::trackBoundingRect() const
|
||||||
qreal hw = static_cast<qreal>(trackWidth)/2;
|
qreal hw = static_cast<qreal>(trackWidth)/2;
|
||||||
|
|
||||||
return Qt::Horizontal == q->orientation()
|
return Qt::Horizontal == q->orientation()
|
||||||
? QRectF(SLIDER_MARGIN, q->rect().height()/2 - hw,
|
? QRectF(SLIDER_MARGIN, q->height()/2 - hw,
|
||||||
q->rect().width() - SLIDER_MARGIN*2, hw*2)
|
q->width() - SLIDER_MARGIN*2, hw*2)
|
||||||
: QRectF(q->rect().width()/2 - hw, SLIDER_MARGIN, hw*2,
|
: QRectF(q->width()/2 - hw, SLIDER_MARGIN, hw*2,
|
||||||
q->rect().height() - SLIDER_MARGIN*2);
|
q->height() - SLIDER_MARGIN*2);
|
||||||
}
|
}
|
||||||
|
|
||||||
QRectF SliderPrivate::thumbBoundingRect() const
|
QRectF SliderPrivate::thumbBoundingRect() const
|
||||||
|
@ -85,12 +88,13 @@ QRectF SliderPrivate::thumbBoundingRect() const
|
||||||
Q_Q(const Slider);
|
Q_Q(const Slider);
|
||||||
|
|
||||||
return Qt::Horizontal == q->orientation()
|
return Qt::Horizontal == q->orientation()
|
||||||
? QRectF(q->thumbOffset(), q->rect().height()/2 - SLIDER_MARGIN,
|
? QRectF(q->thumbOffset(), q->height()/2 - SLIDER_MARGIN,
|
||||||
SLIDER_MARGIN*2, SLIDER_MARGIN*2)
|
SLIDER_MARGIN*2, SLIDER_MARGIN*2)
|
||||||
: QRectF(q->rect().width()/2 - SLIDER_MARGIN, q->thumbOffset(),
|
: QRectF(q->width()/2 - SLIDER_MARGIN, q->thumbOffset(),
|
||||||
SLIDER_MARGIN*2, SLIDER_MARGIN*2);
|
SLIDER_MARGIN*2, SLIDER_MARGIN*2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
void SliderPrivate::paintTrack(QPainter *painter)
|
void SliderPrivate::paintTrack(QPainter *painter)
|
||||||
{
|
{
|
||||||
Q_Q(const Slider);
|
Q_Q(const Slider);
|
||||||
|
@ -139,6 +143,7 @@ void SliderPrivate::paintTrack(QPainter *painter)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
int SliderPrivate::valueFromPosition(const QPoint &pos) const
|
int SliderPrivate::valueFromPosition(const QPoint &pos) const
|
||||||
{
|
{
|
||||||
|
|
|
@ -0,0 +1,80 @@
|
||||||
|
#include "slidertrack.h"
|
||||||
|
#include <QEvent>
|
||||||
|
#include <QPainter>
|
||||||
|
#include "lib/style.h"
|
||||||
|
#include "slider.h"
|
||||||
|
|
||||||
|
SliderTrack::SliderTrack(Slider *slider)
|
||||||
|
: QWidget(slider->parentWidget()),
|
||||||
|
slider(slider),
|
||||||
|
_width(2)
|
||||||
|
{
|
||||||
|
slider->installEventFilter(this);
|
||||||
|
setAttribute(Qt::WA_TransparentForMouseEvents, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
SliderTrack::~SliderTrack()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SliderTrack::eventFilter(QObject *obj, QEvent *event)
|
||||||
|
{
|
||||||
|
if (QEvent::ParentChange == event->type()) {
|
||||||
|
setParent(slider->parentWidget());
|
||||||
|
}
|
||||||
|
return QWidget::eventFilter(obj, event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SliderTrack::paintEvent(QPaintEvent *event)
|
||||||
|
{
|
||||||
|
QPainter painter(this);
|
||||||
|
Style &style = Style::instance();
|
||||||
|
|
||||||
|
QBrush fg;
|
||||||
|
fg.setStyle(Qt::SolidPattern);
|
||||||
|
fg.setColor(slider->isEnabled() ? style.themeColor("primary1")
|
||||||
|
: style.themeColor("disabled"));
|
||||||
|
QBrush bg;
|
||||||
|
bg.setStyle(Qt::SolidPattern);
|
||||||
|
bg.setColor(slider->hovered() ? QColor(0, 0, 0, 150) // @TODO: set theme color
|
||||||
|
: style.themeColor("accent3"));
|
||||||
|
|
||||||
|
qreal offset = slider->thumbOffset() + SLIDER_MARGIN;
|
||||||
|
|
||||||
|
QSizeF box(slider->isEnabled() ? offset : offset - 7,
|
||||||
|
qMax(slider->width(), slider->height()));
|
||||||
|
|
||||||
|
if (Qt::Vertical == slider->orientation())
|
||||||
|
box.transpose();
|
||||||
|
|
||||||
|
QRectF rect = Qt::Vertical == slider->orientation()
|
||||||
|
? QRectF(0, slider->isEnabled() ? offset : offset + 7,
|
||||||
|
box.width(), box.width())
|
||||||
|
: QRectF(slider->isEnabled() ? offset : offset + 7, 0,
|
||||||
|
box.height(), box.height());
|
||||||
|
|
||||||
|
qreal hw = static_cast<qreal>(_width)/2;
|
||||||
|
|
||||||
|
QRectF geometry = Qt::Horizontal == slider->orientation()
|
||||||
|
? QRectF(SLIDER_MARGIN, slider->rect().height()/2 - hw,
|
||||||
|
slider->width() - SLIDER_MARGIN*2, hw*2)
|
||||||
|
: QRectF(slider->width()/2 - hw, SLIDER_MARGIN, hw*2,
|
||||||
|
slider->height() - SLIDER_MARGIN*2);
|
||||||
|
|
||||||
|
bool inverted = slider->invertedAppearance();
|
||||||
|
|
||||||
|
painter.fillRect(QRectF(QPointF(0, 0), box).intersected(geometry),
|
||||||
|
inverted ? bg : fg);
|
||||||
|
painter.fillRect(rect.intersected(geometry), inverted ? fg : bg);
|
||||||
|
|
||||||
|
#ifdef DEBUG_LAYOUT
|
||||||
|
if (hoverTrack) {
|
||||||
|
painter.save();
|
||||||
|
painter.setPen(Qt::red);
|
||||||
|
painter.drawRect(geometry);
|
||||||
|
painter.restore();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
QWidget::paintEvent(event);
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
#ifndef SLIDERTRACK_H
|
||||||
|
#define SLIDERTRACK_H
|
||||||
|
|
||||||
|
#include <QWidget>
|
||||||
|
|
||||||
|
class Slider;
|
||||||
|
|
||||||
|
class SliderTrack : public QWidget
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
Q_PROPERTY(QColor fillColor WRITE setFillColor READ fillColor)
|
||||||
|
Q_PROPERTY(int trackWidth WRITE setTrackWidth READ trackWidth)
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit SliderTrack(Slider *slider);
|
||||||
|
~SliderTrack();
|
||||||
|
|
||||||
|
inline void setFillColor(const QColor &color)
|
||||||
|
{
|
||||||
|
_fillColor = color;
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline QColor fillColor() const
|
||||||
|
{
|
||||||
|
return _fillColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setTrackWidth(int width)
|
||||||
|
{
|
||||||
|
_width = width;
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
int trackWidth() const
|
||||||
|
{
|
||||||
|
return _width;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
bool eventFilter(QObject *obj, QEvent *event);
|
||||||
|
void paintEvent(QPaintEvent *event);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Q_DISABLE_COPY(SliderTrack)
|
||||||
|
|
||||||
|
const Slider *const slider;
|
||||||
|
QColor _fillColor;
|
||||||
|
int _width;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // SLIDERTRACK_H
|
|
@ -54,7 +54,8 @@ SOURCES += main.cpp\
|
||||||
components/sliderthumb.cpp \
|
components/sliderthumb.cpp \
|
||||||
components/searchfield.cpp \
|
components/searchfield.cpp \
|
||||||
lib/theme.cpp \
|
lib/theme.cpp \
|
||||||
components/sliderstatemachine.cpp
|
components/sliderstatemachine.cpp \
|
||||||
|
components/slidertrack.cpp
|
||||||
|
|
||||||
HEADERS += mainwindow.h \
|
HEADERS += mainwindow.h \
|
||||||
components/appbar.h \
|
components/appbar.h \
|
||||||
|
@ -104,7 +105,8 @@ HEADERS += mainwindow.h \
|
||||||
components/searchfield.h \
|
components/searchfield.h \
|
||||||
lib/theme.h \
|
lib/theme.h \
|
||||||
lib/theme_p.h \
|
lib/theme_p.h \
|
||||||
components/sliderstatemachine.h
|
components/sliderstatemachine.h \
|
||||||
|
components/slidertrack.h
|
||||||
|
|
||||||
RESOURCES += \
|
RESOURCES += \
|
||||||
resources.qrc
|
resources.qrc
|
||||||
|
|
Loading…
Reference in New Issue