Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable snapping in georeferenceer #60156

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions python/PyQt6/core/auto_generated/geometry/qgsrectangle.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@





class QgsRectangle
{
%Docstring(signature="appended")
Expand Down
2 changes: 2 additions & 0 deletions python/core/auto_generated/geometry/qgsrectangle.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@





class QgsRectangle
{
%Docstring(signature="appended")
Expand Down
71 changes: 70 additions & 1 deletion src/app/georeferencer/qgsgeorefmainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#include <QUrl>
#include <QActionGroup>

#include "qgsadvanceddigitizingdockwidget.h"
#include "qgsmapcanvassnappingutils.h"
#include "qgsmaptooladvanceddigitizing.h"
#include "qgssettings.h"
#include "qgsapplication.h"
#include "qgsgui.h"
Expand All @@ -46,6 +49,7 @@
#include "qgsmaptoolzoom.h"
#include "qgsmaptoolpan.h"
#include "qgsdatasourceselectdialog.h"
#include "qgssnappingwidget.h"

#include "qgsproject.h"
#include "qgsrasterlayer.h"
Expand Down Expand Up @@ -89,6 +93,11 @@ const QgsSettingsEntryString *QgsGeoreferencerMainWindow::settingLastRasterFileF

const QgsSettingsEntryString *QgsGeoreferencerMainWindow::settingLastTargetCrs = new QgsSettingsEntryString( QStringLiteral( "last-target-crs" ), sTreeGeoreferencer, QString(), QObject::tr( "Last used georeferencer target CRS" ) );

const QgsSettingsEntryBool *QgsGeoreferencerMainWindow::settingSnappingEnabled = new QgsSettingsEntryBool( QStringLiteral( "snapping-enabled" ), sTreeGeoreferencer, false, QObject::tr( "Snapping enabled." ) );

const QgsSettingsEntryEnumFlag<Qgis::SnappingTypes> *QgsGeoreferencerMainWindow::settingSnappingTypes = new QgsSettingsEntryEnumFlag<Qgis::SnappingTypes>( QStringLiteral( "snapping-types" ), sTreeGeoreferencer, Qgis::SnappingType::Vertex, QObject::tr( "Snapping types." ) );


QgsGeorefDockWidget::QgsGeorefDockWidget( const QString &title, QWidget *parent, Qt::WindowFlags flags )
: QgsDockWidget( title, parent, flags )
{
Expand Down Expand Up @@ -1098,6 +1107,66 @@ void QgsGeoreferencerMainWindow::createMapCanvas()

mCentralLayout->addWidget( mCanvas, 0, 0, 2, 1 );

// snapping + cad
mAdvancedDigitizingDockWidget = new QgsAdvancedDigitizingDockWidget( mCanvas );
addDockWidget( Qt::LeftDockWidgetArea, mAdvancedDigitizingDockWidget );
mAdvancedDigitizingDockWidget->hide();
connect( mActionAdvancedDigitizingDock, &QAction::triggered, mAdvancedDigitizingDockWidget, [=]( bool checked ) { mAdvancedDigitizingDockWidget->setVisible( checked ); } );

QgsSnappingConfig snappingConfig;
snappingConfig.setMode( Qgis::SnappingMode::AllLayers );
snappingConfig.setTypeFlag( settingSnappingTypes->value() );
snappingConfig.setTolerance( 10 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you use the default snaping tolerance?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't find any default snapping tolerance!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be "default-snapping-tolerance" available in Options->Map Tools->Digitizing->Snapping

snappingConfig.setUnits( Qgis::MapToolUnit::Pixels );
snappingConfig.setEnabled( settingSnappingEnabled->value() );

mSnappingUtils = new QgsMapCanvasSnappingUtils( mCanvas, this );
mSnappingUtils->setConfig( snappingConfig );
mCanvas->setSnappingUtils( mSnappingUtils );

// type button
mSnappingTypeButton = new QToolButton( this );
mSnappingTypeButton->setCheckable( true );
mSnappingTypeButton->setChecked( snappingConfig.enabled() );
mSnappingTypeButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconSnapping.svg" ) ) );
mSnappingTypeButton->setToolTip( tr( "Snapping Type" ) );
mSnappingTypeButton->setPopupMode( QToolButton::MenuButtonPopup );
SnapTypeMenu *typeMenu = new SnapTypeMenu( tr( "Set Snapping Mode" ), this );

for ( Qgis::SnappingType type : qgsEnumList<Qgis::SnappingType>() )
{
if ( type == Qgis::SnappingType::NoSnap )
continue;
QAction *action = new QAction( QgsSnappingConfig::snappingTypeToIcon( type ), QgsSnappingConfig::snappingTypeToString( type ), typeMenu );
action->setData( QVariant::fromValue( type ) );
action->setCheckable( true );
action->setChecked( snappingConfig.typeFlag().testFlag( type ) );
typeMenu->addAction( action );
mSnappingTypeActions << action;
}

mSnappingTypeButton->setMenu( typeMenu );
mSnappingTypeButton->setObjectName( QStringLiteral( "SnappingTypeButton" ) );
connect( mSnappingTypeButton, &QToolButton::triggered, this, [=]( QAction *action ) {
QgsSnappingConfig snappingConfig = mSnappingUtils->config();
unsigned int type = static_cast<int>( snappingConfig.typeFlag() );
const Qgis::SnappingTypes actionFlag = static_cast<Qgis::SnappingTypes>( action->data().toInt() );
type ^= actionFlag;
snappingConfig.setTypeFlag( static_cast<Qgis::SnappingTypes>( type ) );
mSnappingUtils->setConfig( snappingConfig );
} );

connect( mSnappingTypeButton, &QToolButton::clicked, this, [=]( bool checked ) {
QgsSnappingConfig snappingConfig = mSnappingUtils->config();
snappingConfig.setEnabled( checked );
mSnappingUtils->setConfig( snappingConfig );
for ( QAction *action : std::as_const( mSnappingTypeActions ) )
action->setEnabled( checked );
} );

toolBarEdit->insertWidget( mActionAdvancedDigitizingDock, mSnappingTypeButton );


// set up map tools
mToolZoomIn = new QgsMapToolZoom( mCanvas, false /* zoomOut */ );
mToolZoomIn->setAction( mActionZoomIn );
Expand All @@ -1108,7 +1177,7 @@ void QgsGeoreferencerMainWindow::createMapCanvas()
mToolPan = new QgsMapToolPan( mCanvas );
mToolPan->setAction( mActionPan );

mToolAddPoint = new QgsGeorefToolAddPoint( mCanvas );
mToolAddPoint = new QgsGeorefToolAddPoint( mCanvas, mAdvancedDigitizingDockWidget );
mToolAddPoint->setAction( mActionAddPoint );
connect( mToolAddPoint, &QgsGeorefToolAddPoint::showCoordDialog, this, &QgsGeoreferencerMainWindow::showCoordDialog );

Expand Down
8 changes: 8 additions & 0 deletions src/app/georeferencer/qgsgeorefmainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ class QPlainTextEdit;
class QLabel;

class QgisInterface;
class QgsAdvancedDigitizingDockWidget;
class QgsDoubleSpinBox;
class QgsGeorefDataPoint;
class QgsGCPListWidget;
class QgsMapTool;
class QgsMapCanvas;
class QgsMapCanvasSnappingUtils;
class QgsMapCoordsDialog;
class QgsPointXY;
class QgsRasterLayer;
Expand Down Expand Up @@ -76,6 +78,8 @@ class APP_EXPORT QgsGeoreferencerMainWindow : public QMainWindow, private Ui::Qg
static const QgsSettingsEntryString *settingLastSourceFolder;
static const QgsSettingsEntryString *settingLastRasterFileFilter;
static const QgsSettingsEntryString *settingLastTargetCrs;
static const QgsSettingsEntryBool *settingSnappingEnabled;
static const QgsSettingsEntryEnumFlag<Qgis::SnappingTypes> *settingSnappingTypes;

QgsGeoreferencerMainWindow( QWidget *parent = nullptr, Qt::WindowFlags fl = Qt::WindowFlags() );
~QgsGeoreferencerMainWindow() override;
Expand Down Expand Up @@ -271,6 +275,10 @@ class APP_EXPORT QgsGeoreferencerMainWindow : public QMainWindow, private Ui::Qg
QList<QgsGcpPoint> mSavedPoints;

QgsMapCanvas *mCanvas = nullptr;
QgsMapCanvasSnappingUtils *mSnappingUtils = nullptr;
QgsAdvancedDigitizingDockWidget *mAdvancedDigitizingDockWidget = nullptr;
QToolButton *mSnappingTypeButton = nullptr;
QList<QAction *> mSnappingTypeActions;
std::unique_ptr<QgsMapLayer> mLayer;

QgsMapTool *mToolZoomIn = nullptr;
Expand Down
13 changes: 4 additions & 9 deletions src/app/georeferencer/qgsgeoreftooladdpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@
#include "moc_qgsgeoreftooladdpoint.cpp"
#include "qgsmapmouseevent.h"

QgsGeorefToolAddPoint::QgsGeorefToolAddPoint( QgsMapCanvas *canvas )
: QgsMapToolEmitPoint( canvas )
QgsGeorefToolAddPoint::QgsGeorefToolAddPoint( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *advancedDigitizingDockWidget )
: QgsMapToolCapture( canvas, advancedDigitizingDockWidget, QgsMapToolCapture::CaptureMode::CapturePoint )
{
}

// Mouse press event for overriding
void QgsGeorefToolAddPoint::canvasPressEvent( QgsMapMouseEvent *e )
void QgsGeorefToolAddPoint::pointCaptured( const QgsPoint &point )
{
// Only add point on Qt:LeftButton
if ( Qt::LeftButton == e->button() )
{
emit showCoordDialog( toMapCoordinates( e->pos() ) );
}
emit showCoordDialog( point );
}
10 changes: 5 additions & 5 deletions src/app/georeferencer/qgsgeoreftooladdpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@
#define QGSGEOREFTOOLADDPOINT_H


#include "qgsmaptoolemitpoint.h"
#include "qgsmaptoolcapture.h"

class QgsPointXY;
class QgsMapCanvas;
class QgsAdvancedDigitizingDockWidget;

class QgsGeorefToolAddPoint : public QgsMapToolEmitPoint
class QgsGeorefToolAddPoint : public QgsMapToolCapture
{
Q_OBJECT

public:
explicit QgsGeorefToolAddPoint( QgsMapCanvas *canvas );
explicit QgsGeorefToolAddPoint( QgsMapCanvas *canvas, QgsAdvancedDigitizingDockWidget *advancedDigitizingDockWidget );

// Mouse events for overriding
void canvasPressEvent( QgsMapMouseEvent *e ) override;
void pointCaptured( const QgsPoint &point );

signals:
void showCoordDialog( const QgsPointXY &sourceCoordinates );
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgssnappingwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ class QgsScaleWidget;

#include "qgssnappingconfig.h"

#include <QMenu>
#include <QWidget>
#include <QSettings>

#include "qgis_app.h"

/**
Expand Down
8 changes: 5 additions & 3 deletions src/core/geometry/qgsrectangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@
#ifndef QGSRECTANGLE_H
#define QGSRECTANGLE_H

#include "qgis_core.h"
#include "qgis.h"
#include <iosfwd>
#include <QDomDocument>
#include <QRectF>

#include "qgis_core.h"
#include "qgis.h"
#include "qgspointxy.h"


class QString;
class QRectF;
class QgsBox3D;
#include "qgspointxy.h"


/**
Expand Down
22 changes: 20 additions & 2 deletions src/ui/georeferencer/qgsgeorefpluginguibase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
<addaction name="mActionAddPoint"/>
<addaction name="mActionDeletePoint"/>
<addaction name="mActionMoveGCPPoint"/>
<addaction name="mActionAdvancedDigitizingDock"/>
</widget>
<widget class="QToolBar" name="toolBarView">
<property name="windowTitle">
Expand Down Expand Up @@ -150,7 +151,7 @@
</widget>
<widget class="QgsDockWidget" name="dockWidgetGCPpoints">
<property name="allowedAreas">
<set>Qt::AllDockWidgetAreas</set>
<set>Qt::DockWidgetArea::AllDockWidgetAreas</set>
</property>
<property name="windowTitle">
<string>GCP table</string>
Expand Down Expand Up @@ -396,6 +397,24 @@
<string>Open Vector…</string>
</property>
</action>
<action name="mActionAdvancedDigitizingDock">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/cadtools/cad.svg</normaloff>:/images/themes/default/cadtools/cad.svg</iconset>
</property>
<property name="text">
<string>Advanced Digitizing Dock</string>
</property>
<property name="toolTip">
<string>Show Advanced Digitizing Dock</string>
</property>
<property name="menuRole">
<enum>QAction::MenuRole::NoRole</enum>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
Expand All @@ -407,7 +426,6 @@
</customwidgets>
<resources>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
</resources>
<connections/>
</ui>
Loading