Restore HTML item atlas functionality

This commit is contained in:
Nyall Dawson 2017-12-23 15:22:30 +10:00
parent 8072d4d357
commit fee1c211a6
3 changed files with 25 additions and 46 deletions

View File

@ -352,14 +352,11 @@ void QgsLayoutHtmlWidget::mInsertExpressionButton_clicked()
mHtmlEditor->getCursorPosition( &line, &index ); mHtmlEditor->getCursorPosition( &line, &index );
} }
#if 0 //TODO
// use the atlas coverage layer, if any // use the atlas coverage layer, if any
QgsVectorLayer *coverageLayer = atlasCoverageLayer(); QgsVectorLayer *layer = coverageLayer();
#endif
QgsVectorLayer *coverageLayer = nullptr;
QgsExpressionContext context = mHtml->createExpressionContext(); QgsExpressionContext context = mHtml->createExpressionContext();
QgsExpressionBuilderDialog exprDlg( coverageLayer, selText, this, QStringLiteral( "generic" ), context ); QgsExpressionBuilderDialog exprDlg( layer, selText, this, QStringLiteral( "generic" ), context );
exprDlg.setWindowTitle( tr( "Insert Expression" ) ); exprDlg.setWindowTitle( tr( "Insert Expression" ) );
if ( exprDlg.exec() == QDialog::Accepted ) if ( exprDlg.exec() == QDialog::Accepted )
{ {

View File

@ -28,6 +28,7 @@
#include "qgsmapsettings.h" #include "qgsmapsettings.h"
#include "qgswebpage.h" #include "qgswebpage.h"
#include "qgswebframe.h" #include "qgswebframe.h"
#include "qgslayoutitemmap.h"
#include <QCoreApplication> #include <QCoreApplication>
#include <QPainter> #include <QPainter>
@ -50,23 +51,11 @@ QgsLayoutItemHtml::QgsLayoutItemHtml( QgsLayout *layout )
mWebPage->setNetworkAccessManager( QgsNetworkAccessManager::instance() ); mWebPage->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
#if 0 //TODO //a html item added to a layout needs to have the initial expression context set,
if ( mLayout ) //otherwise fields in the html aren't correctly evaluated until atlas preview feature changes (#9457)
{ setExpressionContext( mLayout->context().feature(), mLayout->context().layer() );
connect( mLayout, &QgsComposition::itemRemoved, this, &QgsComposerMultiFrame::handleFrameRemoval );
}
if ( mComposition && mComposition->atlasMode() == QgsComposition::PreviewAtlas ) connect( &mLayout->context(), &QgsLayoutContext::changed, this, &QgsLayoutItemHtml::refreshExpressionContext );
{
//a html item added while atlas preview is enabled needs to have the expression context set,
//otherwise fields in the html aren't correctly evaluated until atlas preview feature changes (#9457)
setExpressionContext( mComposition->atlasComposition().feature(), mComposition->atlasComposition().coverageLayer() );
}
//connect to atlas feature changes
//to update the expression context
connect( &mComposition->atlasComposition(), &QgsAtlasComposition::featureChanged, this, &QgsLayoutItemHtml::refreshExpressionContext );
#endif
mFetcher = new QgsNetworkContentFetcher(); mFetcher = new QgsNetworkContentFetcher();
} }
@ -496,22 +485,27 @@ void QgsLayoutItemHtml::setExpressionContext( const QgsFeature &feature, QgsVect
} }
else if ( mLayout ) else if ( mLayout )
{ {
#if 0 //TODO
//set to composition's mapsettings' crs //set to composition's mapsettings' crs
QgsComposerMap *referenceMap = mComposition->referenceMap(); QgsLayoutItemMap *referenceMap = mLayout->referenceMap();
if ( referenceMap ) if ( referenceMap )
mDistanceArea->setSourceCrs( referenceMap->crs() ); mDistanceArea.setSourceCrs( referenceMap->crs(), mLayout->project()->transformContext() );
#endif
} }
if ( mLayout ) if ( mLayout )
{ {
mDistanceArea.setEllipsoid( mLayout->project()->ellipsoid() ); mDistanceArea.setEllipsoid( mLayout->project()->ellipsoid() );
} }
// create JSON representation of feature if ( feature.isValid() )
QgsJsonExporter exporter( layer ); {
exporter.setIncludeRelated( true ); // create JSON representation of feature
mAtlasFeatureJSON = exporter.exportFeature( feature ); QgsJsonExporter exporter( layer );
exporter.setIncludeRelated( true );
mAtlasFeatureJSON = exporter.exportFeature( feature );
}
else
{
mAtlasFeatureJSON.clear();
}
} }
void QgsLayoutItemHtml::refreshExpressionContext() void QgsLayoutItemHtml::refreshExpressionContext()
@ -519,16 +513,11 @@ void QgsLayoutItemHtml::refreshExpressionContext()
QgsVectorLayer *vl = nullptr; QgsVectorLayer *vl = nullptr;
QgsFeature feature; QgsFeature feature;
#if 0 //TODO if ( mLayout )
if ( mComposition->atlasComposition().enabled() )
{ {
vl = mComposition->atlasComposition().coverageLayer(); vl = mLayout->context().layer();
feature = mLayout->context().feature();
} }
if ( mComposition->atlasMode() != QgsComposition::AtlasOff )
{
feature = mComposition->atlasComposition().feature();
}
#endif
setExpressionContext( feature, vl ); setExpressionContext( feature, vl );
loadHtml( true ); loadHtml( true );

View File

@ -67,13 +67,6 @@ QgsLayoutItemLabel::QgsLayoutItemLabel( QgsLayout *layout )
//otherwise fields in the label aren't correctly evaluated until atlas preview feature changes (#9457) //otherwise fields in the label aren't correctly evaluated until atlas preview feature changes (#9457)
refreshExpressionContext(); refreshExpressionContext();
if ( mLayout )
{
//connect to context feature changes
//to update the expression context
connect( &mLayout->context(), &QgsLayoutContext::changed, this, &QgsLayoutItemLabel::refreshExpressionContext );
}
mWebPage.reset( new QgsWebPage( this ) ); mWebPage.reset( new QgsWebPage( this ) );
mWebPage->setIdentifier( tr( "Layout label item" ) ); mWebPage->setIdentifier( tr( "Layout label item" ) );
mWebPage->setNetworkAccessManager( QgsNetworkAccessManager::instance() ); mWebPage->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
@ -253,7 +246,7 @@ void QgsLayoutItemLabel::refreshExpressionContext()
//set to composition's reference map's crs //set to composition's reference map's crs
QgsLayoutItemMap *referenceMap = mLayout->referenceMap(); QgsLayoutItemMap *referenceMap = mLayout->referenceMap();
if ( referenceMap ) if ( referenceMap )
mDistanceArea->setSourceCrs( referenceMap->crs() ); mDistanceArea->setSourceCrs( referenceMap->crs(), mLayout->project()->transformContext() );
} }
mDistanceArea->setEllipsoid( mLayout->project()->ellipsoid() ); mDistanceArea->setEllipsoid( mLayout->project()->ellipsoid() );
contentChanged(); contentChanged();
@ -493,7 +486,7 @@ void QgsLayoutItemLabel::setFrameStrokeWidth( const QgsLayoutMeasurement &stroke
void QgsLayoutItemLabel::refresh() void QgsLayoutItemLabel::refresh()
{ {
QgsLayoutItem::refresh(); QgsLayoutItem::refresh();
contentChanged(); refreshExpressionContext();
} }
void QgsLayoutItemLabel::itemShiftAdjustSize( double newWidth, double newHeight, double &xShift, double &yShift ) const void QgsLayoutItemLabel::itemShiftAdjustSize( double newWidth, double newHeight, double &xShift, double &yShift ) const