Skip to content

Commit

Permalink
[needs-docs] Also use click-click behavior for offset point symbols tool
Browse files Browse the repository at this point in the history
To match consistency with all the other QGIS map tools!
  • Loading branch information
nyalldawson committed Oct 21, 2020
1 parent d5164e1 commit 00a3a6b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 36 deletions.
90 changes: 55 additions & 35 deletions src/app/qgsmaptooloffsetpointsymbol.cpp
Expand Up @@ -69,10 +69,52 @@ bool QgsMapToolOffsetPointSymbol::layerIsOffsetable( QgsMapLayer *ml )

void QgsMapToolOffsetPointSymbol::canvasPressEvent( QgsMapMouseEvent *e )
{
mMarkerSymbol.reset( nullptr );
mClickedPoint = e->mapPoint();
mSymbolRotation = 0.0;
QgsMapToolPointSymbol::canvasPressEvent( e );
if ( !mOffsetting )
{
if ( e->button() != Qt::LeftButton )
return;

// first click -- starts offsetting
mMarkerSymbol.reset( nullptr );
mClickedPoint = e->mapPoint();
mSymbolRotation = 0.0;
QgsMapToolPointSymbol::canvasPressEvent( e );
}
else
{
// second click stops it.
// only left clicks "save" edits - right clicks discard them
if ( e->button() == Qt::LeftButton && mActiveLayer )
{
QMap<int, QVariant> attrs = calculateNewOffsetAttributes( mClickedPoint, e->mapPoint() );
mActiveLayer->beginEditCommand( tr( "Offset symbol" ) );
bool offsetSuccess = true;

//write offset to attributes
QMap<int, QVariant>::const_iterator it = attrs.constBegin();
for ( ; it != attrs.constEnd(); ++it )
{
if ( !mActiveLayer->changeAttributeValue( mFeatureNumber, it.key(), it.value() ) )
{
offsetSuccess = false;
}
}

if ( offsetSuccess )
{
mActiveLayer->endEditCommand();
}
else
{
mActiveLayer->destroyEditCommand();
}
}
mOffsetting = false;
delete mOffsetItem;
mOffsetItem = nullptr;
if ( mActiveLayer )
mActiveLayer->triggerRepaint();
}
}

void QgsMapToolOffsetPointSymbol::canvasPressOnFeature( QgsMapMouseEvent *e, const QgsFeature &feature, const QgsPointXY &snappedPoint )
Expand Down Expand Up @@ -130,40 +172,18 @@ void QgsMapToolOffsetPointSymbol::canvasMoveEvent( QgsMapMouseEvent *e )
updateOffsetPreviewItem( mClickedPoint, e->mapPoint() );
}

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

if ( mOffsetting && mActiveLayer )
if ( mOffsetting && e && e->key() == Qt::Key_Escape && !e->isAutoRepeat() )
{
QMap<int, QVariant> attrs = calculateNewOffsetAttributes( mClickedPoint, e->mapPoint() );
mActiveLayer->beginEditCommand( tr( "Offset symbol" ) );
bool offsetSuccess = true;

//write offset to attributes
QMap<int, QVariant>::const_iterator it = attrs.constBegin();
for ( ; it != attrs.constEnd(); ++it )
{
if ( !mActiveLayer->changeAttributeValue( mFeatureNumber, it.key(), it.value() ) )
{
offsetSuccess = false;
}
}

if ( offsetSuccess )
{
mActiveLayer->endEditCommand();
}
else
{
mActiveLayer->destroyEditCommand();
}
mOffsetting = false;
delete mOffsetItem;
mOffsetItem = nullptr;
}
else
{
QgsMapToolPointSymbol::keyPressEvent( e );
}
mOffsetting = false;
delete mOffsetItem;
mOffsetItem = nullptr;
if ( mActiveLayer )
mActiveLayer->triggerRepaint();
}

void QgsMapToolOffsetPointSymbol::createPreviewItem( QgsMarkerSymbol *markerSymbol )
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptooloffsetpointsymbol.h
Expand Up @@ -39,7 +39,7 @@ class APP_EXPORT QgsMapToolOffsetPointSymbol: 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 map layer can be offset. This means the layer
Expand Down

0 comments on commit 00a3a6b

Please sign in to comment.