Skip to content

Commit

Permalink
Move some logic to seperate private methods in class DoubleSlider.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek83 committed Dec 25, 2024
1 parent e3ecc02 commit 3c28a95
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 31 deletions.
4 changes: 4 additions & 0 deletions include/wble/DoubleSlider.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public Q_SLOTS:

double getNormalizedValue(double value) const;

QRect getInnerRectangle() const;

QRect getBarRectangle() const;

const double min_;
const double max_;

Expand Down
68 changes: 37 additions & 31 deletions src/DoubleSlider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,13 @@ double DoubleSlider::getNormalizedValue(double value) const

if (value > max_)
return max_;

return min_;
}

int DoubleSlider::getMousePosition(const QMouseEvent* event) const
{
QStyleOptionSlider defaultStyle;
initStyleOption(&defaultStyle);
const QRect innerRect(defaultStyle.rect);
const QRect innerRect{getInnerRectangle()};
const int sliderBarWidth{innerRect.width() - innerRect.x()};

// Handle drawing rectangle is shifted according to position on handle bar.
Expand Down Expand Up @@ -112,9 +111,7 @@ void DoubleSlider::changeEvent(QEvent* event)

bool DoubleSlider::mouseIsOnHandle(int mousePosX, int handlePos) const
{
QStyleOptionSlider defaultStyle;
initStyleOption(&defaultStyle);
const QRect innerRect(defaultStyle.rect);
const QRect innerRect{getInnerRectangle()};
const int sliderBarWidth{innerRect.width() - innerRect.x()};
const int handlePosX{QStyle::sliderPositionFromValue(
0, MAX_PERCENT, handlePos, sliderBarWidth)};
Expand Down Expand Up @@ -193,9 +190,7 @@ int DoubleSlider::getHandlePosition(Handle handle) const

void DoubleSlider::drawSliderBar(QPainter& painter) const
{
QStyleOptionSlider barStyle;
initStyleOption(&barStyle);
const QRect innerRect(barStyle.rect);
const QRect innerRect{getInnerRectangle()};

const int handleHight{
static_cast<int>(innerRect.height() * BAR_HEIGHT_RATIO)};
Expand Down Expand Up @@ -226,28 +221,7 @@ void DoubleSlider::setHandleRect()

void DoubleSlider::drawSliderBarBetweenHandles(QPainter& painter) const
{
QStyleOptionSlider defaultStyle;
initStyleOption(&defaultStyle);
const QRect innerRect(defaultStyle.rect);

const int barHeight{
static_cast<int>(std::round(innerRect.height() * BAR_HEIGHT_RATIO))};

const int leftHandlePosition{getHandlePosition(Handle::LEFT)};
const int rightHandlePosition{getHandlePosition(Handle::RIGHT)};

const int leftHandleMiddleX{
getHandleMiddlePosX(leftHandlePosition, handleRect_.width(),
innerRect.width() - innerRect.x())};

const int rightHandleMiddleX{
getHandleMiddlePosX(rightHandlePosition, handleRect_.width(),
innerRect.width() - innerRect.x())};

const QRect barRect{QRect(
innerRect.x() + leftHandleMiddleX, innerRect.y() + barHeight,
(innerRect.x() + rightHandleMiddleX) - leftHandleMiddleX, barHeight)};

const QRect barRect{getBarRectangle()};
const QBrush brush{palette().brush(QPalette::Dark)};
::qDrawShadePanel(&painter, barRect, palette(), true, 1, &brush);
}
Expand Down Expand Up @@ -286,3 +260,35 @@ void DoubleSlider::paintEvent(QPaintEvent* event)
drawSliderBarBetweenHandles(painter);
drawHandles(painter);
}

QRect DoubleSlider::getInnerRectangle() const
{
QStyleOptionSlider defaultStyle;
initStyleOption(&defaultStyle);
return defaultStyle.rect;
}

QRect DoubleSlider::getBarRectangle() const
{
const QRect innerRect{getInnerRectangle()};

const int barHeight{
static_cast<int>(std::round(innerRect.height() * BAR_HEIGHT_RATIO))};

const int leftHandlePosition{getHandlePosition(Handle::LEFT)};
const int rightHandlePosition{getHandlePosition(Handle::RIGHT)};

const int leftHandleMiddleX{
getHandleMiddlePosX(leftHandlePosition, handleRect_.width(),
innerRect.width() - innerRect.x())};

const int rightHandleMiddleX{
getHandleMiddlePosX(rightHandlePosition, handleRect_.width(),
innerRect.width() - innerRect.x())};

const QRect barRect{QRect(
innerRect.x() + leftHandleMiddleX, innerRect.y() + barHeight,
(innerRect.x() + rightHandleMiddleX) - leftHandleMiddleX, barHeight)};

return barRect;
}

0 comments on commit 3c28a95

Please sign in to comment.