QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgslayoutitemhtml.h
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutitemhtml.h
3 ------------------------------------------------------------
4 begin : October 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSLAYOUTITEMHTML_H
17#define QGSLAYOUTITEMHTML_H
18
19#include "qgis_core.h"
20#include "qgis_sip.h"
21#include "qgsdistancearea.h"
22#include "qgsfeature.h"
23#include "qgslayoutmultiframe.h"
24
25#include <QUrl>
26
27class QgsWebPage;
28class QImage;
30class QgsVectorLayer;
32
37class CORE_EXPORT QgsLayoutItemHtml : public QgsLayoutMultiFrame
38{
39 Q_OBJECT
40
41 public:
48
55
56 ~QgsLayoutItemHtml() override;
57
58 int type() const override;
59 QIcon icon() const override;
60
64 static QgsLayoutItemHtml *create( QgsLayout *layout ) SIP_FACTORY;
65
71 static QgsLayoutItemHtml *createFromLabel( QgsLayoutItemLabel *label ) SIP_FACTORY;
72
79 void setContentMode( ContentMode mode ) { mContentMode = mode; }
80
87 ContentMode contentMode() const { return mContentMode; }
88
96 void setUrl( const QUrl &url );
97
104 QUrl url() const { return mUrl; }
105
115 void setHtml( const QString &html );
116
123 QString html() const { return mHtml; }
124
132 bool evaluateExpressions() const { return mEvaluateExpressions; }
133
141 void setEvaluateExpressions( bool evaluateExpressions );
142
148 bool useSmartBreaks() const { return mUseSmartBreaks; }
149
155 void setUseSmartBreaks( bool useSmartBreaks );
156
167 void setMaxBreakDistance( double distance );
168
177 double maxBreakDistance() const { return mMaxBreakDistance; }
178
188 void setUserStylesheet( const QString &stylesheet );
189
196 QString userStylesheet() const { return mUserStylesheet; }
197
203 void setUserStylesheetEnabled( bool enabled );
204
210 bool userStylesheetEnabled() const { return mEnableUserStylesheet; }
211
212 QString displayName() const override;
213 QSizeF totalSize() const override;
214 void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex ) override;
215
216 //overridden to break frames without dividing lines of text
217 double findNearbyPageBreak( double yPos ) override;
218
219 public slots:
220
229 void loadHtml( bool useCache = false, const QgsExpressionContext *context = nullptr );
230
232 void recalculateFrameSizes() override;
233
235
236 protected:
237 bool writePropertiesToElement( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
238 bool readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context ) override;
239
240 private:
241 ContentMode mContentMode = QgsLayoutItemHtml::Url;
242 QUrl mUrl;
243 std::unique_ptr< QgsWebPage > mWebPage;
244 QString mHtml;
245 QString mFetchedHtml;
246 QString mLastFetchedUrl;
247 QString mActualFetchedUrl; //may be different if page was redirected
248 QSizeF mSize; //total size in mm
249 double mHtmlUnitsToLayoutUnits = 1.0;
250 QImage mRenderedPage;
251 bool mEvaluateExpressions = true;
252 bool mUseSmartBreaks = true;
253 double mMaxBreakDistance = 10.0;
254
255 QgsFeature mExpressionFeature;
256 QgsVectorLayer *mExpressionLayer = nullptr;
257 QgsDistanceArea mDistanceArea;
258
259 QString mUserStylesheet;
260 bool mEnableUserStylesheet = false;
261
263 QString mAtlasFeatureJSON;
264
265 QgsNetworkContentFetcher *mFetcher = nullptr;
266
267 double htmlUnitsToLayoutUnits(); //calculate scale factor
268
269 //renders a snapshot of the page to a cached image
270 void renderCachedImage();
271
272 //fetches html content from a url and returns it as a string
273 QString fetchHtml( const QUrl &url );
274
276 void setExpressionContext( const QgsFeature &feature, QgsVectorLayer *layer );
277
279 double maxFrameWidth() const;
280
281 void refreshExpressionContext();
282
283 QgsLayoutItemHtml( const QgsLayoutItemHtml & ) = delete;
284 QgsLayoutItemHtml &operator=( const QgsLayoutItemHtml & ) = delete;
285};
286
288#ifndef SIP_RUN
289class JavascriptExecutorLoop : public QEventLoop
290{
291 Q_OBJECT
292 public slots:
293
294 void done();
295 void execIfNotDone();
296 void reportError( const QString &error );
297
298 private:
299 bool mDone = false;
300};
301#endif
303
304#endif // QGSLAYOUTITEMHTML_H
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
A layout multiframe subclass for HTML content.
bool userStylesheetEnabled() const
Returns whether user stylesheets are enabled for the HTML content.
ContentMode contentMode() const
Returns the source mode for item's HTML content.
ContentMode
Source modes for the HTML content to render in the item.
@ ManualHtml
HTML content is manually set for the item.
@ Url
Using this mode item fetches its content via a url.
QString userStylesheet() const
Returns the user stylesheet CSS rules used while rendering the HTML content.
QString html() const
Returns the HTML source displayed in the item if the item is using the QgsLayoutItemHtml::ManualHtml ...
double maxBreakDistance() const
Returns the maximum distance allowed when calculating where to place page breaks in the html.
bool evaluateExpressions() const
Returns whether html item will evaluate QGIS expressions prior to rendering the HTML content.
QUrl url() const
Returns the URL of the content displayed in the item if the item is using the QgsLayoutItemHtml::Url ...
void setContentMode(ContentMode mode)
Sets the source mode for item's HTML content.
QgsLayoutItemHtml(QgsLayout *layout)
Constructor for QgsLayoutItemHtml, with the specified parent layout.
bool useSmartBreaks() const
Returns whether html item is using smart breaks.
A layout item subclass for text labels.
Contains settings and helpers relating to a render of a QgsLayoutItem.
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores multiframe state within an XML DOM element.
virtual QSizeF totalSize() const =0
Returns the total size of the multiframe's content, in layout units.
QgsLayoutMultiFrame(QgsLayout *layout)
Construct a new multiframe item, attached to the specified layout.
virtual int type() const =0
Returns unique multiframe type id.
virtual QIcon icon() const
Returns the item's icon.
virtual double findNearbyPageBreak(double yPos)
Finds the optimal position to break a frame at.
virtual void recalculateFrameSizes()
Recalculates the portion of the multiframe item which is shown in each of its component frames.
virtual void refreshDataDefinedProperty(QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::DataDefinedProperty::AllProperties)
Refreshes a data defined property for the multi frame by reevaluating the property's value and redraw...
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets multiframe state from a DOM element.
virtual QString displayName() const
Returns the multiframe display name.
virtual void render(QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex)=0
Renders a portion of the multiframe's content into a render context.
DataDefinedProperty
Data defined properties for different item types.
@ AllProperties
All properties for item.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:50
HTTP network content fetcher.
A container for the context for various read/write operations on objects.
Represents a vector layer which manages a vector based dataset.
QWebPage subclass which redirects JavaScript errors and console output to the QGIS message log.
Definition qgswebpage.h:179
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52
#define SIP_FACTORY
Definition qgis_sip.h:83