Skip to content

Commit

Permalink
Merge pull request #854 from opengisch/updateSchemasTask
Browse files Browse the repository at this point in the history
Update schemas in separate task to avoid freezes
  • Loading branch information
signedav authored Dec 6, 2023
2 parents 95b31fd + 701d905 commit 0b554db
Showing 1 changed file with 52 additions and 13 deletions.
65 changes: 52 additions & 13 deletions QgisModelBaker/gui/panel/pg_config_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -109,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():
Expand Down Expand Up @@ -171,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()
Expand Down Expand Up @@ -348,7 +363,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
)
Expand Down Expand Up @@ -459,27 +474,24 @@ 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()

def _fill_schema_combo_box(self):

configuration = Ili2DbCommandConfiguration()
if self._read_pg_schemas_task.isRunning():
self._fill_schema_combo_box_timer.start()
return

mode = DbIliMode.pg
configuration = Ili2DbCommandConfiguration()
self.get_fields(configuration)
configuration.tool = DbIliMode.pg

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"

Expand All @@ -497,3 +509,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 = []

0 comments on commit 0b554db

Please sign in to comment.