Skip to content

Commit fda2581

Browse files
nmaarnioLKajan
authored andcommitted
make new template attribute form initialize itself correctly, attempt simple feature saving (WIP)
1 parent 69fead5 commit fda2581

11 files changed

+845
-590
lines changed

arho_feature_template/core/feature_template_library.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,7 @@ def ask_for_feature_attributes(self, feature: QgsFeature) -> None:
186186
if attribute_form.exec_():
187187
layer = get_layer_from_project(self.active_template.config.feature.layer)
188188
# Save the feature
189-
for attributes in attribute_form.attribute_widgets.values():
190-
for attribute, widget in attributes.items():
191-
feature.setAttribute(
192-
attribute,
193-
widget.text(),
194-
)
189+
attribute_form.set_feature_attributes(feature)
195190

196191
layer.beginEditCommand("Create feature from template")
197192
layer.addFeature(feature)

arho_feature_template/core/template_library_config.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,9 @@ def from_dict(cls, data: dict) -> Attribute:
122122
def display(self) -> str:
123123
if self.description is not None:
124124
return self.description
125-
elif self.default is not None:
125+
if self.default is not None:
126126
return self.default
127-
else:
128-
return ""
127+
return ""
129128

130129

131130
def parse_template_library_config(template_library_config: Path) -> TemplateLibraryConfig:
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
from __future__ import annotations
22

3-
from collections import defaultdict
43
from importlib import resources
54
from typing import TYPE_CHECKING
65

76
from qgis.core import QgsApplication
87
from qgis.PyQt import uic
98
from qgis.PyQt.QtCore import pyqtSignal
10-
from qgis.PyQt.QtGui import QFont, QIcon
11-
from qgis.PyQt.QtWidgets import QLabel, QLineEdit, QWidget
9+
from qgis.PyQt.QtWidgets import QWidget
1210

13-
from arho_feature_template.qgis_plugin_tools.tools.resources import plugin_path
11+
from arho_feature_template.gui.plan_regulation_widget import PlanRegulationWidget
1412

1513
if TYPE_CHECKING:
16-
from qgis.gui import QgsCollapsibleGroupBox
17-
from qgis.PyQt.QtWidgets import QGridLayout, QPushButton
14+
from qgis.PyQt.QtWidgets import QFrame, QLineEdit, QPushButton
1815

1916
from arho_feature_template.core.template_library_config import Feature
2017

@@ -32,84 +29,36 @@ def __init__(self, feature: Feature):
3229
self.setupUi(self)
3330

3431
# TYPES
32+
self.frame: QFrame
3533
self.heading: QLineEdit
36-
self.conf_btn: QPushButton
3734
self.del_btn: QPushButton
3835

39-
self.plan_regulation_groupbox: QgsCollapsibleGroupBox
40-
self.plan_regulation_grid_layout: QGridLayout
41-
4236
# INIT
4337
self.feature = feature
4438
self.layer = self.feature.layer # Should be plan_regulation_group layer
4539

46-
self.input_value_header = None
47-
self.input_value_col = None
48-
49-
self.additional_information_header = None
50-
self.additional_information_col = None
51-
52-
self.bold_font = QFont()
53-
self.bold_font.setBold(True)
54-
5540
self.init_buttons()
56-
57-
self.attribute_widgets: dict[str, dict[str, QWidget]] = defaultdict(dict)
58-
59-
for attribute_config in feature.attributes:
60-
if attribute_config.attribute == "name":
61-
self.heading.setText(attribute_config.display())
62-
63-
if feature.child_features is not None:
64-
for child in feature.child_features:
65-
if child.layer == "plan_requlation":
66-
self.create_widgets_for_plan_regulation(child)
41+
self.set_group_heading()
42+
self.add_plan_regulation_widgets()
6743

6844
def request_delete(self):
6945
self.delete_signal.emit(self)
7046

7147
def init_buttons(self):
72-
self.conf_btn.setIcon(QIcon(plugin_path("resources", "icons", "settings.svg")))
7348
self.del_btn.setIcon(QgsApplication.getThemeIcon("mActionDeleteSelected.svg"))
7449
self.del_btn.clicked.connect(self.request_delete)
7550

76-
def create_widgets_for_plan_regulation(self, plan_regulation_feature: Feature):
77-
row = self.plan_regulation_grid_layout.rowCount() + 1
78-
for plan_regulation_config in plan_regulation_feature.attributes:
79-
if plan_regulation_config.attribute == "type_of_plan_regulation_id":
80-
id_label = QLabel(plan_regulation_config.display())
81-
# print(plan_regulation_config)
82-
self.plan_regulation_grid_layout.addWidget(id_label, row, 0)
83-
elif plan_regulation_config.attribute == "numeric_default":
84-
if not self.input_value_header:
85-
self.input_value_header = QLabel("Arvo")
86-
self.input_value_header.setFont(self.bold_font)
87-
self.input_value_col = self.plan_regulation_grid_layout.columnCount() + 1
88-
self.plan_regulation_grid_layout.addWidget(self.input_value_header, 0, self.input_value_col)
89-
90-
input_field = QLineEdit()
91-
self.plan_regulation_grid_layout.addWidget(input_field, row, self.input_value_col)
92-
93-
if plan_regulation_feature.child_features is None:
94-
return
95-
for child in plan_regulation_feature.child_features:
96-
# Additional information here, what else?
97-
# Assume attribute is "additional_information_of_plan_regulation"
98-
# NOTE: Could additional information be attribute of plan regulation instead of child feature?
99-
100-
# Add header if not added yet
101-
if not self.additional_information_header:
102-
self.additional_information_header = QLabel("Lisätiedot")
103-
self.additional_information_header.setFont(self.bold_font)
104-
self.additonal_information_col = self.plan_regulation_grid_layout.columnCount() + 1
105-
self.plan_regulation_grid_layout.addWidget(
106-
self.additional_information_header, 0, self.additonal_information_col
107-
)
108-
109-
# TBD: Multiple additional feature per plan regulation
110-
for attribute in child.attributes:
111-
# Assume "type_of_additional_information_id"
112-
additional_information_label = QLabel(attribute.display())
113-
self.plan_regulation_grid_layout.addWidget(
114-
additional_information_label, row, self.additonal_information_col
115-
)
51+
def set_group_heading(self):
52+
for attribute_config in self.feature.attributes:
53+
if attribute_config.attribute == "name":
54+
self.heading.setText(attribute_config.display())
55+
56+
def add_plan_regulation_widgets(self):
57+
if self.feature.child_features is not None:
58+
for child in self.feature.child_features:
59+
if child.layer == "plan_requlation":
60+
self.add_plan_regulation_widget(child)
61+
62+
def add_plan_regulation_widget(self, plan_regulation_feature: Feature):
63+
plan_regulation_widget = PlanRegulationWidget(plan_regulation_feature)
64+
self.frame.layout().addWidget(plan_regulation_widget)

arho_feature_template/gui/plan_regulation_group_widget.ui

+52-167
Original file line numberDiff line numberDiff line change
@@ -6,191 +6,76 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>582</width>
10-
<height>195</height>
9+
<width>514</width>
10+
<height>65</height>
1111
</rect>
1212
</property>
13+
<property name="sizePolicy">
14+
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
15+
<horstretch>0</horstretch>
16+
<verstretch>0</verstretch>
17+
</sizepolicy>
18+
</property>
1319
<property name="windowTitle">
1420
<string>Form</string>
1521
</property>
16-
<layout class="QVBoxLayout" name="verticalLayout">
17-
<item>
18-
<layout class="QHBoxLayout" name="plan_regulation_group_heading_layout">
19-
<item>
20-
<widget class="QLabel" name="heading_label">
21-
<property name="sizePolicy">
22-
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
23-
<horstretch>0</horstretch>
24-
<verstretch>0</verstretch>
25-
</sizepolicy>
26-
</property>
27-
<property name="text">
28-
<string>Otsikko:</string>
29-
</property>
30-
</widget>
31-
</item>
32-
<item>
33-
<widget class="QLineEdit" name="heading">
34-
<property name="sizePolicy">
35-
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
36-
<horstretch>0</horstretch>
37-
<verstretch>0</verstretch>
38-
</sizepolicy>
39-
</property>
40-
<property name="text">
41-
<string/>
42-
</property>
43-
</widget>
44-
</item>
45-
<item>
46-
<widget class="QPushButton" name="conf_btn">
47-
<property name="sizePolicy">
48-
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
49-
<horstretch>0</horstretch>
50-
<verstretch>0</verstretch>
51-
</sizepolicy>
52-
</property>
53-
<property name="maximumSize">
54-
<size>
55-
<width>30</width>
56-
<height>16777215</height>
57-
</size>
58-
</property>
59-
<property name="text">
60-
<string/>
61-
</property>
62-
</widget>
63-
</item>
64-
<item>
65-
<widget class="QPushButton" name="del_btn">
66-
<property name="sizePolicy">
67-
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
68-
<horstretch>0</horstretch>
69-
<verstretch>0</verstretch>
70-
</sizepolicy>
71-
</property>
72-
<property name="maximumSize">
73-
<size>
74-
<width>30</width>
75-
<height>16777215</height>
76-
</size>
77-
</property>
78-
<property name="text">
79-
<string/>
80-
</property>
81-
</widget>
82-
</item>
83-
<item>
84-
<spacer name="plan_regulation_group_heading_spacer">
85-
<property name="orientation">
86-
<enum>Qt::Horizontal</enum>
87-
</property>
88-
<property name="sizeType">
89-
<enum>QSizePolicy::Maximum</enum>
90-
</property>
91-
<property name="sizeHint" stdset="0">
92-
<size>
93-
<width>40</width>
94-
<height>20</height>
95-
</size>
96-
</property>
97-
</spacer>
98-
</item>
99-
</layout>
100-
</item>
22+
<layout class="QVBoxLayout" name="verticalLayout_2">
10123
<item>
102-
<layout class="QHBoxLayout" name="plan_regulation_group_contents_layout">
103-
<item>
104-
<spacer name="plan_regulation_groupbox_spacer1">
105-
<property name="orientation">
106-
<enum>Qt::Horizontal</enum>
107-
</property>
108-
<property name="sizeType">
109-
<enum>QSizePolicy::Fixed</enum>
110-
</property>
111-
<property name="sizeHint" stdset="0">
112-
<size>
113-
<width>20</width>
114-
<height>20</height>
115-
</size>
116-
</property>
117-
</spacer>
118-
</item>
119-
<item>
120-
<widget class="QgsCollapsibleGroupBox" name="plan_regulation_groupbox">
121-
<property name="title">
122-
<string/>
123-
</property>
124-
<property name="flat">
125-
<bool>false</bool>
126-
</property>
127-
<property name="checkable">
128-
<bool>false</bool>
129-
</property>
130-
<layout class="QHBoxLayout" name="horizontalLayout_4">
24+
<widget class="QFrame" name="frame">
25+
<property name="sizePolicy">
26+
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
27+
<horstretch>0</horstretch>
28+
<verstretch>0</verstretch>
29+
</sizepolicy>
30+
</property>
31+
<property name="frameShape">
32+
<enum>QFrame::StyledPanel</enum>
33+
</property>
34+
<property name="frameShadow">
35+
<enum>QFrame::Raised</enum>
36+
</property>
37+
<layout class="QVBoxLayout" name="verticalLayout">
38+
<item>
39+
<layout class="QHBoxLayout" name="horizontalLayout">
13140
<item>
132-
<layout class="QGridLayout" name="plan_regulation_grid_layout">
133-
<item row="0" column="0">
134-
<widget class="QLabel" name="plan_regulation_label">
135-
<property name="sizePolicy">
136-
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
137-
<horstretch>0</horstretch>
138-
<verstretch>0</verstretch>
139-
</sizepolicy>
140-
</property>
141-
<property name="minimumSize">
142-
<size>
143-
<width>175</width>
144-
<height>0</height>
145-
</size>
146-
</property>
147-
<property name="maximumSize">
148-
<size>
149-
<width>175</width>
150-
<height>16777215</height>
151-
</size>
152-
</property>
153-
<property name="font">
154-
<font>
155-
<weight>75</weight>
156-
<bold>true</bold>
157-
</font>
158-
</property>
159-
<property name="text">
160-
<string>Kaavamääräyslaji</string>
161-
</property>
162-
</widget>
163-
</item>
164-
</layout>
41+
<widget class="QLineEdit" name="heading">
42+
<property name="sizePolicy">
43+
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
44+
<horstretch>0</horstretch>
45+
<verstretch>0</verstretch>
46+
</sizepolicy>
47+
</property>
48+
<property name="text">
49+
<string/>
50+
</property>
51+
</widget>
16552
</item>
16653
<item>
167-
<spacer name="plan_regulation_groupbox_spacer2">
168-
<property name="orientation">
169-
<enum>Qt::Horizontal</enum>
54+
<widget class="QPushButton" name="del_btn">
55+
<property name="sizePolicy">
56+
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
57+
<horstretch>0</horstretch>
58+
<verstretch>0</verstretch>
59+
</sizepolicy>
17060
</property>
171-
<property name="sizeHint" stdset="0">
61+
<property name="maximumSize">
17262
<size>
173-
<width>0</width>
174-
<height>20</height>
63+
<width>30</width>
64+
<height>16777215</height>
17565
</size>
17666
</property>
177-
</spacer>
67+
<property name="text">
68+
<string/>
69+
</property>
70+
</widget>
17871
</item>
17972
</layout>
180-
</widget>
181-
</item>
182-
</layout>
73+
</item>
74+
</layout>
75+
</widget>
18376
</item>
18477
</layout>
18578
</widget>
186-
<customwidgets>
187-
<customwidget>
188-
<class>QgsCollapsibleGroupBox</class>
189-
<extends>QGroupBox</extends>
190-
<header>qgscollapsiblegroupbox.h</header>
191-
<container>1</container>
192-
</customwidget>
193-
</customwidgets>
19479
<resources/>
19580
<connections/>
19681
</ui>

0 commit comments

Comments
 (0)