Skip to content

Commit

Permalink
[FEATURE][composer] Add a checkbox to filter attribute tables to
Browse files Browse the repository at this point in the history
features which intersect the current atlas feature. Sponsored by City
of Uster, Switzerland.
  • Loading branch information
nyalldawson committed Sep 22, 2014
1 parent 14690d0 commit 30ada28
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 14 deletions.
21 changes: 18 additions & 3 deletions python/core/composer/qgscomposerattributetablev2.sip
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class QgsComposerAttributeTableV2 : QgsComposerTableV2
* @param features maximum number of features to show in the table
* @see maximumNumberOfFeatures
*/
void setMaximumNumberOfFeatures( int features );
void setMaximumNumberOfFeatures( const int features );

/**Returns the maximum number of features to be shown by the table.
* @returns maximum number of features
Expand All @@ -154,7 +154,7 @@ class QgsComposerAttributeTableV2 : QgsComposerTableV2
* @see displayOnlyVisibleFeatures
* @see setComposerMap
*/
void setDisplayOnlyVisibleFeatures( bool visibleOnly );
void setDisplayOnlyVisibleFeatures( const bool visibleOnly );

/**Returns true if the table is set to show only features visible on a corresponding
* composer map item.
Expand All @@ -163,6 +163,21 @@ class QgsComposerAttributeTableV2 : QgsComposerTableV2
* @see setDisplayOnlyVisibleFeatures
*/
bool displayOnlyVisibleFeatures() const;

/**Sets attribute table to only show features which intersect the current atlas
* feature.
* @param filterToAtlas set to true to show only features which intersect
* the atlas feature
* @see filterToAtlasFeature
*/
void setFilterToAtlasFeature( const bool filterToAtlas );

/**Returns true if the table is set to only show features which intersect the current atlas
* feature.
* @returns true if table only shows features which intersect the atlas feature
* @see setFilterToAtlasFeature
*/
bool filterToAtlasFeature() const;

/**Returns true if a feature filter is active on the attribute table
* @returns bool state of the feature filter
Expand All @@ -178,7 +193,7 @@ class QgsComposerAttributeTableV2 : QgsComposerTableV2
* @see filterFeatures
* @see setFeatureFilter
*/
void setFilterFeatures( bool filter );
void setFilterFeatures( const bool filter );

/**Returns the current expression used to filter features for the table. The filter is only
* active if filterFeatures() is true.
Expand Down
30 changes: 30 additions & 0 deletions src/app/composer/qgscomposerattributetablewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ void QgsComposerAttributeTableWidget::updateGuiElements()
mComposerMapLabel->setEnabled( false );
}

mIntersectAtlasCheckBox->setChecked( mComposerTable->filterToAtlasFeature() );
mFeatureFilterEdit->setText( mComposerTable->featureFilter() );
mFeatureFilterCheckBox->setCheckState( mComposerTable->filterFeatures() ? Qt::Checked : Qt::Unchecked );
mFeatureFilterEdit->setEnabled( mComposerTable->filterFeatures() );
Expand Down Expand Up @@ -521,6 +522,11 @@ void QgsComposerAttributeTableWidget::atlasToggled()
mSourceComboBox->blockSignals( true );
mSourceComboBox->setCurrentIndex( mSourceComboBox->findData( mComposerTable->source() ) );
mSourceComboBox->blockSignals( false );

if ( !atlasEnabled && mComposerTable && mComposerTable->filterToAtlasFeature() )
{
mComposerTable->setFilterToAtlasFeature( false );
}
}

void QgsComposerAttributeTableWidget::updateRelationsCombo()
Expand Down Expand Up @@ -556,6 +562,7 @@ void QgsComposerAttributeTableWidget::toggleAtlasSpecificControls( const bool at
mRelationsComboBox->setEnabled( false );
mRelationsComboBox->clear();
mRelationsComboBox->blockSignals( false );
mIntersectAtlasCheckBox->setEnabled( false );
}
else
{
Expand All @@ -573,6 +580,7 @@ void QgsComposerAttributeTableWidget::toggleAtlasSpecificControls( const bool at
//add relations for coverage layer
updateRelationsCombo();
mRelationsComboBox->setEnabled( true );
mIntersectAtlasCheckBox->setEnabled( true );
}
}

Expand All @@ -587,6 +595,7 @@ void QgsComposerAttributeTableWidget::blockAllSignals( bool b )
mGridStrokeWidthSpinBox->blockSignals( b );
mShowGridGroupCheckBox->blockSignals( b );
mShowOnlyVisibleFeaturesCheckBox->blockSignals( b );
mIntersectAtlasCheckBox->blockSignals( b );
mFeatureFilterEdit->blockSignals( b );
mFeatureFilterCheckBox->blockSignals( b );
mHeaderHAlignmentComboBox->blockSignals( b );
Expand Down Expand Up @@ -631,6 +640,27 @@ void QgsComposerAttributeTableWidget::on_mShowOnlyVisibleFeaturesCheckBox_stateC
mComposerMapLabel->setEnabled( state == Qt::Checked );
}

void QgsComposerAttributeTableWidget::on_mIntersectAtlasCheckBox_stateChanged( int state )
{
if ( !mComposerTable )
{
return;
}

QgsComposition* composition = mComposerTable->composition();
if ( composition )
{
composition->beginMultiFrameCommand( mComposerTable, tr( "Table filter to atlas changed" ) );
}
bool filterToAtlas = ( state == Qt::Checked );
mComposerTable->setFilterToAtlasFeature( filterToAtlas );
mComposerTable->update();
if ( composition )
{
composition->endMultiFrameCommand();
}
}

void QgsComposerAttributeTableWidget::on_mFeatureFilterCheckBox_stateChanged( int state )
{
if ( !mComposerTable )
Expand Down
2 changes: 2 additions & 0 deletions src/app/composer/qgscomposerattributetablewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class QgsComposerAttributeTableWidget: public QgsComposerItemBaseWidget, private
void on_mRelationsComboBox_currentIndexChanged( int index );
void on_mEmptyModeComboBox_currentIndexChanged( int index );
void on_mEmptyMessageLineEdit_editingFinished();
void on_mIntersectAtlasCheckBox_stateChanged( int state );

/**Inserts a new maximum number of features into the spin box (without the spinbox emitting a signal)*/
void setMaximumNumberOfFeatures( int n );
Expand All @@ -82,6 +83,7 @@ class QgsComposerAttributeTableWidget: public QgsComposerItemBaseWidget, private
void atlasToggled();

void updateRelationsCombo();

};

#endif // QGSCOMPOSERATTRIBUTETABLEWIDGET_H
37 changes: 34 additions & 3 deletions src/core/composer/qgscomposerattributetablev2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "qgsatlascomposition.h"
#include "qgsproject.h"
#include "qgsrelationmanager.h"
#include "qgsgeometry.h"

//QgsComposerAttributeTableCompareV2

Expand Down Expand Up @@ -104,6 +105,7 @@ QgsComposerAttributeTableV2::QgsComposerAttributeTableV2( QgsComposition* compos
, mComposerMap( 0 )
, mMaximumNumberOfFeatures( 5 )
, mShowOnlyVisibleFeatures( false )
, mFilterToAtlasIntersection( false )
, mFilterFeatures( false )
, mFeatureFilter( "" )
{
Expand Down Expand Up @@ -279,7 +281,7 @@ void QgsComposerAttributeTableV2::setComposerMap( const QgsComposerMap* map )
emit changed();
}

void QgsComposerAttributeTableV2::setMaximumNumberOfFeatures( int features )
void QgsComposerAttributeTableV2::setMaximumNumberOfFeatures( const int features )
{
if ( features == mMaximumNumberOfFeatures )
{
Expand All @@ -291,7 +293,7 @@ void QgsComposerAttributeTableV2::setMaximumNumberOfFeatures( int features )
emit changed();
}

void QgsComposerAttributeTableV2::setDisplayOnlyVisibleFeatures( bool visibleOnly )
void QgsComposerAttributeTableV2::setDisplayOnlyVisibleFeatures( const bool visibleOnly )
{
if ( visibleOnly == mShowOnlyVisibleFeatures )
{
Expand All @@ -303,7 +305,19 @@ void QgsComposerAttributeTableV2::setDisplayOnlyVisibleFeatures( bool visibleOnl
emit changed();
}

void QgsComposerAttributeTableV2::setFilterFeatures( bool filter )
void QgsComposerAttributeTableV2::setFilterToAtlasFeature( const bool filterToAtlas )
{
if ( filterToAtlas == mFilterToAtlasIntersection )
{
return;
}

mFilterToAtlasIntersection = filterToAtlas;
refreshAttributes();
emit changed();
}

void QgsComposerAttributeTableV2::setFilterFeatures( const bool filter )
{
if ( filter == mFilterFeatures )
{
Expand Down Expand Up @@ -505,6 +519,21 @@ bool QgsComposerAttributeTableV2::getTableContents( QgsComposerTableContents &co
continue;
}
}
//check against atlas feature intersection
if ( mFilterToAtlasIntersection )
{
if ( !f.geometry() || ! mComposition->atlasComposition().enabled() )
{
continue;
}
QgsFeature* atlasFeature = mComposition->atlasComposition().currentFeature();
if ( !atlasFeature || !atlasFeature->geometry() ||
!f.geometry()->intersects( atlasFeature->geometry() ) )
{
//feature falls outside current atlas feature
continue;
}
}

QgsComposerTableRow currentRow;

Expand Down Expand Up @@ -616,6 +645,7 @@ bool QgsComposerAttributeTableV2::writeXML( QDomElement& elem, QDomDocument & do
composerTableElem.setAttribute( "source", QString::number(( int )mSource ) );
composerTableElem.setAttribute( "relationId", mRelationId );
composerTableElem.setAttribute( "showOnlyVisibleFeatures", mShowOnlyVisibleFeatures );
composerTableElem.setAttribute( "filterToAtlasIntersection", mFilterToAtlasIntersection );
composerTableElem.setAttribute( "maxFeatures", mMaximumNumberOfFeatures );
composerTableElem.setAttribute( "filterFeatures", mFilterFeatures ? "true" : "false" );
composerTableElem.setAttribute( "featureFilter", mFeatureFilter );
Expand Down Expand Up @@ -669,6 +699,7 @@ bool QgsComposerAttributeTableV2::readXML( const QDomElement& itemElem, const QD
}

mShowOnlyVisibleFeatures = itemElem.attribute( "showOnlyVisibleFeatures", "1" ).toInt();
mFilterToAtlasIntersection = itemElem.attribute( "filterToAtlasIntersection", "0" ).toInt();
mFilterFeatures = itemElem.attribute( "filterFeatures", "false" ) == "true" ? true : false;
mFeatureFilter = itemElem.attribute( "featureFilter", "" );
mMaximumNumberOfFeatures = itemElem.attribute( "maxFeatures", "5" ).toInt();
Expand Down
24 changes: 21 additions & 3 deletions src/core/composer/qgscomposerattributetablev2.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class CORE_EXPORT QgsComposerAttributeTableV2: public QgsComposerTableV2
* @param features maximum number of features to show in the table
* @see maximumNumberOfFeatures
*/
void setMaximumNumberOfFeatures( int features );
void setMaximumNumberOfFeatures( const int features );

/**Returns the maximum number of features to be shown by the table.
* @returns maximum number of features
Expand All @@ -177,7 +177,7 @@ class CORE_EXPORT QgsComposerAttributeTableV2: public QgsComposerTableV2
* @see displayOnlyVisibleFeatures
* @see setComposerMap
*/
void setDisplayOnlyVisibleFeatures( bool visibleOnly );
void setDisplayOnlyVisibleFeatures( const bool visibleOnly );

/**Returns true if the table is set to show only features visible on a corresponding
* composer map item.
Expand All @@ -187,6 +187,21 @@ class CORE_EXPORT QgsComposerAttributeTableV2: public QgsComposerTableV2
*/
bool displayOnlyVisibleFeatures() const { return mShowOnlyVisibleFeatures; }

/**Sets attribute table to only show features which intersect the current atlas
* feature.
* @param filterToAtlas set to true to show only features which intersect
* the atlas feature
* @see filterToAtlasFeature
*/
void setFilterToAtlasFeature( const bool filterToAtlas );

/**Returns true if the table is set to only show features which intersect the current atlas
* feature.
* @returns true if table only shows features which intersect the atlas feature
* @see setFilterToAtlasFeature
*/
bool filterToAtlasFeature() const { return mFilterToAtlasIntersection; }

/**Returns true if a feature filter is active on the attribute table
* @returns bool state of the feature filter
* @see setFilterFeatures
Expand All @@ -201,7 +216,7 @@ class CORE_EXPORT QgsComposerAttributeTableV2: public QgsComposerTableV2
* @see filterFeatures
* @see setFeatureFilter
*/
void setFilterFeatures( bool filter );
void setFilterFeatures( const bool filter );

/**Returns the current expression used to filter features for the table. The filter is only
* active if filterFeatures() is true.
Expand Down Expand Up @@ -266,6 +281,9 @@ class CORE_EXPORT QgsComposerAttributeTableV2: public QgsComposerTableV2
/**Shows only the features that are visible in the associated composer map (true by default)*/
bool mShowOnlyVisibleFeatures;

/**Shows only the features that intersect the current atlas feature*/
bool mFilterToAtlasIntersection;

/**True if feature filtering enabled*/
bool mFilterFeatures;
/**Feature filter expression*/
Expand Down
18 changes: 13 additions & 5 deletions src/ui/qgscomposerattributetablewidgetbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
<property name="geometry">
<rect>
<x>0</x>
<y>-195</y>
<y>0</y>
<width>392</width>
<height>1020</height>
<height>1048</height>
</rect>
</property>
<layout class="QVBoxLayout" name="mainLayout">
Expand Down Expand Up @@ -161,7 +161,7 @@
<item row="1" column="0" colspan="2">
<widget class="QCheckBox" name="mShowOnlyVisibleFeaturesCheckBox">
<property name="text">
<string>Show only visible features</string>
<string>Show only features visible within a map</string>
</property>
</widget>
</item>
Expand All @@ -181,14 +181,14 @@
<item row="2" column="1">
<widget class="QComboBox" name="mComposerMapComboBox"/>
</item>
<item row="3" column="0">
<item row="4" column="0">
<widget class="QCheckBox" name="mFeatureFilterCheckBox">
<property name="text">
<string>Filter with</string>
</property>
</widget>
</item>
<item row="3" column="1">
<item row="4" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="mFeatureFilterEdit"/>
Expand All @@ -206,13 +206,21 @@
</item>
</layout>
</item>
<item row="3" column="0" colspan="2">
<widget class="QCheckBox" name="mIntersectAtlasCheckBox">
<property name="text">
<string>Show only features intersecting atlas feature</string>
</property>
</widget>
</item>
</layout>
<zorder>mShowOnlyVisibleFeaturesCheckBox</zorder>
<zorder>mComposerMapLabel</zorder>
<zorder>mComposerMapComboBox</zorder>
<zorder>mFeatureFilterCheckBox</zorder>
<zorder>mMaximumRowsSpinBox</zorder>
<zorder>mMaxNumFeaturesLabel</zorder>
<zorder>mIntersectAtlasCheckBox</zorder>
</widget>
</item>
<item>
Expand Down

0 comments on commit 30ada28

Please sign in to comment.