Skip to content

Commit

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

The rotation wasn't being taken into account, which meant that
visible features weren't shown (and vice versa)
  • Loading branch information
nyalldawson committed Jun 9, 2019
1 parent fe7f510 commit 762ea69
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/core/layout/qgslayoutitemattributetable.cpp
Expand Up @@ -29,6 +29,7 @@
#include "qgsexception.h"
#include "qgsmapsettings.h"
#include "qgsexpressioncontextutils.h"
#include "qgsgeometryengine.h"

//QgsLayoutAttributeTableCompare

Expand Down Expand Up @@ -236,12 +237,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 @@ -422,23 +425,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 )
return false;
}
}
visibleMapEngine.reset( QgsGeometry::createGeometryEngine( visibleRegion.constGet() ) );
visibleMapEngine->prepareGeometry();
}

if ( mSource == QgsLayoutItemAttributeTable::RelationChildren )
Expand Down Expand Up @@ -477,6 +486,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 @@ -556,6 +576,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 @@ -736,6 +757,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 @@ -346,6 +346,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 @@ -1444,7 +1452,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 762ea69

Please sign in to comment.