Skip to content

Commit 3a39916

Browse files
committed
style: some refactoring
1 parent 972dabb commit 3a39916

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

ORStools/gui/ORStoolsDialog.py

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,8 @@ def __init__(self, iface: QgisInterface, parent=None) -> None:
441441

442442
# Set things around the custom map tool
443443
self.line_tool = None
444-
self.last_maptool = self._iface.mapCanvas().mapTool()
444+
self.canvas = self._iface.mapCanvas()
445+
self.last_maptool = self.canvas.mapTool()
445446
self.annotations = []
446447
self.rubber_band = None
447448

@@ -494,8 +495,6 @@ def __init__(self, iface: QgisInterface, parent=None) -> None:
494495
self.routing_fromline_list.model().rowsMoved.connect(self._reindex_list_items)
495496
self.routing_fromline_list.model().rowsRemoved.connect(self._reindex_list_items)
496497

497-
self.annotation_canvas = self._iface.mapCanvas()
498-
499498
self.moving = None
500499
self.moved_idxs = 0
501500
self.error_idxs = 0
@@ -522,7 +521,7 @@ def _save_vertices_to_layer(self) -> None:
522521

523522
point_layer.dataProvider().addFeature(feature)
524523
QgsProject.instance().addMapLayer(point_layer)
525-
self._iface.mapCanvas().refresh()
524+
self.canvas.refresh()
526525

527526
self._iface.messageBar().pushMessage(
528527
"Success", "Vertices saved to layer.", level=Qgis.MessageLevel.Success
@@ -555,7 +554,7 @@ def _linetool_annotate_point(
555554
self, point: QgsPointXY, idx: int, crs: Optional[QgsCoordinateReferenceSystem] = None
556555
) -> QgsAnnotation:
557556
if not crs:
558-
crs = self._iface.mapCanvas().mapSettings().destinationCrs()
557+
crs = self.canvas.mapSettings().destinationCrs()
559558

560559
annotation = QgsTextAnnotation()
561560

@@ -570,11 +569,11 @@ def _linetool_annotate_point(
570569
annotation.setMapPosition(point)
571570
annotation.setMapPositionCrs(crs)
572571

573-
return QgsMapCanvasAnnotationItem(annotation, self.annotation_canvas).annotation()
572+
return QgsMapCanvasAnnotationItem(annotation, self.canvas).annotation()
574573

575574
def _clear_annotations(self) -> None:
576575
"""Clears annotations"""
577-
for annotation_item in self.annotation_canvas.annotationItems():
576+
for annotation_item in self.canvas.annotationItems():
578577
annotation = annotation_item.annotation()
579578
if annotation in self.project.annotationManager().annotations():
580579
self.project.annotationManager().removeAnnotation(annotation)
@@ -587,8 +586,8 @@ def _on_linetool_init(self) -> None:
587586
self.hide()
588587
self._clear_annotations()
589588
self.routing_fromline_list.clear()
590-
self.line_tool = maptools.LineTool(self._iface.mapCanvas())
591-
self._iface.mapCanvas().setMapTool(self.line_tool)
589+
self.line_tool = maptools.LineTool(self.canvas)
590+
self.canvas.setMapTool(self.line_tool)
592591
self.line_tool.pointPressed.connect(lambda point: self._on_movetool_map_press(point))
593592
self.line_tool.pointReleased.connect(
594593
lambda point, idx: self._on_movetool_map_release(point, idx)
@@ -597,8 +596,8 @@ def _on_linetool_init(self) -> None:
597596
self.line_tool.mouseMoved.connect(lambda pos: self.change_cursor_on_hover(pos))
598597

599598
def change_cursor_on_hover(self, pos):
600-
idx = self.check_annotation_hover(pos)
601-
if idx:
599+
hovering = self.check_annotation_hover(pos)
600+
if hovering:
602601
QApplication.setOverrideCursor(Qt.OpenHandCursor)
603602
else:
604603
if not self.moving:
@@ -609,13 +608,12 @@ def check_annotation_hover(self, pos):
609608
dists = {}
610609
for i, anno in enumerate(self.annotations):
611610
x, y = anno.mapPosition()
612-
mapcanvas = self._iface.mapCanvas()
613-
point = mapcanvas.getCoordinateTransform().transform(x, y) # die ist es
611+
point = self.canvas.getCoordinateTransform().transform(x, y) # die ist es
614612
p = [point.x(), point.y()]
615613

616614
distance = 0.0
617-
for i in range(len(click)):
618-
distance += (click[i] - p[i]) ** 2
615+
for j in range(len(click)):
616+
distance += (click[j] - p[j]) ** 2
619617
distance = math.sqrt(distance)
620618

621619
if distance > 0:
@@ -625,13 +623,13 @@ def check_annotation_hover(self, pos):
625623
return idx
626624

627625
def _on_movetool_map_press(self, pos):
628-
idx = self.check_annotation_hover(pos)
629-
if idx:
626+
hovering = self.check_annotation_hover(pos)
627+
if hovering:
630628
self.line_tool.mouseMoved.disconnect()
631629
QApplication.setOverrideCursor(Qt.ClosedHandCursor)
632630
if self.rubber_band:
633631
self.rubber_band.reset()
634-
self.move_i = self.annotations.index(idx)
632+
self.move_i = self.annotations.index(hovering)
635633
self.project.annotationManager().removeAnnotation(self.annotations.pop(self.move_i))
636634
self.moving = True
637635

@@ -640,7 +638,7 @@ def _on_movetool_map_release(self, point, idx):
640638
try:
641639
self.moving = False
642640
QApplication.restoreOverrideCursor()
643-
crs = self._iface.mapCanvas().mapSettings().destinationCrs()
641+
crs = self.canvas.mapSettings().destinationCrs()
644642

645643
annotation = self._linetool_annotate_point(point, self.move_i, crs=crs)
646644
self.annotations.insert(self.move_i, annotation)
@@ -713,9 +711,7 @@ def _on_movetool_map_release(self, point, idx):
713711

714712
if num < 2:
715713
self.routing_fromline_list.clear()
716-
for annotation in self.annotations:
717-
self.project.annotationManager().removeAnnotation(annotation)
718-
self.annotations = []
714+
self._clear_annotations()
719715
else:
720716
self.routing_fromline_list.takeItem(num)
721717
self.create_rubber_band()
@@ -730,7 +726,7 @@ def _on_movetool_map_release(self, point, idx):
730726
def create_rubber_band(self):
731727
if self.rubber_band:
732728
self.rubber_band.reset()
733-
self.rubber_band = QgsRubberBand(self._iface.mapCanvas(), QgsWkbTypes.LineGeometry)
729+
self.rubber_band = QgsRubberBand(self.canvas, QgsWkbTypes.LineGeometry)
734730
color = QColor(ROUTE_COLOR)
735731
color.setAlpha(100)
736732
self.rubber_band.setStrokeColor(color)
@@ -741,11 +737,12 @@ def create_rubber_band(self):
741737
feature = next(route_layer.getFeatures())
742738
self.rubber_band.addGeometry(feature.geometry(), route_layer)
743739
self.rubber_band.show()
740+
print("debug1")
744741
else:
745742
self._clear_annotations()
746743
self._on_clear_listwidget_click()
747744
else:
748-
dest_crs = self._iface.mapCanvas().mapSettings().destinationCrs()
745+
dest_crs = self.canvas.mapSettings().destinationCrs()
749746
original_crs = QgsCoordinateReferenceSystem("EPSG:4326")
750747
transform = QgsCoordinateTransform(original_crs, dest_crs, QgsProject.instance())
751748
items = [
@@ -764,20 +761,20 @@ def create_rubber_band(self):
764761

765762
def create_vertex(self, point, idx):
766763
"""Adds an item to QgsListWidget and annotates the point in the map canvas"""
767-
map_crs = self._iface.mapCanvas().mapSettings().destinationCrs()
764+
map_crs = self.canvas.mapSettings().destinationCrs()
768765

769766
transformer = transform.transformToWGS(map_crs)
770767
point_wgs = transformer.transform(point)
771768
self.routing_fromline_list.addItem(f"Point {idx}: {point_wgs.x():.6f}, {point_wgs.y():.6f}")
772769

773-
crs = self._iface.mapCanvas().mapSettings().destinationCrs()
770+
crs = self.canvas.mapSettings().destinationCrs()
774771
annotation = self._linetool_annotate_point(point, idx, crs)
775772
self.annotations.append(annotation)
776773
self.project.annotationManager().addAnnotation(annotation)
777774

778775
def _on_linetool_map_click(self, point: QgsPointXY, idx: int) -> None:
779776
"""Adds an item to QgsListWidget and annotates the point in the map canvas"""
780-
map_crs = self._iface.mapCanvas().mapSettings().destinationCrs()
777+
map_crs = self.canvas.mapSettings().destinationCrs()
781778

782779
transformer = transform.transformToWGS(map_crs)
783780
point_wgs = transformer.transform(point)
@@ -819,5 +816,5 @@ def _on_line_tool_map_doubleclick(self):
819816
self.line_tool.doubleClicked.disconnect()
820817
self.line_tool.pointReleased.disconnect()
821818
QApplication.restoreOverrideCursor()
822-
self._iface.mapCanvas().setMapTool(self.last_maptool)
819+
self.canvas.setMapTool(self.last_maptool)
823820
self.show()

0 commit comments

Comments
 (0)