Skip to content

Commit

Permalink
feat: promt user for duplicates and colorize them
Browse files Browse the repository at this point in the history
  • Loading branch information
merydian committed Aug 26, 2024
1 parent c5774dd commit 7498f36
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion ORStools/gui/ORStoolsDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
from qgis.gui import QgsMapCanvasAnnotationItem

from qgis.PyQt.QtCore import QSizeF, QPointF, QCoreApplication
from qgis.PyQt.QtGui import QIcon, QTextDocument
from qgis.PyQt.QtGui import QIcon, QTextDocument, QColor
from qgis.PyQt.QtWidgets import (
QAction,
QDialog,
Expand Down Expand Up @@ -320,6 +320,22 @@ def run_gui_control(self) -> None:
try:
params = directions.get_parameters()
if self.dlg.optimization_group.isChecked():
# check for duplicate points
points = [
self.dlg.routing_fromline_list.item(x).text()
for x in range(self.dlg.routing_fromline_list.count())
]
if len(points) != len(set(points)):
QMessageBox.warning(
self.dlg,
"Duplicates",
"""There are duplicate points in the input layer, which need to be removed.""",
)
msg = "The request has been aborted!"
logger.log(msg, 0)
self.dlg.debug_text.setText(msg)
return

if len(params["jobs"]) <= 1: # Start/end locations don't count as job
QMessageBox.critical(
self.dlg,
Expand Down Expand Up @@ -490,6 +506,14 @@ def __init__(self, iface: QgisInterface, parent=None) -> None:
self.routing_fromline_list.model().rowsMoved.connect(self._reindex_list_items)
self.routing_fromline_list.model().rowsRemoved.connect(self._reindex_list_items)

# Connect signals to the color_duplicate_items function
self.routing_fromline_list.model().rowsRemoved.connect(
lambda: self.color_duplicate_items(self.routing_fromline_list)
)
self.routing_fromline_list.model().rowsInserted.connect(
lambda: self.color_duplicate_items(self.routing_fromline_list)
)

self.annotation_canvas = self._iface.mapCanvas()

def _save_vertices_to_layer(self) -> None:
Expand Down Expand Up @@ -633,3 +657,19 @@ def _on_linetool_map_doubleclick(self) -> None:
QApplication.restoreOverrideCursor()
self._iface.mapCanvas().setMapTool(self.last_maptool)
self.show()

def color_duplicate_items(self, list_widget):
item_dict = {}
for index in range(list_widget.count()):
item = list_widget.item(index)
text = item.text()
if text in item_dict:
item_dict[text].append(index)
else:
item_dict[text] = [index]

for indices in item_dict.values():
if len(indices) > 1:
for index in indices:
item = list_widget.item(index)
item.setBackground(QColor("lightsalmon"))

0 comments on commit 7498f36

Please sign in to comment.