From f5a4a96b133de5141b89d579ceb210d797eae028 Mon Sep 17 00:00:00 2001
From: Niko Aarnio <niko.aarnio@protonmail.com>
Date: Thu, 5 Dec 2024 12:23:41 +0200
Subject: [PATCH] fixes to serialize/save plan form

---
 arho_feature_template/gui/serialize_plan.py |  53 +++---
 arho_feature_template/gui/serialize_plan.ui | 184 ++++++++++++--------
 2 files changed, 132 insertions(+), 105 deletions(-)

diff --git a/arho_feature_template/gui/serialize_plan.py b/arho_feature_template/gui/serialize_plan.py
index dc8163d..6ed35b0 100644
--- a/arho_feature_template/gui/serialize_plan.py
+++ b/arho_feature_template/gui/serialize_plan.py
@@ -1,50 +1,39 @@
+from __future__ import annotations
+
 from importlib import resources
+from typing import TYPE_CHECKING
 
 from qgis.PyQt import uic
-from qgis.PyQt.QtWidgets import QDialog, QDialogButtonBox, QFileDialog, QLineEdit, QPushButton
+from qgis.PyQt.QtWidgets import QDialog, QDialogButtonBox
+
+if TYPE_CHECKING:
+    from qgis.gui import QgsFileWidget
+
 
 ui_path = resources.files(__package__) / "serialize_plan.ui"
 FormClass, _ = uic.loadUiType(ui_path)
 
 
 class SerializePlan(QDialog, FormClass):  # type: ignore
-    plan_outline_path_edit: QLineEdit
-    plan_path_edit: QLineEdit
-    plan_outline_select_button: QPushButton
-    plan_select_button: QPushButton
+    plan_outline_file: QgsFileWidget
+    plan_file: QgsFileWidget
+    button_box: QDialogButtonBox
 
     def __init__(self):
         super().__init__()
         self.setupUi(self)
 
-        self.plan_outline_select_button.clicked.connect(self.select_plan_outline_file)
-        self.plan_select_button.clicked.connect(self.select_plan_file)
-
-        # Disable Save button initially
-        self.buttonBox.button(QDialogButtonBox.Save).setEnabled(False)
-        self.buttonBox.accepted.connect(self.accept)
-        self.buttonBox.rejected.connect(self.reject)
-
-    def select_plan_outline_file(self):
-        file_path, _ = QFileDialog.getSaveFileName(
-            self, "Valitse kaavan ulkorajan tallennuspolku", "", "JSON Files (*.json)"
-        )
-        if file_path:
-            self.plan_outline_path_edit.setText(file_path)
-            self.check_inputs()
-
-    def select_plan_file(self):
-        file_path, _ = QFileDialog.getSaveFileName(self, "Valitse kaavan tallennuspolku", "", "JSON Files (*.json)")
-        if file_path:
-            self.plan_path_edit.setText(file_path)
-            self.check_inputs()
+        self.plan_outline_file.fileChanged.connect(self.check_inputs)
+        self.plan_file.fileChanged.connect(self.check_inputs)
+        self.setMaximumHeight(107)  # Lock height
+        self.check_inputs()
 
     def check_inputs(self):
-        # Enable Save button if both text fields have paths
-        if self.plan_outline_path_edit.text() and self.plan_path_edit.text():
-            self.buttonBox.button(QDialogButtonBox.Save).setEnabled(True)
+        """Enables ok/save button only if both file paths are defined."""
+        if self.plan_outline_file.filePath() and self.plan_file.filePath():
+            self.button_box.button(QDialogButtonBox.Ok).setEnabled(True)
         else:
-            self.buttonBox.button(QDialogButtonBox.Save).setEnabled(False)
+            self.button_box.button(QDialogButtonBox.Ok).setEnabled(False)
 
-    def get_paths(self):
-        return self.plan_outline_path_edit.text(), self.plan_path_edit.text()
+    def get_paths(self) -> tuple[str, str]:
+        return self.plan_outline_file.filePath(), self.plan_file.filePath()
diff --git a/arho_feature_template/gui/serialize_plan.ui b/arho_feature_template/gui/serialize_plan.ui
index 1f2cdba..3020dbc 100644
--- a/arho_feature_template/gui/serialize_plan.ui
+++ b/arho_feature_template/gui/serialize_plan.ui
@@ -6,84 +6,122 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>558</width>
-    <height>193</height>
+    <width>519</width>
+    <height>107</height>
    </rect>
   </property>
+  <property name="sizePolicy">
+   <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
+    <horstretch>0</horstretch>
+    <verstretch>0</verstretch>
+   </sizepolicy>
+  </property>
+  <property name="maximumSize">
+   <size>
+    <width>16777215</width>
+    <height>16777215</height>
+   </size>
+  </property>
   <property name="windowTitle">
    <string>Tallenna kaava JSON</string>
   </property>
-  <widget class="QDialogButtonBox" name="buttonBox">
-   <property name="geometry">
-    <rect>
-     <x>20</x>
-     <y>150</y>
-     <width>521</width>
-     <height>32</height>
-    </rect>
-   </property>
-   <property name="standardButtons">
-    <set>QDialogButtonBox::Cancel|QDialogButtonBox::Save</set>
-   </property>
-   <property name="centerButtons">
-    <bool>true</bool>
-   </property>
-  </widget>
-  <widget class="QWidget" name="layoutWidget">
-   <property name="geometry">
-    <rect>
-     <x>20</x>
-     <y>20</y>
-     <width>521</width>
-     <height>120</height>
-    </rect>
-   </property>
-   <layout class="QGridLayout" name="gridLayout">
-    <item row="0" column="0">
-     <widget class="QLabel" name="plan_outline_label">
-      <property name="text">
-       <string>Kaavan ulkorajan tallennuspolku:</string>
-      </property>
-     </widget>
-    </item>
-    <item row="0" column="1">
-     <widget class="QLineEdit" name="plan_outline_path_edit">
-      <property name="readOnly">
-       <bool>true</bool>
-      </property>
-     </widget>
-    </item>
-    <item row="0" column="2">
-     <widget class="QPushButton" name="plan_outline_select_button">
-      <property name="text">
-       <string>Valitse...</string>
-      </property>
-     </widget>
-    </item>
-    <item row="1" column="0">
-     <widget class="QLabel" name="plan_label">
-      <property name="text">
-       <string>Kaavan tallennuspolku:</string>
-      </property>
-     </widget>
-    </item>
-    <item row="1" column="1">
-     <widget class="QLineEdit" name="plan_path_edit">
-      <property name="readOnly">
-       <bool>true</bool>
-      </property>
-     </widget>
-    </item>
-    <item row="1" column="2">
-     <widget class="QPushButton" name="plan_select_button">
-      <property name="text">
-       <string>Valitse...</string>
-      </property>
-     </widget>
-    </item>
-   </layout>
-  </widget>
+  <layout class="QVBoxLayout" name="verticalLayout">
+   <item>
+    <layout class="QFormLayout" name="formLayout">
+     <item row="0" column="0">
+      <widget class="QLabel" name="plan_outline_label">
+       <property name="text">
+        <string>Kaavan ulkorajan tallennuspolku</string>
+       </property>
+      </widget>
+     </item>
+     <item row="0" column="1">
+      <widget class="QgsFileWidget" name="plan_outline_file">
+       <property name="dialogTitle">
+        <string>Valitse kaavan ulkorajan tallennuspolku</string>
+       </property>
+       <property name="filter">
+        <string>JSON Files (*.json)</string>
+       </property>
+       <property name="storageMode">
+        <enum>QgsFileWidget::SaveFile</enum>
+       </property>
+      </widget>
+     </item>
+     <item row="1" column="0">
+      <widget class="QLabel" name="plan_label">
+       <property name="text">
+        <string>Kaavan tallennuspolku</string>
+       </property>
+      </widget>
+     </item>
+     <item row="1" column="1">
+      <widget class="QgsFileWidget" name="plan_file">
+       <property name="dialogTitle">
+        <string>Valitse kaavan tallennuspolku</string>
+       </property>
+       <property name="filter">
+        <string>JSON Files (*.json)</string>
+       </property>
+       <property name="storageMode">
+        <enum>QgsFileWidget::SaveFile</enum>
+       </property>
+      </widget>
+     </item>
+    </layout>
+   </item>
+   <item>
+    <widget class="QDialogButtonBox" name="button_box">
+     <property name="standardButtons">
+      <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
+     </property>
+     <property name="centerButtons">
+      <bool>false</bool>
+     </property>
+    </widget>
+   </item>
+  </layout>
  </widget>
+ <customwidgets>
+  <customwidget>
+   <class>QgsFileWidget</class>
+   <extends>QWidget</extends>
+   <header>qgsfilewidget.h</header>
+  </customwidget>
+ </customwidgets>
  <resources/>
- <connections/>
+ <connections>
+  <connection>
+   <sender>button_box</sender>
+   <signal>accepted()</signal>
+   <receiver>Dialog</receiver>
+   <slot>accept()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>247</x>
+     <y>85</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>247</x>
+     <y>53</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>button_box</sender>
+   <signal>rejected()</signal>
+   <receiver>Dialog</receiver>
+   <slot>reject()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>247</x>
+     <y>85</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>247</x>
+     <y>53</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
 </ui>