Skip to content

Commit

Permalink
Fix QgsComposerAttributeTableV2 setting displayed fields under qt5
Browse files Browse the repository at this point in the history
Also don't run QgsComposerAttributeTable test when disable
deprecated flag is set.
  • Loading branch information
nyalldawson committed May 23, 2016
1 parent 008310d commit eb3a34c
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 10 deletions.
2 changes: 0 additions & 2 deletions ci/travis/linux/qt5/blacklist.txt
Expand Up @@ -12,6 +12,4 @@ PyQgsSpatialiteProvider
PyQgsVirtualLayerDefinition
PyQgsVirtualLayerProvider
qgis_composermapgridtest
qgis_composertabletest
qgis_composertablev2test
qgis_composerutils
14 changes: 12 additions & 2 deletions python/core/composer/qgscomposerattributetablev2.sip
Expand Up @@ -236,9 +236,19 @@ class QgsComposerAttributeTableV2 : QgsComposerTableV2
* @param refresh set to true to force the table to refetch features from its vector layer
* and immediately update the display of the table. This may result in the table changing size
* to accommodate the new displayed feature attributes.
* @see displayAttributes
* @deprecated use setDisplayedFields() instead
*/
void setDisplayAttributes( const QSet<int>& attr, bool refresh = true );
void setDisplayAttributes( const QSet<int>& attr, bool refresh = true ) /Deprecated/;

/** Sets the attributes to display in the table.
* @param fields list of fields names from the vector layer to show.
* Set to an empty list to show all feature attributes.
* @param refresh set to true to force the table to refetch features from its vector layer
* and immediately update the display of the table. This may result in the table changing size
* to accommodate the new displayed feature attributes.
* @note added in QGIS 2.16
*/
void setDisplayedFields( const QStringList& fields, bool refresh = true );

/** Returns the attributes used to sort the table's features.
* @returns a QList of integer/bool pairs, where the integer refers to the attribute index and
Expand Down
50 changes: 50 additions & 0 deletions src/core/composer/qgscomposerattributetablev2.cpp
Expand Up @@ -361,6 +361,56 @@ void QgsComposerAttributeTableV2::setDisplayAttributes( const QSet<int>& attr, b
}
}

void QgsComposerAttributeTableV2::setDisplayedFields( const QStringList& fields, bool refresh )
{
QgsVectorLayer* source = sourceLayer();
if ( !source )
{
return;
}

//rebuild columns list, taking only fields contained in supplied list
qDeleteAll( mColumns );
mColumns.clear();

QgsFields layerFields = source->fields();

if ( !fields.isEmpty() )
{
Q_FOREACH ( const QString& field, fields )
{
int attrIdx = layerFields.fieldNameIndex( field );
if ( attrIdx < 0 )
continue;

QString currentAlias = source->attributeDisplayName( attrIdx );
QgsComposerTableColumn* col = new QgsComposerTableColumn;
col->setAttribute( layerFields.at( attrIdx ).name() );
col->setHeading( currentAlias );
mColumns.append( col );
}
}
else
{
//resetting, so add all attributes to columns
int idx = 0;
Q_FOREACH ( const QgsField& field, layerFields )
{
QString currentAlias = source->attributeDisplayName( idx );
QgsComposerTableColumn* col = new QgsComposerTableColumn;
col->setAttribute( field.name() );
col->setHeading( currentAlias );
mColumns.append( col );
idx++;
}
}

if ( refresh )
{
refreshAttributes();
}
}

void QgsComposerAttributeTableV2::restoreFieldAliasMap( const QMap<int, QString>& map )
{
QgsVectorLayer* source = sourceLayer();
Expand Down
14 changes: 12 additions & 2 deletions src/core/composer/qgscomposerattributetablev2.h
Expand Up @@ -258,9 +258,19 @@ class CORE_EXPORT QgsComposerAttributeTableV2: public QgsComposerTableV2
* @param refresh set to true to force the table to refetch features from its vector layer
* and immediately update the display of the table. This may result in the table changing size
* to accommodate the new displayed feature attributes.
* @see displayAttributes
* @deprecated use setDisplayedFields() instead
*/
void setDisplayAttributes( const QSet<int>& attr, bool refresh = true );
Q_DECL_DEPRECATED void setDisplayAttributes( const QSet<int>& attr, bool refresh = true );

/** Sets the attributes to display in the table.
* @param fields list of fields names from the vector layer to show.
* Set to an empty list to show all feature attributes.
* @param refresh set to true to force the table to refetch features from its vector layer
* and immediately update the display of the table. This may result in the table changing size
* to accommodate the new displayed feature attributes.
* @note added in QGIS 2.16
*/
void setDisplayedFields( const QStringList& fields, bool refresh = true );

/** Returns the attributes used to sort the table's features.
* @returns a QList of integer/bool pairs, where the integer refers to the attribute index and
Expand Down
2 changes: 2 additions & 0 deletions tests/src/core/CMakeLists.txt
Expand Up @@ -113,7 +113,9 @@ ADD_QGIS_TEST(composerpicturetest testqgscomposerpicture.cpp)
ADD_QGIS_TEST(composerrotationtest testqgscomposerrotation.cpp)
ADD_QGIS_TEST(composerscalebartest testqgscomposerscalebar.cpp )
ADD_QGIS_TEST(composershapestest testqgscomposershapes.cpp)
IF(NOT DISABLE_DEPRECATED)
ADD_QGIS_TEST(composertabletest testqgscomposertable.cpp)
ENDIF(NOT DISABLE_DEPRECATED)
ADD_QGIS_TEST(composertablev2test testqgscomposertablev2.cpp)
ADD_QGIS_TEST(composerutils testqgscomposerutils.cpp)
ADD_QGIS_TEST(connectionpooltest testqgsconnectionpool.cpp)
Expand Down
8 changes: 4 additions & 4 deletions tests/src/core/testqgscomposertablev2.cpp
Expand Up @@ -255,9 +255,9 @@ void TestQgsComposerTableV2::attributeTableFilterFeatures()
void TestQgsComposerTableV2::attributeTableSetAttributes()
{
//test subset of attributes in table
QSet<int> attributes;
attributes << 0 << 3 << 4;
mComposerAttributeTable->setDisplayAttributes( attributes );
QStringList attributes;
attributes << "Class" << "Pilots" << "Cabin Crew";
mComposerAttributeTable->setDisplayedFields( attributes );
mComposerAttributeTable->setMaximumNumberOfFeatures( 3 );

//check headers
Expand Down Expand Up @@ -291,7 +291,7 @@ void TestQgsComposerTableV2::attributeTableSetAttributes()
compareTable( expectedRows );

attributes.clear();
mComposerAttributeTable->setDisplayAttributes( attributes );
mComposerAttributeTable->setDisplayedFields( attributes );
}

void TestQgsComposerTableV2::attributeTableVisibleOnly()
Expand Down

0 comments on commit eb3a34c

Please sign in to comment.