Skip to content

Commit

Permalink
fix tool interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
hamiltoncj committed Dec 16, 2019
1 parent 33c003b commit 0eb6a1a
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PLUGINNAME = latlontools
PLUGINS = "$(HOME)"/AppData/Roaming/QGIS/QGIS3/profiles/default/python/plugins/$(PLUGINNAME)
PY_FILES = latLonTools.py __init__.py copyLatLonTool.py zoomToLatLon.py settings.py multizoom.py mgrs.py showOnMapTool.py mapProviders.py tomgrs.py mgrstogeom.py digitizer.py util.py geom2field.py field2geom.py olc.py provider.py pluscodes.py utm.py coordinateConverter.py geohash.py
PY_FILES = latLonTools.py __init__.py copyLatLonTool.py captureCoordinate.py zoomToLatLon.py settings.py multizoom.py mgrs.py showOnMapTool.py mapProviders.py tomgrs.py mgrstogeom.py digitizer.py util.py geom2field.py field2geom.py olc.py provider.py pluscodes.py utm.py coordinateConverter.py geohash.py
EXTRAS = metadata.txt

deploy:
Expand Down
65 changes: 65 additions & 0 deletions captureCoordinate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from qgis.PyQt.QtCore import Qt, pyqtSignal
from qgis.PyQt.QtGui import QColor
from qgis.PyQt.QtCore import pyqtSlot
from qgis.core import QgsCoordinateTransform, QgsPointXY, QgsProject, QgsSettings
from qgis.gui import QgsMapToolEmitPoint, QgsVertexMarker
from .util import epsg4326

class CaptureCoordinate(QgsMapToolEmitPoint):
'''Class to interact with the map canvas to capture the coordinate
when the mouse button is pressed.'''
capturePoint = pyqtSignal(QgsPointXY)
captureStopped = pyqtSignal()

def __init__(self, canvas):
QgsMapToolEmitPoint.__init__(self, canvas)
self.canvas = canvas
self.vertex = None

def activate(self):
'''When activated set the cursor to a crosshair.'''
self.canvas.setCursor(Qt.CrossCursor)
self.snapcolor = QgsSettings().value( "/qgis/digitizing/snap_color" , QColor( Qt.magenta ) )

def deactivate(self):
self.removeVertexMarker()
self.captureStopped.emit()

def snappoint(self, qpoint):
match = self.canvas.snappingUtils().snapToMap(qpoint)
if match.isValid():
if self.vertex is None:
self.vertex = QgsVertexMarker(self.canvas)
self.vertex.setIconSize(12)
self.vertex.setPenWidth(2)
self.vertex.setColor(self.snapcolor)
self.vertex.setIconType(QgsVertexMarker.ICON_BOX)
self.vertex.setCenter(match.point())
return (match.point()) # Returns QgsPointXY
else:
self.removeVertexMarker()
return self.toMapCoordinates(qpoint) # QPoint input, returns QgsPointXY

def canvasMoveEvent(self, event):
'''Capture the coordinate as the user moves the mouse over
the canvas.'''
self.snappoint(event.originalPixelPoint()) # input is QPoint

def canvasReleaseEvent(self, event):
'''Capture the coordinate when the mouse button has been released,
format it, and copy it to the clipboard. pt is QgsPointXY'''
pt = self.snappoint(event.originalPixelPoint())
self.removeVertexMarker()

try:
canvasCRS = self.canvas.mapSettings().destinationCrs()
transform = QgsCoordinateTransform(canvasCRS, epsg4326, QgsProject.instance())
pt4326 = transform.transform(pt.x(), pt.y())
self.capturePoint.emit(pt4326)
except Exception as e:
pass

def removeVertexMarker(self):
if self.vertex is not None:
self.canvas.scene().removeItem(self.vertex)
self.vertex = None
23 changes: 17 additions & 6 deletions coordinateConverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .util import epsg4326, parseDMSString, formatDmsString
# import traceback

from .captureCoordinate import CaptureCoordinate
from .settings import settings
from . import mgrs
from . import olc
Expand All @@ -30,11 +31,14 @@ def __init__(self, lltools, settingsDialog, iface, parent):
self.canvas = iface.mapCanvas()
self.lltools = lltools
self.settings = settingsDialog
self.savedMapTool = None

self.clipboard = QApplication.clipboard()

# Set up a connection with the coordinate capture tool
self.lltools.mapTool.capturesig.connect(self.capturedPoint)
self.captureCoordinate = CaptureCoordinate(self.canvas)
self.captureCoordinate.capturePoint.connect(self.capturedPoint)
self.captureCoordinate.captureStopped.connect(self.stopCapture)

self.xymenu = QMenu()
icon = QIcon(os.path.dirname(__file__) + '/images/yx.png')
Expand Down Expand Up @@ -121,6 +125,11 @@ def showEvent(self, e):
self.xyButton.setDefaultAction(self.xymenu.actions()[settings.converterCoordOrder])
self.updateLabel()

def closeEvent(self, e):
if self.savedMapTool:
self.canvas.setMapTool(self.savedMapTool)
self.savedMapTool = None

def xyTriggered(self, action):
self.xyButton.setDefaultAction(action)
self.inputXYOrder = action.data()
Expand Down Expand Up @@ -465,13 +474,15 @@ def capturedPoint(self, pt):

def startCapture(self):
if self.coordCaptureButton.isChecked():
self.lltools.mapTool.capture4326 = True
self.lltools.startCapture()
self.savedMapTool = self.canvas.mapTool()
self.canvas.setMapTool(self.captureCoordinate)
else:
self.lltools.mapTool.capture4326 = False

if self.savedMapTool:
self.canvas.setMapTool(self.savedMapTool)
self.savedMapTool = None

@pyqtSlot()
def stopCapture(self):
self.lltools.mapTool.capture4326 = False
self.coordCaptureButton.setChecked(False)

def showSettings(self):
Expand Down
10 changes: 2 additions & 8 deletions copyLatLonTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ class CopyLatLonTool(QgsMapToolEmitPoint):
'''Class to interact with the map canvas to capture the coordinate
when the mouse button is pressed and to display the coordinate in
in the status bar.'''
capturesig = pyqtSignal(QgsPointXY)
captureStopped = pyqtSignal()

def __init__(self, settings, iface):
QgsMapToolEmitPoint.__init__(self, iface.mapCanvas())
self.iface = iface
self.canvas = iface.mapCanvas()
self.settings = settings
self.capture4326 = False
self.marker = None
self.vertex = None

Expand All @@ -35,6 +34,7 @@ def activate(self):
def deactivate(self):
self.removeMarker()
self.removeVertexMarker()
self.captureStopped.emit()

def formatCoord(self, pt, delimiter):
'''Format the coordinate string according to the settings from
Expand Down Expand Up @@ -226,12 +226,6 @@ def canvasReleaseEvent(self, event):
self.removeMarker()

try:
if self.capture4326:
canvasCRS = self.canvas.mapSettings().destinationCrs()
transform = QgsCoordinateTransform(canvasCRS, epsg4326, QgsProject.instance())
pt4326 = transform.transform(pt.x(), pt.y())
self.capturesig.emit(pt4326)
return
msg = self.formatCoord(pt, self.settings.delimiter)
formatString = self.coordFormatString()
if msg is not None:
Expand Down
20 changes: 10 additions & 10 deletions latLonTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,23 @@ def initGui(self):
self.iface.addPluginToMenu('Lat Lon Tools', self.helpAction)

self.iface.currentLayerChanged.connect(self.currentLayerChanged)
self.canvas.mapToolSet.connect(self.unsetTool)
self.canvas.mapToolSet.connect(self.resetTools)
self.enableDigitizeTool()

# Add the processing provider
QgsApplication.processingRegistry().addProvider(self.provider)

def unsetTool(self, tool):
def resetTools(self, newtool, oldtool):
'''Uncheck the Copy Lat Lon tool'''
try:
if not isinstance(tool, CopyLatLonTool):
if oldtool is self.mapTool:
self.copyAction.setChecked(False)
self.multiZoomDialog.stopCapture()
self.mapTool.capture4326 = False
if not isinstance(tool, ShowOnMapTool):
if oldtool is self.showMapTool:
self.externMapAction.setChecked(False)
if newtool is self.mapTool:
self.copyAction.setChecked(True)
if newtool is self.showMapTool:
self.externMapAction.setChecked(True)
except Exception:
pass

Expand Down Expand Up @@ -197,8 +199,7 @@ def unload(self):
QgsApplication.processingRegistry().removeProvider(self.provider)

def startCapture(self):
'''Set the focus of the copy coordinate tool and check it'''
self.copyAction.setChecked(True)
'''Set the focus of the copy coordinate tool'''
self.canvas.setMapTool(self.mapTool)

def copyCanvas(self):
Expand Down Expand Up @@ -244,8 +245,7 @@ def copyCanvas(self):
self.iface.messageBar().pushMessage("", "'{}' copied to the clipboard".format(outStr), level=Qgis.Info, duration=4)

def setShowMapTool(self):
'''Set the focus of the external map tool and check it'''
self.externMapAction.setChecked(True)
'''Set the focus of the external map tool.'''
self.canvas.setMapTool(self.showMapTool)

def showZoomToDialog(self):
Expand Down
5 changes: 3 additions & 2 deletions metadata.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[general]
name=Lat Lon Tools
qgisMinimumVersion=3.0
qgisMinimumVersion=3.2
description=Tools to capture and zoom to coordinates using decimal, DMS, WKT, GeoJSON, MGRS, UTM, Geohash, and Plus Codes formats. Provides external map support, point digitizing tools, and coordinate conversion tools.
version=3.3.12
version=3.3.13
author=C Hamilton
[email protected]
about=
Expand All @@ -27,6 +27,7 @@ icon=images/copyicon.png
experimental=False
deprecated=False
changelog=
3.3.13 - Fixed tool interactions
3.3.12 - Made coordinate conversion dialog box dockable & added zoom to button.
3.3.11 - Fix bug
3.3.10 - Added 'degree minute' capture and to coordinate conversion dialog box.
Expand Down
20 changes: 14 additions & 6 deletions multizoom.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
QgsField, QgsFeature, QgsGeometry, QgsPointXY,
QgsPalLayerSettings, QgsVectorLayerSimpleLabeling, QgsProject, Qgis)
from qgis.gui import QgsVertexMarker
from .captureCoordinate import CaptureCoordinate
from .util import epsg4326, parseDMSStringSingle, parseDMSString
from .utm import utmString2Crs
from . import mgrs
Expand All @@ -34,9 +35,12 @@ def __init__(self, lltools, settings, parent):
self.iface = lltools.iface
self.canvas = self.iface.mapCanvas()
self.lltools = lltools
self.savedMapTool = None

# Set up a connection with the coordinate capture tool
self.lltools.mapTool.capturesig.connect(self.capturedPoint)
self.captureCoordinate = CaptureCoordinate(self.canvas)
self.captureCoordinate.capturePoint.connect(self.capturedPoint)
self.captureCoordinate.captureStopped.connect(self.stopCapture)

self.addButton.setIcon(QIcon(os.path.dirname(__file__) + "/images/check.png"))
self.coordCaptureButton.setIcon(QIcon(os.path.dirname(__file__) + "/images/coordinate_capture.png"))
Expand Down Expand Up @@ -118,9 +122,11 @@ def settingsChanged(self):
def closeEvent(self, e):
'''Called when the dialog box is being closed. We want to clear selected features and remove
all the markers.'''
if self.savedMapTool:
self.canvas.setMapTool(self.savedMapTool)
self.savedMapTool = None
self.resultsTable.clearSelection()
self.removeMarkers()
self.stopCapture()
self.hide()

def showEvent(self, e):
Expand All @@ -145,13 +151,15 @@ def capturedPoint(self, pt):

def startCapture(self):
if self.coordCaptureButton.isChecked():
self.lltools.mapTool.capture4326 = True
self.lltools.startCapture()
self.savedMapTool = self.canvas.mapTool()
self.canvas.setMapTool(self.captureCoordinate)
else:
self.lltools.mapTool.capture4326 = False
if self.savedMapTool:
self.canvas.setMapTool(self.savedMapTool)
self.savedMapTool = None

@pyqtSlot()
def stopCapture(self):
self.lltools.mapTool.capture4326 = False
self.coordCaptureButton.setChecked(False)

def clearAll(self):
Expand Down

0 comments on commit 0eb6a1a

Please sign in to comment.