Skip to content

Commit

Permalink
Merge pull request #938 from opengisch/oid-constraints
Browse files Browse the repository at this point in the history
OID Manager settings for not-null and unique constraint
  • Loading branch information
signedav authored Jun 27, 2024
2 parents 5d2effd + 93ed127 commit fd9f66a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 8 deletions.
70 changes: 65 additions & 5 deletions QgisModelBaker/gui/panel/layer_tids_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class TIDModel(QAbstractTableModel):
"oid_domain": "STANDARDOID",
"interlis_topic" : "OIDMadness_V1",
"default_value_expression": "uuid()",
"in_form": True
"in_form": True,
"not_null": True,
"unique": True,
}
[...]
}
Expand All @@ -66,6 +68,8 @@ class Columns(IntEnum):
OID_DOMAIN = 1
DEFAULT_VALUE = 2
IN_FORM = 3
NOTNULL = 4
UNIQUE = 5

def __init__(self):
super().__init__()
Expand All @@ -78,7 +82,13 @@ def rowCount(self, parent):
return len(self.oid_settings.keys())

def flags(self, index):
if index.column() == TIDModel.Columns.IN_FORM:
if index.column() == any(
[
TIDModel.Columns.IN_FORM,
TIDModel.Columns.NOTNULL,
TIDModel.Columns.UNIQUE,
]
):
return Qt.ItemIsEnabled
if index.column() == TIDModel.Columns.DEFAULT_VALUE:
return Qt.ItemIsEditable | Qt.ItemIsEnabled
Expand Down Expand Up @@ -106,6 +116,10 @@ def headerData(self, section, orientation, role):
return self.tr("Default Value Expression")
if section == TIDModel.Columns.IN_FORM:
return self.tr("Show")
if section == TIDModel.Columns.NOTNULL:
return self.tr("Not Null")
if section == TIDModel.Columns.UNIQUE:
return self.tr("Unique")

def data(self, index, role):
if role == int(Qt.DisplayRole) or role == int(Qt.EditRole):
Expand All @@ -118,6 +132,10 @@ def data(self, index, role):
return self.oid_settings[key]["default_value_expression"]
if index.column() == TIDModel.Columns.IN_FORM:
return self.oid_settings[key]["in_form"]
if index.column() == TIDModel.Columns.NOTNULL:
return self.oid_settings[key]["not_null"]
if index.column() == TIDModel.Columns.UNIQUE:
return self.oid_settings[key]["unique"]
elif role == int(Qt.ToolTipRole):
key = list(self.oid_settings.keys())[index.row()]
if index.column() == TIDModel.Columns.NAME:
Expand Down Expand Up @@ -159,6 +177,27 @@ def data(self, index, role):
return self.oid_settings[key]["default_value_expression"]
if index.column() == TIDModel.Columns.IN_FORM:
return self.tr("Show t_ili_tid field (OID) in attribute form.")
if index.column() == TIDModel.Columns.NOTNULL:
oid_domain = self.oid_settings[key].get("oid_domain", None)
if oid_domain:
return self.tr(
"t_ili_tid fields based on defined OIDs usually have a not-null-constraint set."
)
else:
return self.tr(
"t_ili_tid fields based on undefined OIDs usually have no constraints set. But feel free to change it."
)
if index.column() == TIDModel.Columns.UNIQUE:
oid_domain = self.oid_settings[key].get("oid_domain", None)
if oid_domain:
return self.tr(
"t_ili_tid fields based on defined OIDs usually have a unique-constraint set."
)
else:
return self.tr(
"t_ili_tid fields based on undefined OIDs usually have no constraints set. But feel free to change it."
)
return self.tr("Show t_ili_tid field (OID) in attribute form.")
elif role == int(TIDModel.Roles.LAYER):
key = list(self.oid_settings.keys())[index.row()]
return self.oid_settings[key]["layer"]
Expand All @@ -174,6 +213,14 @@ def setData(self, index, data, role):
key = list(self.oid_settings.keys())[index.row()]
self.oid_settings[key]["in_form"] = data
self.dataChanged.emit(index, index)
if index.column() == TIDModel.Columns.NOTNULL:
key = list(self.oid_settings.keys())[index.row()]
self.oid_settings[key]["not_null"] = data
self.dataChanged.emit(index, index)
if index.column() == TIDModel.Columns.UNIQUE:
key = list(self.oid_settings.keys())[index.row()]
self.oid_settings[key]["unique"] = data
self.dataChanged.emit(index, index)
return True

def load_tid_config(self, qgis_project=None):
Expand Down Expand Up @@ -204,7 +251,6 @@ def setEditorData(self, editor, index):

def setModelData(self, editor, model, index):
value = editor.expression()
print(f"new exp{value}")
model.setData(index, value, int(Qt.EditRole))

def updateEditorGeometry(self, editor, option, index):
Expand Down Expand Up @@ -242,14 +288,28 @@ def __init__(self, parent=None):
self.layer_tids_view.horizontalHeader().setSectionResizeMode(
TIDModel.Columns.IN_FORM, QHeaderView.ResizeToContents
)
self.layer_tids_view.horizontalHeader().setSectionResizeMode(
TIDModel.Columns.NOTNULL, QHeaderView.ResizeToContents
)
self.layer_tids_view.horizontalHeader().setSectionResizeMode(
TIDModel.Columns.UNIQUE, QHeaderView.ResizeToContents
)

self.layer_tids_view.setItemDelegateForColumn(
TIDModel.Columns.DEFAULT_VALUE,
FieldExpressionDelegate(self),
)
self.layer_tids_view.setItemDelegateForColumn(
TIDModel.Columns.IN_FORM,
CheckDelegate(self, Qt.EditRole),
)
self.layer_tids_view.setItemDelegateForColumn(
TIDModel.Columns.DEFAULT_VALUE,
FieldExpressionDelegate(self),
TIDModel.Columns.NOTNULL,
CheckDelegate(self, Qt.EditRole),
)
self.layer_tids_view.setItemDelegateForColumn(
TIDModel.Columns.UNIQUE,
CheckDelegate(self, Qt.EditRole),
)
self.layer_tids_view.setEditTriggers(QAbstractItemView.AllEditTriggers)

Expand Down
6 changes: 5 additions & 1 deletion QgisModelBaker/ui/basket_panel.ui
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QTableView" name="basket_view"/>
<widget class="QTableView" name="basket_view">
<property name="selectionMode">
<enum>QAbstractItemView::NoSelection</enum>
</property>
</widget>
</item>
</layout>
</widget>
Expand Down
10 changes: 8 additions & 2 deletions QgisModelBaker/ui/layer_tids_panel.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<width>1127</width>
<height>600</height>
</rect>
</property>
Expand All @@ -28,9 +28,15 @@
</property>
<item row="1" column="0">
<widget class="QTableView" name="layer_tids_view">
<property name="selectionMode">
<enum>QAbstractItemView::NoSelection</enum>
</property>
<property name="sortingEnabled">
<bool>true</bool>
<bool>false</bool>
</property>
<attribute name="horizontalHeaderShowSortIndicator" stdset="0">
<bool>false</bool>
</attribute>
</widget>
</item>
</layout>
Expand Down

0 comments on commit fd9f66a

Please sign in to comment.