QGIS API Documentation 3.41.0-Master (af5edcb665c)
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 "qgslayoutmultiframe.h"
22#include "qgsfeature.h"
23#include "qgsdistancearea.h"
24#include <QUrl>
25
26class QgsWebPage;
27class QImage;
29class QgsVectorLayer;
31
36class CORE_EXPORT QgsLayoutItemHtml: public QgsLayoutMultiFrame
37{
38 Q_OBJECT
39
40 public:
41
44 {
46 ManualHtml
47 };
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
238 bool writePropertiesToElement( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
239 bool readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context ) override;
240
241 private:
242 ContentMode mContentMode = QgsLayoutItemHtml::Url;
243 QUrl mUrl;
244 std::unique_ptr< QgsWebPage > mWebPage;
245 QString mHtml;
246 QString mFetchedHtml;
247 QString mLastFetchedUrl;
248 QString mActualFetchedUrl; //may be different if page was redirected
249 QSizeF mSize; //total size in mm
250 double mHtmlUnitsToLayoutUnits = 1.0;
251 QImage mRenderedPage;
252 bool mEvaluateExpressions = true;
253 bool mUseSmartBreaks = true;
254 double mMaxBreakDistance = 10.0;
255
256 QgsFeature mExpressionFeature;
257 QgsVectorLayer *mExpressionLayer = nullptr;
258 QgsDistanceArea mDistanceArea;
259
260 QString mUserStylesheet;
261 bool mEnableUserStylesheet = false;
262
264 QString mAtlasFeatureJSON;
265
266 QgsNetworkContentFetcher *mFetcher = nullptr;
267
268 double htmlUnitsToLayoutUnits(); //calculate scale factor
269
270 //renders a snapshot of the page to a cached image
271 void renderCachedImage();
272
273 //fetches html content from a url and returns it as a string
274 QString fetchHtml( const QUrl &url );
275
277 void setExpressionContext( const QgsFeature &feature, QgsVectorLayer *layer );
278
280 double maxFrameWidth() const;
281
282 void refreshExpressionContext();
283
284 QgsLayoutItemHtml( const QgsLayoutItemHtml & ) = delete;
285 QgsLayoutItemHtml &operator=( const QgsLayoutItemHtml & ) = delete;
286};
287
289#ifndef SIP_RUN
290class JavascriptExecutorLoop : public QEventLoop
291{
292 Q_OBJECT
293 public slots:
294
295 void done();
296 void execIfNotDone();
297 void reportError( const QString &error );
298
299 private:
300
301 bool mDone = false;
302
303};
304#endif
306
307#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:58
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.
@ 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.
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.
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
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.
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:49
HTTP network content fetcher.
The class is used as a container of context for various read/write operations on other objects.
Represents a vector layer which manages a vector based data sets.
QWebPage subclass which redirects JavaScript errors and console output to the QGIS message log.
Definition qgswebpage.h:217
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_FACTORY
Definition qgis_sip.h:76