QGIS API Documentation  2.99.0-Master (19b062c)
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 
35 class CORE_EXPORT QgsComposerHtml: public QgsComposerMultiFrame
36 {
37  Q_OBJECT
38  public:
39 
44  {
45  Url,
46  ManualHtml
47  };
48 
49  QgsComposerHtml( QgsComposition *c SIP_TRANSFERTHIS, bool createUndoCommands );
50 
51  ~QgsComposerHtml();
52 
61  void setContentMode( ContentMode mode ) { mContentMode = mode; }
62 
71  ContentMode contentMode() const { return mContentMode; }
72 
81  void setUrl( const QUrl &url );
82 
90  QUrl url() const { return mUrl; }
91 
103  void setHtml( const QString &html );
104 
113  QString html() const { return mHtml; }
114 
124  bool evaluateExpressions() const { return mEvaluateExpressions; }
125 
135  void setEvaluateExpressions( bool evaluateExpressions );
136 
143  bool useSmartBreaks() const { return mUseSmartBreaks; }
144 
152  void setUseSmartBreaks( bool useSmartBreaks );
153 
167  void setMaxBreakDistance( double maxBreakDistance );
168 
179  double maxBreakDistance() const { return mMaxBreakDistance; }
180 
192  void setUserStylesheet( const QString &stylesheet );
193 
202  QString userStylesheet() const { return mUserStylesheet; }
203 
211  void setUserStylesheetEnabled( const bool stylesheetEnabled );
212 
220  bool userStylesheetEnabled() const { return mEnableUserStylesheet; }
221 
222  virtual QString displayName() const override;
223  QSizeF totalSize() const override;
224  void render( QPainter *p, const QRectF &renderExtent, const int frameIndex ) override;
225  bool writeXml( QDomElement &elem, QDomDocument &doc, bool ignoreFrames = false ) const override;
226  bool readXml( const QDomElement &itemElem, const QDomDocument &doc, bool ignoreFrames = false ) override;
227  void addFrame( QgsComposerFrame *frame SIP_TRANSFER, bool recalcFrameSizes = true ) override;
228  //overridden to break frames without dividing lines of text
229  double findNearbyPageBreak( double yPos ) override;
230 
231  public slots:
232 
241  void loadHtml( const bool useCache = false, const QgsExpressionContext *context = nullptr );
242 
244  void recalculateFrameSizes() override;
245  void refreshExpressionContext();
246 
247  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext *context = nullptr ) override;
248 
249  private slots:
250  void frameLoaded( bool ok = true );
251 
252  private:
253  ContentMode mContentMode;
254  QUrl mUrl;
255  QgsWebPage *mWebPage = nullptr;
256  QString mHtml;
257  QString mFetchedHtml;
258  QString mLastFetchedUrl;
259  QString mActualFetchedUrl; //may be different if page was redirected
260  bool mLoaded;
261  QSizeF mSize; //total size in mm
262  double mHtmlUnitsToMM;
263  QImage *mRenderedPage = nullptr;
264  bool mEvaluateExpressions;
265  bool mUseSmartBreaks;
266  double mMaxBreakDistance;
267 
268  QgsFeature mExpressionFeature;
269  QgsVectorLayer *mExpressionLayer = nullptr;
270  QgsDistanceArea *mDistanceArea = nullptr;
271 
272  QString mUserStylesheet;
273  bool mEnableUserStylesheet;
274 
276  QString mAtlasFeatureJSON;
277 
278  QgsNetworkContentFetcher *mFetcher = nullptr;
279 
280  double htmlUnitsToMM(); //calculate scale factor
281 
282  //renders a snapshot of the page to a cached image
283  void renderCachedImage();
284 
285  //fetches html content from a url and returns it as a string
286  QString fetchHtml( const QUrl &url );
287 
289  void setExpressionContext( const QgsFeature &feature, QgsVectorLayer *layer );
290 
292  double maxFrameWidth() const;
293 };
294 
295 #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:46
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:62
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 (...
#define SIP_TRANSFER
Definition: qgis_sip.h:36
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:216
All properties for item.