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;
|
||||
}
|
||||
|
||||
void Slider::setTrackWidth(int width)
|
||||
{
|
||||
Q_D(Slider);
|
||||
|
||||
d->trackWidth = width;
|
||||
update();
|
||||
}
|
||||
|
||||
int Slider::trackWidth() const
|
||||
bool Slider::hovered() const
|
||||
{
|
||||
Q_D(const Slider);
|
||||
|
||||
return d->trackWidth;
|
||||
return d->hover;
|
||||
}
|
||||
|
||||
void Slider::sliderChange(SliderChange change)
|
||||
|
@ -107,12 +99,6 @@ void Slider::paintEvent(QPaintEvent *event)
|
|||
{
|
||||
Q_UNUSED(event)
|
||||
|
||||
Q_D(Slider);
|
||||
|
||||
QPainter painter(this);
|
||||
|
||||
d->paintTrack(&painter);
|
||||
|
||||
#ifdef DEBUG_LAYOUT
|
||||
if (hasFocus())
|
||||
painter.drawRect(rect().adjusted(0, 0, -1, -1));
|
||||
|
|
|
@ -12,8 +12,6 @@ class Slider : public QAbstractSlider
|
|||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(int trackWidth WRITE setTrackWidth READ trackWidth)
|
||||
|
||||
public:
|
||||
explicit Slider(QWidget *parent = 0);
|
||||
~Slider();
|
||||
|
@ -25,8 +23,7 @@ public:
|
|||
void setPageStepMode(bool pageStep);
|
||||
bool pageStepMode() const;
|
||||
|
||||
void setTrackWidth(int width);
|
||||
int trackWidth() const;
|
||||
bool hovered() const;
|
||||
|
||||
protected:
|
||||
void sliderChange(SliderChange change) Q_DECL_OVERRIDE;
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include <QDebug>
|
||||
#include "lib/style.h"
|
||||
#include "slider.h"
|
||||
#include "slidertrack.h"
|
||||
#include "sliderthumb.h"
|
||||
#include "sliderstatemachine.h"
|
||||
|
||||
|
@ -20,12 +21,13 @@ public:
|
|||
QRectF trackBoundingRect() const;
|
||||
QRectF thumbBoundingRect() const;
|
||||
|
||||
void paintTrack(QPainter *painter);
|
||||
//void paintTrack(QPainter *painter);
|
||||
int valueFromPosition(const QPoint &pos) const;
|
||||
|
||||
void setHovered(bool status);
|
||||
|
||||
Slider *const q_ptr;
|
||||
SliderTrack *const track;
|
||||
SliderThumb *const thumb;
|
||||
SliderStateMachine *const machine;
|
||||
bool hoverTrack;
|
||||
|
@ -40,6 +42,7 @@ public:
|
|||
|
||||
SliderPrivate::SliderPrivate(Slider *parent)
|
||||
: q_ptr(parent),
|
||||
track(new SliderTrack(parent)),
|
||||
thumb(new SliderThumb(parent)),
|
||||
machine(new SliderStateMachine(parent, thumb)),
|
||||
hoverTrack(false),
|
||||
|
@ -74,10 +77,10 @@ QRectF SliderPrivate::trackBoundingRect() const
|
|||
qreal hw = static_cast<qreal>(trackWidth)/2;
|
||||
|
||||
return Qt::Horizontal == q->orientation()
|
||||
? QRectF(SLIDER_MARGIN, q->rect().height()/2 - hw,
|
||||
q->rect().width() - SLIDER_MARGIN*2, hw*2)
|
||||
: QRectF(q->rect().width()/2 - hw, SLIDER_MARGIN, hw*2,
|
||||
q->rect().height() - SLIDER_MARGIN*2);
|
||||
? QRectF(SLIDER_MARGIN, q->height()/2 - hw,
|
||||
q->width() - SLIDER_MARGIN*2, hw*2)
|
||||
: QRectF(q->width()/2 - hw, SLIDER_MARGIN, hw*2,
|
||||
q->height() - SLIDER_MARGIN*2);
|
||||
}
|
||||
|
||||
QRectF SliderPrivate::thumbBoundingRect() const
|
||||
|
@ -85,12 +88,13 @@ QRectF SliderPrivate::thumbBoundingRect() const
|
|||
Q_Q(const Slider);
|
||||
|
||||
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)
|
||||
: QRectF(q->rect().width()/2 - SLIDER_MARGIN, q->thumbOffset(),
|
||||
: QRectF(q->width()/2 - SLIDER_MARGIN, q->thumbOffset(),
|
||||
SLIDER_MARGIN*2, SLIDER_MARGIN*2);
|
||||
}
|
||||
|
||||
/*
|
||||
void SliderPrivate::paintTrack(QPainter *painter)
|
||||
{
|
||||
Q_Q(const Slider);
|
||||
|
@ -139,6 +143,7 @@ void SliderPrivate::paintTrack(QPainter *painter)
|
|||
}
|
||||
#endif
|
||||
}
|
||||
*/
|
||||
|
||||
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/searchfield.cpp \
|
||||
lib/theme.cpp \
|
||||
components/sliderstatemachine.cpp
|
||||
components/sliderstatemachine.cpp \
|
||||
components/slidertrack.cpp
|
||||
|
||||
HEADERS += mainwindow.h \
|
||||
components/appbar.h \
|
||||
|
@ -104,7 +105,8 @@ HEADERS += mainwindow.h \
|
|||
components/searchfield.h \
|
||||
lib/theme.h \
|
||||
lib/theme_p.h \
|
||||
components/sliderstatemachine.h
|
||||
components/sliderstatemachine.h \
|
||||
components/slidertrack.h
|
||||
|
||||
RESOURCES += \
|
||||
resources.qrc
|
||||
|
|
Loading…
Reference in New Issue