Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tarkista lommakkeella tallennettavien määräysryhmien lyhyet nimet #197

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions arho_feature_template/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ def from_config_file(cls, config_fp: Path) -> RegulationGroupLibrary:
],
)

def get_short_names(self) -> set[str]:
"""Returns set of non-empty short names (letter codes) of regulation groups part of the library."""
return {
regulation_group.short_name
for category in self.regulation_group_categories
for regulation_group in category.regulation_groups
if regulation_group.short_name
}


@dataclass
class RegulationLibrary:
Expand Down
31 changes: 19 additions & 12 deletions arho_feature_template/core/plan_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ def __init__(self):
self.regulation_groups_dock.new_regulation_group_requested.connect(self.create_new_regulation_group)
self.regulation_groups_dock.edit_regulation_group_requested.connect(self.edit_regulation_group)
self.regulation_groups_dock.delete_regulation_group_requested.connect(self.delete_regulation_group)
if get_active_plan_id():
self.regulation_groups_dock.initialize_regulation_groups(regulation_group_library_from_active_plan())
self.update_active_plan_regulation_group_library()
self.regulation_groups_dock.hide()

# Initialize digitize tools
Expand Down Expand Up @@ -156,24 +155,28 @@ def initialize_libraries(self):
]
self.new_feature_dock.initialize_feature_template_libraries(self.feature_template_libraries)

def update_active_plan_regulation_group_library(self):
self.active_plan_regulation_group_library = regulation_group_library_from_active_plan()
self.regulation_groups_dock.update_regulation_groups(self.active_plan_regulation_group_library)

def create_new_regulation_group(self):
self._open_regulation_group_form(RegulationGroup())

def edit_regulation_group(self, regulation_group: RegulationGroup):
self._open_regulation_group_form(regulation_group)

def _open_regulation_group_form(self, regulation_group: RegulationGroup):
regulation_group_form = PlanRegulationGroupForm(regulation_group)
regulation_group_form = PlanRegulationGroupForm(regulation_group, self.active_plan_regulation_group_library)
if regulation_group_form.exec_():
if regulation_group_form.save_as_config:
save_regulation_group_as_config(regulation_group_form.model)
else:
save_regulation_group(regulation_group_form.model)
self.regulation_groups_dock.initialize_regulation_groups(regulation_group_library_from_active_plan())
self.update_active_plan_regulation_group_library()

def delete_regulation_group(self, group: RegulationGroup):
delete_regulation_group(group)
self.regulation_groups_dock.initialize_regulation_groups(regulation_group_library_from_active_plan())
self.update_active_plan_regulation_group_library()

def toggle_identify_plan_features(self, activate: bool): # noqa: FBT001
if activate:
Expand Down Expand Up @@ -240,7 +243,7 @@ def edit_plan(self):
attribute_form = PlanAttributeForm(plan_model, self.regulation_group_libraries)
if attribute_form.exec_():
feature = save_plan(attribute_form.model)
self.regulation_groups_dock.initialize_regulation_groups(regulation_group_library_from_active_plan())
self.update_active_plan_regulation_group_library()

def edit_lifecycles(self):
plan_layer = PlanLayer.get_from_project()
Expand Down Expand Up @@ -311,23 +314,23 @@ def _plan_feature_geom_digitized(self, feature: QgsFeature):

plan_feature.geom = feature.geometry()
attribute_form = PlanFeatureForm(
plan_feature, title, [*self.regulation_group_libraries, regulation_group_library_from_active_plan()]
plan_feature, title, self.regulation_group_libraries, self.active_plan_regulation_group_library
)
if attribute_form.exec_():
save_plan_feature(attribute_form.model)
self.regulation_groups_dock.initialize_regulation_groups(regulation_group_library_from_active_plan())
self.update_active_plan_regulation_group_library()

def edit_plan_feature(self, feature: QgsFeature, layer_name: str):
layer_class = FEATURE_LAYER_NAME_TO_CLASS_MAP[layer_name]
plan_feature = layer_class.model_from_feature(feature)

title = plan_feature.name if plan_feature.name else layer_name
attribute_form = PlanFeatureForm(
plan_feature, title, [*self.regulation_group_libraries, regulation_group_library_from_active_plan()]
plan_feature, title, self.regulation_group_libraries, self.active_plan_regulation_group_library
)
if attribute_form.exec_():
save_plan_feature(attribute_form.model)
self.regulation_groups_dock.initialize_regulation_groups(regulation_group_library_from_active_plan())
self.update_active_plan_regulation_group_library()

def set_active_plan(self, plan_id: str | None):
"""Update the project layers based on the selected land use plan.
Expand All @@ -352,8 +355,7 @@ def set_active_plan(self, plan_id: str | None):
if previously_in_edit_mode:
plan_layer.startEditing()

# Update regulation group dock
self.regulation_groups_dock.initialize_regulation_groups(regulation_group_library_from_active_plan())
self.update_active_plan_regulation_group_library()

def load_land_use_plan(self):
"""Load an existing land use plan using a dialog selection."""
Expand Down Expand Up @@ -456,6 +458,11 @@ def unload(self):


def regulation_group_library_from_active_plan() -> RegulationGroupLibrary:
if not get_active_plan_id():
return RegulationGroupLibrary(
name="Käytössä olevat kaavamääräysryhmät", version=None, description=None, regulation_group_categories=[]
)

id_of_general_regulation_group_type = PlanRegulationGroupTypeLayer.get_attribute_value_by_another_attribute_value(
"id", "value", "generalRegulations"
)
Expand Down
58 changes: 53 additions & 5 deletions arho_feature_template/gui/dialogs/plan_feature_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
QDialog,
QDialogButtonBox,
QLineEdit,
QMessageBox,
QScrollArea,
QSizePolicy,
QSpacerItem,
Expand Down Expand Up @@ -39,7 +40,11 @@ class PlanFeatureForm(QDialog, FormClass): # type: ignore
"""Parent class for feature forms for adding and modifying feature attribute data."""

def __init__(
self, plan_feature: PlanFeature, form_title: str, regulation_group_libraries: list[RegulationGroupLibrary]
self,
plan_feature: PlanFeature,
form_title: str,
regulation_group_libraries: list[RegulationGroupLibrary],
active_plan_regulation_groups_library: RegulationGroupLibrary,
):
super().__init__()
self.setupUi(self)
Expand All @@ -57,7 +62,9 @@ def __init__(
self.regulation_groups_tree_layout: QVBoxLayout

# INIT
self.regulation_group_libraries = regulation_group_libraries
self.existing_group_short_names = active_plan_regulation_groups_library.get_short_names()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Voisi muuttaa set:ksi, niin haku tehokkaampaa. Ei nyt oikeesti tässä mittaluokassa merkittävää.
Tai sitten tuon get_short_names() voisi muuttaa palauttamaan suoraan set:n.

Suggested change
self.existing_group_short_names = active_plan_regulation_groups_library.get_short_names()
self.existing_group_short_names = set(active_plan_regulation_groups_library.get_short_names())

self.active_plan_regulation_groups_library = active_plan_regulation_groups_library
self.regulation_group_libraries = [*regulation_group_libraries, active_plan_regulation_groups_library]
self.plan_regulation_group_libraries_combobox.addItems(
library.name for library in self.regulation_group_libraries
)
Expand Down Expand Up @@ -97,6 +104,44 @@ def _remove_spacer(self):
self.plan_regulation_group_scrollarea_contents.layout().removeItem(self.scroll_area_spacer)
self.scroll_area_spacer = None

def _check_regulation_group_short_names(self) -> bool:
seen_names = set()
existing_names = set()
duplicate_names = set()

for reg_group_widget in self.regulation_group_widgets:
short_name = reg_group_widget.short_name.text()
if not short_name:
continue

if short_name in self.existing_group_short_names:
existing_names.add(short_name)
if short_name in seen_names:
duplicate_names.add(short_name)
seen_names.add(short_name)

def format_names(names, last_item_conjucation: str = "ja"):
names = list(names)
formatted_names = ", ".join(f"'<b>{name}</b>'" for name in names[:-1])
formatted_names += f" {last_item_conjucation} " if len(names) > 1 else ""
formatted_names += f"'<b>{names[-1]}</b>'" if names else ""
return formatted_names

if existing_names:
if len(existing_names) == 1:
msg = f"Kaavamääräysryhmä lyhyellä nimellä {format_names(existing_names)} on jo olemassa."
else:
msg = f"Kaavamääräysryhmät lyhyillä nimillä {format_names(existing_names)} ovat jo olemassa."
QMessageBox.critical(self, "Virhe", msg)
return False

if duplicate_names:
msg = f"Lomakkeella on useita kaavamääräysryhmiä, joilla on lyhyt nimi {format_names(duplicate_names, 'tai')}."
QMessageBox.critical(self, "Virhe", msg)
return False

return True

def add_selected_plan_regulation_group(self, item: QTreeWidgetItem, column: int):
if not item.parent():
return
Expand All @@ -113,7 +158,9 @@ def add_plan_regulation_group(self, definition: RegulationGroup):
self._add_spacer()

def open_plan_regulation_group_form(self, regulation_group_widget: RegulationGroupWidget):
group_as_form = PlanRegulationGroupForm(regulation_group_widget.into_model())
group_as_form = PlanRegulationGroupForm(
regulation_group_widget.into_model(), self.active_plan_regulation_groups_library
)
if group_as_form.exec_():
regulation_group_widget.from_model(group_as_form.model)

Expand Down Expand Up @@ -146,5 +193,6 @@ def into_model(self) -> PlanFeature:
)

def _on_ok_clicked(self):
self.model = self.into_model()
self.accept()
if self._check_regulation_group_short_names():
self.model = self.into_model()
self.accept()
20 changes: 17 additions & 3 deletions arho_feature_template/gui/dialogs/plan_regulation_group_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
QDialogButtonBox,
QHBoxLayout,
QLabel,
QMessageBox,
QSizePolicy,
QTextBrowser,
QTreeWidgetItem,
Expand All @@ -23,6 +24,7 @@
Regulation,
RegulationConfig,
RegulationGroup,
RegulationGroupLibrary,
RegulationLibrary,
)
from arho_feature_template.gui.components.plan_proposition_widget import PropositionWidget
Expand All @@ -45,7 +47,9 @@
class PlanRegulationGroupForm(QDialog, FormClass): # type: ignore
"""Form to create a new plan regulation group."""

def __init__(self, regulation_group: RegulationGroup):
def __init__(
self, regulation_group: RegulationGroup, active_plan_regulation_groups_library: RegulationGroupLibrary
):
super().__init__()
self.setupUi(self)

Expand Down Expand Up @@ -74,6 +78,7 @@ def __init__(self, regulation_group: RegulationGroup):
self.regulation_widgets: list[RegulationWidget] = []
self.proposition_widgets: list[PropositionWidget] = []
self.save_as_config = False
self.existing_group_short_names = active_plan_regulation_groups_library.get_short_names()

# Initialize regulation library
self.regulations_selection_widget = TreeWithSearchWidget()
Expand Down Expand Up @@ -137,6 +142,14 @@ def _initalize_regulation_from_config(self, config: RegulationConfig, parent: QT
for child_config in config.child_regulations:
self._initalize_regulation_from_config(child_config, item)

def _check_short_name(self) -> bool:
short_name = self.short_name.text()
if short_name and short_name in self.existing_group_short_names:
msg = f"Kaavamääräysryhmä lyhyellä nimellä '<b>{short_name}</b>' on jo olemassa."
QMessageBox.critical(self, "Virhe", msg)
return False
return True

def update_selected_regulation(self, item: QTreeWidgetItem, column: int):
config: RegulationConfig = item.data(column, Qt.UserRole) # Retrieve the associated config
self.regulation_info.setText(config.description)
Expand Down Expand Up @@ -190,5 +203,6 @@ def into_model(self) -> RegulationGroup:
)

def _on_ok_clicked(self):
self.model = self.into_model()
self.accept()
if self._check_short_name():
self.model = self.into_model()
self.accept()
2 changes: 1 addition & 1 deletion arho_feature_template/gui/docks/regulation_groups_dock.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def connect_buttons(self):
self.edit_btn.clicked.connect(self.on_edit_btn_clicked)
self.delete_btn.clicked.connect(self.on_delete_btn_clicked)

def initialize_regulation_groups(self, regulation_group_library: RegulationGroupLibrary):
def update_regulation_groups(self, regulation_group_library: RegulationGroupLibrary):
self.regulation_group_list.clear()

for category in regulation_group_library.regulation_group_categories:
Expand Down
Loading