QGIS API Documentation  2.99.0-Master (d55fa22)
qgscomposerhtml.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposerhtml.h
3  ------------------------------------------------------------
4  begin : July 2012
5  copyright : (C) 2012 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
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 QGSCOMPOSERHTML_H
17 #define QGSCOMPOSERHTML_H
18 
19 #include "qgis_core.h"
20 #include "qgis.h"
21 #include "qgscomposermultiframe.h"
22 #include "qgsfeature.h"
23 #include <QUrl>
24 
25 class QgsWebPage;
26 class QImage;
27 class QgsVectorLayer;
29 class QgsDistanceArea;
30 
34 class CORE_EXPORT QgsComposerHtml: public QgsComposerMultiFrame
35 {
36  Q_OBJECT
37  public:
38 
42  {
43  Url,
44  ManualHtml
45  };
46 
47  QgsComposerHtml( QgsComposition *c SIP_TRANSFERTHIS, bool createUndoCommands );
48 
49  ~QgsComposerHtml();
50 
58  void setContentMode( ContentMode mode ) { mContentMode = mode; }
59 
67  ContentMode contentMode() const { return mContentMode; }
68 
76  void setUrl( const QUrl &url );
77 
84  QUrl url() const { return mUrl; }
85 
96  void setHtml( const QString &html );
97 
105  QString html() const { return mHtml; }
106 
115  bool evaluateExpressions() const { return mEvaluateExpressions; }
116 
125  void setEvaluateExpressions( bool evaluateExpressions );
126 
132  bool useSmartBreaks() const { return mUseSmartBreaks; }
133 
140  void setUseSmartBreaks( bool useSmartBreaks );
141 
154  void setMaxBreakDistance( double maxBreakDistance );
155 
165  double maxBreakDistance() const { return mMaxBreakDistance; }
166 
177  void setUserStylesheet( const QString &stylesheet );
178 
186  QString userStylesheet() const { return mUserStylesheet; }
187 
194  void setUserStylesheetEnabled( const bool stylesheetEnabled );
195 
202  bool userStylesheetEnabled() const { return mEnableUserStylesheet; }
203 
204  virtual QString displayName() const override;
205  QSizeF totalSize() const override;
206  void render( QPainter *p, const QRectF &renderExtent, const int frameIndex ) override;
207  bool writeXml( QDomElement &elem, QDomDocument &doc, bool ignoreFrames = false ) const override;
208  bool readXml( const QDomElement &itemElem, const QDomDocument &doc, bool ignoreFrames = false ) override;
209  void addFrame( QgsComposerFrame *frame, bool recalcFrameSizes = true ) override;
210  //overridden to break frames without dividing lines of text
211  double findNearbyPageBreak( double yPos ) override;
212 
213  public slots:
214 
222  void loadHtml( const bool useCache = false, const QgsExpressionContext *context = nullptr );
223 
225  void recalculateFrameSizes() override;
226  void refreshExpressionContext();
227 
228  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext *context = nullptr ) override;
229 
230  private slots:
231  void frameLoaded( bool ok = true );
232 
233  private:
234  ContentMode mContentMode;
235  QUrl mUrl;
236  QgsWebPage *mWebPage = nullptr;
237  QString mHtml;
238  QString mFetchedHtml;
239  QString mLastFetchedUrl;
240  QString mActualFetchedUrl; //may be different if page was redirected
241  bool mLoaded;
242  QSizeF mSize; //total size in mm
243  double mHtmlUnitsToMM;
244  QImage *mRenderedPage = nullptr;
245  bool mEvaluateExpressions;
246  bool mUseSmartBreaks;
247  double mMaxBreakDistance;
248 
249  QgsFeature mExpressionFeature;
250  QgsVectorLayer *mExpressionLayer = nullptr;
251  QgsDistanceArea *mDistanceArea = nullptr;
252 
253  QString mUserStylesheet;
254  bool mEnableUserStylesheet;
255 
257  QString mAtlasFeatureJSON;
258 
259  QgsNetworkContentFetcher *mFetcher = nullptr;
260 
261  double htmlUnitsToMM(); //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 
276 #endif // QGSCOMPOSERHTML_H
virtual void recalculateFrameSizes()
Recalculates the portion of the multiframe item which is shown in each of it&#39;s component frames...
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:34
QString userStylesheet() const
Returns the user stylesheet CSS rules used while rendering the HTML content.
virtual void refreshDataDefinedProperty(const DataDefinedProperty property=AllProperties, const QgsExpressionContext *context=nullptr)
Refreshes a data defined property for the item by reevaluating the property&#39;s value and redrawing the...
QUrl url() const
Returns the URL of the content displayed in the item if the item is using the QgsComposerHtml::Url mo...
Using this mode item fetches its content via a url.
virtual QString displayName() const
Get multiframe display name.
virtual QSizeF totalSize() const =0
Returns the total size of the multiframe&#39;s content.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:61
double maxBreakDistance() const
Returns the maximum distance allowed when calculating where to place page breaks in the html...
DataDefinedProperty
Data defined properties for different item types.
ContentMode contentMode() const
Returns the source mode for item&#39;s HTML content.
virtual void addFrame(QgsComposerFrame *frame, bool recalcFrameSizes=true)=0
Adds a frame to the multiframe.
HTTP network content fetcher.
virtual bool readXml(const QDomElement &itemElem, const QDomDocument &doc, bool ignoreFrames=false)=0
Reads multiframe state information from a DOM element.
bool useSmartBreaks() const
Returns whether html item is using smart breaks.
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 (...
bool evaluateExpressions() const
Returns whether html item will evaluate QGIS expressions prior to rendering the HTML content...
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.
Frame item for a composer multiframe item.
void setContentMode(ContentMode mode)
Sets the source mode for item&#39;s HTML content.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
virtual bool writeXml(QDomElement &elem, QDomDocument &doc, bool ignoreFrames=false) const =0
Stores state information about multiframe in DOM element.
bool userStylesheetEnabled() const
Returns whether user stylesheets are enabled for the HTML content.
virtual void render(QPainter *painter, const QRectF &renderExtent, const int frameIndex)=0
Renders a portion of the multiframe&#39;s content into a painter.
Represents a vector layer which manages a vector based data sets.
virtual double findNearbyPageBreak(double yPos)
Finds the optimal position to break a frame at.
ContentMode
Source modes for the HTML content to render in the item.
QWebPage subclass which redirects JavaScript errors and console output to the QGIS message log...
Definition: qgswebpage.h:212
All properties for item.