mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-06 00:07:29 -04:00
Filter out field with readonly in the quick field calculator.
Fix #34331
This commit is contained in:
parent
c796861b13
commit
562b11fd9b
@ -39,6 +39,7 @@ It can be associated with a QgsMapLayerModel to dynamically display a layer and
|
||||
IsEmptyRole,
|
||||
EditorWidgetType,
|
||||
JoinedFieldIsEditable,
|
||||
FieldIsWidgetEditable,
|
||||
};
|
||||
|
||||
explicit QgsFieldModel( QObject *parent /TransferThis/ = 0 );
|
||||
|
@ -387,6 +387,12 @@ QVariant QgsFieldModel::data( const QModelIndex &index, int role ) const
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
case FieldIsWidgetEditable:
|
||||
{
|
||||
return !( mLayer->editFormConfig().readOnly( index.row() - fieldOffset ) );
|
||||
}
|
||||
|
||||
|
||||
case Qt::DisplayRole:
|
||||
case Qt::EditRole:
|
||||
case Qt::ToolTipRole:
|
||||
|
@ -58,6 +58,7 @@ class CORE_EXPORT QgsFieldModel : public QAbstractItemModel
|
||||
IsEmptyRole = Qt::UserRole + 8, //!< Return if the index corresponds to the empty value
|
||||
EditorWidgetType = Qt::UserRole + 9, //!< Editor widget type
|
||||
JoinedFieldIsEditable = Qt::UserRole + 10, //!< TRUE if a joined field is editable (returns QVariant if not a joined field)
|
||||
FieldIsWidgetEditable = Qt::UserRole + 11, //!< TRUE if a is editable from the widget
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -62,8 +62,18 @@ bool QgsFieldProxyModel::isReadOnly( const QModelIndex &index ) const
|
||||
|
||||
case QgsFields::OriginEdit:
|
||||
case QgsFields::OriginProvider:
|
||||
//not read only
|
||||
return false;
|
||||
{
|
||||
if ( !sourceModel()->data( index, QgsFieldModel::FieldIsWidgetEditable ).toBool() )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
//not read only
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return false; // avoid warnings
|
||||
}
|
||||
|
@ -349,6 +349,26 @@ class TestQgsFieldModel(unittest.TestCase):
|
||||
self.assertEqual(proxy_m.rowCount(), 1)
|
||||
self.assertEqual(proxy_m.data(proxy_m.index(0, 0)), 'id_a')
|
||||
|
||||
def testFieldIsWidgetEditableRole(self):
|
||||
l, m = create_model()
|
||||
self.assertTrue(m.data(m.indexFromName('fldtxt'), QgsFieldModel.FieldIsWidgetEditable))
|
||||
self.assertTrue(m.data(m.indexFromName('fldint'), QgsFieldModel.FieldIsWidgetEditable))
|
||||
self.assertFalse(m.data(m.indexFromName('an expression'), QgsFieldModel.FieldIsWidgetEditable))
|
||||
self.assertFalse(m.data(m.indexFromName(None), QgsFieldModel.FieldIsWidgetEditable))
|
||||
m.setAllowExpression(True)
|
||||
m.setExpression('an expression')
|
||||
self.assertTrue(m.data(m.indexFromName('an expression'), QgsFieldModel.FieldIsWidgetEditable))
|
||||
m.setAllowEmptyFieldName(True)
|
||||
self.assertTrue(m.data(m.indexFromName(None), QgsFieldModel.FieldIsWidgetEditable))
|
||||
|
||||
editFormConfig = l.editFormConfig()
|
||||
idx = l.fields().indexOf('fldtxt')
|
||||
# Make fldtxt readOnly
|
||||
editFormConfig.setReadOnly(idx, True)
|
||||
l.setEditFormConfig(editFormConfig)
|
||||
# It's read only, so the widget is NOT editable
|
||||
self.assertFalse(m.data(m.indexFromName('fldtxt'), QgsFieldModel.FieldIsWidgetEditable))
|
||||
|
||||
def testFieldTooltip(self):
|
||||
f = QgsField('my_string', QVariant.String, 'string')
|
||||
self.assertEqual(QgsFieldModel.fieldToolTip(f), "<b>my_string</b><br><font style='font-family:monospace; white-space: nowrap;'>string NULL</font>")
|
||||
|
Loading…
x
Reference in New Issue
Block a user