Skip to content

Commit

Permalink
Add tabWidget; check if config file is found or not; multiple validat…
Browse files Browse the repository at this point in the history
…ions; refresh source comboBox once a new service has been created (cloned)
  • Loading branch information
gacarrillor committed Apr 12, 2024
1 parent 5715869 commit 1884dd0
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 78 deletions.
82 changes: 72 additions & 10 deletions pg_service_parser/gui/dlg_pg_service.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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!")
1 change: 1 addition & 0 deletions pg_service_parser/imgs/warning.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion pg_service_parser/pg_service_parser_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ def unload(self):
del self.action

def run(self):
print("Eureka!")
dlg = PgServiceDialog(self.iface.mainWindow())
dlg.exec_()
6 changes: 6 additions & 0 deletions pg_service_parser/pg_service_parser_wrapper.py
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
Loading

0 comments on commit 1884dd0

Please sign in to comment.