Skip to content

Commit

Permalink
Show focus rects on range slider sub components
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 26, 2020
1 parent a9e2644 commit 7177226
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/gui/qgsrangeslider.cpp
Expand Up @@ -404,6 +404,33 @@ void QgsRangeSlider::paintEvent( QPaintEvent * )
else
mStyleOption.state &= ~QStyle::State_Sunken;
style()->drawComplexControl( QStyle::CC_Slider, &mStyleOption, &painter );

if ( hasFocus() && mFocusControl != None )
{
//draw focus rect
QStyleOptionFocusRect option;
option.initFrom( this );
option.state = QStyle::State_KeyboardFocusChange;
if ( mFocusControl == Lower )
{
mStyleOption.sliderPosition = mFlipped ? mStyleOption.maximum - mLowerValue : mLowerValue;
option.rect = style()->subControlRect( QStyle::CC_Slider, &mStyleOption, QStyle::SC_SliderHandle, this );
}
else if ( mFocusControl == Upper )
{
mStyleOption.sliderPosition = mFlipped ? mStyleOption.maximum - mUpperValue : mUpperValue;
option.rect = style()->subControlRect( QStyle::CC_Slider, &mStyleOption, QStyle::SC_SliderHandle, this );
}
else if ( mFocusControl == Range )
{
option.rect = selectedRangeRect();
if ( mStyleOption.orientation == Qt::Horizontal )
option.rect = option.rect.adjusted( 0, -1, 0, 1 );
else
option.rect = option.rect.adjusted( -1, 0, 1, 0 );
}
style()->drawPrimitive( QStyle::PE_FrameFocusRect, &option, &painter );
}
}

void QgsRangeSlider::mousePressEvent( QMouseEvent *event )
Expand Down Expand Up @@ -436,16 +463,24 @@ void QgsRangeSlider::mousePressEvent( QMouseEvent *event )
{
mActiveControl = Range; // shift + drag over handle moves the whole range
mRangeDragOffset = overUpperControl ? mUpperClickOffset : mLowerClickOffset;
mFocusControl = overUpperControl ? Upper : Lower;
}
else if ( overLowerControl && overUpperControl )
mActiveControl = Both;
else if ( overLowerControl )
{
mActiveControl = Lower;
mFocusControl = Lower;
}
else if ( overUpperControl )
{
mActiveControl = Upper;
mFocusControl = Upper;
}
else if ( overSelectedRange )
{
mActiveControl = Range;
mFocusControl = Range;
}
else
mActiveControl = None;
Expand Down Expand Up @@ -476,6 +511,7 @@ void QgsRangeSlider::mouseMoveEvent( QMouseEvent *event )
if ( newPosition < mStartDragPos )
{
destControl = Lower;
mFocusControl = Lower;
if ( mUpperValue != mPreDragUpperValue )
{
changed = true;
Expand All @@ -485,6 +521,7 @@ void QgsRangeSlider::mouseMoveEvent( QMouseEvent *event )
else if ( newPosition > mStartDragPos )
{
destControl = Upper;
mFocusControl = Upper;
if ( mLowerValue != mPreDragLowerValue )
{
changed = true;
Expand Down Expand Up @@ -618,3 +655,4 @@ QSize QgsRangeSlider::minimumSizeHint() const
return s;
}


2 changes: 2 additions & 0 deletions src/gui/qgsrangeslider.h
Expand Up @@ -221,6 +221,7 @@ class GUI_EXPORT QgsRangeSlider : public QWidget
bool updateHoverControl( const QPoint &pos );
bool newHoverControl( const QPoint &pos );
QRect selectedRangeRect();
void drawFocusRect();

int mLowerValue = 0;
int mUpperValue = 0;
Expand All @@ -242,6 +243,7 @@ class GUI_EXPORT QgsRangeSlider : public QWidget
int mPreDragLowerValue = -1;
int mPreDragUpperValue = -1;
Control mHoverControl = None;
Control mFocusControl = Lower;
QStyle::SubControl mHoverSubControl = QStyle::SC_None;
QRect mHoverRect;

Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_qgsrangeslider.py
Expand Up @@ -260,7 +260,7 @@ def testChangeLimitsOutsideValue(self):
w.setRange(0, 10)
self.assertEqual(len(spy), 5)

w.setRangeLimits(7, 3) # flipped
w.setRangeLimits(7, 3) # flipped
self.assertEqual(w.lowerValue(), 3)
self.assertEqual(w.upperValue(), 7)
self.assertEqual(len(spy), 6)
Expand Down

0 comments on commit 7177226

Please sign in to comment.