From e3e2233893b8cfca3093aaac3d16c9e20a807ae8 Mon Sep 17 00:00:00 2001 From: Damiano Lombardi Date: Thu, 30 Nov 2023 10:04:12 +0100 Subject: [PATCH 1/3] Update schemas in separate task to avoid freezes --- QgisModelBaker/gui/panel/pg_config_panel.py | 46 +++++++++++++++++---- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/QgisModelBaker/gui/panel/pg_config_panel.py b/QgisModelBaker/gui/panel/pg_config_panel.py index 6f8a63590..3d9fbda1f 100644 --- a/QgisModelBaker/gui/panel/pg_config_panel.py +++ b/QgisModelBaker/gui/panel/pg_config_panel.py @@ -19,7 +19,7 @@ import logging from enum import IntEnum -from qgis.PyQt.QtCore import Qt, QTimer +from qgis.PyQt.QtCore import Qt, QThread, QTimer import QgisModelBaker.libs.modelbaker.libs.pgserviceparser as pgserviceparser import QgisModelBaker.libs.modelbaker.utils.db_utils as db_utils @@ -69,6 +69,9 @@ def __init__(self, parent, db_action_type): self._fill_schema_combo_box_timer.setSingleShot(True) self._fill_schema_combo_box_timer.timeout.connect(self._fill_schema_combo_box) + self._read_pg_schemas_task = ReadPgSchemasTask(self) + self._read_pg_schemas_task.finished.connect(self._read_pg_schemas_task_finished) + from QgisModelBaker.libs.modelbaker.iliwrapper.ili2dbconfig import ( BaseConfiguration, ) @@ -348,7 +351,7 @@ def _pg_service_combo_box_changed(self): index, PgConfigPanel._SERVICE_COMBOBOX_ROLE.DATABASE ) ) - self.pg_schema_combo_box.setText( + self.pg_schema_combo_box.setCurrentText( self.pg_service_combo_box.itemData( index, PgConfigPanel._SERVICE_COMBOBOX_ROLE.DBSCHEMA ) @@ -466,6 +469,10 @@ def _fields_modified(self): def _fill_schema_combo_box(self): + if self._read_pg_schemas_task.isRunning(): + self._fill_schema_combo_box_timer.start() + return + configuration = Ili2DbCommandConfiguration() mode = DbIliMode.pg @@ -474,12 +481,10 @@ def _fill_schema_combo_box(self): configuration.tool = mode configuration.db_ili_version = db_utils.db_ili_version(configuration) - db_connector = db_utils.get_db_connector(configuration) - if not db_connector: - logging.warning("Refresh schema list connection error") - return + self._read_pg_schemas_task.configuration_changed(configuration) - schemas = db_connector.get_schemas() + def _read_pg_schemas_task_finished(self): + schemas = self._read_pg_schemas_task.schemas AUTO_ADDED_SCHEMA = "auto_added_schema" @@ -497,3 +502,30 @@ def _fill_schema_combo_box(self): currentTextIndex = self.pg_schema_combo_box.findText(currentText) if currentTextIndex > -1: self.pg_schema_combo_box.setCurrentIndex(currentTextIndex) + + +class ReadPgSchemasTask(QThread): + def __init__(self, parent): + super().__init__(parent) + + self.schemas = [] + self._configuration = None + + def configuration_changed(self, configuration): + self._configuration = configuration + self.start() + + def run(self): + + try: + db_connector = db_utils.get_db_connector(self._configuration) + if not db_connector: + logging.warning("Refresh schema list connection error") + self.schemas = [] + return + + self.schemas = db_connector.get_schemas() + + except Exception as exception: + logging.warning(f"Refresh schema list error: {exception}") + self.schemas = [] From 1c0100d3dffa00f209aa45d0c3d0f78d1792c7dd Mon Sep 17 00:00:00 2001 From: Damiano Lombardi Date: Fri, 1 Dec 2023 14:40:49 +0100 Subject: [PATCH 2/3] Update schemas combo on user/password changes --- QgisModelBaker/gui/panel/pg_config_panel.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/QgisModelBaker/gui/panel/pg_config_panel.py b/QgisModelBaker/gui/panel/pg_config_panel.py index 3d9fbda1f..6253e86e0 100644 --- a/QgisModelBaker/gui/panel/pg_config_panel.py +++ b/QgisModelBaker/gui/panel/pg_config_panel.py @@ -112,6 +112,10 @@ def __init__(self, parent, db_action_type): self.pg_database_line_edit.textChanged.connect(self._fields_modified) self.pg_schema_combo_box.currentTextChanged.connect(self.notify_fields_modified) + self.pg_auth_settings.usernameChanged.connect(self._fields_modified) + self.pg_auth_settings.passwordChanged.connect(self._fields_modified) + self.pg_auth_settings.configIdChanged.connect(self._fields_modified) + # Fill pg_services combo box self.pg_service_combo_box.addItem(self.tr("None"), None) for service in pgserviceparser.service_names(): @@ -174,6 +178,14 @@ def __init__(self, parent, db_action_type): self._show_panel() + def __del__(self): + # Make sure the refresh schemas task is finished to avoid crashes + try: + if self._read_pg_schemas_task: + self._read_pg_schemas_task.wait() + except RuntimeError: + pass + def _show_panel(self): self._fill_schema_combo_box() @@ -462,7 +474,6 @@ def _keep_custom_settings(self): ) def _fields_modified(self): - self._fill_schema_combo_box_timer.start(self.REFRESH_SCHEMAS_TIMEOUT_MS) self.notify_fields_modified.emit() From 701d9056f99ea057383526b4d6e93273db585d63 Mon Sep 17 00:00:00 2001 From: Damiano Lombardi Date: Tue, 5 Dec 2023 19:02:08 +0100 Subject: [PATCH 3/3] Don't execute queries outside the thread --- QgisModelBaker/gui/panel/pg_config_panel.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/QgisModelBaker/gui/panel/pg_config_panel.py b/QgisModelBaker/gui/panel/pg_config_panel.py index 6253e86e0..131126fd8 100644 --- a/QgisModelBaker/gui/panel/pg_config_panel.py +++ b/QgisModelBaker/gui/panel/pg_config_panel.py @@ -485,12 +485,8 @@ def _fill_schema_combo_box(self): return configuration = Ili2DbCommandConfiguration() - - mode = DbIliMode.pg self.get_fields(configuration) - - configuration.tool = mode - configuration.db_ili_version = db_utils.db_ili_version(configuration) + configuration.tool = DbIliMode.pg self._read_pg_schemas_task.configuration_changed(configuration)