QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
26 class QgsWebPage;
27 class QImage;
28 class QgsVectorLayer;
30 
36 class CORE_EXPORT QgsLayoutItemHtml: public QgsLayoutMultiFrame
37 {
38  Q_OBJECT
39 
40  public:
41 
44  {
45  Url,
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 
72  void setContentMode( ContentMode mode ) { mContentMode = mode; }
73 
80  ContentMode contentMode() const { return mContentMode; }
81 
89  void setUrl( const QUrl &url );
90 
97  QUrl url() const { return mUrl; }
98 
108  void setHtml( const QString &html );
109 
116  QString html() const { return mHtml; }
117 
125  bool evaluateExpressions() const { return mEvaluateExpressions; }
126 
134  void setEvaluateExpressions( bool evaluateExpressions );
135 
141  bool useSmartBreaks() const { return mUseSmartBreaks; }
142 
148  void setUseSmartBreaks( bool useSmartBreaks );
149 
160  void setMaxBreakDistance( double distance );
161 
170  double maxBreakDistance() const { return mMaxBreakDistance; }
171 
181  void setUserStylesheet( const QString &stylesheet );
182 
189  QString userStylesheet() const { return mUserStylesheet; }
190 
196  void setUserStylesheetEnabled( bool enabled );
197 
203  bool userStylesheetEnabled() const { return mEnableUserStylesheet; }
204 
205  QString displayName() const override;
206  QSizeF totalSize() const override;
207  void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex ) override;
208 
209  //overridden to break frames without dividing lines of text
210  double findNearbyPageBreak( double yPos ) override;
211 
212  public slots:
213 
222  void loadHtml( bool useCache = false, const QgsExpressionContext *context = nullptr );
223 
225  void recalculateFrameSizes() override;
226 
228 
229  protected:
230 
231  bool writePropertiesToElement( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
232  bool readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context ) override;
233 
234  private:
235  ContentMode mContentMode = QgsLayoutItemHtml::Url;
236  QUrl mUrl;
237  std::unique_ptr< QgsWebPage > mWebPage;
238  QString mHtml;
239  QString mFetchedHtml;
240  QString mLastFetchedUrl;
241  QString mActualFetchedUrl; //may be different if page was redirected
242  QSizeF mSize; //total size in mm
243  double mHtmlUnitsToLayoutUnits = 1.0;
244  QImage mRenderedPage;
245  bool mEvaluateExpressions = true;
246  bool mUseSmartBreaks = true;
247  double mMaxBreakDistance = 10.0;
248 
249  QgsFeature mExpressionFeature;
250  QgsVectorLayer *mExpressionLayer = nullptr;
251  QgsDistanceArea mDistanceArea;
252 
253  QString mUserStylesheet;
254  bool mEnableUserStylesheet = false;
255 
257  QString mAtlasFeatureJSON;
258 
259  QgsNetworkContentFetcher *mFetcher = nullptr;
260 
261  double htmlUnitsToLayoutUnits(); //calculate scale factor
262 
263  //renders a snapshot of the page to a cached image
264  void renderCachedImage();
265 
266  //fetches html content from a url and returns it as a string
267  QString fetchHtml( const QUrl &url );
268 
270  void setExpressionContext( const QgsFeature &feature, QgsVectorLayer *layer );
271 
273  double maxFrameWidth() const;
274 
275  void refreshExpressionContext();
276 
277  QgsLayoutItemHtml( const QgsLayoutItemHtml & ) = delete;
278  QgsLayoutItemHtml &operator=( const QgsLayoutItemHtml & ) = delete;
279 };
280 
282 #ifndef SIP_RUN
283 class JavascriptExecutorLoop : public QEventLoop
284 {
285  Q_OBJECT
286  public slots:
287 
288  void done();
289  void execIfNotDone();
290  void reportError( const QString &error );
291 
292  private:
293 
294  bool mDone = false;
295 
296 };
297 #endif
298 
300 #endif // QGSLAYOUTITEMHTML_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsLayoutMultiFrame::totalSize
virtual QSizeF totalSize() const =0
Returns the total size of the multiframe's content, in layout units.
QgsLayoutMultiFrame::findNearbyPageBreak
virtual double findNearbyPageBreak(double yPos)
Finds the optimal position to break a frame at.
Definition: qgslayoutmultiframe.cpp:51
QgsLayoutItemHtml::evaluateExpressions
bool evaluateExpressions() const
Returns whether html item will evaluate QGIS expressions prior to rendering the HTML content.
Definition: qgslayoutitemhtml.h:125
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsLayoutMultiFrame::refreshDataDefinedProperty
virtual void refreshDataDefinedProperty(QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::AllProperties)
Refreshes a data defined property for the multi frame by reevaluating the property's value and redraw...
Definition: qgslayoutmultiframe.cpp:242
QgsLayoutItemRenderContext
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:44
qgslayoutmultiframe.h
qgsfeature.h
QgsLayoutItemHtml
A layout multiframe subclass for HTML content.
Definition: qgslayoutitemhtml.h:36
QgsLayoutMultiFrame
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
Definition: qgslayoutmultiframe.h:48
QgsLayoutMultiFrame::icon
virtual QIcon icon() const
Returns the item's icon.
Definition: qgslayoutmultiframe.h:154
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsWebPage
QWebPage subclass which redirects JavaScript errors and console output to the QGIS message log.
Definition: qgswebpage.h:217
QgsLayoutItemHtml::userStylesheet
QString userStylesheet() const
Returns the user stylesheet CSS rules used while rendering the HTML content.
Definition: qgslayoutitemhtml.h:189
QgsLayoutItemHtml::ContentMode
ContentMode
Source modes for the HTML content to render in the item.
Definition: qgslayoutitemhtml.h:43
QgsLayoutObject::AllProperties
@ AllProperties
All properties for item.
Definition: qgslayoutobject.h:135
QgsLayoutMultiFrame::type
virtual int type() const =0
Returns unique multiframe type id.
QgsNetworkContentFetcher
HTTP network content fetcher. A simple method for fetching remote HTTP content and converting the con...
Definition: qgsnetworkcontentfetcher.h:39
QgsLayoutItemHtml::maxBreakDistance
double maxBreakDistance() const
Returns the maximum distance allowed when calculating where to place page breaks in the html.
Definition: qgslayoutitemhtml.h:170
QgsLayoutItemHtml::useSmartBreaks
bool useSmartBreaks() const
Returns whether html item is using smart breaks.
Definition: qgslayoutitemhtml.h:141
qgis_sip.h
QgsLayoutMultiFrame::writePropertiesToElement
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores multiframe state within an XML DOM element.
Definition: qgslayoutmultiframe.cpp:573
QgsLayoutItemHtml::html
QString html() const
Returns the HTML source displayed in the item if the item is using the QgsLayoutItemHtml::ManualHtml ...
Definition: qgslayoutitemhtml.h:116
QgsLayoutMultiFrame::render
virtual void render(QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex)=0
Renders a portion of the multiframe's content into a render context.
QgsLayoutItemHtml::userStylesheetEnabled
bool userStylesheetEnabled() const
Returns whether user stylesheets are enabled for the HTML content.
Definition: qgslayoutitemhtml.h:203
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsLayoutItemHtml::contentMode
ContentMode contentMode() const
Returns the source mode for item's HTML content.
Definition: qgslayoutitemhtml.h:80
QgsLayoutItemHtml::url
QUrl url() const
Returns the URL of the content displayed in the item if the item is using the QgsLayoutItemHtml::Url ...
Definition: qgslayoutitemhtml.h:97
QgsDistanceArea
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
Definition: qgsdistancearea.h:52
qgsdistancearea.h
QgsLayoutMultiFrame::recalculateFrameSizes
virtual void recalculateFrameSizes()
Recalculates the portion of the multiframe item which is shown in each of its component frames.
Definition: qgslayoutmultiframe.cpp:96
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsLayoutMultiFrame::readPropertiesFromElement
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets multiframe state from a DOM element.
Definition: qgslayoutmultiframe.cpp:578
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsLayoutMultiFrame::displayName
virtual QString displayName() const
Returns the multiframe display name.
Definition: qgslayoutmultiframe.cpp:273
QgsLayoutItemHtml::setContentMode
void setContentMode(ContentMode mode)
Sets the source mode for item's HTML content.
Definition: qgslayoutitemhtml.h:72
QgsLayoutObject::DataDefinedProperty
DataDefinedProperty
Data defined properties for different item types.
Definition: qgslayoutobject.h:132
QgsLayoutItemHtml::Url
@ Url
Using this mode item fetches its content via a url.
Definition: qgslayoutitemhtml.h:45