Skip to content

Commit

Permalink
[needs-docs] Follow standard click-click behavior for rotate point
Browse files Browse the repository at this point in the history
markers tool

Instead of click and hold to rotate, make this tool follow every
other QGIS map tool and use click-click to rotate, click-right click
to cancel.
  • Loading branch information
nyalldawson committed Oct 21, 2020
1 parent 0480a64 commit d5164e1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 44 deletions.
105 changes: 62 additions & 43 deletions src/app/qgsmaptoolrotatepointsymbols.cpp
Expand Up @@ -69,9 +69,59 @@ bool QgsMapToolRotatePointSymbols::layerIsRotatable( QgsMapLayer *ml )

void QgsMapToolRotatePointSymbols::canvasPressEvent( QgsMapMouseEvent *e )
{
mCurrentRotationAttributes.clear();
mMarkerSymbol.reset( nullptr );
QgsMapToolPointSymbol::canvasPressEvent( e );
if ( !mRotating )
{
if ( e->button() != Qt::LeftButton )
return;

// first click -- starts rotation
mCurrentRotationAttributes.clear();
mMarkerSymbol.reset( nullptr );
QgsMapToolPointSymbol::canvasPressEvent( e );
}
else
{
// second click stops it.
// only left clicks "save" edits - right clicks discard them
if ( e->button() == Qt::LeftButton && mActiveLayer )
{
mActiveLayer->beginEditCommand( tr( "Rotate symbol" ) );
bool rotateSuccess = true;

//write mCurrentRotationFeature to all rotation attributes of feature (mFeatureNumber)
int rotation;
if ( mCtrlPressed ) //round to 15 degrees
{
rotation = roundTo15Degrees( mCurrentRotationFeature );
}
else
{
rotation = static_cast<int>( mCurrentRotationFeature );
}

QSet<int>::const_iterator it = mCurrentRotationAttributes.constBegin();
for ( ; it != mCurrentRotationAttributes.constEnd(); ++it )
{
if ( !mActiveLayer->changeAttributeValue( mFeatureNumber, *it, rotation ) )
{
rotateSuccess = false;
}
}

if ( rotateSuccess )
{
mActiveLayer->endEditCommand();
}
else
{
mActiveLayer->destroyEditCommand();
}
mActiveLayer->triggerRepaint();
}
mRotating = false;
delete mRotationItem;
mRotationItem = nullptr;
}
}

void QgsMapToolRotatePointSymbols::canvasPressOnFeature( QgsMapMouseEvent *e, const QgsFeature &feature, const QgsPointXY &snappedPoint )
Expand Down Expand Up @@ -156,49 +206,18 @@ void QgsMapToolRotatePointSymbols::canvasMoveEvent( QgsMapMouseEvent *e )
setPixmapItemRotation( displayValue );
}

void QgsMapToolRotatePointSymbols::canvasReleaseEvent( QgsMapMouseEvent *e )
void QgsMapToolRotatePointSymbols::keyPressEvent( QKeyEvent *e )
{
Q_UNUSED( e )

if ( mRotating && mActiveLayer )
if ( mRotating && e && e->key() == Qt::Key_Escape && !e->isAutoRepeat() )
{
mActiveLayer->beginEditCommand( tr( "Rotate symbol" ) );
bool rotateSuccess = true;

//write mCurrentRotationFeature to all rotation attributes of feature (mFeatureNumber)
int rotation;
if ( mCtrlPressed ) //round to 15 degrees
{
rotation = roundTo15Degrees( mCurrentRotationFeature );
}
else
{
rotation = static_cast<int>( mCurrentRotationFeature );
}

QSet<int>::const_iterator it = mCurrentRotationAttributes.constBegin();
for ( ; it != mCurrentRotationAttributes.constEnd(); ++it )
{
if ( !mActiveLayer->changeAttributeValue( mFeatureNumber, *it, rotation ) )
{
rotateSuccess = false;
}
}

if ( rotateSuccess )
{
mActiveLayer->endEditCommand();
}
else
{
mActiveLayer->destroyEditCommand();
}
mRotating = false;
delete mRotationItem;
mRotationItem = nullptr;
}
else
{
QgsMapToolPointSymbol::keyPressEvent( e );
}
mRotating = false;
delete mRotationItem;
mRotationItem = nullptr;
if ( mActiveLayer )
mActiveLayer->triggerRepaint();
}

double QgsMapToolRotatePointSymbols::calculateAzimut( QPoint mousePos )
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptoolrotatepointsymbols.h
Expand Up @@ -39,7 +39,7 @@ class APP_EXPORT QgsMapToolRotatePointSymbols: public QgsMapToolPointSymbol

void canvasPressEvent( QgsMapMouseEvent *e ) override;
void canvasMoveEvent( QgsMapMouseEvent *e ) override;
void canvasReleaseEvent( QgsMapMouseEvent *e ) override;
void keyPressEvent( QKeyEvent *e ) override;

/**
* Returns TRUE if the symbols of a maplayer can be rotated. This means the layer
Expand Down

0 comments on commit d5164e1

Please sign in to comment.