-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from opengisch/connections
Add Connections tab to list and manage (add/edit/remove) connections to a particular service
- Loading branch information
Showing
9 changed files
with
411 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from qgis.core import QgsAbstractDatabaseProviderConnection | ||
from qgis.PyQt.QtCore import QAbstractTableModel, QModelIndex, Qt | ||
from qgis.PyQt.QtGui import QFont | ||
|
||
|
||
class ServiceConnectionModel(QAbstractTableModel): | ||
KEY_COL = 0 | ||
VALUE_COL = 1 | ||
|
||
def __init__( | ||
self, service_name: str, connections: dict[str, QgsAbstractDatabaseProviderConnection] | ||
) -> None: | ||
super().__init__() | ||
self.__service_name = service_name | ||
self.__model_data = connections | ||
|
||
def rowCount(self, parent=QModelIndex()): | ||
return len(self.__model_data) | ||
|
||
def columnCount(self, parent=QModelIndex()): | ||
return 2 | ||
|
||
def index_to_connection_key(self, index): | ||
return list(self.__model_data.keys())[index.row()] | ||
|
||
def data(self, index, role=Qt.ItemDataRole.DisplayRole): | ||
if not index.isValid(): | ||
return None | ||
|
||
key = list(self.__model_data.keys())[index.row()] | ||
if role == Qt.ItemDataRole.DisplayRole: | ||
if index.column() == self.KEY_COL: | ||
return key | ||
elif index.column() == self.VALUE_COL: | ||
return self.__model_data[key].uri() | ||
elif role == Qt.ItemDataRole.FontRole: | ||
if index.column() == self.KEY_COL: | ||
font = QFont() | ||
font.setBold(True) | ||
return font | ||
elif index.column() == self.VALUE_COL: | ||
font = QFont() | ||
font.setItalic(True) | ||
return font | ||
elif role == Qt.ItemDataRole.ToolTipRole: | ||
if index.column() == self.VALUE_COL: | ||
return self.__model_data[key].uri() | ||
|
||
return None | ||
|
||
def headerData(self, section, orientation, role): | ||
if orientation == Qt.Orientation.Horizontal and role == Qt.ItemDataRole.DisplayRole: | ||
if section == self.KEY_COL: | ||
return "Connection name" | ||
elif section == self.VALUE_COL: | ||
return "URI" | ||
|
||
return QAbstractTableModel.headerData(self, section, orientation, role) | ||
|
||
def flags(self, idx): | ||
if not idx.isValid(): | ||
return ~Qt.ItemFlag.ItemIsSelectable & ~Qt.ItemFlag.ItemIsEnabled | ||
|
||
return Qt.ItemFlag.ItemIsSelectable | Qt.ItemFlag.ItemIsEnabled | ||
|
||
def service_name(self): | ||
return self.__service_name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from qgis.core import ( | ||
QgsAbstractDatabaseProviderConnection, | ||
QgsDataSourceUri, | ||
QgsProviderRegistry, | ||
) | ||
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]: | ||
res = {} | ||
provider = QgsProviderRegistry.instance().providerMetadata("postgres") | ||
conns = provider.connections() | ||
for key, pg_conn in conns.items(): | ||
if QgsDataSourceUri(pg_conn.uri()).service() == service: | ||
res[key] = pg_conn | ||
|
||
return res | ||
|
||
|
||
def create_connection(service: str, connection_name: str) -> None: | ||
config = {} | ||
uri = f"service='{service}'" | ||
provider = QgsProviderRegistry.instance().providerMetadata("postgres") | ||
conn = provider.createConnection(uri, config) | ||
provider.saveConnection(conn, connection_name) | ||
|
||
|
||
def remove_connection(connection_name: str) -> None: | ||
provider = QgsProviderRegistry.instance().providerMetadata("postgres") | ||
provider.deleteConnection(connection_name) | ||
|
||
|
||
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") | ||
widget = pg.createDataSourceWidget( | ||
parent, widgetMode=QgsProviderRegistry.WidgetMode.Standalone | ||
) | ||
|
||
settings = QSettings() | ||
settings.setValue("PostgreSQL/connections/selected", connection_name) | ||
|
||
widget.refresh() # To reflect the newly selected connection | ||
widget.btnEdit_clicked() |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from enum import Enum | ||
|
||
from qgis.PyQt.QtCore import pyqtSlot | ||
from qgis.PyQt.QtWidgets import QDialog, QWidget | ||
|
||
from pg_service_parser.utils import get_ui_class | ||
|
||
DIALOG_UI = get_ui_class("new_name_dialog.ui") | ||
|
||
|
||
class EnumNewName(Enum): | ||
SERVICE = 0 | ||
CONNECTION = 1 | ||
|
||
|
||
class NewNameDialog(QDialog, DIALOG_UI): | ||
|
||
def __init__(self, mode: EnumNewName, parent: QWidget, data: str = "") -> None: | ||
QDialog.__init__(self, parent) | ||
self.setupUi(self) | ||
self.__mode = mode | ||
|
||
self.buttonBox.accepted.connect(self.__accepted) | ||
|
||
if self.__mode == EnumNewName.SERVICE: | ||
self.setWindowTitle("Service name") | ||
self.label.setText("Enter a service name") | ||
self.txtNewName.setPlaceholderText("e.g., my-service") | ||
self.new_name = "my-service" | ||
elif self.__mode == EnumNewName.CONNECTION: | ||
self.setWindowTitle("Connection name") | ||
self.label.setText("Enter a connection name") | ||
self.txtNewName.setPlaceholderText("e.g., My Service Connection") | ||
self.new_name = f"{data} connection" | ||
|
||
@pyqtSlot() | ||
def __accepted(self): | ||
if self.txtNewName.text().strip(): | ||
if self.__mode == EnumNewName.SERVICE: | ||
self.new_name = self.txtNewName.text().strip().replace(" ", "-") | ||
elif self.__mode == EnumNewName.CONNECTION: | ||
self.new_name = self.txtNewName.text().strip() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.