Skip to content

Commit

Permalink
Ensure that attribute table config is always initially populated
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson authored and m-kuhn committed Jun 7, 2016
1 parent e0d6c3f commit 5301a97
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
2 changes: 0 additions & 2 deletions src/app/qgsattributetabledialog.cpp
Expand Up @@ -127,8 +127,6 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid
mMainView->init( mLayer, QgisApp::instance()->mapCanvas(), r, mEditorContext );

QgsAttributeTableConfig config = mLayer->attributeTableConfig();
if ( config.isEmpty() )
config.update( mLayer->fields() );
mMainView->setAttributeTableConfig( config );

// Initialize filter gui elements
Expand Down
7 changes: 6 additions & 1 deletion src/core/qgsvectorlayer.cpp
Expand Up @@ -3751,7 +3751,12 @@ void QgsVectorLayer::readSldLabeling( const QDomNode& node )

QgsAttributeTableConfig QgsVectorLayer::attributeTableConfig() const
{
return mAttributeTableConfig;
QgsAttributeTableConfig config = mAttributeTableConfig;

if ( config.isEmpty() )
config.update( fields() );

return config;
}

void QgsVectorLayer::setAttributeTableConfig( const QgsAttributeTableConfig& attributeTableConfig )
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -13,6 +13,7 @@ ADD_PYTHON_TEST(PyQgsAnalysis test_qgsanalysis.py)
ADD_PYTHON_TEST(PyQgsApplication test_qgsapplication.py)
ADD_PYTHON_TEST(PyQgsAtlasComposition test_qgsatlascomposition.py)
ADD_PYTHON_TEST(PyQgsAttributeFormEditorWidget test_qgsattributeformeditorwidget.py)
ADD_PYTHON_TEST(PyQgsAttributeTableConfig test_qgsattributetableconfig.py)
ADD_PYTHON_TEST(PyQgsAttributeTableModel test_qgsattributetablemodel.py)
#ADD_PYTHON_TEST(PyQgsAuthenticationSystem test_qgsauthsystem.py)
ADD_PYTHON_TEST(PyQgsBlendModes test_qgsblendmodes.py)
Expand Down
51 changes: 51 additions & 0 deletions tests/src/python/test_qgsattributetableconfig.py
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsAttributeTableConfig.
.. 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__ = '07/06/2016'
__copyright__ = 'Copyright 2016, 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 (QgsAttributeTableConfig,
QgsVectorLayer
)
from qgis.testing import start_app, unittest

start_app()


class TestQgsAttributeTableConfig(unittest.TestCase):

def testLayerConfig(self):
""" test retrieving attribute table config from a layer """

# make a layer
point_layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
"pointlayer", "memory")

# make sure attribute table config is initially populated
config = point_layer.attributeTableConfig()
self.assertFalse(config.isEmpty())
self.assertEqual(config.columns()[0].name, 'fldtxt')
self.assertEqual(config.columns()[1].name, 'fldint')

# try replacing it
config.setColumns([config.columns()[1], config.columns()[0]])
point_layer.setAttributeTableConfig(config)

# and make sure changes were applied
config = point_layer.attributeTableConfig()
self.assertFalse(config.isEmpty())
self.assertEqual(config.columns()[0].name, 'fldint')
self.assertEqual(config.columns()[1].name, 'fldtxt')

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

0 comments on commit 5301a97

Please sign in to comment.