diff --git a/components/slider.cpp b/components/slider.cpp index 1ffe742..404192f 100644 --- a/components/slider.cpp +++ b/components/slider.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include "slider_p.h" @@ -11,7 +12,7 @@ Slider::Slider(QWidget *parent) : QAbstractSlider(parent), d_ptr(new SliderPrivate(this)) { - d_ptr->init(this); + d_func()->init(this); setFocusPolicy(Qt::StrongFocus); @@ -20,6 +21,8 @@ Slider::Slider(QWidget *parent) sp.transpose(); setSizePolicy(sp); setAttribute(Qt::WA_WState_OwnSizePolicy, false); + + QCoreApplication::processEvents(); } Slider::~Slider() @@ -38,7 +41,8 @@ int Slider::thumbOffset() const maximum(), sliderPosition(), Qt::Horizontal == orientation() - ? rect().width() - 20 : rect().height() - 20, + ? rect().width() -SLIDER_MARGIN*2 + : rect().height() - SLIDER_MARGIN*2, invertedAppearance()); } diff --git a/components/slider_p.h b/components/slider_p.h index 180a521..24d6aab 100644 --- a/components/slider_p.h +++ b/components/slider_p.h @@ -11,9 +11,6 @@ #include "lib/style.h" #include "sliderthumb.h" -// change -#define THUMB_OUTER_SIZE 20 - class SliderPrivate { Q_DISABLE_COPY(SliderPrivate) @@ -81,15 +78,15 @@ void SliderPrivate::init(Slider *slider) pulseInState->assignProperty(thumb, "haloSize", 28); disabledState->assignProperty(thumb, "diameter", 7); - //disabledState->assignProperty(thumb, "fillColor", QColor(200, 200, 200)); + disabledState->assignProperty(thumb, "fillColor", QColor(200, 200, 200)); inactiveState->assignProperty(thumb, "diameter", 11); focusState->assignProperty(thumb, "diameter", 11); slidingState->assignProperty(thumb, "diameter", 17); - //inactiveState->assignProperty(thumb, "fillColor", QColor(0, 0, 0)); - //focusState->assignProperty(thumb, "fillColor", QColor(0, 0, 0)); - //slidingState->assignProperty(thumb, "fillColor", QColor(0, 0, 0)); + inactiveState->assignProperty(thumb, "fillColor", QColor(0, 0, 0)); + focusState->assignProperty(thumb, "fillColor", QColor(0, 0, 0)); + slidingState->assignProperty(thumb, "fillColor", QColor(0, 0, 0)); machine.addState(topState); @@ -181,29 +178,25 @@ void SliderPrivate::init(Slider *slider) QState *minState = new QState(sndState); QState *normalState = new QState(sndState); - minState->assignProperty(thumb, "fillColor", QColor(0, 0, 0)); - minState->assignProperty(thumb, "borderWidth", 0); - normalState->assignProperty(thumb, "fillColor", QColor(255, 255, 255)); - normalState->assignProperty(thumb, "borderWidth", 1.5); + minState->assignProperty(thumb, "minFillColor", QColor(255, 255, 255)); + minState->assignProperty(thumb, "fillColor", QColor(255, 255, 255)); + minState->assignProperty(thumb, "borderWidth", 2); + normalState->assignProperty(thumb, "fillColor", QColor(0, 0, 0)); + normalState->assignProperty(thumb, "minFillColor", QColor(0, 0, 0)); + normalState->assignProperty(thumb, "borderWidth", 0); sndState->setInitialState(minState); - transition = new QSignalTransition(slider, SIGNAL(changedToMinimum())); + transition = new QSignalTransition(slider, SIGNAL(changedFromMinimum())); transition->setTargetState(normalState); animation = new QPropertyAnimation(thumb, "fillColor"); animation->setDuration(200); transition->addAnimation(animation); - animation = new QPropertyAnimation(thumb, "borderWidth"); - animation->setDuration(200); - transition->addAnimation(animation); minState->addTransition(transition); - transition = new QSignalTransition(slider, SIGNAL(changedFromMinimum())); + transition = new QSignalTransition(slider, SIGNAL(changedToMinimum())); transition->setTargetState(minState); - animation = new QPropertyAnimation(thumb, "fillColor"); - animation->setDuration(200); - transition->addAnimation(animation); - animation = new QPropertyAnimation(thumb, "borderWidth"); + animation = new QPropertyAnimation(thumb, "minFillColor"); animation->setDuration(200); transition->addAnimation(animation); normalState->addTransition(transition); @@ -216,10 +209,10 @@ QRectF SliderPrivate::trackGeometry() const Q_Q(const Slider); return Qt::Horizontal == q->orientation() - ? QRectF(THUMB_OUTER_SIZE/2, q->rect().height()/2 - 1, - q->rect().width() - THUMB_OUTER_SIZE, 2) - : QRectF(q->rect().width()/2 - 1, THUMB_OUTER_SIZE/2, 2, - q->rect().height() - THUMB_OUTER_SIZE); + ? QRectF(SLIDER_MARGIN, q->rect().height()/2 - 1, + q->rect().width() - SLIDER_MARGIN*2, 2) + : QRectF(q->rect().width()/2 - 1, SLIDER_MARGIN, 2, + q->rect().height() - SLIDER_MARGIN*2); } QRectF SliderPrivate::thumbGeometry() const @@ -227,10 +220,10 @@ QRectF SliderPrivate::thumbGeometry() const Q_Q(const Slider); return Qt::Horizontal == q->orientation() - ? QRectF(q->thumbOffset(), q->rect().height()/2 - THUMB_OUTER_SIZE/2, - THUMB_OUTER_SIZE, THUMB_OUTER_SIZE) - : QRectF(q->rect().width()/2 - THUMB_OUTER_SIZE/2, q->thumbOffset(), - THUMB_OUTER_SIZE, THUMB_OUTER_SIZE); + ? QRectF(q->thumbOffset(), q->rect().height()/2 - SLIDER_MARGIN, + SLIDER_MARGIN*2, SLIDER_MARGIN*2) + : QRectF(q->rect().width()/2 - SLIDER_MARGIN, q->thumbOffset(), + SLIDER_MARGIN*2, SLIDER_MARGIN*2); } void SliderPrivate::paintTrack(QPainter *painter) @@ -247,7 +240,7 @@ void SliderPrivate::paintTrack(QPainter *painter) bg.setStyle(Qt::SolidPattern); bg.setColor(trackColor); - qreal offset = q->thumbOffset() + THUMB_OUTER_SIZE/2; + qreal offset = q->thumbOffset() + SLIDER_MARGIN; QSizeF box(q->isEnabled() ? offset : offset - 7, qMax(q->width(), q->height())); @@ -286,13 +279,13 @@ int SliderPrivate::valueFromPosition(const QPoint &pos) const int position = Qt::Horizontal == q->orientation() ? pos.x() : pos.y(); int span = Qt::Horizontal == q->orientation() - ? q->rect().width() - THUMB_OUTER_SIZE - : q->rect().height() - THUMB_OUTER_SIZE; + ? q->rect().width() - SLIDER_MARGIN*2 + : q->rect().height() - SLIDER_MARGIN*2; return Style::sliderValueFromPosition( q->minimum(), q->maximum(), - position - THUMB_OUTER_SIZE/2, + position - SLIDER_MARGIN, span, q->invertedAppearance()); } diff --git a/components/sliderthumb.cpp b/components/sliderthumb.cpp index 2e4054b..32fb0e7 100644 --- a/components/sliderthumb.cpp +++ b/components/sliderthumb.cpp @@ -3,16 +3,11 @@ #include #include "slider.h" -// change -#define THUMB_OUTER_SIZE 20 - SliderThumb::SliderThumb(Slider *slider) : QWidget(slider->parentWidget()), slider(slider), _diameter(11), - _borderWidth(0), - _fillColor(Qt::white), - _minFillColor(Qt::white), + _borderWidth(2), _haloSize(0) { slider->installEventFilter(this); @@ -49,10 +44,10 @@ void SliderThumb::paintEvent(QPaintEvent *event) painter.setRenderHint(QPainter::Antialiasing); QPointF disp = Qt::Horizontal == slider->orientation() - ? QPointF(THUMB_OUTER_SIZE/2 + slider->thumbOffset(), + ? QPointF(SLIDER_MARGIN + slider->thumbOffset(), slider->height()/2) : QPointF(slider->width()/2, - THUMB_OUTER_SIZE/2 + slider->thumbOffset()); + SLIDER_MARGIN + slider->thumbOffset()); QRectF halo((slider->pos() - QPointF(_haloSize, _haloSize)/2) + disp, QSizeF(_haloSize, _haloSize)); @@ -62,12 +57,13 @@ void SliderThumb::paintEvent(QPaintEvent *event) // Knob brush.setStyle(Qt::SolidPattern); - brush.setColor(slider->sliderPosition() > slider->minimum() + brush.setColor(slider->value() > slider->minimum() ? _fillColor : _minFillColor); painter.setBrush(brush); if (_borderWidth > 0) { QPen pen; + pen.setColor(slider->trackColor()); pen.setWidthF(_borderWidth); painter.setPen(pen); } else { @@ -75,10 +71,10 @@ void SliderThumb::paintEvent(QPaintEvent *event) } QRectF geometry = Qt::Horizontal == slider->orientation() - ? QRectF(slider->thumbOffset(), slider->height()/2 - THUMB_OUTER_SIZE/2, - THUMB_OUTER_SIZE, THUMB_OUTER_SIZE).translated(slider->pos()) - : QRectF(slider->width()/2 - THUMB_OUTER_SIZE/2, slider->thumbOffset(), - THUMB_OUTER_SIZE, THUMB_OUTER_SIZE).translated(slider->pos()); + ? QRectF(slider->thumbOffset(), slider->height()/2 - SLIDER_MARGIN, + SLIDER_MARGIN*2, SLIDER_MARGIN*2).translated(slider->pos()) + : QRectF(slider->width()/2 - SLIDER_MARGIN, slider->thumbOffset(), + SLIDER_MARGIN*2, SLIDER_MARGIN*2).translated(slider->pos()); QRectF thumb(0, 0, _diameter, _diameter); @@ -93,6 +89,8 @@ void SliderThumb::paintEvent(QPaintEvent *event) painter.setPen(pen); painter.setBrush(Qt::NoBrush); + painter.drawRect(geometry); + painter.drawRect(rect().adjusted(0, 0, -2, -2)); #endif diff --git a/components/sliderthumb.h b/components/sliderthumb.h index 003dec6..1d6fca4 100644 --- a/components/sliderthumb.h +++ b/components/sliderthumb.h @@ -1,6 +1,8 @@ #ifndef SLIDERTHUMB_H #define SLIDERTHUMB_H +#define SLIDER_MARGIN 30 + #include class Slider; diff --git a/examples/sliderexamples.cpp b/examples/sliderexamples.cpp index 477d2f4..41821e9 100644 --- a/examples/sliderexamples.cpp +++ b/examples/sliderexamples.cpp @@ -176,6 +176,7 @@ SliderExamples::SliderExamples(QWidget *parent) } { Slider *slider = new Slider; + //slider->setValue(20); slider->setDisabled(true); slider->setMinimumWidth(250); @@ -191,6 +192,44 @@ SliderExamples::SliderExamples(QWidget *parent) mainLayout->addWidget(frame); } + { + QVBoxLayout *layout = new QVBoxLayout; + QWidget *widget = new QWidget; + widget->setLayout(layout); + widget->setMinimumWidth(350); + + Slider *slider = new Slider; + + QHBoxLayout *hLayout = new QHBoxLayout; + hLayout->addWidget(slider); + + layout->addLayout(hLayout); + + slider = new Slider; + + hLayout = new QHBoxLayout; + hLayout->addWidget(slider); + + layout->addLayout(hLayout); + + slider = new Slider; + + hLayout = new QHBoxLayout; + hLayout->addWidget(slider); + + layout->addLayout(hLayout); + + ExampleView *view = new ExampleView; + view->setWidget(widget); + + Frame *frame = new Frame; + frame->setCodeSnippet( + "xx" + ); + frame->setWidget(view); + + mainLayout->addWidget(frame); + } }