Skip to content

Commit

Permalink
[connections] Code cleanup, pass parent and widget mode to edit conne…
Browse files Browse the repository at this point in the history
…ction dialog
  • Loading branch information
gacarrillor committed Oct 4, 2024
1 parent a0468a3 commit 1c71366
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
13 changes: 7 additions & 6 deletions pg_service_parser/core/service_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
)
from qgis.gui import QgsGui
from qgis.PyQt.QtCore import QSettings
from qgis.PyQt.QtWidgets import QWidget


def get_connections(service: str) -> dict[str, QgsAbstractDatabaseProviderConnection]:
Expand All @@ -24,24 +25,24 @@ def create_connection(service: str, connection_name: str) -> None:
provider = QgsProviderRegistry.instance().providerMetadata("postgres")
conn = provider.createConnection(uri, config)
provider.saveConnection(conn, connection_name)
# conn.store(name)


def remove_connection(connection_name: str) -> None:
provider = QgsProviderRegistry.instance().providerMetadata("postgres")
provider.deleteConnection(connection_name)


def edit_connection(connection_name: str) -> None:
def edit_connection(connection_name: str, parent: QWidget) -> None:
provider = QgsProviderRegistry.instance().providerMetadata("postgres")

if connection_name in provider.dbConnections():
pg = QgsGui.sourceSelectProviderRegistry().providerByName("postgres")
w = pg.createDataSourceWidget()
widget = pg.createDataSourceWidget(
parent, widgetMode=QgsProviderRegistry.WidgetMode.Standalone
)

settings = QSettings()
settings.value("PostgreSQL/connections/selected")
settings.setValue("PostgreSQL/connections/selected", connection_name)

w.refresh() # To reflect the newly selected connection
w.btnEdit_clicked()
widget.refresh() # To reflect the newly selected connection
widget.btnEdit_clicked()
2 changes: 1 addition & 1 deletion pg_service_parser/gui/dlg_pg_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def __edit_double_clicked_connection(self, index):

def __edit_connection(self, index):
connection_name = self.__connection_model.index_to_connection_key(index)
edit_connection(connection_name)
edit_connection(connection_name, self)
self.__initialize_service_connections(index)

@pyqtSlot()
Expand Down
2 changes: 1 addition & 1 deletion pg_service_parser/gui/item_delegates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtWidgets import QComboBox, QStyledItemDelegate

from pg_service_parser.core.item_models import ServiceConfigModel
from pg_service_parser.core.setting_model import ServiceConfigModel


class ServiceConfigDelegate(QStyledItemDelegate):
Expand Down

0 comments on commit 1c71366

Please sign in to comment.