Skip to content
Permalink
Browse files

Merge pull request #31387 from gacarrillor/colour_refactor_fields

Show 'template layer' constraints info in the Refactor fields algorithm's UI
  • Loading branch information
m-kuhn committed Mar 18, 2020
2 parents eab1688 + d016d46 commit 342f1e339638e093179b427ff980382c0d9830b0
@@ -330,6 +330,8 @@ qgis:refactorfields: >

The original layer is not modified. A new layer is generated, which contains a modified attribute table, according to the provided fields mapping.

Rows in orange have constraints in the template layer from which these fields were loaded. Treat this information as a hint during configuration. No constraints will be added on an output layer nor will they be checked or enforced by the algorithm.

qgis:regularpoints: >
This algorithm creates a point layer with a given number of regular points, all of them within a given extent.

@@ -34,6 +34,12 @@
pyqtSlot,
QCoreApplication
)

from qgis.PyQt.QtGui import (
QBrush,
QColor
)

from qgis.PyQt.QtWidgets import (
QComboBox,
QHeaderView,
@@ -54,6 +60,7 @@
QgsProcessingUtils,
QgsProject,
QgsVectorLayer,
QgsFieldConstraints
)
from qgis.gui import QgsFieldExpressionWidget

@@ -79,6 +86,12 @@ class FieldsMappingModel(QAbstractTableModel):
(QVariant.List, "List"),
(QVariant.Bool, "Boolean")])

constraints = {
QgsFieldConstraints.ConstraintNotNull: "NOT NULL",
QgsFieldConstraints.ConstraintUnique: "Unique",
QgsFieldConstraints.ConstraintExpression: "Expression constraint"
}

def __init__(self, parent=None):
super(FieldsMappingModel, self).__init__(parent)
self._mapping = []
@@ -109,6 +122,10 @@ def configure(self):
'name': 'precision',
'type': QVariant.Int,
'header': self.tr("Precision")
}, {
'name': 'constraints',
'type': QVariant.String,
'header': self.tr("Template properties")
}]

def columnIndex(self, column_name):
@@ -161,20 +178,28 @@ def headerData(self, section, orientation, role=Qt.DisplayRole):
return section

def flags(self, index):
return Qt.ItemFlags(Qt.ItemIsSelectable |
Qt.ItemIsEditable |
Qt.ItemIsEnabled)
column_def = self.columns[index.column()]

flags = Qt.ItemFlags(Qt.ItemIsSelectable |
Qt.ItemIsEnabled)
if column_def['name'] != 'constraints':
flags = flags | Qt.ItemIsEditable

return flags

def data(self, index, role=Qt.DisplayRole):
field = self._mapping[index.row()]
column_def = self.columns[index.column()]

if role == Qt.DisplayRole:
value = field[column_def['name']]
value = field[column_def['name']] if column_def['name'] in field else QVariant()
if column_def['type'] == QVariant.Type:
if value == QVariant.Invalid:
return ''
return self.fieldTypes[value]
elif column_def['name'] == 'constraints' and value:
return self.tr("Constraints active")

return value

if role == Qt.EditRole:
@@ -187,6 +212,13 @@ def data(self, index, role=Qt.DisplayRole):
hAlign = Qt.AlignLeft
return hAlign + Qt.AlignVCenter

if role == Qt.BackgroundRole:
return QBrush(QColor(255, 224, 178)) if 'constraints' in field and field['constraints'] else QVariant()

if role == Qt.ToolTipRole:
if column_def['name'] == 'constraints' and 'constraints' in field:
return "<br>".join([self.constraints[constraint] for constraint in field['constraints']])

def setData(self, index, value, role=Qt.EditRole):
field = self._mapping[index.row()]
column_def = self.columns[index.column()]
@@ -222,13 +254,15 @@ def newField(self, field=None):
'type': QVariant.Invalid,
'length': 0,
'precision': 0,
'expression': ''}
'expression': '',
'constraints': ''}

return {'name': field.name(),
'type': field.type(),
'length': field.length(),
'precision': field.precision(),
'expression': QgsExpression.quotedColumnRef(field.name())}
'expression': QgsExpression.quotedColumnRef(field.name()),
'constraints': self.get_field_constraints(field.constraints())}

def loadLayerFields(self, layer):
self.beginResetModel()
@@ -240,6 +274,26 @@ def loadLayerFields(self, layer):

self.endResetModel()

def get_field_constraints(self, field_constraints):
constraints = list()

if field_constraints.constraints() & QgsFieldConstraints.ConstraintNotNull and \
field_constraints.constraintStrength(
QgsFieldConstraints.ConstraintNotNull) & QgsFieldConstraints.ConstraintStrengthHard:
constraints.append(QgsFieldConstraints.ConstraintNotNull)

if field_constraints.constraints() & QgsFieldConstraints.ConstraintUnique and \
field_constraints.constraintStrength(
QgsFieldConstraints.ConstraintUnique) & QgsFieldConstraints.ConstraintStrengthHard:
constraints.append(QgsFieldConstraints.ConstraintUnique)

if field_constraints.constraints() & QgsFieldConstraints.ConstraintExpression and \
field_constraints.constraintStrength(
QgsFieldConstraints.ConstraintExpression) & QgsFieldConstraints.ConstraintStrengthHard:
constraints.append(QgsFieldConstraints.ConstraintExpression)

return constraints


class FieldTypeDelegate(QStyledItemDelegate):

@@ -109,7 +109,7 @@
<item>
<widget class="QLabel" name="loadFromLayerLabel">
<property name="text">
<string>Load fields from layer</string>
<string>Load fields from template layer</string>
</property>
</widget>
</item>
@@ -132,7 +132,7 @@
</sizepolicy>
</property>
<property name="toolTip">
<string>Load fields from selected layer</string>
<string>Load fields from selected template layer</string>
</property>
<property name="text">
<string>Load Fields</string>

0 comments on commit 342f1e3

Please sign in to comment.
You can’t perform that action at this time.