Skip to content

Commit

Permalink
Preserve edits for multiline editor when length exceeds field size
Browse files Browse the repository at this point in the history
Previously when using the multiline option for text edit widgets
the entire contents of the field would be discarded if the entered
value exceeded the maximum length for a string field.

Now the entered string is truncated to the maximum field length.

(cherry-picked from 7d8fba8)
  • Loading branch information
nyalldawson committed Oct 6, 2016
1 parent c2fbab1 commit 83d5719
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/gui/editorwidgets/qgstexteditwrapper.cpp
Expand Up @@ -66,9 +66,19 @@ QVariant QgsTextEditWrapper::value() const

QVariant res( v );
if ( field().convertCompatible( res ) )
{
return res;
}
else if ( field().type() == QVariant::String && field().length() > 0 )
{
// for string fields convertCompatible may return false due to field length limit - in this case just truncate
// input rather then discarding it entirely
return QVariant( v.left( field().length() ) );
}
else
{
return QVariant( field().type() );
}
}

QWidget* QgsTextEditWrapper::createWidget( QWidget* parent )
Expand Down
32 changes: 31 additions & 1 deletion tests/src/python/test_qgseditwidgets.py
Expand Up @@ -14,10 +14,13 @@

import qgis # NOQA

from qgis.core import QgsMapLayerRegistry, QgsFeature, QgsGeometry, QgsPoint, QgsProject, QgsRelation, QgsVectorLayer, NULL
from qgis.core import (QgsMapLayerRegistry, QgsFeature, QgsGeometry, QgsPoint, QgsProject, QgsRelation, QgsVectorLayer, NULL,
QgsField)
from qgis.gui import QgsEditorWidgetRegistry

from qgis.testing import start_app, unittest
from qgis.PyQt.QtCore import QVariant
from qgis.PyQt.QtWidgets import QTextEdit

start_app()

Expand Down Expand Up @@ -63,6 +66,33 @@ def test_SetValue(self):
self.doAttributeTest(0, ['value', '123', NULL, NULL])
self.doAttributeTest(1, [NULL, 123, NULL, NULL])

def testStringWithMaxLen(self):
""" tests that text edit wrappers correctly handle string fields with a maximum length """
layer = QgsVectorLayer("none?field=fldint:integer", "layer", "memory")
assert layer.isValid()
layer.dataProvider().addAttributes([QgsField('max', QVariant.String, 'string', 10),
QgsField('nomax', QVariant.String, 'string', 0)])
layer.updateFields()
QgsMapLayerRegistry.instance().addMapLayer(layer)

reg = QgsEditorWidgetRegistry.instance()
config = {'IsMultiline': 'True'}

# first test for field without character limit
editor = QTextEdit()
editor.setPlainText('this_is_a_long_string')
w = reg.create('TextEdit', layer, 2, config, editor, None)
self.assertEqual(w.value(), 'this_is_a_long_string')

# next test for field with character limit
editor = QTextEdit()
editor.setPlainText('this_is_a_long_string')
w = reg.create('TextEdit', layer, 1, config, editor, None)

self.assertEqual(w.value(), 'this_is_a_')

QgsMapLayerRegistry.instance().removeAllMapLayers()

def test_ValueRelation_representValue(self):

first_layer = QgsVectorLayer("none?field=foreign_key:integer",
Expand Down

0 comments on commit 83d5719

Please sign in to comment.