Skip to content
Permalink
Browse files

Save/restore form "editable" and "label on top" settings

Fixes #17860
  • Loading branch information
nyalldawson committed Jan 16, 2018
1 parent 7998745 commit f3f1b8eaa382fa8ab834802875a810dc696d6e2b
Showing with 138 additions and 0 deletions.
  1. +37 −0 src/core/qgseditformconfig.cpp
  2. +1 −0 tests/src/python/CMakeLists.txt
  3. +100 −0 tests/src/python/test_qgseditformconfig.py
@@ -261,6 +261,7 @@ void QgsEditFormConfig::setSuppress( QgsEditFormConfig::FeatureFormSuppress s )
void QgsEditFormConfig::readXml( const QDomNode &node, const QgsReadWriteContext &context )
{
d.detach();

QDomNode editFormNode = node.namedItem( QStringLiteral( "editform" ) );
if ( !editFormNode.isNull() )
{
@@ -338,6 +339,22 @@ void QgsEditFormConfig::readXml( const QDomNode &node, const QgsReadWriteContext
}
}

d->mFieldEditables.clear();
QDomNodeList editableNodeList = node.namedItem( QStringLiteral( "editable" ) ).toElement().childNodes();
for ( int i = 0; i < editableNodeList.size(); ++i )
{
QDomElement editableElement = editableNodeList.at( i ).toElement();
d->mFieldEditables.insert( editableElement.attribute( QStringLiteral( "name" ) ), static_cast< bool >( editableElement.attribute( QStringLiteral( "editable" ) ).toInt() ) );
}

d->mLabelOnTop.clear();
QDomNodeList labelOnTopNodeList = node.namedItem( QStringLiteral( "labelOnTop" ) ).toElement().childNodes();
for ( int i = 0; i < labelOnTopNodeList.size(); ++i )
{
QDomElement labelOnTopElement = labelOnTopNodeList.at( i ).toElement();
d->mLabelOnTop.insert( labelOnTopElement.attribute( QStringLiteral( "name" ) ), static_cast< bool >( labelOnTopElement.attribute( QStringLiteral( "labelOnTop" ) ).toInt() ) );
}

QDomNodeList widgetsNodeList = node.namedItem( QStringLiteral( "widgets" ) ).toElement().childNodes();

for ( int i = 0; i < widgetsNodeList.size(); ++i )
@@ -439,6 +456,26 @@ void QgsEditFormConfig::writeXml( QDomNode &node, const QgsReadWriteContext &con
node.appendChild( tabsElem );
}

QDomElement editableElem = doc.createElement( QStringLiteral( "editable" ) );
for ( auto editIt = d->mFieldEditables.constBegin(); editIt != d->mFieldEditables.constEnd(); ++editIt )
{
QDomElement fieldElem = doc.createElement( QStringLiteral( "field" ) );
fieldElem.setAttribute( QStringLiteral( "name" ), editIt.key() );
fieldElem.setAttribute( QStringLiteral( "editable" ), editIt.value() ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
editableElem.appendChild( fieldElem );
}
node.appendChild( editableElem );

QDomElement labelOnTopElem = doc.createElement( QStringLiteral( "labelOnTop" ) );
for ( auto labelOnTopIt = d->mLabelOnTop.constBegin(); labelOnTopIt != d->mLabelOnTop.constEnd(); ++labelOnTopIt )
{
QDomElement fieldElem = doc.createElement( QStringLiteral( "field" ) );
fieldElem.setAttribute( QStringLiteral( "name" ), labelOnTopIt.key() );
fieldElem.setAttribute( QStringLiteral( "labelOnTop" ), labelOnTopIt.value() ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
labelOnTopElem.appendChild( fieldElem );
}
node.appendChild( labelOnTopElem );

QDomElement widgetsElem = doc.createElement( QStringLiteral( "widgets" ) );

QMap<QString, QVariantMap >::ConstIterator configIt( d->mWidgetConfigs.constBegin() );
@@ -34,6 +34,7 @@ ADD_PYTHON_TEST(PyQgsDateTimeEdit test_qgsdatetimeedit.py)
ADD_PYTHON_TEST(PyQgsDateTimeStatisticalSummary test_qgsdatetimestatisticalsummary.py)
ADD_PYTHON_TEST(PyQgsDelimitedTextProvider test_qgsdelimitedtextprovider.py)
ADD_PYTHON_TEST(PyQgsDistanceArea test_qgsdistancearea.py)
ADD_PYTHON_TEST(PyQgsEditFormConfig test_qgseditformconfig.py)
ADD_PYTHON_TEST(PyQgsEditWidgets test_qgseditwidgets.py)
ADD_PYTHON_TEST(PyQgsEllipsoidUtils test_qgsellipsoidutils.py)
ADD_PYTHON_TEST(PyQgsEncodingSelectionDialog test_qgsencodingselectiondialog.py)
@@ -0,0 +1,100 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsEditFormConfig.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Nyall Dawson'
__date__ = '11/04/2017'
__copyright__ = 'Copyright 2018, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis # NOQA

from qgis.core import (QgsVectorLayer,
QgsReadWriteContext)
from qgis.gui import QgsGui

from qgis.testing import start_app, unittest
from qgis.PyQt.QtXml import QDomDocument, QDomElement

start_app()


class TestQgsEditFormConfig(unittest.TestCase):

@classmethod
def setUpClass(cls):
QgsGui.editorWidgetRegistry().initEditors()

def createLayer(self):
self.layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
"addfeat", "memory")
return self.layer

def testReadWriteXml(self):
layer = self.createLayer()
config = layer.editFormConfig()

config.setReadOnly(0, True)
config.setReadOnly(1, False)
config.setLabelOnTop(0, False)
config.setLabelOnTop(1, True)

doc = QDomDocument("testdoc")
elem = doc.createElement('edit')
config.writeXml(elem, QgsReadWriteContext())

layer2 = self.createLayer()
config2 = layer2.editFormConfig()
config2.readXml(elem, QgsReadWriteContext())

self.assertTrue(config2.readOnly(0))
self.assertFalse(config2.readOnly(1))
self.assertFalse(config2.labelOnTop(0))
self.assertTrue(config2.labelOnTop(1))

def testReadOnly(self):
layer = self.createLayer()
config = layer.editFormConfig()

# safety checks
config.setReadOnly(-1, True)
config.setReadOnly(100, True)

# real checks
config.setReadOnly(0, True)
config.setReadOnly(1, True)
self.assertTrue(config.readOnly(0))
self.assertTrue(config.readOnly(1))

config.setReadOnly(0, False)
config.setReadOnly(1, False)
self.assertFalse(config.readOnly(0))
self.assertFalse(config.readOnly(1))

def testLabelOnTop(self):
layer = self.createLayer()
config = layer.editFormConfig()

# safety checks
config.setLabelOnTop(-1, True)
config.setLabelOnTop(100, True)

# real checks
config.setLabelOnTop(0, True)
config.setLabelOnTop(1, True)
self.assertTrue(config.labelOnTop(0))
self.assertTrue(config.labelOnTop(1))

config.setLabelOnTop(0, False)
config.setLabelOnTop(1, False)
self.assertFalse(config.labelOnTop(0))
self.assertFalse(config.labelOnTop(1))


if __name__ == '__main__':
unittest.main()

0 comments on commit f3f1b8e

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