Skip to content

Commit

Permalink
[layouts] Fix filtering of attribute table to visible features
Browse files Browse the repository at this point in the history
when linked map is rotated

The rotation wasn't being taken into account, which meant that
visible features weren't shown (and vice versa)

(cherry picked from commit 762ea69)
  • Loading branch information
nyalldawson committed Jun 10, 2019
1 parent ecedfac commit 2e6025f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
26 changes: 24 additions & 2 deletions src/core/layout/qgslayoutitemattributetable.cpp
Expand Up @@ -28,6 +28,7 @@
#include "qgsgeometry.h"
#include "qgsexception.h"
#include "qgsmapsettings.h"
#include "qgsgeometryengine.h"

//QgsLayoutAttributeTableCompare

Expand Down Expand Up @@ -235,12 +236,14 @@ void QgsLayoutItemAttributeTable::setMap( QgsLayoutItemMap *map )
{
//disconnect from previous map
disconnect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutTable::refreshAttributes );
disconnect( mMap, &QgsLayoutItemMap::mapRotationChanged, this, &QgsLayoutTable::refreshAttributes );
}
mMap = map;
if ( mMap )
{
//listen out for extent changes in linked map
connect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutTable::refreshAttributes );
connect( mMap, &QgsLayoutItemMap::mapRotationChanged, this, &QgsLayoutTable::refreshAttributes );
}
refreshAttributes();
emit changed();
Expand Down Expand Up @@ -421,23 +424,29 @@ bool QgsLayoutItemAttributeTable::getTableContents( QgsLayoutTableContents &cont
}

QgsRectangle selectionRect;
QgsGeometry visibleRegion;
std::unique_ptr< QgsGeometryEngine > visibleMapEngine;
if ( mMap && mShowOnlyVisibleFeatures )
{
selectionRect = mMap->extent();
visibleRegion = QgsGeometry::fromQPolygonF( mMap->visibleExtentPolygon() );
selectionRect = visibleRegion.boundingBox();
if ( layer )
{
//transform back to layer CRS
QgsCoordinateTransform coordTransform( layer->crs(), mMap->crs(), mLayout->project() );
try
{
selectionRect = coordTransform.transformBoundingBox( selectionRect, QgsCoordinateTransform::ReverseTransform );
visibleRegion.transform( coordTransform, QgsCoordinateTransform::ReverseTransform );
}
catch ( QgsCsException &cse )
{
Q_UNUSED( cse );
Q_UNUSED( cse )
return false;
}
}
visibleMapEngine.reset( QgsGeometry::createGeometryEngine( visibleRegion.constGet() ) );
visibleMapEngine->prepareGeometry();
}

if ( mSource == QgsLayoutItemAttributeTable::RelationChildren )
Expand Down Expand Up @@ -476,6 +485,17 @@ bool QgsLayoutItemAttributeTable::getTableContents( QgsLayoutTableContents &cont
continue;
}
}

// check against exact map bounds
if ( visibleMapEngine )
{
if ( !f.hasGeometry() )
continue;

if ( !visibleMapEngine->intersects( f.geometry().constGet() ) )
continue;
}

//check against atlas feature intersection
if ( mFilterToAtlasIntersection )
{
Expand Down Expand Up @@ -555,6 +575,7 @@ void QgsLayoutItemAttributeTable::finalizeRestoreFromXml()
{
//if we have found a valid map item, listen out to extent changes on it and refresh the table
connect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutTable::refreshAttributes );
connect( mMap, &QgsLayoutItemMap::mapRotationChanged, this, &QgsLayoutTable::refreshAttributes );
}
}
}
Expand Down Expand Up @@ -735,6 +756,7 @@ bool QgsLayoutItemAttributeTable::readPropertiesFromElement( const QDomElement &
if ( mMap )
{
disconnect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutTable::refreshAttributes );
disconnect( mMap, &QgsLayoutItemMap::mapRotationChanged, this, &QgsLayoutTable::refreshAttributes );
mMap = nullptr;
}
// setting new mMap occurs in finalizeRestoreFromXml
Expand Down
10 changes: 9 additions & 1 deletion tests/src/core/testqgslayouttable.cpp
Expand Up @@ -345,6 +345,14 @@ void TestQgsLayoutTable::attributeTableVisibleOnly()

//retrieve rows and check
compareTable( table, expectedRows );

// with rotation
map->setMapRotation( 90 );
expectedRows.clear();
row.clear();
row << QStringLiteral( "Jet" ) << QStringLiteral( "90" ) << QStringLiteral( "3" ) << QStringLiteral( "2" ) << QStringLiteral( "0" ) << QStringLiteral( "2" );
expectedRows.append( row );
compareTable( table, expectedRows );
}

void TestQgsLayoutTable::attributeTableRender()
Expand Down Expand Up @@ -1443,7 +1451,7 @@ void TestQgsLayoutTable::wrappedText()

QFont f;
QString sourceText( "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua" );
QString wrapText = t->wrappedText( sourceText, 100 /*columnWidth*/, f );
QString wrapText = t->wrappedText( sourceText, 101 /*columnWidth*/, f );
//there should be no line break before the last word (bug #20546)
QVERIFY( !wrapText.endsWith( "\naliqua" ) );
}
Expand Down

0 comments on commit 2e6025f

Please sign in to comment.