Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ux] Adjust settings dialog #43

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions pg_service_parser/conf/service_settings.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
# Settings available for manual addition
# See https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
SERVICE_SETTINGS = {
"host": "localhost",
"port": "5432",
"dbname": "test",
"user": "",
"password": "",
"passfile": "",
"host": {"default": "localhost", "description": "Name of host to connect to."},
"port": {"default": "5432", "description": "Port number to connect to at the server host."},
"dbname": {"default": "test", "description": "The database name."},
"user": {"default": "", "description": "PostgreSQL user name to connect as."},
"password": {
"default": "",
"description": "Password to be used if the server demands password authentication.",
},
"sslmode": {
"default": "",
"description": "This option determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the server.",
},
"passfile": {
"default": "",
"description": "Specifies the name of the file used to store passwords.",
},
}

# Settings to initialize new files
Expand Down
12 changes: 11 additions & 1 deletion pg_service_parser/core/item_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from qgis.PyQt.QtCore import QAbstractTableModel, QModelIndex, Qt, pyqtSignal
from qgis.PyQt.QtGui import QColorConstants, QFont

from pg_service_parser.conf.service_settings import SERVICE_SETTINGS


class ServiceConfigModel(QAbstractTableModel):
KEY_COL = 0
Expand All @@ -13,6 +15,7 @@ def __init__(self, service_name: str, service_config: dict):
self.__service_name = service_name
self.__model_data = service_config
self.__original_data = service_config.copy()
self.__settings_data = SERVICE_SETTINGS # Read-only dict with further info about settings
self.__dirty = False

def rowCount(self, parent=QModelIndex()):
Expand All @@ -24,7 +27,7 @@ def columnCount(self, parent=QModelIndex()):
def index_to_setting_key(self, index):
return list(self.__model_data.keys())[index.row()]

def add_settings(self, settings: dict):
def add_settings(self, settings: dict[str, str]):
self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount() + len(settings) - 1)
self.__model_data.update(settings)
self.__set_dirty_status(True)
Expand Down Expand Up @@ -55,6 +58,13 @@ def data(self, index, role=Qt.ItemDataRole.DisplayRole):
return key
elif index.column() == self.VALUE_COL:
return self.__model_data[key]
elif role == Qt.ItemDataRole.ToolTipRole:
if index.column() == self.KEY_COL:
return (
self.__settings_data[key].get("description", None)
if key in self.__settings_data
else None
)
elif role == Qt.ItemDataRole.EditRole and index.column() == self.VALUE_COL:
return self.__model_data[key]
elif role == Qt.ItemDataRole.FontRole:
Expand Down
4 changes: 3 additions & 1 deletion pg_service_parser/gui/dlg_pg_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ def __add_settings_clicked(self):
dlg.exec()

if dlg.settings_to_add:
settings = {k: v for k, v in SERVICE_SETTINGS.items() if k in dlg.settings_to_add}
settings = {
k: v["default"] for k, v in SERVICE_SETTINGS.items() if k in dlg.settings_to_add
}
self.__edit_model.add_settings(settings)

@pyqtSlot()
Expand Down
27 changes: 22 additions & 5 deletions pg_service_parser/gui/dlg_service_settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from qgis.PyQt.QtCore import pyqtSlot
from qgis.PyQt.QtWidgets import QDialog
from qgis.PyQt.QtCore import Qt, pyqtSlot
from qgis.PyQt.QtWidgets import QDialog, QDialogButtonBox, QListWidgetItem

from pg_service_parser.conf.service_settings import SERVICE_SETTINGS
from pg_service_parser.utils import get_ui_class
Expand All @@ -9,16 +9,33 @@

class ServiceSettingsDialog(QDialog, DIALOG_UI):

def __init__(self, parent, settings_to_hide: list[str]):
def __init__(self, parent, used_settings: list[str]):
QDialog.__init__(self, parent)
self.setupUi(self)

self.lstSettings.itemSelectionChanged.connect(self.__selection_changed)
self.buttonBox.accepted.connect(self.__accepted)

settings = set(SERVICE_SETTINGS.keys()) - set(settings_to_hide)
self.lstSettings.addItems(settings)
self.__selection_changed() # Initialize button status

# Load data
for setting, data in SERVICE_SETTINGS.items():
item = QListWidgetItem(setting)
if setting in used_settings:
item.setFlags(item.flags() & ~Qt.ItemIsSelectable & ~Qt.ItemIsEnabled)
if data.get("description", None):
item.setToolTip(data["description"])

self.lstSettings.addItem(item)

self.settings_to_add = []

@pyqtSlot()
def __selection_changed(self):
self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(
bool(self.lstSettings.selectedItems())
)

@pyqtSlot()
def __accepted(self):
self.settings_to_add = [item.text() for item in self.lstSettings.selectedItems()]