Skip to content

Commit

Permalink
Filter out field with readonly in the quick field calculator.
Browse files Browse the repository at this point in the history
Fix #34331
  • Loading branch information
ismailsunni committed Apr 4, 2020
1 parent c796861 commit 562b11f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions python/core/auto_generated/qgsfieldmodel.sip.in
Expand Up @@ -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 );
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsfieldmodel.cpp
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsfieldmodel.h
Expand Up @@ -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
};

/**
Expand Down
14 changes: 12 additions & 2 deletions src/core/qgsfieldproxymodel.cpp
Expand Up @@ -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
}
Expand Down
20 changes: 20 additions & 0 deletions tests/src/python/test_qgsfieldmodel.py
Expand Up @@ -349,6 +349,26 @@ def testJoinedFieldIsEditableRole(self):
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>")
Expand Down

0 comments on commit 562b11f

Please sign in to comment.