Skip to content

Commit c782829

Browse files
nmaarnioLKajan
authored andcommitted
continued new plan regulation group form
- changed plan regulation type selection from combobox to qtreewidget - create widgets when user presses buttons of a plan regulation
1 parent 3420294 commit c782829

4 files changed

+139
-87
lines changed

arho_feature_template/gui/new_plan_regulation_group_form.py

+39-13
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
from importlib import resources
44
from typing import TYPE_CHECKING
55

6-
from qgis.core import QgsApplication
76
from qgis.PyQt import uic
8-
from qgis.PyQt.QtWidgets import QDialog
7+
from qgis.PyQt.QtWidgets import QDialog, QTreeWidget, QTreeWidgetItem
98

109
from arho_feature_template.gui.new_plan_regulation_widget import NewPlanRegulationWidget
1110

1211
if TYPE_CHECKING:
13-
from qgis.PyQt.QtWidgets import QBoxLayout, QPushButton, QWidget
12+
from qgis.PyQt.QtWidgets import QBoxLayout, QWidget
1413

1514
ui_path = resources.files(__package__) / "new_plan_regulation_group_form.ui"
1615
FormClass, _ = uic.loadUiType(ui_path)
@@ -24,21 +23,48 @@ def __init__(self):
2423
self.setupUi(self)
2524

2625
# TYPES
27-
self.add_plan_regulation_btn: QPushButton
26+
self.plan_regulations_view: QTreeWidget
2827
self.plan_regulations_scroll_area_contents: QWidget
2928
self.plan_regulations_layout: QBoxLayout
3029

3130
# INIT
32-
self.add_plan_regulation_btn.clicked.connect(self.add_plan_regulation)
33-
self.add_plan_regulation_btn.setIcon(QgsApplication.getThemeIcon("mActionAdd.svg"))
34-
self.add_plan_regulation()
35-
36-
def add_plan_regulation(self):
37-
index = self.plan_regulations_layout.count() - 2
38-
new_plan_regulation_widget = NewPlanRegulationWidget(self.plan_regulations_scroll_area_contents)
39-
new_plan_regulation_widget.delete_signal.connect(self.remove_plan_regulation_group)
31+
self.initialize_plan_regulations()
32+
self.plan_regulations_view.itemDoubleClicked.connect(self.add_selected_plan_regulation)
33+
34+
def initialize_plan_regulations(self):
35+
# NOTE: Replace the dummy plan regulation codes below with codes from DB
36+
plan_regulation_codes = {
37+
"category 1": ["regulation 1", "regulation 2"],
38+
"category 2": ["regulation 3"],
39+
"category 3": ["regulation 4", "regulation 5", "regulation 6"],
40+
}
41+
42+
# Initialize categories
43+
for category_name, plan_regulations in plan_regulation_codes.items():
44+
category_item = QTreeWidgetItem()
45+
category_item.setText(0, category_name)
46+
self.plan_regulations_view.addTopLevelItem(category_item)
47+
48+
# Initialize plan regulations under the categories
49+
for plan_regulation_type in plan_regulations:
50+
plan_regulation_item = QTreeWidgetItem(category_item)
51+
plan_regulation_item.setText(0, plan_regulation_type)
52+
self.plan_regulations_view.addTopLevelItem(plan_regulation_item)
53+
54+
def add_selected_plan_regulation(self, item: QTreeWidgetItem, column: int):
55+
# If user double clicked category, don't add plan regulation
56+
if not item.parent():
57+
return
58+
self.add_plan_regulation(item.text(column))
59+
60+
def add_plan_regulation(self, regulation_type: str):
61+
new_plan_regulation_widget = NewPlanRegulationWidget(
62+
regulation_type=regulation_type, parent=self.plan_regulations_scroll_area_contents
63+
)
64+
new_plan_regulation_widget.delete_signal.connect(self.delete_plan_regulation)
65+
index = self.plan_regulations_layout.count() - 1
4066
self.plan_regulations_layout.insertWidget(index, new_plan_regulation_widget)
4167

42-
def remove_plan_regulation_group(self, plan_regulation_widget: NewPlanRegulationWidget):
68+
def delete_plan_regulation(self, plan_regulation_widget: NewPlanRegulationWidget):
4369
self.plan_regulations_layout.removeWidget(plan_regulation_widget)
4470
plan_regulation_widget.deleteLater()

arho_feature_template/gui/new_plan_regulation_group_form.ui

+27-33
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>640</width>
10-
<height>480</height>
9+
<width>769</width>
10+
<height>535</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -34,8 +34,8 @@
3434
<rect>
3535
<x>0</x>
3636
<y>0</y>
37-
<width>598</width>
38-
<height>380</height>
37+
<width>727</width>
38+
<height>435</height>
3939
</rect>
4040
</property>
4141
<layout class="QFormLayout" name="formLayout">
@@ -89,7 +89,25 @@
8989
<attribute name="title">
9090
<string>Kaavamääräykset</string>
9191
</attribute>
92-
<layout class="QVBoxLayout" name="verticalLayout">
92+
<layout class="QHBoxLayout" name="horizontalLayout">
93+
<item>
94+
<widget class="QTreeWidget" name="plan_regulations_view">
95+
<property name="sizePolicy">
96+
<sizepolicy hsizetype="Maximum" vsizetype="Expanding">
97+
<horstretch>0</horstretch>
98+
<verstretch>0</verstretch>
99+
</sizepolicy>
100+
</property>
101+
<attribute name="headerVisible">
102+
<bool>false</bool>
103+
</attribute>
104+
<column>
105+
<property name="text">
106+
<string notr="true">1</string>
107+
</property>
108+
</column>
109+
</widget>
110+
</item>
93111
<item>
94112
<widget class="QScrollArea" name="plan_regulations_scroll_area">
95113
<property name="widgetResizable">
@@ -100,35 +118,11 @@
100118
<rect>
101119
<x>0</x>
102120
<y>0</y>
103-
<width>598</width>
104-
<height>380</height>
121+
<width>465</width>
122+
<height>435</height>
105123
</rect>
106124
</property>
107125
<layout class="QVBoxLayout" name="plan_regulations_layout">
108-
<item>
109-
<layout class="QHBoxLayout" name="horizontalLayout_3">
110-
<item>
111-
<widget class="QPushButton" name="add_plan_regulation_btn">
112-
<property name="text">
113-
<string>Lisää kaavamääräys</string>
114-
</property>
115-
</widget>
116-
</item>
117-
<item>
118-
<spacer name="horizontalSpacer_2">
119-
<property name="orientation">
120-
<enum>Qt::Horizontal</enum>
121-
</property>
122-
<property name="sizeHint" stdset="0">
123-
<size>
124-
<width>40</width>
125-
<height>20</height>
126-
</size>
127-
</property>
128-
</spacer>
129-
</item>
130-
</layout>
131-
</item>
132126
<item>
133127
<spacer name="verticalSpacer">
134128
<property name="orientation">
@@ -163,8 +157,8 @@
163157
<rect>
164158
<x>0</x>
165159
<y>0</y>
166-
<width>598</width>
167-
<height>380</height>
160+
<width>727</width>
161+
<height>435</height>
168162
</rect>
169163
</property>
170164
</widget>
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,47 @@
11
from __future__ import annotations
22

3+
from enum import Enum
34
from importlib import resources
45
from typing import TYPE_CHECKING
56

67
from qgis.core import QgsApplication
8+
from qgis.gui import QgsDoubleSpinBox, QgsFileWidget, QgsSpinBox
79
from qgis.PyQt import uic
810
from qgis.PyQt.QtCore import pyqtSignal
9-
from qgis.PyQt.QtWidgets import QMenu, QWidget
11+
from qgis.PyQt.QtWidgets import QComboBox, QFormLayout, QMenu, QTextEdit, QWidget
1012

1113
if TYPE_CHECKING:
12-
from qgis.PyQt.QtWidgets import QComboBox, QPushButton
14+
from qgis.PyQt.QtWidgets import QLineEdit, QPushButton
1315

1416
ui_path = resources.files(__package__) / "new_plan_regulation_widget.ui"
1517
FormClass, _ = uic.loadUiType(ui_path)
1618

1719

20+
class InputTypes(Enum):
21+
TEXT_VALUE = 1
22+
NUMERIC_VALUE = 2
23+
CODE_VALUE = 3
24+
25+
TEXT_INFO = 4
26+
NUMERIC_INFO = 5
27+
CODE_INFO = 6
28+
29+
REGULATION_NUMBER = 7
30+
RELATED_FILE = 8
31+
32+
1833
class NewPlanRegulationWidget(QWidget, FormClass): # type: ignore
1934
"""A widget representation of a plan regulation."""
2035

2136
delete_signal = pyqtSignal(QWidget)
2237

23-
def __init__(self, parent=None):
38+
def __init__(self, regulation_type: str, parent=None):
2439
super().__init__(parent)
2540
self.setupUi(self)
2641

2742
# TYPES
28-
self.plan_regulation_type: QComboBox
43+
self.plan_regulation_type: QLineEdit
44+
self.form_layout: QFormLayout
2945

3046
self.add_input_btn: QPushButton
3147
self.add_additional_information_btn: QPushButton
@@ -34,7 +50,8 @@ def __init__(self, parent=None):
3450
self.del_btn: QPushButton
3551

3652
# INIT
37-
self.init_plan_regulation_types()
53+
self.widgets: dict[InputTypes, list[QWidget]] = {}
54+
self.plan_regulation_type.setText(regulation_type)
3855
self.init_buttons()
3956

4057
def request_delete(self):
@@ -45,17 +62,53 @@ def init_buttons(self):
4562
self.del_btn.clicked.connect(self.request_delete)
4663

4764
input_menu = QMenu()
48-
input_menu.addAction("Teksti")
49-
input_menu.addAction("Numeerinen")
50-
input_menu.addAction("Koodi")
65+
input_menu.addAction("Teksti").triggered.connect(lambda: self.add_input_field(InputTypes.TEXT_VALUE))
66+
input_menu.addAction("Numeerinen").triggered.connect(lambda: self.add_input_field(InputTypes.NUMERIC_VALUE))
67+
input_menu.addAction("Koodi").triggered.connect(lambda: self.add_input_field(InputTypes.CODE_VALUE))
5168
self.add_input_btn.setMenu(input_menu)
5269

53-
add_additional_information_menu = QMenu()
54-
add_additional_information_menu.addAction("Teksti")
55-
add_additional_information_menu.addAction("Numeerinen")
56-
add_additional_information_menu.addAction("Koodi")
57-
self.add_additional_information_btn.setMenu(add_additional_information_menu)
58-
59-
def init_plan_regulation_types(self):
60-
# Get plan regulation type codes from DB
61-
pass
70+
additional_info_menu = QMenu()
71+
additional_info_menu.addAction("Teksti").triggered.connect(lambda: self.add_input_field(InputTypes.TEXT_INFO))
72+
additional_info_menu.addAction("Numeerinen").triggered.connect(
73+
lambda: self.add_input_field(InputTypes.NUMERIC_INFO)
74+
)
75+
additional_info_menu.addAction("Koodi").triggered.connect(lambda: self.add_input_field(InputTypes.CODE_INFO))
76+
self.add_additional_information_btn.setMenu(additional_info_menu)
77+
78+
self.add_regulation_number_btn.clicked.connect(lambda: self.add_input_field(InputTypes.REGULATION_NUMBER))
79+
self.add_file_btn.clicked.connect(lambda: self.add_input_field(InputTypes.RELATED_FILE))
80+
81+
def add_input_field(self, input_field_type: InputTypes):
82+
if input_field_type == InputTypes.TEXT_VALUE:
83+
widget = QTextEdit()
84+
self.form_layout.addRow("Tekstiarvo", widget)
85+
elif input_field_type == InputTypes.NUMERIC_VALUE:
86+
widget = QgsDoubleSpinBox() # Or QgsSpinBox?
87+
self.form_layout.addRow("Numeerinen arvo", widget)
88+
elif input_field_type == InputTypes.CODE_VALUE:
89+
widget = QComboBox()
90+
# TODO: widget.addItems()
91+
self.form_layout.addRow("Koodiarvo", widget)
92+
93+
elif input_field_type == InputTypes.TEXT_INFO:
94+
widget = QTextEdit()
95+
self.form_layout.addRow("Lisätieto", widget)
96+
elif input_field_type == InputTypes.NUMERIC_INFO:
97+
widget = QgsDoubleSpinBox()
98+
self.form_layout.addRow("Lisätieto", widget)
99+
elif input_field_type == InputTypes.CODE_INFO:
100+
widget = QComboBox()
101+
# TODO: widget.addItems()
102+
self.form_layout.addRow("Lisätieto", widget)
103+
104+
elif input_field_type == InputTypes.REGULATION_NUMBER:
105+
widget = QgsSpinBox()
106+
self.form_layout.addRow("Määräysnumero", widget)
107+
elif input_field_type == InputTypes.RELATED_FILE:
108+
widget = QgsFileWidget()
109+
self.form_layout.addRow("Liiteasiakirja", widget)
110+
111+
if self.widgets.get(input_field_type) is None:
112+
self.widgets[input_field_type] = [widget]
113+
else:
114+
self.widgets[input_field_type].append(widget)

arho_feature_template/gui/new_plan_regulation_widget.ui

+3-24
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>695</width>
9+
<width>694</width>
1010
<height>108</height>
1111
</rect>
1212
</property>
@@ -15,7 +15,7 @@
1515
</property>
1616
<layout class="QVBoxLayout" name="verticalLayout">
1717
<item>
18-
<layout class="QFormLayout" name="formLayout_7">
18+
<layout class="QFormLayout" name="form_layout">
1919
<item row="0" column="0">
2020
<widget class="QLabel" name="label_4">
2121
<property name="text">
@@ -26,28 +26,7 @@
2626
<item row="0" column="1">
2727
<layout class="QHBoxLayout" name="horizontalLayout">
2828
<item>
29-
<widget class="QComboBox" name="plan_regulation_type">
30-
<item>
31-
<property name="text">
32-
<string>Laji 1</string>
33-
</property>
34-
</item>
35-
<item>
36-
<property name="text">
37-
<string>Laji 2</string>
38-
</property>
39-
</item>
40-
<item>
41-
<property name="text">
42-
<string>Laji 3</string>
43-
</property>
44-
</item>
45-
<item>
46-
<property name="text">
47-
<string>Laji 4</string>
48-
</property>
49-
</item>
50-
</widget>
29+
<widget class="QLineEdit" name="plan_regulation_type"/>
5130
</item>
5231
<item>
5332
<widget class="QPushButton" name="del_btn">

0 commit comments

Comments
 (0)