Skip to content

Commit 55707b8

Browse files
committed
Merge branch 'main' into 27-tallenna/vie-kaava-jsonina
2 parents c6b68d5 + eab22d5 commit 55707b8

6 files changed

+101
-40
lines changed

arho_feature_template/core/plan_manager.py

+10-17
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
from arho_feature_template.gui.load_plan_dialog import LoadPlanDialog
1111
from arho_feature_template.gui.serialize_plan import SerializePlan
1212
from arho_feature_template.utils.db_utils import get_existing_database_connection_names
13-
from arho_feature_template.utils.misc_utils import get_active_plan_id, get_settings, handle_unsaved_changes
13+
from arho_feature_template.utils.misc_utils import (
14+
get_active_plan_id,
15+
get_layer_by_name,
16+
get_settings,
17+
handle_unsaved_changes,
18+
)
1419

1520

1621
class PlanManager:
@@ -28,15 +33,7 @@ def __init__(self):
2833
proxy.setPort(int(proxy_port))
2934
self.network_manager.setProxy(proxy)
3035

31-
self.kaava_layer = self.get_layer_by_name("Kaava")
32-
33-
def get_layer_by_name(self, layer_name):
34-
"""Retrieve a layer by name from the project."""
35-
layers = QgsProject.instance().mapLayersByName(layer_name)
36-
if layers:
37-
return layers[0]
38-
iface.messageBar().pushMessage("Error", f"Layer '{layer_name}' not found", level=3)
39-
return None
36+
self.kaava_layer = get_layer_by_name("Kaava")
4037

4138
def add_new_plan(self):
4239
"""Initiate the process to add a new plan to the Kaava layer."""
@@ -157,7 +154,7 @@ def _handle_response(self, reply: QNetworkReply, json_plan_path: str, json_plan_
157154
return
158155

159156
try:
160-
response_data = reply.readAll().data().decode('utf-8')
157+
response_data = reply.readAll().data().decode("utf-8")
161158
response_json = json.loads(response_data)
162159

163160
details = response_json.get("details")
@@ -186,9 +183,7 @@ def _handle_response(self, reply: QNetworkReply, json_plan_path: str, json_plan_
186183
# Build the structured outline JSON and write to file
187184
outline_json = {
188185
"type": "Feature",
189-
"properties": {
190-
"name": "Example Polygon"
191-
},
186+
"properties": {"name": "Example Polygon"},
192187
"srid": geographical_area.get("srid"),
193188
"geometry": geographical_area.get("geometry"),
194189
}
@@ -204,9 +199,7 @@ def _handle_response(self, reply: QNetworkReply, json_plan_path: str, json_plan_
204199
f"'geographicalArea' ei sisällä vaadittuja tietoja: {e}",
205200
)
206201
else:
207-
QMessageBox.warning(
208-
None, "Varoitus", "Kenttä 'geographicalArea' puuttuu kaavan tiedoista."
209-
)
202+
QMessageBox.warning(None, "Varoitus", "Kenttä 'geographicalArea' puuttuu kaavan tiedoista.")
210203
except json.JSONDecodeError as e:
211204
QMessageBox.critical(None, "Virhe", f"Vastaus JSON:in purkaminen epäonnistui: {e}")
212205

arho_feature_template/core/plan_regulation_config.py

+5-14
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@
88
from typing import TYPE_CHECKING, cast
99

1010
import yaml
11-
from qgis.core import QgsProject
1211
from qgis.utils import iface
1312

1413
from arho_feature_template.qgis_plugin_tools.tools.resources import resources_path
14+
from arho_feature_template.utils.misc_utils import get_layer_by_name
1515

1616
if TYPE_CHECKING:
1717
from numbers import Number
1818
from typing import Literal
1919

20-
from qgis.core import QgsMapLayer
2120
from qgis.gui import QgisInterface
2221

2322
iface: QgisInterface = cast("QgisInterface", iface) # type: ignore[no-redef]
@@ -56,16 +55,6 @@ class Unit(Enum):
5655
DECIBEL = "dB"
5756

5857

59-
# TODO: Same as in PlanManager, should refactor
60-
def get_layer_by_name(layer_name: str) -> QgsMapLayer | None:
61-
"""Retrieve a layer by name from the project."""
62-
layers = QgsProject.instance().mapLayersByName(layer_name)
63-
if layers:
64-
return layers[0]
65-
iface.messageBar().pushMessage("Error", f"Layer '{layer_name}' not found", level=3)
66-
return None
67-
68-
6958
def get_name_mapping_for_plan_regulations(layer_name: str) -> dict[str, dict[str, str]] | None:
7059
layer = get_layer_by_name(layer_name)
7160
if not layer:
@@ -188,7 +177,9 @@ class PlanRegulationDefinition:
188177

189178
regulation_config: PlanRegulationConfig
190179
default_value: str | Number | None
191-
additional_info: dict[str, str | Number | None] # NOTE: Correct typing for additional information values?
180+
additional_information: list[
181+
dict[str, str | Number | None]
182+
] # NOTE: Correct typing for additional information values?
192183
regulation_number: int | None
193184
attached_files: list[Path]
194185

@@ -197,7 +188,7 @@ def from_dict(cls, data: dict) -> PlanRegulationDefinition:
197188
return cls(
198189
regulation_config=data["config"],
199190
default_value=data.get("default_value"),
200-
additional_info=data.get("additional_info", {}),
191+
additional_information=data.get("additional_information", []),
201192
regulation_number=data.get("regulation_number"),
202193
attached_files=data.get("attached_files", []),
203194
)

arho_feature_template/gui/plan_regulation_group_widget.ui

+13
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@
3737
<layout class="QVBoxLayout" name="verticalLayout">
3838
<item>
3939
<layout class="QHBoxLayout" name="horizontalLayout">
40+
<item>
41+
<widget class="QLabel" name="label">
42+
<property name="font">
43+
<font>
44+
<weight>75</weight>
45+
<bold>true</bold>
46+
</font>
47+
</property>
48+
<property name="text">
49+
<string>Otsikko</string>
50+
</property>
51+
</widget>
52+
</item>
4053
<item>
4154
<widget class="QLineEdit" name="heading">
4255
<property name="sizePolicy">

arho_feature_template/gui/plan_regulation_widget.py

+25-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from collections import defaultdict
44
from importlib import resources
5-
from typing import TYPE_CHECKING
5+
from typing import TYPE_CHECKING, cast
66

77
from qgis.core import QgsApplication
88
from qgis.gui import QgsDoubleSpinBox, QgsFileWidget, QgsSpinBox
@@ -20,6 +20,7 @@
2020
)
2121

2222
from arho_feature_template.core.plan_regulation_config import PlanRegulationConfig, Unit, ValueType
23+
from arho_feature_template.utils.misc_utils import get_additional_information_name
2324

2425
if TYPE_CHECKING:
2526
from numbers import Number
@@ -28,7 +29,7 @@
2829

2930
from arho_feature_template.core.plan_regulation_group_config import PlanRegulationDefinition
3031

31-
ui_path = resources.files(__package__) / "new_plan_regulation_widget.ui"
32+
ui_path = resources.files(__package__) / "plan_regulation_widget.ui"
3233
FormClass, _ = uic.loadUiType(ui_path)
3334

3435

@@ -41,6 +42,9 @@
4142
TEXT_VALUE_FIELD = "text_value"
4243
REGULATION_TYPE_ADDITIONAL_INFORMATION_ID = "regulation_type_additional_information_id"
4344

45+
# TO BE REPLACED
46+
ADDITIONAL_INFORMATION_TYPES_WITH_INPUT = ["kayttotarkoituskohdistus"]
47+
4448

4549
class PlanRegulationWidget(QWidget, FormClass): # type: ignore
4650
"""A widget representation of a plan regulation."""
@@ -73,7 +77,18 @@ def __init__(self, config: PlanRegulationConfig, parent=None):
7377
self.init_buttons()
7478

7579
def populate_from_definition(self, definition: PlanRegulationDefinition):
76-
pass
80+
# Additional information
81+
for info in definition.additional_information:
82+
info_type: str = cast("str", info["type"])
83+
layout = self.add_additional_info(info_type)
84+
if info_type in ADDITIONAL_INFORMATION_TYPES_WITH_INPUT:
85+
info_value_widget = QLineEdit()
86+
layout.addWidget(info_value_widget)
87+
value = info.get("value")
88+
if value:
89+
info_value_widget.setText(value)
90+
91+
# TODO: Other saved information from PlanRegulationDefinition
7792

7893
def init_value_fields(self):
7994
layout = QHBoxLayout()
@@ -186,10 +201,14 @@ def add_versioned_text_input(self, layout: QHBoxLayout):
186201
layout.addWidget(text_widget)
187202
self.form_layout.addRow("Kieliversioitu teksti", layout)
188203

189-
def add_additional_info(self, info_type):
190-
info_type_label = QLineEdit(info_type)
204+
def add_additional_info(self, info_type: str) -> QHBoxLayout:
205+
layout = QHBoxLayout()
206+
info_name = get_additional_information_name(info_type)
207+
info_type_label = QLineEdit(info_name)
191208
info_type_label.setReadOnly(True)
192-
self.form_layout.addRow("Lisätiedonlaji", info_type_label)
209+
layout.addWidget(info_type_label)
210+
self.form_layout.addRow("Lisätiedonlaji", layout)
211+
return layout
193212

194213
def add_regulation_number(self):
195214
if not self.regulation_number_added:

arho_feature_template/gui/template_attribute_form.ui

+6-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<item row="0" column="0">
2828
<widget class="QLabel" name="feature_name_label">
2929
<property name="text">
30-
<string>Kaavakohteen nimi</string>
30+
<string>Nimi</string>
3131
</property>
3232
</widget>
3333
</item>
@@ -37,7 +37,7 @@
3737
<item row="1" column="0">
3838
<widget class="QLabel" name="feature_description_label">
3939
<property name="text">
40-
<string>Kaavakohteen kuvaus</string>
40+
<string>Kuvaus</string>
4141
</property>
4242
</widget>
4343
</item>
@@ -82,6 +82,9 @@
8282
</item>
8383
<item>
8484
<widget class="QgsFilterLineEdit" name="mLineEdit">
85+
<property name="enabled">
86+
<bool>false</bool>
87+
</property>
8588
<property name="sizePolicy">
8689
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
8790
<horstretch>0</horstretch>
@@ -92,7 +95,7 @@
9295
<string>Suodata kaavamääräysryhmiä</string>
9396
</property>
9497
<property name="clearButtonEnabled">
95-
<bool>true</bool>
98+
<bool>false</bool>
9699
</property>
97100
<property name="showSearchIcon">
98101
<bool>true</bool>

arho_feature_template/utils/misc_utils.py

+42
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,54 @@
1+
from __future__ import annotations
2+
13
import os
4+
from typing import TYPE_CHECKING, cast
25

36
from qgis.core import QgsExpressionContextUtils, QgsProject, QgsVectorLayer
47
from qgis.PyQt.QtCore import QSettings
58
from qgis.PyQt.QtWidgets import QMessageBox
9+
from qgis.utils import iface
10+
11+
if TYPE_CHECKING:
12+
from typing import Literal
13+
14+
from qgis.core import QgsMapLayer
15+
from qgis.gui import QgisInterface
16+
17+
iface: QgisInterface = cast("QgisInterface", iface) # type: ignore[no-redef]
618

719
PLUGIN_PATH = os.path.dirname(os.path.dirname(__file__))
820

921

22+
# NOTE: Consider creating "layer_utils.py" or similar for layer related utils in the future
23+
def get_layer_by_name(layer_name: str) -> QgsMapLayer | None:
24+
"""
25+
Retrieve a layer by name from the project.
26+
27+
If multiple layers with the same name exist, returns the first one. Returns None
28+
if layer with a matching name is not found.
29+
"""
30+
layers = QgsProject.instance().mapLayersByName(layer_name)
31+
if layers:
32+
return layers[0]
33+
iface.messageBar().pushWarning("Error", f"Layer '{layer_name}' not found")
34+
return None
35+
36+
37+
def get_additional_information_name(info_type: str, language: Literal["fin", "eng", "swe"] = "fin") -> str:
38+
"""
39+
Retrieve name of input additional information type from associated QGIS layer.
40+
41+
Returns input `info_type` if name is not found.
42+
"""
43+
type_of_additional_information_layer_name = "Lisätiedonlaji"
44+
layer = get_layer_by_name(type_of_additional_information_layer_name)
45+
if layer:
46+
for feature in layer.getFeatures():
47+
if feature["value"] == info_type:
48+
return feature["name"][language]
49+
return info_type
50+
51+
1052
def check_layer_changes() -> bool:
1153
"""Check if there are unsaved changes in any QGIS layers."""
1254
project = QgsProject.instance()

0 commit comments

Comments
 (0)