Skip to content

Commit

Permalink
Nicer keyboard interaction when sliders are overlapping
Browse files Browse the repository at this point in the history
Since it's impossible to tell which slider is focused in this case,
automatically target whichever slider makes sense given the key pressed
  • Loading branch information
nyalldawson committed Nov 26, 2020
1 parent 5b72825 commit f30ced9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
47 changes: 43 additions & 4 deletions src/gui/qgsrangeslider.cpp
Expand Up @@ -708,13 +708,20 @@ void QgsRangeSlider::mouseReleaseEvent( QMouseEvent *event )

void QgsRangeSlider::keyPressEvent( QKeyEvent *event )
{
Control destControl = mFocusControl;
if ( ( destControl == Lower || destControl == Upper ) && mLowerValue == mUpperValue )
destControl = Both; //ambiguous destination, because both sliders are on top of each other

switch ( event->key() )
{
case Qt::Key_Left:
{
switch ( mStyleOption.orientation )
{
case Qt::Horizontal:
if ( destControl == Both )
mFocusControl = mFlipped ? Upper : Lower;

applyStep( mFlipped ? mSingleStep : -mSingleStep );
break;

Expand Down Expand Up @@ -764,6 +771,8 @@ void QgsRangeSlider::keyPressEvent( QKeyEvent *event )
switch ( mStyleOption.orientation )
{
case Qt::Horizontal:
if ( destControl == Both )
mFocusControl = mFlipped ? Lower : Upper;
applyStep( mFlipped ? -mSingleStep : mSingleStep );
break;

Expand Down Expand Up @@ -851,6 +860,9 @@ void QgsRangeSlider::keyPressEvent( QKeyEvent *event )
break;

case Qt::Vertical:
if ( destControl == Both )
mFocusControl = mFlipped ? Upper : Lower;

applyStep( mFlipped ? mSingleStep : -mSingleStep );
break;
}
Expand Down Expand Up @@ -900,6 +912,9 @@ void QgsRangeSlider::keyPressEvent( QKeyEvent *event )
break;

case Qt::Vertical:
if ( destControl == Both )
mFocusControl = mFlipped ? Lower : Upper;

applyStep( mFlipped ? -mSingleStep : mSingleStep );
break;
}
Expand All @@ -911,10 +926,16 @@ void QgsRangeSlider::keyPressEvent( QKeyEvent *event )
switch ( mStyleOption.orientation )
{
case Qt::Horizontal:
if ( destControl == Both )
mFocusControl = mFlipped ? Lower : Upper;

applyStep( mFlipped ? -mPageStep : mPageStep );
break;

case Qt::Vertical:
if ( destControl == Both )
mFocusControl = mFlipped ? Upper : Lower;

applyStep( mFlipped ? mPageStep : -mPageStep );
break;
}
Expand All @@ -926,18 +947,24 @@ void QgsRangeSlider::keyPressEvent( QKeyEvent *event )
switch ( mStyleOption.orientation )
{
case Qt::Horizontal:
if ( destControl == Both )
mFocusControl = mFlipped ? Upper : Lower;

applyStep( mFlipped ? mPageStep : -mPageStep );
break;

case Qt::Vertical:
if ( destControl == Both )
mFocusControl = mFlipped ? Lower : Upper;

applyStep( mFlipped ? -mPageStep : mPageStep );
break;
}
break;
}

case Qt::Key_Home:
switch ( mFocusControl )
switch ( destControl )
{
case Lower:
applyStep( mFlipped ? mUpperValue - mLowerValue : mStyleOption.minimum - mLowerValue );
Expand All @@ -951,15 +978,21 @@ void QgsRangeSlider::keyPressEvent( QKeyEvent *event )
applyStep( mFlipped ? mStyleOption.maximum - mUpperValue : mStyleOption.minimum - mLowerValue );
break;

case None:
case Both:
if ( destControl == Both )
mFocusControl = mFlipped ? Upper : Lower;

applyStep( mFlipped ? mStyleOption.maximum - mUpperValue : mStyleOption.minimum - mLowerValue );
break;

case None:
break;
}

break;

case Qt::Key_End:
switch ( mFocusControl )
switch ( destControl )
{
case Lower:
applyStep( mFlipped ? mStyleOption.minimum - mLowerValue : mUpperValue - mLowerValue );
Expand All @@ -973,8 +1006,14 @@ void QgsRangeSlider::keyPressEvent( QKeyEvent *event )
applyStep( mFlipped ? mStyleOption.minimum - mLowerValue : mStyleOption.maximum - mUpperValue );
break;

case None:
case Both:
if ( destControl == Both )
mFocusControl = mFlipped ? Lower : Upper;

applyStep( mFlipped ? mStyleOption.minimum - mLowerValue : mStyleOption.maximum - mUpperValue );
break;

case None:
break;
}
break;
Expand Down
5 changes: 3 additions & 2 deletions src/gui/qgsrangeslider.h
Expand Up @@ -264,8 +264,6 @@ class GUI_EXPORT QgsRangeSlider : public QWidget
QRect selectedRangeRect();
void drawFocusRect();

void applyStep( int step );

int mLowerValue = 0;
int mUpperValue = 0;

Expand All @@ -281,6 +279,9 @@ class GUI_EXPORT QgsRangeSlider : public QWidget
Both,
Range
};

void applyStep( int step );

Control mActiveControl = None;
int mStartDragPos = -1;
int mLowerClickOffset = 0;
Expand Down

0 comments on commit f30ced9

Please sign in to comment.