make slider behave nicely
This commit is contained in:
parent
66084f8e4a
commit
58f157db4b
|
@ -175,7 +175,7 @@ void Slider::mousePressEvent(QMouseEvent *event)
|
|||
: SliderPageStepSub;
|
||||
|
||||
triggerAction(action);
|
||||
setRepeatAction(action);
|
||||
setRepeatAction(action, 400, 8);
|
||||
}
|
||||
|
||||
void Slider::mouseReleaseEvent(QMouseEvent *event)
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
#include <QPainter>
|
||||
#include <QApplication>
|
||||
#include <QDebug>
|
||||
#include "lib/style.h"
|
||||
#include "slider.h"
|
||||
#include "slidertrack.h"
|
||||
|
@ -48,7 +47,7 @@ SliderPrivate::SliderPrivate(Slider *parent)
|
|||
hoverThumb(false),
|
||||
hover(false),
|
||||
step(false),
|
||||
pageStepMode(false),
|
||||
pageStepMode(true),
|
||||
stepTo(0),
|
||||
oldValue(parent->value()),
|
||||
trackWidth(2)
|
||||
|
@ -57,6 +56,7 @@ SliderPrivate::SliderPrivate(Slider *parent)
|
|||
|
||||
q->setMouseTracking(true);
|
||||
q->setFocusPolicy(Qt::StrongFocus);
|
||||
q->setPageStep(1);
|
||||
|
||||
QSizePolicy sp(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||||
if (q->orientation() == Qt::Vertical)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "sliderthumb.h"
|
||||
#include <QEvent>
|
||||
#include <QPainter>
|
||||
#include <QDebug>
|
||||
#include "lib/style.h"
|
||||
#include "slider.h"
|
||||
|
||||
|
@ -24,8 +25,15 @@ SliderThumb::~SliderThumb()
|
|||
|
||||
bool SliderThumb::eventFilter(QObject *obj, QEvent *event)
|
||||
{
|
||||
if (QEvent::ParentChange == event->type()) {
|
||||
QEvent::Type type = event->type();
|
||||
|
||||
if (QEvent::ParentChange == type) {
|
||||
setParent(slider->parentWidget());
|
||||
} else if (QEvent::Resize == type || QEvent::Move == type) {
|
||||
QWidget *widget;
|
||||
if ((widget = parentWidget())) {
|
||||
setGeometry(widget->rect());
|
||||
}
|
||||
}
|
||||
return QWidget::eventFilter(obj, event);
|
||||
}
|
||||
|
@ -45,10 +53,8 @@ void SliderThumb::paintEvent(QPaintEvent *event)
|
|||
painter.setRenderHint(QPainter::Antialiasing);
|
||||
|
||||
QPointF disp = Qt::Horizontal == slider->orientation()
|
||||
? QPointF(SLIDER_MARGIN + slider->thumbOffset(),
|
||||
slider->height()/2)
|
||||
: QPointF(slider->width()/2,
|
||||
SLIDER_MARGIN + slider->thumbOffset());
|
||||
? QPointF(SLIDER_MARGIN + slider->thumbOffset(), slider->height()/2)
|
||||
: QPointF(slider->width()/2, SLIDER_MARGIN + slider->thumbOffset());
|
||||
|
||||
QRectF halo((slider->pos() - QPointF(_haloSize, _haloSize)/2) + disp,
|
||||
QSizeF(_haloSize, _haloSize));
|
||||
|
@ -57,9 +63,9 @@ void SliderThumb::paintEvent(QPaintEvent *event)
|
|||
|
||||
// Knob
|
||||
|
||||
brush.setStyle(Qt::SolidPattern);
|
||||
brush.setColor(slider->value() > slider->minimum()
|
||||
? (slider->isEnabled() ? _fillColor : Style::instance().themeColor("disabled"))
|
||||
? (slider->isEnabled()
|
||||
? _fillColor : Style::instance().themeColor("disabled"))
|
||||
: _minFillColor);
|
||||
painter.setBrush(brush);
|
||||
|
||||
|
|
|
@ -11,6 +11,8 @@ SliderTrack::SliderTrack(Slider *slider)
|
|||
{
|
||||
slider->installEventFilter(this);
|
||||
setAttribute(Qt::WA_TransparentForMouseEvents, true);
|
||||
|
||||
connect(slider, SIGNAL(sliderMoved(int)), this, SLOT(update()));
|
||||
}
|
||||
|
||||
SliderTrack::~SliderTrack()
|
||||
|
@ -19,8 +21,14 @@ SliderTrack::~SliderTrack()
|
|||
|
||||
bool SliderTrack::eventFilter(QObject *obj, QEvent *event)
|
||||
{
|
||||
if (QEvent::ParentChange == event->type()) {
|
||||
QEvent::Type type = event->type();
|
||||
|
||||
if (QEvent::ParentChange == type) {
|
||||
setParent(slider->parentWidget());
|
||||
} else if (QEvent::Resize == type || QEvent::Move == type) {
|
||||
if (parentWidget()) {
|
||||
setGeometry(parentWidget()->rect());
|
||||
}
|
||||
}
|
||||
return QWidget::eventFilter(obj, event);
|
||||
}
|
||||
|
@ -36,44 +44,49 @@ void SliderTrack::paintEvent(QPaintEvent *event)
|
|||
: style.themeColor("disabled"));
|
||||
QBrush bg;
|
||||
bg.setStyle(Qt::SolidPattern);
|
||||
bg.setColor(slider->isEnabled() ? _fillColor : style.themeColor("disabled"));
|
||||
bg.setColor(slider->isEnabled() ? _fillColor
|
||||
: style.themeColor("disabled"));
|
||||
|
||||
painter.setRenderHint(QPainter::Antialiasing);
|
||||
|
||||
qreal offset = slider->thumbOffset();
|
||||
|
||||
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() ? slider->y() + offset + SLIDER_MARGIN
|
||||
: slider->y() + offset + SLIDER_MARGIN + 7,
|
||||
box.width(), box.width())
|
||||
: QRectF(slider->isEnabled() ? slider->x() + offset + SLIDER_MARGIN
|
||||
: slider->x() + offset + SLIDER_MARGIN + 7, 0,
|
||||
box.height(), box.height());
|
||||
|
||||
qreal hw = static_cast<qreal>(_width)/2;
|
||||
if (Qt::Horizontal == slider->orientation()) {
|
||||
painter.translate(slider->x() + SLIDER_MARGIN,
|
||||
slider->y() + slider->height()/2
|
||||
- static_cast<qreal>(_width)/2);
|
||||
} else {
|
||||
painter.translate(slider->x() + slider->width()/2
|
||||
- static_cast<qreal>(_width)/2,
|
||||
slider->y() + SLIDER_MARGIN);
|
||||
}
|
||||
|
||||
QRectF geometry = Qt::Horizontal == slider->orientation()
|
||||
? QRectF(slider->x() + SLIDER_MARGIN,
|
||||
slider->y() + slider->height()/2 - hw,
|
||||
slider->width() - SLIDER_MARGIN*2,
|
||||
hw*2)
|
||||
: QRectF(slider->x() + slider->width()/2 - hw,
|
||||
slider->y() + SLIDER_MARGIN,
|
||||
hw*2,
|
||||
slider->height() - SLIDER_MARGIN*2);
|
||||
? QRectF(0, 0, slider->width() - SLIDER_MARGIN*2, _width)
|
||||
: QRectF(0, 0, _width, slider->height() - SLIDER_MARGIN*2);
|
||||
|
||||
bool inverted = slider->invertedAppearance();
|
||||
QRectF bgRect;
|
||||
QRectF fgRect;
|
||||
|
||||
QPointF pos = Qt::Horizontal == slider->orientation()
|
||||
? QPointF(slider->x() + SLIDER_MARGIN, 0)
|
||||
: QPointF(0, slider->y() + SLIDER_MARGIN);
|
||||
if (Qt::Horizontal == slider->orientation()) {
|
||||
fgRect = QRectF(0, 0, offset, _width);
|
||||
bgRect = QRectF(offset, 0, slider->width(), _width).intersected(geometry);
|
||||
} else {
|
||||
fgRect = QRectF(0, 0, _width, offset);
|
||||
bgRect = QRectF(0, offset, _width, slider->height()).intersected(geometry);
|
||||
}
|
||||
|
||||
painter.fillRect(QRectF(pos, box).intersected(geometry), inverted ? bg : fg);
|
||||
painter.fillRect(rect.intersected(geometry), inverted ? fg : bg);
|
||||
if (!slider->isEnabled()) {
|
||||
fgRect = fgRect.width() < 9 ? QRectF() : fgRect.adjusted(0, 0, -6, 0);
|
||||
bgRect = bgRect.width() < 9 ? QRectF() : bgRect.adjusted(6, 0, 0, 0);
|
||||
}
|
||||
|
||||
if (slider->invertedAppearance()) {
|
||||
qSwap(bgRect, fgRect);
|
||||
}
|
||||
|
||||
painter.fillRect(bgRect, bg);
|
||||
painter.fillRect(fgRect, fg);
|
||||
|
||||
#ifdef DEBUG_LAYOUT
|
||||
if (slider->hovered()) {
|
||||
|
|
Loading…
Reference in New Issue