33 #include <QCoreApplication> 36 #include <QNetworkReply> 43 , mHtmlUnitsToMM( 1.0 )
44 , mRenderedPage( nullptr )
45 , mEvaluateExpressions( true )
46 , mUseSmartBreaks( true )
47 , mMaxBreakDistance( 10 )
48 , mExpressionLayer( nullptr )
49 , mDistanceArea( nullptr )
50 , mEnableUserStylesheet( false )
54 mHtmlUnitsToMM = htmlUnitsToMM();
62 palette.
setBrush( QPalette::Base, Qt::transparent );
66 QObject::connect( mWebPage, SIGNAL( loadFinished(
bool ) ),
this, SLOT( frameLoaded(
bool ) ) );
87 connect( mFetcher, SIGNAL( finished() ),
this, SLOT( frameLoaded() ) );
139 evalContext = scopedContext.
data();
143 switch ( mContentMode )
161 if ( !( useCache && currentUrl == mLastFetchedUrl ) )
163 loadedHtml = fetchHtml(
QUrl( currentUrl ) );
164 mLastFetchedUrl = currentUrl;
168 loadedHtml = mFetchedHtml;
179 if ( mEvaluateExpressions )
191 QUrl( mActualFetchedUrl ) :
197 if ( mEnableUserStylesheet && ! mUserStylesheet.
isEmpty() )
201 QUrl cssFileURL = QUrl(
"data:text/css;charset=utf-8;base64," + ba.
toBase64() );
211 qApp->processEvents();
215 if ( !mAtlasFeatureJSON.
isEmpty() )
219 qApp->processEvents();
227 void QgsComposerHtml::frameLoaded(
bool ok )
233 double QgsComposerHtml::maxFrameWidth()
const 239 maxWidth = qMax( maxWidth, static_cast< double >(( *frameIt )->boundingRect().width() ) );
252 double maxWidth = maxFrameWidth();
254 contentsSize.
setWidth( maxWidth * mHtmlUnitsToMM );
267 void QgsComposerHtml::renderCachedImage()
272 delete mRenderedPage;
275 if ( mRenderedPage->
isNull() )
279 mRenderedPage->
fill( Qt::transparent );
281 painter.
begin( mRenderedPage );
294 qApp->processEvents();
309 Q_UNUSED( frameIndex );
318 p->
scale( 1.0 / mHtmlUnitsToMM, 1.0 / mHtmlUnitsToMM );
319 p->
translate( 0.0, -renderExtent.
top() * mHtmlUnitsToMM );
324 double QgsComposerHtml::htmlUnitsToMM()
343 if ( recalcFrameSizes )
351 if ( c1.second < c2.second )
353 else if ( c1.second > c2.second )
355 else if ( c1.first > c2.first )
363 if ( !mWebPage || !mRenderedPage || !mUseSmartBreaks )
369 int idealPos = yPos * htmlUnitsToMM();
372 if ( idealPos >= mRenderedPage->
height() )
377 int maxSearchDistance = mMaxBreakDistance * htmlUnitsToMM();
383 bool currentPixelTransparent =
false;
384 bool previousPixelTransparent =
false;
387 int minRow = qMax( idealPos - maxSearchDistance, 0 );
388 for (
int candidateRow = idealPos; candidateRow >= minRow; --candidateRow )
391 currentColor = qRgba( 0, 0, 0, 0 );
393 for (
int col = 0; col < mRenderedPage->
width(); ++col )
399 pixelColor = mRenderedPage->
pixel( col, candidateRow );
400 currentPixelTransparent = qAlpha( pixelColor ) == 0;
401 if ( pixelColor != currentColor && !( currentPixelTransparent && previousPixelTransparent ) )
404 currentColor = pixelColor;
407 previousPixelTransparent = currentPixelTransparent;
409 candidates.
append( qMakePair( candidateRow, changes ) );
420 int maxCandidateRow = candidates[0].
first;
421 int minCandidateRow = maxCandidateRow + 1;
422 int minCandidateChanges = candidates[0].second;
425 for ( it = candidates.
begin(); it != candidates.
end(); ++it )
427 if (( *it ).second != minCandidateChanges || ( *it ).first != minCandidateRow - 1 )
432 return ( minCandidateRow + ( maxCandidateRow - minCandidateRow ) / 2 ) / htmlUnitsToMM();
434 minCandidateRow = ( *it ).
first;
439 return candidates[0].
first / htmlUnitsToMM();
458 mUserStylesheet = stylesheet;
467 if ( mEnableUserStylesheet != stylesheetEnabled )
469 mEnableUserStylesheet = stylesheetEnabled;
477 return tr(
"<HTML frame>" );
486 htmlElem.
setAttribute(
"evaluateExpressions", mEvaluateExpressions ?
"true" :
"false" );
487 htmlElem.
setAttribute(
"useSmartBreaks", mUseSmartBreaks ?
"true" :
"false" );
490 htmlElem.
setAttribute(
"stylesheetEnabled", mEnableUserStylesheet ?
"true" :
"false" );
492 bool state =
_writeXML( htmlElem, doc, ignoreFrames );
505 if ( !
_readXML( itemElem, doc, ignoreFrames ) )
512 if ( !contentModeOK )
516 mEvaluateExpressions = itemElem.
attribute(
"evaluateExpressions",
"true" ) ==
"true" ? true :
false;
517 mUseSmartBreaks = itemElem.
attribute(
"useSmartBreaks",
"true" ) ==
"true" ? true :
false;
520 mUserStylesheet = itemElem.
attribute(
"stylesheet" );
521 mEnableUserStylesheet = itemElem.
attribute(
"stylesheetEnabled",
"false" ) ==
"true" ? true :
false;
538 mExpressionFeature = feature;
539 mExpressionLayer = layer;
577 setExpressionContext( feature, vl );
588 evalContext = scopedContext.
data();
void setBrush(ColorRole role, const QBrush &brush)
virtual void recalculateFrameSizes()
Recalculates the portion of the multiframe item which is shown in each of it's component frames...
void setRenderHint(RenderHint hint, bool on)
QDomNode appendChild(const QDomNode &newChild)
void push_back(const T &value)
void setIdentifier(const QString &identifier)
Sets an identifier for the QgsWebPage.
QString attribute(const QString &name, const QString &defValue) const
bool writeXML(QDomElement &elem, QDomDocument &doc, bool ignoreFrames=false) const override
Stores state information about multiframe in DOM element.
virtual QString displayName() const override
Get multiframe display name.
void setSourceCrs(long srsid)
sets source spatial reference system (by QGIS CRS)
int printResolution() const
void scale(qreal sx, qreal sy)
const QgsCoordinateReferenceSystem & crs() const
Returns layer's spatial reference system.
QMap< QgsComposerObject::DataDefinedProperty, QString > mDataDefinedNames
Map of data defined properties for the item to string name to use when exporting item to xml...
A item that forms part of a map composition.
virtual void refreshDataDefinedProperty(const DataDefinedProperty property=AllProperties, const QgsExpressionContext *context=nullptr)
Refreshes a data defined property for the item by reevaluating the property's value and redrawing the...
bool hasCrsTransformEnabled() const
returns true if projections are enabled for this layer set
QgsComposerHtml(QgsComposition *c, bool createUndoCommands)
QNetworkReply * reply()
Returns a reference to the network reply.
bool setEllipsoid(const QString &ellipsoid)
Sets ellipsoid by its acronym.
void setEvaluateExpressions(bool evaluateExpressions)
Sets whether the html item will evaluate QGIS expressions prior to rendering the HTML content...
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
double maxBreakDistance() const
Returns the maximum distance allowed when calculating where to place page breaks in the html...
double toDouble(bool *ok) const
void setIncludeRelated(bool includeRelated)
Sets whether to include attributes of features linked via references in the JSON exports.
QString toString(QFlags< QUrl::FormattingOption > options) const
QString contentAsString() const
Returns the fetched content as a string.
QString tr(const char *sourceText, const char *disambiguation, int n)
Handles exporting QgsFeature features to GeoJSON features.
QgsComposition::AtlasMode atlasMode() const
Returns the current atlas mode of the composition.
DataDefinedProperty
Data defined properties for different item types.
QSizeF totalSize() const override
Returns the total size of the multiframe's content.
const QString GEO_NONE
Constant that holds the string representation for "No ellips/No CRS".
void loadHtml(const bool useCache=false, const QgsExpressionContext *context=nullptr)
Reloads the html source from the url and redraws the item.
void setHtml(const QString &html)
Sets the HTML to display in the item when the item is using the QgsComposerHtml::ManualHtml mode...
bool _readXML(const QDomElement &itemElem, const QDomDocument &doc, bool ignoreFrames=false)
Restores state information about base multiframe object from a DOM element.
QRgb pixel(int x, int y) const
QString number(int n, int base)
void append(const T &value)
QVariant property(const char *name) const
void setHtml(const QString &html, const QUrl &baseUrl)
void setUserStyleSheetUrl(const QUrl &location)
void fill(uint pixelValue)
HTTP network content fetcher.
const QUrl & url() const
Returns the URL of the content displayed in the item if the item is using the QgsComposerHtml::Url mo...
void setAttribute(const QString &name, const QString &value)
void setWidth(qreal width)
bool useSmartBreaks() const
Returns whether html item is using smart breaks.
void recalculateFrameSizes() override
Recalculates the frame sizes for the current viewport dimensions.
int toInt(bool *ok, int base) const
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Abstract base class for composer items with the ability to distribute the content to several frames (...
void addFrame(QgsComposerFrame *frame, bool recalcFrameSizes=true) override
Adds a frame to the multiframe.
QList< QgsComposerFrame * > mFrameItems
QWebFrame * mainFrame() const
void setMaxBreakDistance(double maxBreakDistance)
Sets the maximum distance allowed when calculating where to place page breaks in the html...
bool dataDefinedEvaluate(const QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue, const QgsExpressionContext &context=QgsExpressionContext()) const
Evaluate a data defined property and return the calculated value.
void setUseSmartBreaks(bool useSmartBreaks)
Sets whether the html item should use smart breaks.
bool evaluateExpressions() const
Returns whether html item will evaluate QGIS expressions prior to rendering the HTML content...
QVariant evaluateJavaScript(const QString &scriptSource)
QString html() const
Returns the HTML source displayed in the item if the item is using the QgsComposerHtml::ManualHtml mo...
Graphics scene for map printing.
const QgsMapSettings & mapSettings() const
Return setting of QGIS map canvas.
Frame item for a composer multiframe item.
QByteArray & append(char ch)
QWebSettings * settings() const
General purpose distance and area calculator.
virtual void refreshDataDefinedProperty(const QgsComposerObject::DataDefinedProperty property=QgsComposerObject::AllProperties, const QgsExpressionContext *context=nullptr) override
QgsComposition * mComposition
void deleteFrames()
Removes and deletes all child frames.
void setUserStylesheet(const QString &stylesheet)
Sets the user stylesheet CSS rules to use while rendering the HTML content.
void refreshExpressionContext()
void render(QPainter *painter)
void contentsChanged()
Emitted when the contents of the multi frame have changed and the frames must be redrawn.
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
int frameCount() const
Returns the number of frames associated with this multiframe.
void addComposerHtmlFrame(QgsComposerHtml *html, QgsComposerFrame *frame)
Adds composer html frame and advises composer to create a widget for it (through signal) ...
void setUrl(const QUrl &url)
Sets the URL for content to display in the item when the item is using the QgsComposerHtml::Url mode...
static QgsProject * instance()
Returns the QgsProject singleton instance.
void translate(const QPointF &offset)
static Q_DECL_DEPRECATED QString replaceExpressionText(const QString &action, const QgsFeature *feat, QgsVectorLayer *layer, const QMap< QString, QVariant > *substitutionMap=nullptr, const QgsDistanceArea *distanceArea=nullptr)
This function currently replaces each expression between [% and %] in the string with the result of i...
bool candidateSort(QPair< int, int > c1, QPair< int, int > c2)
int frameIndex(QgsComposerFrame *frame) const
Returns the index of a frame within the multiframe.
QgsVectorLayer * coverageLayer() const
Returns the coverage layer used for the atlas features.
QgsAtlasComposition & atlasComposition()
iterator insert(const Key &key, const T &value)
QgsFeature feature() const
Returns the current atlas feature.
QByteArray toBase64() const
void setUserStylesheetEnabled(const bool stylesheetEnabled)
Sets whether user stylesheets are enabled for the HTML content.
bool readXML(const QDomElement &itemElem, const QDomDocument &doc, bool ignoreFrames=false) override
Reads multiframe state information from a DOM element.
const_iterator constEnd() const
QDomElement createElement(const QString &tagName)
const_iterator constBegin() const
long srsid() const
Returns the SrsId, if available.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void fetchContent(const QUrl &url)
Fetches content from a remote URL and handles redirects.
Represents a vector layer which manages a vector based data sets.
bool begin(QPaintDevice *device)
ContentMode
Source modes for the HTML content to render in the item.
void setHeight(qreal height)
void setViewportSize(const QSize &size) const
double findNearbyPageBreak(double yPos) override
Finds the optimal position to break a frame at.
QgsComposerFrame * frame(int i) const
Returns a child frame from the multiframe.
void setNetworkAccessManager(QNetworkAccessManager *manager)
QString exportFeature(const QgsFeature &feature, const QVariantMap &extraProperties=QVariantMap(), const QVariant &id=QVariant()) const
Returns a GeoJSON string representation of a feature.
void handleFrameRemoval(QgsComposerItem *item)
Called before a frame is going to be removed.
QWebPage subclass which redirects JavaScript errors and console output to the QGIS message log...
const QgsCoordinateReferenceSystem & destinationCrs() const
returns CRS of destination coordinate reference system
bool enabled() const
Returns whether the atlas generation is enabled.
virtual QgsExpressionContext * createExpressionContext() const
Creates an expression context relating to the objects' current state.
bool _writeXML(QDomElement &elem, QDomDocument &doc, bool ignoreFrames=false) const
Stores state information about base multiframe object in DOM element.
void setEllipsoidalMode(bool flag)
Sets whether coordinates must be projected to ellipsoid before measuring.
QUrl fromLocalFile(const QString &localFile)
The QWebSettings class is a collection of stubs to mimic the API of a QWebSettings on systems where Q...
void render(QPainter *p, const QRectF &renderExtent, const int frameIndex) override
Renders a portion of the multiframe's content into a painter.
void changed()
Emitted when the properties of a multi frame have changed, and the GUI item widget must be updated...
QByteArray toUtf8() const