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] Use a combobox to edit sslmode setting #45

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions pg_service_parser/conf/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from enum import Enum


class SslModeEnum(Enum):
DISABLE = "disable"
ALLOW = "allow"
PREFER = "prefer"
REQUIRE = "require"
VERIFY_CA = "verify-ca"
VERIFY_FULL = "verify-full"
5 changes: 4 additions & 1 deletion pg_service_parser/conf/service_settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pg_service_parser.conf.enums import SslModeEnum

# Settings available for manual addition
# See https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
SERVICE_SETTINGS = {
Expand All @@ -10,8 +12,9 @@
"description": "Password to be used if the server demands password authentication.",
},
"sslmode": {
"default": "",
"default": SslModeEnum.PREFER.value,
"description": "This option determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the server.",
"values": SslModeEnum,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also add tooltips for each value here, perhaps as a separate key (e.g., "value_tooltips") and assign a list of tooltips.

Now, this dict is growing as you said, we could perhaps address this in a follow-up PR later (I'll wait for your instructions regarding that).

},
"passfile": {
"default": "",
Expand Down
14 changes: 14 additions & 0 deletions pg_service_parser/core/item_models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from qgis.PyQt.QtCore import QAbstractTableModel, QModelIndex, Qt, pyqtSignal
from qgis.PyQt.QtGui import QColorConstants, QFont

from pg_service_parser.conf.enums import SslModeEnum
from pg_service_parser.conf.service_settings import SERVICE_SETTINGS


class ServiceConfigModel(QAbstractTableModel):
KEY_COL = 0
VALUE_COL = 1

SSLMODE_KEY = "sslmode"

is_dirty_changed = pyqtSignal(bool) # Whether the model gets dirty or not

def __init__(self, service_name: str, service_config: dict):
Expand Down Expand Up @@ -85,6 +88,9 @@ def data(self, index, role=Qt.ItemDataRole.DisplayRole):
or self.__model_data[key] != self.__original_data[key]
):
return QColorConstants.DarkGreen
elif role == Qt.ItemDataRole.UserRole:
if index.column() == self.VALUE_COL and key == self.SSLMODE_KEY:
return {key: [e.value for e in SslModeEnum]}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at it, I guess this list could be created at pg_service_parser/conf/service_settings.py, and assign it to the "values" key, instead of assigning the whole SslModeEnum.


return None

Expand Down Expand Up @@ -144,3 +150,11 @@ def invalid_settings(self):
:return: List of invalid settings.
"""
return [k for k, v in self.__model_data.items() if v.strip() == ""]

@staticmethod
def is_sslmode_edit_cell(index):
return (
index.column() == ServiceConfigModel.VALUE_COL
and isinstance(index.data(Qt.ItemDataRole.UserRole), dict)
and ServiceConfigModel.SSLMODE_KEY in index.data(Qt.ItemDataRole.UserRole)
)
1 change: 1 addition & 0 deletions pg_service_parser/gui/dlg_pg_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)
from pg_service_parser.gui.dlg_service_name import ServiceNameDialog
from pg_service_parser.gui.dlg_service_settings import ServiceSettingsDialog
from pg_service_parser.gui.item_delegates import ServiceConfigDelegate
from pg_service_parser.utils import get_ui_class

DIALOG_UI = get_ui_class("pg_service_dialog.ui")
Expand Down
45 changes: 45 additions & 0 deletions pg_service_parser/gui/item_delegates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Adapted from
# https://github.com/xxyzz/WordDumb/blob/097dd6c1651fdc08b472e0bf639aec444b6e14ec/custom_lemmas.py#L398C1-L438C46

from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtWidgets import QComboBox, QStyledItemDelegate

from pg_service_parser.core.item_models import ServiceConfigModel


class ServiceConfigDelegate(QStyledItemDelegate):
def __init__(self, parent):
super().__init__(parent)

def createEditor(self, parent, option, index):
if ServiceConfigModel.is_sslmode_edit_cell(index):
options = list(index.data(Qt.ItemDataRole.UserRole).values())[0]
combo_box = QComboBox(parent)
if isinstance(options, list):
for value in options:
combo_box.addItem(value, value)

combo_box.currentIndexChanged.connect(self.commit_and_close_editor)

return combo_box

return QStyledItemDelegate.createEditor(self, parent, option, index)

def commit_and_close_editor(self):
editor = self.sender()
self.commitData.emit(editor)
self.closeEditor.emit(editor)

def setEditorData(self, editor, index):
if ServiceConfigModel.is_sslmode_edit_cell(index):
value = index.data(Qt.ItemDataRole.DisplayRole)
editor.setCurrentText(value)
else:
QStyledItemDelegate.setEditorData(self, editor, index)

def setModelData(self, editor, model, index):
if ServiceConfigModel.is_sslmode_edit_cell(index):
value = editor.currentData()
model.setData(index, value, Qt.ItemDataRole.EditRole)
else:
QStyledItemDelegate.setModelData(self, editor, model, index)
Loading