Skip to content

Commit

Permalink
Update schemas in separate task to avoid freezes
Browse files Browse the repository at this point in the history
  • Loading branch information
domi4484 committed Nov 30, 2023
1 parent 50d6270 commit e3e2233
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 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 @@ -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
)
Expand Down Expand Up @@ -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
Expand All @@ -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"

Expand All @@ -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 = []

0 comments on commit e3e2233

Please sign in to comment.