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

Lomakkeen tuottaminen templaatista #38

Merged
merged 5 commits into from
Oct 30, 2024
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
11 changes: 3 additions & 8 deletions arho_feature_template/core/feature_template_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
TemplateSyntaxError,
parse_template_library_config,
)
from arho_feature_template.gui.feature_attribute_form import FeatureAttributeForm
from arho_feature_template.gui.template_attribute_form import TemplateAttributeForm
from arho_feature_template.gui.template_dock import TemplateLibraryDock
from arho_feature_template.resources.template_libraries import library_config_files

Expand Down Expand Up @@ -181,17 +181,12 @@ def ask_for_feature_attributes(self, feature: QgsFeature) -> None:
if not self.active_template:
return

attribute_form = FeatureAttributeForm(self.active_template.config.feature)
attribute_form = TemplateAttributeForm(self.active_template.config)

if attribute_form.exec_():
layer = get_layer_from_project(self.active_template.config.feature.layer)
# Save the feature
for attributes in attribute_form.attribute_widgets.values():
for attribute, widget in attributes.items():
feature.setAttribute(
attribute,
widget.text(),
)
attribute_form.set_feature_attributes(feature)

layer.beginEditCommand("Create feature from template")
layer.addFeature(feature)
Expand Down
10 changes: 9 additions & 1 deletion arho_feature_template/core/template_library_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,18 @@ class Attribute:

attribute: str
default: str | None
description: str | None

@classmethod
def from_dict(cls, data: dict) -> Attribute:
return cls(attribute=data["attribute"], default=data.get("default"))
return cls(attribute=data["attribute"], default=data.get("default"), description=data.get("description"))

def display(self) -> str:
if self.description is not None:
return self.description
if self.default is not None:
return self.default
return ""


def parse_template_library_config(template_library_config: Path) -> TemplateLibraryConfig:
Expand Down
64 changes: 64 additions & 0 deletions arho_feature_template/gui/plan_regulation_group_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from __future__ import annotations

from importlib import resources
from typing import TYPE_CHECKING

from qgis.core import QgsApplication
from qgis.PyQt import uic
from qgis.PyQt.QtCore import pyqtSignal
from qgis.PyQt.QtWidgets import QWidget

from arho_feature_template.gui.plan_regulation_widget import PlanRegulationWidget

if TYPE_CHECKING:
from qgis.PyQt.QtWidgets import QFrame, QLineEdit, QPushButton

from arho_feature_template.core.template_library_config import Feature

ui_path = resources.files(__package__) / "plan_regulation_group_widget.ui"
FormClass, _ = uic.loadUiType(ui_path)


class PlanRegulationGroupWidget(QWidget, FormClass): # type: ignore
"""A widget representation of a plan regulation group."""

delete_signal = pyqtSignal(QWidget)

def __init__(self, feature: Feature):
super().__init__()
self.setupUi(self)

# TYPES
self.frame: QFrame
self.heading: QLineEdit
self.del_btn: QPushButton
Comment on lines +22 to +34
Copy link
Contributor

Choose a reason for hiding this comment

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

My understanding is that its a common practice to define the types in the class section as following:

Suggested change
class PlanRegulationGroupWidget(QWidget, FormClass): # type: ignore
"""A widget representation of a plan regulation group."""
delete_signal = pyqtSignal(QWidget)
def __init__(self, feature: Feature):
super().__init__()
self.setupUi(self)
# TYPES
self.frame: QFrame
self.heading: QLineEdit
self.del_btn: QPushButton
class PlanRegulationGroupWidget(QWidget, FormClass): # type: ignore
"""A widget representation of a plan regulation group."""
# TYPES
frame: QFrame
heading: QLineEdit
del_btn: QPushButton
delete_signal = pyqtSignal(QWidget)
def __init__(self, feature: Feature):
super().__init__()
self.setupUi(self)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I used to do it like this too, until at some point VSCode stopped recognizing this sort of typing for me... If it is an editor bug, I will look into it more


# INIT
self.feature = feature
self.layer = self.feature.layer # Should be plan_regulation_group layer

self.init_buttons()
self.set_group_heading()
self.add_plan_regulation_widgets()

def request_delete(self):
self.delete_signal.emit(self)

def init_buttons(self):
self.del_btn.setIcon(QgsApplication.getThemeIcon("mActionDeleteSelected.svg"))
self.del_btn.clicked.connect(self.request_delete)

def set_group_heading(self):
for attribute_config in self.feature.attributes:
if attribute_config.attribute == "name":
self.heading.setText(attribute_config.display())

def add_plan_regulation_widgets(self):
if self.feature.child_features is not None:
for child in self.feature.child_features:
if child.layer == "plan_requlation":
self.add_plan_regulation_widget(child)

def add_plan_regulation_widget(self, plan_regulation_feature: Feature):
plan_regulation_widget = PlanRegulationWidget(plan_regulation_feature)
self.frame.layout().addWidget(plan_regulation_widget)
81 changes: 81 additions & 0 deletions arho_feature_template/gui/plan_regulation_group_widget.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>plan_regulation_group_form</class>
<widget class="QWidget" name="plan_regulation_group_form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>514</width>
<height>65</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QFrame" name="frame">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="heading">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="del_btn">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>30</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
196 changes: 196 additions & 0 deletions arho_feature_template/gui/plan_regulation_group_widget_old.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>plan_regulation_group_form</class>
<widget class="QWidget" name="plan_regulation_group_form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>582</width>
<height>195</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="plan_regulation_group_heading_layout">
<item>
<widget class="QLabel" name="heading_label">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Otsikko:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="heading">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="conf_btn">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>30</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="del_btn">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>30</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<spacer name="plan_regulation_group_heading_spacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Maximum</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="plan_regulation_group_contents_layout">
<item>
<spacer name="plan_regulation_groupbox_spacer1">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QgsCollapsibleGroupBox" name="plan_regulation_groupbox">
<property name="title">
<string/>
</property>
<property name="flat">
<bool>false</bool>
</property>
<property name="checkable">
<bool>false</bool>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<layout class="QGridLayout" name="plan_regulation_grid_layout">
<item row="0" column="0">
<widget class="QLabel" name="plan_regulation_label">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>175</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>175</width>
<height>16777215</height>
</size>
</property>
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Kaavamääräyslaji</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="plan_regulation_groupbox_spacer2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsCollapsibleGroupBox</class>
<extends>QGroupBox</extends>
<header>qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
Loading
Loading