Skip to content

Commit

Permalink
Modernize code
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 24, 2017
1 parent 30ce60b commit 12e69ab
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 37 deletions.
40 changes: 13 additions & 27 deletions src/core/layout/qgslayoutitemhtml.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,16 +36,9 @@


QgsLayoutItemHtml::QgsLayoutItemHtml( QgsLayout *layout ) QgsLayoutItemHtml::QgsLayoutItemHtml( QgsLayout *layout )
: QgsLayoutMultiFrame( layout ) : QgsLayoutMultiFrame( layout )
, mContentMode( QgsLayoutItemHtml::Url )
, mHtmlUnitsToLayoutUnits( 1.0 )
, mEvaluateExpressions( true )
, mUseSmartBreaks( true )
, mMaxBreakDistance( 10 )
, mEnableUserStylesheet( false )
{ {
mDistanceArea = new QgsDistanceArea();
mHtmlUnitsToLayoutUnits = htmlUnitsToLayoutUnits(); mHtmlUnitsToLayoutUnits = htmlUnitsToLayoutUnits();
mWebPage = new QgsWebPage(); mWebPage = qgis::make_unique< QgsWebPage >();
mWebPage->setIdentifier( tr( "Layout HTML item" ) ); mWebPage->setIdentifier( tr( "Layout HTML item" ) );
mWebPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff ); mWebPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
mWebPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff ); mWebPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
Expand Down Expand Up @@ -80,9 +73,6 @@ QgsLayoutItemHtml::QgsLayoutItemHtml( QgsLayout *layout )


QgsLayoutItemHtml::~QgsLayoutItemHtml() QgsLayoutItemHtml::~QgsLayoutItemHtml()
{ {
delete mDistanceArea;
delete mWebPage;
delete mRenderedPage;
mFetcher->deleteLater(); mFetcher->deleteLater();
} }


Expand Down Expand Up @@ -179,13 +169,13 @@ void QgsLayoutItemHtml::loadHtml( const bool useCache, const QgsExpressionContex
//evaluate expressions //evaluate expressions
if ( mEvaluateExpressions ) if ( mEvaluateExpressions )
{ {
loadedHtml = QgsExpression::replaceExpressionText( loadedHtml, evalContext, mDistanceArea ); loadedHtml = QgsExpression::replaceExpressionText( loadedHtml, evalContext, &mDistanceArea );
} }


bool loaded = false; bool loaded = false;


QEventLoop loop; QEventLoop loop;
connect( mWebPage, &QWebPage::loadFinished, &loop, [&loaded, &loop ] { loaded = true; loop.quit(); } ); connect( mWebPage.get(), &QWebPage::loadFinished, &loop, [&loaded, &loop ] { loaded = true; loop.quit(); } );
connect( mFetcher, &QgsNetworkContentFetcher::finished, &loop, [&loaded, &loop ] { loaded = true; loop.quit(); } ); connect( mFetcher, &QgsNetworkContentFetcher::finished, &loop, [&loaded, &loop ] { loaded = true; loop.quit(); } );


//reset page size. otherwise viewport size increases but never decreases again //reset page size. otherwise viewport size increases but never decreases again
Expand Down Expand Up @@ -264,18 +254,14 @@ void QgsLayoutItemHtml::recalculateFrameSizes()
void QgsLayoutItemHtml::renderCachedImage() void QgsLayoutItemHtml::renderCachedImage()
{ {
//render page to cache image //render page to cache image
if ( mRenderedPage ) mRenderedPage = QImage( mWebPage->viewportSize(), QImage::Format_ARGB32 );
{ if ( mRenderedPage.isNull() )
delete mRenderedPage;
}
mRenderedPage = new QImage( mWebPage->viewportSize(), QImage::Format_ARGB32 );
if ( mRenderedPage->isNull() )
{ {
return; return;
} }
mRenderedPage->fill( Qt::transparent ); mRenderedPage.fill( Qt::transparent );
QPainter painter; QPainter painter;
painter.begin( mRenderedPage ); painter.begin( &mRenderedPage );
mWebPage->mainFrame()->render( &painter ); mWebPage->mainFrame()->render( &painter );
painter.end(); painter.end();
} }
Expand Down Expand Up @@ -340,7 +326,7 @@ bool candidateSort( QPair<int, int> c1, QPair<int, int> c2 )


double QgsLayoutItemHtml::findNearbyPageBreak( double yPos ) double QgsLayoutItemHtml::findNearbyPageBreak( double yPos )
{ {
if ( !mWebPage || !mRenderedPage || !mUseSmartBreaks ) if ( !mWebPage || mRenderedPage.isNull() || !mUseSmartBreaks )
{ {
return yPos; return yPos;
} }
Expand All @@ -349,7 +335,7 @@ double QgsLayoutItemHtml::findNearbyPageBreak( double yPos )
int idealPos = yPos * htmlUnitsToLayoutUnits(); int idealPos = yPos * htmlUnitsToLayoutUnits();


//if ideal break pos is past end of page, there's nothing we need to do //if ideal break pos is past end of page, there's nothing we need to do
if ( idealPos >= mRenderedPage->height() ) if ( idealPos >= mRenderedPage.height() )
{ {
return yPos; return yPos;
} }
Expand All @@ -370,13 +356,13 @@ double QgsLayoutItemHtml::findNearbyPageBreak( double yPos )
changes = 0; changes = 0;
currentColor = qRgba( 0, 0, 0, 0 ); currentColor = qRgba( 0, 0, 0, 0 );
//check all pixels in this line //check all pixels in this line
for ( int col = 0; col < mRenderedPage->width(); ++col ) for ( int col = 0; col < mRenderedPage.width(); ++col )
{ {
//count how many times the pixels change color in this row //count how many times the pixels change color in this row
//eventually, we select a row to break at with the minimum number of color changes //eventually, we select a row to break at with the minimum number of color changes
//since this is likely a line break, or gap between table cells, etc //since this is likely a line break, or gap between table cells, etc
//but very unlikely to be midway through a text line or picture //but very unlikely to be midway through a text line or picture
pixelColor = mRenderedPage->pixel( col, candidateRow ); pixelColor = mRenderedPage.pixel( col, candidateRow );
currentPixelTransparent = qAlpha( pixelColor ) == 0; currentPixelTransparent = qAlpha( pixelColor ) == 0;
if ( pixelColor != currentColor && !( currentPixelTransparent && previousPixelTransparent ) ) if ( pixelColor != currentColor && !( currentPixelTransparent && previousPixelTransparent ) )
{ {
Expand Down Expand Up @@ -506,7 +492,7 @@ void QgsLayoutItemHtml::setExpressionContext( const QgsFeature &feature, QgsVect
//setup distance area conversion //setup distance area conversion
if ( layer ) if ( layer )
{ {
mDistanceArea->setSourceCrs( layer->crs() ); mDistanceArea.setSourceCrs( layer->crs() );
} }
else if ( mLayout ) else if ( mLayout )
{ {
Expand All @@ -519,7 +505,7 @@ void QgsLayoutItemHtml::setExpressionContext( const QgsFeature &feature, QgsVect
} }
if ( mLayout ) if ( mLayout )
{ {
mDistanceArea->setEllipsoid( mLayout->project()->ellipsoid() ); mDistanceArea.setEllipsoid( mLayout->project()->ellipsoid() );
} }


// create JSON representation of feature // create JSON representation of feature
Expand Down
20 changes: 10 additions & 10 deletions src/core/layout/qgslayoutitemhtml.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
#include "qgis.h" #include "qgis.h"
#include "qgslayoutmultiframe.h" #include "qgslayoutmultiframe.h"
#include "qgsfeature.h" #include "qgsfeature.h"
#include "qgsdistancearea.h"
#include <QUrl> #include <QUrl>


class QgsWebPage; class QgsWebPage;
class QImage; class QImage;
class QgsVectorLayer; class QgsVectorLayer;
class QgsNetworkContentFetcher; class QgsNetworkContentFetcher;
class QgsDistanceArea;


#ifndef SIP_RUN #ifndef SIP_RUN


Expand Down Expand Up @@ -234,26 +234,26 @@ class CORE_EXPORT QgsLayoutItemHtml: public QgsLayoutMultiFrame
bool readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context ) override; bool readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context ) override;


private: private:
ContentMode mContentMode; ContentMode mContentMode = QgsLayoutItemHtml::Url;
QUrl mUrl; QUrl mUrl;
QgsWebPage *mWebPage = nullptr; std::unique_ptr< QgsWebPage > mWebPage;
QString mHtml; QString mHtml;
QString mFetchedHtml; QString mFetchedHtml;
QString mLastFetchedUrl; QString mLastFetchedUrl;
QString mActualFetchedUrl; //may be different if page was redirected QString mActualFetchedUrl; //may be different if page was redirected
QSizeF mSize; //total size in mm QSizeF mSize; //total size in mm
double mHtmlUnitsToLayoutUnits; double mHtmlUnitsToLayoutUnits = 1.0;
QImage *mRenderedPage = nullptr; QImage mRenderedPage;
bool mEvaluateExpressions; bool mEvaluateExpressions = true;
bool mUseSmartBreaks; bool mUseSmartBreaks = true;
double mMaxBreakDistance; double mMaxBreakDistance = 10.0;


QgsFeature mExpressionFeature; QgsFeature mExpressionFeature;
QgsVectorLayer *mExpressionLayer = nullptr; QgsVectorLayer *mExpressionLayer = nullptr;
QgsDistanceArea *mDistanceArea = nullptr; QgsDistanceArea mDistanceArea;


QString mUserStylesheet; QString mUserStylesheet;
bool mEnableUserStylesheet; bool mEnableUserStylesheet = false;


//! JSON string representation of current atlas feature //! JSON string representation of current atlas feature
QString mAtlasFeatureJSON; QString mAtlasFeatureJSON;
Expand Down

0 comments on commit 12e69ab

Please sign in to comment.