Skip to content

Commit

Permalink
feat: exit digitization on right click and ESC button (#286)
Browse files Browse the repository at this point in the history
Co-authored-by: Jakob Schnell <Jakob.Schnell@heigit.org>
  • Loading branch information
merydian and koebi authored Nov 21, 2024
1 parent 8497c67 commit 42cad6c
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 24 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ RELEASING:

## Unreleased

### Added
- Exit digitization on right click or Escape key press ([#285](https://github.com/GIScience/orstools-qgis-plugin/issues/285))

## [1.9.0] - 2024-10-29

### Fixed
Expand Down
9 changes: 5 additions & 4 deletions ORStools/gui/ORStoolsDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ def _on_linetool_init(self) -> None:
self.line_tool.pointDrawn.connect(
lambda point, idx: self._on_linetool_map_click(point, idx)
)
self.line_tool.doubleClicked.connect(self._on_linetool_map_doubleclick)
self.line_tool.digitizationEnded.connect(self._on_linetool_digitization_ended)

def _on_linetool_map_click(self, point: QgsPointXY, idx: int) -> None:
"""Adds an item to QgsListWidget and annotates the point in the map canvas"""
Expand Down Expand Up @@ -666,16 +666,17 @@ def _reindex_list_items(self) -> None:
annotation = self._linetool_annotate_point(point, idx, crs)
self.project.annotationManager().addAnnotation(annotation)

def _on_linetool_map_doubleclick(self) -> None:
def _on_linetool_digitization_ended(self) -> None:
"""
Populate line list widget with coordinates, end line drawing and show dialog again.
"""

self.line_tool.pointDrawn.disconnect()
self.line_tool.doubleClicked.disconnect()
self.line_tool.digitizationEnded.disconnect()
self.line_tool = None

QApplication.restoreOverrideCursor()
self._iface.mapCanvas().setMapTool(self.last_maptool)
self.line_tool = None
self.show()

def color_duplicate_items(self, list_widget):
Expand Down
2 changes: 1 addition & 1 deletion ORStools/gui/ORStoolsDialogUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ def retranslateUi(self, ORStoolsDialogBase):
self.routing_travel_label.setText(_translate("ORStoolsDialogBase", "Go with"))
self.routing_travel_combo.setToolTip(_translate("ORStoolsDialogBase", "Mode of travel"))
self.routing_preference_combo.setToolTip(_translate("ORStoolsDialogBase", "Preference"))
self.routing_fromline_map.setToolTip(_translate("ORStoolsDialogBase", "<html><head/><body><p>Add wayoints interactively from the map canvas.</p><p>Double-click will terminate waypoint selection.</p></body></html>"))
self.routing_fromline_map.setToolTip(_translate("ORStoolsDialogBase", "<html><head/><body><p>Add wayoints interactively from the map canvas.</p><p>The Escape key, right- or double-click will terminate waypoint selection.</p></body></html>"))
self.routing_fromline_clear.setToolTip(_translate("ORStoolsDialogBase", "<html><head/><body><p>If waypoints are selected in the list, only these will be deleted. Else all waypoints will be deleted.</p></body></html>"))
self.routing_fromline_list.setToolTip(_translate("ORStoolsDialogBase", "Select waypoints from the map!"))
self.save_vertices.setToolTip(_translate("ORStoolsDialogBase", "<html><head/><body><p>Save points in list to layer. Use the processing algorithms (batch jobs) to work with points from layers.</p></body></html>"))
Expand Down
2 changes: 1 addition & 1 deletion ORStools/gui/ORStoolsDialogUI.ui
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@
</sizepolicy>
</property>
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Add wayoints interactively from the map canvas.&lt;/p&gt;&lt;p&gt;Double-click will terminate waypoint selection.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Add wayoints interactively from the map canvas.&lt;/p&gt;&lt;p&gt;The Escape key, right- or double-click will terminate waypoint selection.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string/>
Expand Down
8 changes: 4 additions & 4 deletions ORStools/i18n/orstools_de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,8 @@ Duplikate entfernen oder Wegpunktoptimierung abwählen.</translation>
</message>
<message>
<location filename="../gui/ORStoolsDialogUI.ui" line="276"/>
<source>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Add wayoints interactively from the map canvas.&lt;/p&gt;&lt;p&gt;Double-click will terminate waypoint selection.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</source>
<translation>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Wegpunkte aus Kartenansicht hinzufügen&lt;/p&gt;&lt;p&gt;Doppelklick beendet die Wegpunkt-Auswahl&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</translation>
<source>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Add wayoints interactively from the map canvas.&lt;/p&gt;&lt;p&gt;The Escape key, right- or double-click will terminate waypoint selection.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</source>
<translation>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Wegpunkte aus Kartenansicht hinzufügen&lt;/p&gt;&lt;p&gt;Escape, Rechts- oder Doppelklick beendeen die Wegpunkt-Auswahl.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</translation>
</message>
<message>
<location filename="../gui/ORStoolsDialogUI.ui" line="296"/>
Expand Down Expand Up @@ -466,7 +466,7 @@ p, li { white-space: pre-wrap; }
<translation>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Ubuntu'; font-size:11pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:&apos;Ubuntu&apos;; font-size:11pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; padding: 10px; -qt-block-indent:0; text-indent:0px ; background-color:#e7f2fa; color: #999999&quot;&gt;&lt;img stype=&quot;margin: 10px&quot; src=&quot;:/plugins/ORStools/img/icon_about.png&quot; width=16 height=16 /&gt; Sämtliche Einstellungen werden überschrieben&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</translation>
</message>
<message>
Expand Down Expand Up @@ -776,7 +776,7 @@ Duplikate entfernen oder Wegpunktoptimierung abwählen.</translation>
<source>
Did you forget to set an &lt;b&gt;API key&lt;/b&gt; for openrouteservice?&lt;br&gt;&lt;br&gt;

If you don't have an API key, please visit https://openrouteservice.org/sign-up to get one. &lt;br&gt;&lt;br&gt;
If you don&apos;t have an API key, please visit https://openrouteservice.org/sign-up to get one. &lt;br&gt;&lt;br&gt;
Then enter the API key for openrouteservice provider in Web ► ORS Tools ► Provider Settings or the
settings symbol in the main ORS Tools GUI, next to the provider dropdown.</source>
<translation>Haben Sie einen &lt;b&gt;API-Key&lt;/b&gt; für den openrouteservice gesetzt?&lt;br&gt;&lt;br&gt;
Expand Down
31 changes: 21 additions & 10 deletions ORStools/utils/maptools.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from qgis.core import QgsWkbTypes
from qgis.gui import QgsMapToolEmitPoint, QgsRubberBand

from qgis.PyQt.QtCore import pyqtSignal
from qgis.PyQt.QtCore import pyqtSignal, Qt
from qgis.PyQt.QtGui import QColor

from ORStools import DEFAULT_COLOR
Expand Down Expand Up @@ -66,14 +66,22 @@ def reset(self):

pointDrawn = pyqtSignal(["QgsPointXY", "int"])

def keyPressEvent(self, event) -> None:
# Check if the pressed key is the Escape key
if event.key() == Qt.Key_Escape:
self.end_digitization()

def canvasReleaseEvent(self, e):
"""Add marker to canvas and shows line."""
new_point = self.toMapCoordinates(e.pos())
self.points.append(new_point)
if e.button() == Qt.RightButton:
self.end_digitization()
else:
new_point = self.toMapCoordinates(e.pos())
self.points.append(new_point)

# noinspection PyUnresolvedReferences
self.pointDrawn.emit(new_point, self.points.index(new_point))
self.showLine()
# noinspection PyUnresolvedReferences
self.pointDrawn.emit(new_point, self.points.index(new_point))
self.showLine()

def showLine(self):
"""Builds rubber band from all points and adds it to the map canvas."""
Expand All @@ -84,16 +92,19 @@ def showLine(self):
self.rubberBand.addPoint(point, False)
self.rubberBand.show()

doubleClicked = pyqtSignal()
digitizationEnded = pyqtSignal()

# noinspection PyUnusedLocal
def canvasDoubleClickEvent(self, e):
"""Ends line drawing and deletes rubber band and markers from map canvas."""
# noinspection PyUnresolvedReferences
self.doubleClicked.emit()
self.canvas.scene().removeItem(self.rubberBand)
del self.rubberBand
self.end_digitization()

def deactivate(self):
super(LineTool, self).deactivate()
self.deactivated.emit()

def end_digitization(self):
self.digitizationEnded.emit()
self.canvas.scene().removeItem(self.rubberBand)
del self.rubberBand
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
s = QgsSettings()
data = s.value("ORStools/config")


def pytest_sessionstart(session):
"""
Called after the Session object has been created and
Expand Down
4 changes: 1 addition & 3 deletions tests/test_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,4 @@ def test_ORStoolsDialog(self):
dlg.line_tool.canvasDoubleClickEvent(map_dclick)

self.assertTrue(dlg.isVisible())
self.assertEqual(
dlg.routing_fromline_list.item(0).text(), "Point 0: -0.187575, 56.516620"
)
self.assertEqual(dlg.routing_fromline_list.item(0).text(), "Point 0: -0.187575, 56.516620")
4 changes: 3 additions & 1 deletion tests/test_proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ def test_export(self):
export = ORSExportAlgo().create()
dest_id = export.processAlgorithm(parameters, self.context, self.feedback)
processed_layer = QgsProcessingUtils.mapLayerFromString(dest_id["OUTPUT"], self.context)
processed_nodes = QgsProcessingUtils.mapLayerFromString(dest_id["OUTPUT_POINT"], self.context)
processed_nodes = QgsProcessingUtils.mapLayerFromString(
dest_id["OUTPUT_POINT"], self.context
)

self.assertEqual(type(processed_layer), QgsVectorLayer)
self.assertEqual(type(processed_nodes), QgsVectorLayer)

0 comments on commit 42cad6c

Please sign in to comment.