Skip to content

Commit

Permalink
Enhancement: highlight state 0 features in psector
Browse files Browse the repository at this point in the history
(cherry picked from commit c77b435)
  • Loading branch information
smaspons committed Nov 7, 2023
1 parent ef2b7d4 commit 4e88d0e
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 11 deletions.
116 changes: 106 additions & 10 deletions core/shared/psector.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from .document import GwDocument, global_vars
from ..shared.psector_duplicate import GwPsectorDuplicate
from ..ui.ui_manager import GwPsectorUi, GwPsectorRapportUi, GwPsectorManagerUi, GwPriceManagerUi, GwReplaceArc
from ..ui.ui_manager import GwPsectorUi, GwPsectorRapportUi, GwPsectorManagerUi, GwPriceManagerUi, GwReplaceArc, GwDialogTextUi
from ..utils import tools_gw
from ...lib import tools_db, tools_qgis, tools_qt, tools_log, tools_os
from ..utils.snap_manager import GwSnapManager
Expand All @@ -42,10 +42,12 @@ def __init__(self):
self.rubber_band_point = tools_gw.create_rubberband(self.canvas)
self.rubber_band_line = tools_gw.create_rubberband(self.canvas)
self.rubber_band_rectangle = tools_gw.create_rubberband(self.canvas)
self.rubber_band_op = tools_gw.create_rubberband(self.canvas)
self.emit_point = None
self.vertex_marker = None
self.dict_to_update = {}
self.my_json = {}
self.feature_geoms = {}
self.tablename_psector_x_arc = "plan_psector_x_arc"
self.tablename_psector_x_node = "plan_psector_x_node"
self.tablename_psector_x_connec = "v_edit_plan_psector_x_connec"
Expand Down Expand Up @@ -220,34 +222,43 @@ def get_psector(self, psector_id=None, list_coord=None):

# tbl_psector_x_arc
self.fill_table(self.dlg_plan_psector, self.qtbl_arc, self.tablename_psector_x_arc,
set_edit_triggers=QTableView.DoubleClicked, expr=expr)
set_edit_triggers=QTableView.DoubleClicked, expr=expr, feature_type="arc", field_id="arc_id")
tools_gw.set_tablemodel_config(self.dlg_plan_psector, self.qtbl_arc, self.tablename_psector_x_arc)
self.qtbl_arc.setProperty('tablename', self.tablename_psector_x_arc)

self.qtbl_arc.selectionModel().selectionChanged.connect(partial(
tools_qgis.highlight_features_by_id, self.qtbl_arc, "v_edit_arc", "arc_id", self.rubber_band_point, 5
tools_qgis.highlight_features_by_id, self.qtbl_arc, "v_edit_arc", "arc_id", self.rubber_band_line, 5
))
self.qtbl_arc.selectionModel().selectionChanged.connect(partial(
self._highlight_features_by_id, self.qtbl_arc, "arc", "arc_id", self.rubber_band_op, 5
))

# tbl_psector_x_node
self.fill_table(self.dlg_plan_psector, self.qtbl_node, self.tablename_psector_x_node,
set_edit_triggers=QTableView.DoubleClicked, expr=expr)
set_edit_triggers=QTableView.DoubleClicked, expr=expr, feature_type="node", field_id="node_id")
tools_gw.set_tablemodel_config(self.dlg_plan_psector, self.qtbl_node, self.tablename_psector_x_node)
self.qtbl_node.setProperty('tablename', self.tablename_psector_x_node)

self.qtbl_node.selectionModel().selectionChanged.connect(partial(
tools_qgis.highlight_features_by_id, self.qtbl_node, "v_edit_node", "node_id", self.rubber_band_point, 10
))
self.qtbl_node.selectionModel().selectionChanged.connect(partial(
self._highlight_features_by_id, self.qtbl_node, "node", "node_id", self.rubber_band_op, 5
))

# tbl_psector_x_connec
self.fill_table(self.dlg_plan_psector, self.qtbl_connec, self.tablename_psector_x_connec,
set_edit_triggers=QTableView.DoubleClicked, expr=expr)
set_edit_triggers=QTableView.DoubleClicked, expr=expr, feature_type="connec", field_id="connec_id")
tools_gw.set_tablemodel_config(self.dlg_plan_psector, self.qtbl_connec, self.tablename_psector_x_connec)
self.qtbl_connec.setProperty('tablename', self.tablename_psector_x_connec)

self.qtbl_connec.selectionModel().selectionChanged.connect(partial(
tools_qgis.highlight_features_by_id, self.qtbl_connec, "v_edit_connec", "connec_id", self.rubber_band_point,
10
))
self.qtbl_connec.selectionModel().selectionChanged.connect(partial(
self._highlight_features_by_id, self.qtbl_connec, "connec", "connec_id", self.rubber_band_op, 5
))
self.qtbl_connec.selectionModel().selectionChanged.connect(partial(
self._manage_tab_feature_buttons
))
Expand All @@ -256,14 +267,17 @@ def get_psector(self, psector_id=None, list_coord=None):
# tbl_psector_x_gully
if self.project_type.upper() == 'UD':
self.fill_table(self.dlg_plan_psector, self.qtbl_gully, self.tablename_psector_x_gully,
set_edit_triggers=QTableView.DoubleClicked, expr=expr)
set_edit_triggers=QTableView.DoubleClicked, expr=expr, feature_type="gully", field_id="gully_id")
tools_gw.set_tablemodel_config(self.dlg_plan_psector, self.qtbl_gully, self.tablename_psector_x_gully)
self.qtbl_gully.setProperty('tablename', self.tablename_psector_x_gully)

self.qtbl_gully.selectionModel().selectionChanged.connect(partial(
tools_qgis.highlight_features_by_id, self.qtbl_gully, "v_edit_gully", "gully_id",
self.rubber_band_point, 10
))
self.qtbl_gully.selectionModel().selectionChanged.connect(partial(
self._highlight_features_by_id, self.qtbl_gully, "gully", "gully_id", self.rubber_band_op, 5
))
self.qtbl_gully.selectionModel().selectionChanged.connect(partial(
self._manage_tab_feature_buttons
))
Expand Down Expand Up @@ -991,6 +1005,7 @@ def close_psector(self, cur_active_layer=None):
try:
tools_gw.reset_rubberband(self.rubber_band_point)
tools_gw.reset_rubberband(self.rubber_band_line)
tools_gw.reset_rubberband(self.rubber_band_op)
self._clear_my_json()
self.reload_states_selector()
if cur_active_layer:
Expand Down Expand Up @@ -1144,14 +1159,41 @@ def insert_or_update_new_psector(self, tablename, close_dlg=False):

def set_plan(self):

# TODO: Check this
extras = f'"psectorId":"{tools_qt.get_text(self.dlg_plan_psector, self.psector_id)}"'
body = tools_gw.create_body(extras=extras)
json_result = tools_gw.execute_procedure('gw_fct_setplan', body)
json_result = tools_gw.execute_procedure('gw_fct_setplan', body, is_thread=True) # is_thread=True because we don't want to call manage_json_response yet
tools_gw.manage_current_selections_docker(json_result)
if not json_result or 'body' not in json_result or 'data' not in json_result['body']:
return

try:
if json_result['body']['data']['info']['values']:
text = "There are some topological inconsistences on this psector. Would you like to see the log?"
function = partial(self.show_psector_topoerror_log, json_result)
tools_qgis.show_message_function(text, function, message_level=1, duration=0)
except KeyError:
pass

return json_result


def show_psector_topoerror_log(self, json_result):

# Remove message
self.iface.messageBar().popWidget()

# Create log dialog
self.dlg_infolog = GwDialogTextUi()
tools_qt.set_widget_visible(self.dlg_infolog, 'btn_accept', False)
self.dlg_infolog.btn_close.clicked.connect(partial(tools_gw.close_dialog, self.dlg_infolog, True))

text, _ = tools_gw.fill_tab_log(self.dlg_infolog, json_result['body']['data'], close=False)
tools_gw.open_dialog(self.dlg_infolog)

# Draw log features on map
tools_gw.manage_json_response(json_result)


def price_selector(self, dialog, tableleft, tableright):


Expand Down Expand Up @@ -1403,7 +1445,8 @@ def fill_table_by_query(self, qtable, query):
tools_qgis.show_warning(model.lastError().text())


def fill_table(self, dialog, widget, table_name, hidde=False, set_edit_triggers=QTableView.NoEditTriggers, expr=None):
def fill_table(self, dialog, widget, table_name, hidde=False, set_edit_triggers=QTableView.NoEditTriggers,
expr=None, feature_type=None, field_id=None):
""" Set a model with selected filter.
Attach that model to selected table
@setEditStrategy:
Expand Down Expand Up @@ -1446,6 +1489,8 @@ def fill_table(self, dialog, widget, table_name, hidde=False, set_edit_triggers=

if hidde:
self.refresh_table(dialog, widget)
if feature_type is not None and field_id is not None:
self._manage_features_geom(widget, feature_type, field_id)


def refresh_table(self, dialog, widget):
Expand Down Expand Up @@ -1759,7 +1804,7 @@ def charge_psector(self, qtbl_psm):

# Open form
self.master_new_psector(psector_id)

# Refresh canvas
tools_qgis.refresh_map_canvas()

Expand Down Expand Up @@ -2314,6 +2359,7 @@ def _open_arc_replace_form(self, point):
self.dlg_replace_arc.btn_cancel.clicked.connect(partial(tools_gw.close_dialog, self.dlg_replace_arc))
self.dlg_replace_arc.rejected.connect(partial(tools_gw.reset_rubberband, self.rubber_band_point))
self.dlg_replace_arc.rejected.connect(partial(tools_gw.reset_rubberband, self.rubber_band_line))
self.dlg_replace_arc.rejected.connect(partial(tools_gw.reset_rubberband, self.rubber_band_op))
self.dlg_replace_arc.rejected.connect(self._reset_snapping)

# Open form
Expand Down Expand Up @@ -2347,6 +2393,7 @@ def _set_plan_replace_feature(self):
self.dlg_replace_arc.close()
tools_gw.reset_rubberband(self.rubber_band_point)
tools_gw.reset_rubberband(self.rubber_band_line)
tools_gw.reset_rubberband(self.rubber_band_op)
self.iface.actionPan().trigger()

# Activate again if variable is True
Expand Down Expand Up @@ -2566,4 +2613,53 @@ def _toggle_feature_psector(self, dialog, idx):
tools_gw.set_model_signals(self)


def _manage_features_geom(self, qtable, feature_type, field_id):
tools_gw.reset_rubberband(self.rubber_band_op)
ids = []
table_model = qtable.model()
for row_index in range(table_model.rowCount()):
column_index = tools_qt.get_col_index_by_col_name(qtable, "state")
if table_model.record(row_index).value(column_index) != 0:
continue
column_index = tools_qt.get_col_index_by_col_name(qtable, field_id)
_id = table_model.record(row_index).value(column_index)
ids.append(_id)

if not ids:
return
ids_str = json.dumps(ids).replace('"', '')
extras = f'"feature_type": "{feature_type}", "ids": "{ids_str}"'
body = tools_gw.create_body(extras=extras)
json_result = tools_gw.execute_procedure('gw_fct_getfeaturegeom', body)
if not json_result or json_result.get("status") != "Accepted":
return

self.feature_geoms[feature_type] = json_result['body']['data']


def _highlight_features_by_id(self, qtable, feature_type, field_id, rubber_band, width, selected, deselected):

tools_gw.reset_rubberband(self.rubber_band_op)
geoms = self.feature_geoms.get(feature_type)
if not geoms:
return

ids = []
for idx, index in enumerate(qtable.selectionModel().selectedRows()):
row = index.row()
column_index = tools_qt.get_col_index_by_col_name(qtable, "state")
if index.sibling(row, column_index).data() != 0:
continue
column_index = tools_qt.get_col_index_by_col_name(qtable, field_id)
_id = index.sibling(row, column_index).data()
ids.append(_id)

if not ids:
return

for _id in ids:
geom = geoms[_id]['st_astext']
color = QColor(0, 90, 255, 125)
tools_qgis.draw_polyline(geom, rubber_band, color, width)

# endregion
47 changes: 46 additions & 1 deletion lib/tools_qgis.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,48 @@ def show_message_link(text, url, btn_text="Open", message_level=0, duration=10,
global_vars.logger.info(text)


def show_message_function(text, function, btn_text="Open", message_level=0, duration=10, context_name=None, logger_file=True,
dialog=iface):
"""
Show message to the user with selected message level and a button to open the url
:param text: The text to be shown (String)
:param function: The function (can be a ``partial()`` object) to execute.
:param btn_text: The text of the button (String)
:param message_level: {INFO = 0(blue), WARNING = 1(yellow), CRITICAL = 2(red), SUCCESS = 3(green)}
:param duration: The duration of the message (int)
:param context_name: Where to look for translating the message
:param logger_file: Whether it should log the message in a file or not (bool)
"""

global user_parameters

# Get optional parameter 'show_message_durations'
dev_duration = user_parameters.get('show_message_durations')
# If is set, use this value
if dev_duration not in (None, "None") and duration > 0:
if message_level in (1, 2) and int(dev_duration) < 10:
duration = 10
else:
duration = int(dev_duration)
msg = None
if text:
msg = tools_qt.tr(text, context_name, user_parameters['aux_context'])

# Create the message with the button
widget = iface.messageBar().createMessage(f"{msg}")
button = QPushButton(widget)
button.setText(f"{btn_text}")
button.pressed.connect(function)
widget.layout().addWidget(button)

# Show the message
dialog.messageBar().pushWidget(widget, message_level, duration)

# Check if logger to file
if global_vars.logger and logger_file:
global_vars.logger.info(text)


def show_info(text, duration=10, context_name=None, parameter=None, logger_file=True, title="", dialog=iface):
"""
Show information message to the user
Expand Down Expand Up @@ -932,7 +974,10 @@ def draw_polyline(points, rubber_band, color=QColor(255, 0, 0, 100), width=5, du
if reset_rb:
rubber_band.reset(1)
rubber_band.setIconSize(20)
polyline = QgsGeometry.fromPolylineXY(points)
if type(points) is str:
polyline = QgsGeometry.fromWkt(points)
else:
polyline = QgsGeometry.fromPolylineXY(points)
if rubber_band.size() == 0:
rubber_band.setToGeometry(polyline, None)
else:
Expand Down

0 comments on commit 4e88d0e

Please sign in to comment.