Skip to content

Commit 5301a97

Browse files
nyalldawsonm-kuhn
authored andcommitted
Ensure that attribute table config is always initially populated
1 parent e0d6c3f commit 5301a97

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

src/app/qgsattributetabledialog.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid
127127
mMainView->init( mLayer, QgisApp::instance()->mapCanvas(), r, mEditorContext );
128128

129129
QgsAttributeTableConfig config = mLayer->attributeTableConfig();
130-
if ( config.isEmpty() )
131-
config.update( mLayer->fields() );
132130
mMainView->setAttributeTableConfig( config );
133131

134132
// Initialize filter gui elements

src/core/qgsvectorlayer.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -3751,7 +3751,12 @@ void QgsVectorLayer::readSldLabeling( const QDomNode& node )
37513751

37523752
QgsAttributeTableConfig QgsVectorLayer::attributeTableConfig() const
37533753
{
3754-
return mAttributeTableConfig;
3754+
QgsAttributeTableConfig config = mAttributeTableConfig;
3755+
3756+
if ( config.isEmpty() )
3757+
config.update( fields() );
3758+
3759+
return config;
37553760
}
37563761

37573762
void QgsVectorLayer::setAttributeTableConfig( const QgsAttributeTableConfig& attributeTableConfig )

tests/src/python/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ADD_PYTHON_TEST(PyQgsAnalysis test_qgsanalysis.py)
1313
ADD_PYTHON_TEST(PyQgsApplication test_qgsapplication.py)
1414
ADD_PYTHON_TEST(PyQgsAtlasComposition test_qgsatlascomposition.py)
1515
ADD_PYTHON_TEST(PyQgsAttributeFormEditorWidget test_qgsattributeformeditorwidget.py)
16+
ADD_PYTHON_TEST(PyQgsAttributeTableConfig test_qgsattributetableconfig.py)
1617
ADD_PYTHON_TEST(PyQgsAttributeTableModel test_qgsattributetablemodel.py)
1718
#ADD_PYTHON_TEST(PyQgsAuthenticationSystem test_qgsauthsystem.py)
1819
ADD_PYTHON_TEST(PyQgsBlendModes test_qgsblendmodes.py)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsAttributeTableConfig.
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Nyall Dawson'
10+
__date__ = '07/06/2016'
11+
__copyright__ = 'Copyright 2016, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import qgis # NOQA
16+
17+
from qgis.core import (QgsAttributeTableConfig,
18+
QgsVectorLayer
19+
)
20+
from qgis.testing import start_app, unittest
21+
22+
start_app()
23+
24+
25+
class TestQgsAttributeTableConfig(unittest.TestCase):
26+
27+
def testLayerConfig(self):
28+
""" test retrieving attribute table config from a layer """
29+
30+
# make a layer
31+
point_layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
32+
"pointlayer", "memory")
33+
34+
# make sure attribute table config is initially populated
35+
config = point_layer.attributeTableConfig()
36+
self.assertFalse(config.isEmpty())
37+
self.assertEqual(config.columns()[0].name, 'fldtxt')
38+
self.assertEqual(config.columns()[1].name, 'fldint')
39+
40+
# try replacing it
41+
config.setColumns([config.columns()[1], config.columns()[0]])
42+
point_layer.setAttributeTableConfig(config)
43+
44+
# and make sure changes were applied
45+
config = point_layer.attributeTableConfig()
46+
self.assertFalse(config.isEmpty())
47+
self.assertEqual(config.columns()[0].name, 'fldint')
48+
self.assertEqual(config.columns()[1].name, 'fldtxt')
49+
50+
if __name__ == '__main__':
51+
unittest.main()

0 commit comments

Comments
 (0)