From 1884dd0d4591245781bb98c3a95f78c0c256c437 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Carrillo?= Date: Thu, 11 Apr 2024 14:58:38 +0200 Subject: [PATCH] Add tabWidget; check if config file is found or not; multiple validations; refresh source comboBox once a new service has been created (cloned) --- pg_service_parser/gui/dlg_pg_service.py | 82 +++++- pg_service_parser/imgs/warning.svg | 1 + pg_service_parser/pg_service_parser_plugin.py | 1 - .../pg_service_parser_wrapper.py | 6 + pg_service_parser/ui/pg_service_dialog.ui | 273 +++++++++++++----- 5 files changed, 285 insertions(+), 78 deletions(-) create mode 100644 pg_service_parser/imgs/warning.svg diff --git a/pg_service_parser/gui/dlg_pg_service.py b/pg_service_parser/gui/dlg_pg_service.py index d5e7cb4..7395522 100644 --- a/pg_service_parser/gui/dlg_pg_service.py +++ b/pg_service_parser/gui/dlg_pg_service.py @@ -1,11 +1,17 @@ -from qgis.PyQt.QtCore import pyqtSlot -from qgis.PyQt.QtWidgets import QDialog +from qgis.PyQt.QtCore import (Qt, + pyqtSlot) +from qgis.PyQt.QtWidgets import (QDialog, + QSizePolicy) +from qgis.gui import QgsMessageBar -from pg_service_parser.pg_service_parser_wrapper import (copy_service_settings, +from pg_service_parser.pg_service_parser_wrapper import (conf_path, + copy_service_settings, service_names) from pg_service_parser.utils import get_ui_class DIALOG_UI = get_ui_class('pg_service_dialog.ui') +COPY_TAB_INDEX = 0 +EDIT_TAB_INDEX = 1 class PgServiceDialog(QDialog, DIALOG_UI): @@ -14,21 +20,77 @@ def __init__(self, parent): QDialog.__init__(self, parent) self.setupUi(self) - self.cboSourceService.addItems(service_names()) - self.cboTargetService.addItems(service_names()) + conf_file_path = conf_path() + if not conf_file_path: + self.lblConfFile.setText("Config file not found!") + self.lblConfFile.setToolTip("Set your PGSERVICEFILE environment variable and reopen the dialog.") + self.txtConfFile.setVisible(False) + self.tabWidget.setEnabled(False) + return + + self.txtConfFile.setText(conf_file_path) + self.lblWarning.setVisible(False) + self.tabWidget.setTabEnabled(EDIT_TAB_INDEX, False) # Not yet implemented self.radOverwrite.toggled.connect(self.__update_target_controls) - self.buttonBox.accepted.connect(self.__accepted) + self.btnCopyService.clicked.connect(self.__copy_service) + self.cboSourceService.currentIndexChanged.connect(self.__source_service_changed) + self.__initialize_copy_services() self.__update_target_controls(True) + self.bar = QgsMessageBar() + self.bar.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed) + self.layout().insertWidget(0, self.bar) + @pyqtSlot(bool) def __update_target_controls(self, checked): self.cboTargetService.setEnabled(self.radOverwrite.isChecked()) self.txtNewService.setEnabled(not self.radOverwrite.isChecked()) - def __accepted(self): - target_service = self.cboTargetService.currentText() if self.radOverwrite.isChecked() else self.txtNewService.text() + @pyqtSlot(int) + def __source_service_changed(self, index): + # Remember latest currentText only if current item in source + # is different from current item in target + current_text = self.cboTargetService.currentText() + current_text = current_text if self.cboSourceService.currentText() != current_text else "" + + self.cboTargetService.clear() + self.cboTargetService.addItems([""] + service_names()) + + model = self.cboTargetService.model() + item = model.item(index + 1) # Account for the first (empty) item + item.setFlags(item.flags() & ~Qt.ItemIsEnabled) # Disable mirror item + + self.cboTargetService.setCurrentText(current_text) + + def __initialize_copy_services(self): + current_text = self.cboSourceService.currentText() # Remember latest currentText + self.cboSourceService.blockSignals(True) # Avoid triggering custom slot while clearing + self.cboSourceService.clear() + self.cboSourceService.blockSignals(False) + self.cboSourceService.addItems(service_names()) + self.cboSourceService.setCurrentText(current_text) + + def __copy_service(self): + # Validations + if self.radCreate.isChecked(): + if not self.txtNewService.text().strip(): + self.bar.pushInfo("PG service", "Enter a service name and try again.") + return + elif self.txtNewService.text().strip() in service_names(): + self.bar.pushWarning("PG service", "Service name already exists! Change it and try again.") + return + elif self.radOverwrite.isChecked(): + if not self.cboTargetService.currentText(): + self.bar.pushInfo("PG service", "Select a valid target service and try again.") + return + + target_service = self.cboTargetService.currentText() if self.radOverwrite.isChecked() else self.txtNewService.text().strip() - res = copy_service_settings(self.cboSourceService.currentText(), target_service) - print(res) + if copy_service_settings(self.cboSourceService.currentText(), target_service): + self.bar.pushSuccess("PG service", f"PG service copied to '{target_service}'!") + if self.radCreate.isChecked(): + self.__initialize_copy_services() # Reflect the newly added service + else: + self.bar.pushWarning("PG service", "There was a problem copying the service!") diff --git a/pg_service_parser/imgs/warning.svg b/pg_service_parser/imgs/warning.svg new file mode 100644 index 0000000..c6b8c23 --- /dev/null +++ b/pg_service_parser/imgs/warning.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/pg_service_parser/pg_service_parser_plugin.py b/pg_service_parser/pg_service_parser_plugin.py index 165708c..df0d78f 100644 --- a/pg_service_parser/pg_service_parser_plugin.py +++ b/pg_service_parser/pg_service_parser_plugin.py @@ -18,6 +18,5 @@ def unload(self): del self.action def run(self): - print("Eureka!") dlg = PgServiceDialog(self.iface.mainWindow()) dlg.exec_() diff --git a/pg_service_parser/pg_service_parser_wrapper.py b/pg_service_parser/pg_service_parser_wrapper.py index 4a80dae..5b12bc7 100644 --- a/pg_service_parser/pg_service_parser_wrapper.py +++ b/pg_service_parser/pg_service_parser_wrapper.py @@ -1,8 +1,14 @@ +import os.path + import pgserviceparser from typing import (List, Optional) +def conf_path() -> str: + path = pgserviceparser.conf_path() + return path if os.path.exists(path) else None + def service_names(conf_file_path: Optional[str] = None) -> List[str]: return pgserviceparser.service_names(conf_file_path) diff --git a/pg_service_parser/ui/pg_service_dialog.ui b/pg_service_parser/ui/pg_service_dialog.ui index a812943..83f617a 100644 --- a/pg_service_parser/ui/pg_service_dialog.ui +++ b/pg_service_parser/ui/pg_service_dialog.ui @@ -6,102 +6,241 @@ 0 0 - 489 - 208 + 497 + 316 - Dialog + PG service parser - - - Qt::Vertical - - - QSizePolicy::MinimumExpanding - - - - 20 - 40 - - - - - - - - QLayout::SetDefaultConstraint - - - + + + + + true + - + 0 0 - - false + + + 24 + 24 + + + + Set your PGSERVICEFILE environment variable and reopen the dialog. - - - - - Overwrite + + + + ../imgs/warning.svg - + true - - - - Create new + + + + - - - - - - - 0 - 0 - + + + + + Config file path found at - - - - New service name - - - false + + + + true - - - Qt::Vertical - - - QSizePolicy::MinimumExpanding + + + true - - - 20 - 40 - + + 0 - + + + Copy + + + + + + QLayout::SetDefaultConstraint + + + + + + 0 + 0 + + + + false + + + + + + + Overwrite + + + true + + + + + + + Create new + + + + + + + + 0 + 0 + + + + + + + + New service name + + + false + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Copy service + + + + + + + + + + Edit + + + + + + + + Edit service + + + + + + + + 0 + 0 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + true + + + 0 + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Update service + + + + + + + + @@ -109,7 +248,7 @@ Qt::Horizontal - QDialogButtonBox::Cancel|QDialogButtonBox::Ok + QDialogButtonBox::Close