Skip to content

Commit

Permalink
qgselevationprofilewidget: move tool can lock the abscissa with ctrl …
Browse files Browse the repository at this point in the history
…keypress
  • Loading branch information
Djedouas committed Jan 13, 2025
1 parent b693b38 commit 152799b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
70 changes: 68 additions & 2 deletions src/app/elevation/qgselevationprofiletoolmovepoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ QgsElevationProfileToolMovePoint::QgsElevationProfileToolMovePoint( QgsElevation

QgsElevationProfileToolMovePoint::~QgsElevationProfileToolMovePoint() = default;

void QgsElevationProfileToolMovePoint::keyPressEvent( QKeyEvent *e )
{
if ( !e->isAutoRepeat() )
{
switch ( e->key() )
{
case Qt::Key_Control:
mAbscissaLocked = true;
QgisApp::instance()->statusBar()->showMessage( tr( "Abscissa locked" ) );
break;
default:
break;
}
}
}

void QgsElevationProfileToolMovePoint::keyReleaseEvent( QKeyEvent * /* e */ )
{
QgisApp::instance()->statusBar()->showMessage( tr( "Hold Ctrl key to lock the abscissa" ) );
qgis::down_cast<QgsElevationProfileCanvas *>( mCanvas )->setCrossHairsItemIsDelegate( false );
mAbscissaLocked = false;
}

void QgsElevationProfileToolMovePoint::plotReleaseEvent( QgsPlotMouseEvent *event )
{
event->ignore();
Expand All @@ -51,7 +74,18 @@ void QgsElevationProfileToolMovePoint::plotReleaseEvent( QgsPlotMouseEvent *even

if ( mDragging )
{
mRubberBand->finish( QPointF(), Qt::KeyboardModifiers() );
QgsPointXY snappedPoint;
if ( mAbscissaLocked )
{
QgsPointXY point = event->pos();
point.setX( mStartX );
snappedPoint = mCanvas->snapToPlot( point.toQPointF().toPoint() );
if ( snappedPoint.isEmpty() )
snappedPoint = point;
}
else
snappedPoint = event->snappedPoint();

QgsGeometry geometry( std::make_unique<QgsPoint>( toMapCoordinates( snappedPoint ) ) );

if ( geometry.isNull() || geometry.isEmpty() )
Expand All @@ -74,12 +108,14 @@ void QgsElevationProfileToolMovePoint::plotReleaseEvent( QgsPlotMouseEvent *even
}

mLayer->changeGeometry( mFeatureId, geometry );
mRubberBand->finish( QPointF(), Qt::KeyboardModifiers() );
}
else
{
if ( !findFeature( snappedPoint.toQPointF() ) )
return;

mStartX = snappedPoint.toQPointF().x();
mRubberBand->start( snappedPoint.toQPointF(), Qt::KeyboardModifiers() );
}
mDragging = !mDragging;
Expand All @@ -91,7 +127,25 @@ void QgsElevationProfileToolMovePoint::plotMoveEvent( QgsPlotMouseEvent *event )
if ( !mDragging || !mRubberBand )
return;

mRubberBand->update( event->snappedPoint().toQPointF(), Qt::KeyboardModifiers() );
QgsElevationProfileCanvas *profileCanvas = qgis::down_cast<QgsElevationProfileCanvas *>( mCanvas );
profileCanvas->setCrossHairsItemIsDelegate( mAbscissaLocked );

QgsPointXY snappedPoint;
if ( mAbscissaLocked )
{
QgsPointXY point = event->pos();
point.setX( mStartX );
snappedPoint = mCanvas->snapToPlot( point.toQPointF().toPoint() );
if ( snappedPoint.isEmpty() )
snappedPoint = point;

profileCanvas->showCrossHairsItem();
profileCanvas->setCrossHairsItemPoint( snappedPoint.toQPointF().toPoint() );
}
else
snappedPoint = event->snappedPoint();

mRubberBand->update( snappedPoint.toQPointF(), Qt::KeyboardModifiers() );
}

bool QgsElevationProfileToolMovePoint::findFeature( QPointF pos )
Expand Down Expand Up @@ -138,6 +192,18 @@ void QgsElevationProfileToolMovePoint::setLayer( QgsVectorLayer *layer )
toggleAction();
}

void QgsElevationProfileToolMovePoint::deactivate()
{
QgisApp::instance()->statusBar()->clearMessage();
QgsPlotTool::deactivate();
}

void QgsElevationProfileToolMovePoint::activate()
{
QgisApp::instance()->statusBar()->showMessage( tr( "Hold Ctrl key to lock the abscissa" ) );
QgsPlotTool::activate();
}

void QgsElevationProfileToolMovePoint::toggleAction()
{
QAction *movePointAction = action();
Expand Down
8 changes: 6 additions & 2 deletions src/app/elevation/qgselevationprofiletoolmovepoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class QgsElevationProfileToolMovePoint : public QgsPlotTool

void plotMoveEvent( QgsPlotMouseEvent *event ) override;
void plotReleaseEvent( QgsPlotMouseEvent *event ) override;
void keyPressEvent( QKeyEvent *event ) override;
void keyReleaseEvent( QKeyEvent *e ) override;
void activate() override;
void deactivate() override;

void setLayer( QgsVectorLayer *layer );

Expand All @@ -51,8 +55,8 @@ class QgsElevationProfileToolMovePoint : public QgsPlotTool

std::unique_ptr< QgsPlotPointRubberBand > mRubberBand;
bool mDragging = false;

bool mAbscissaLocked = false;
double mStartX = 0;
};

#endif // QGSELEVATIONPROFILETOOLMOVEPOINT_H

0 comments on commit 152799b

Please # to comment.