QGIS API Documentation  2.99.0-Master (40f86b2)
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 "qgscomposermultiframe.h"
21 #include "qgsfeature.h"
22 #include <QUrl>
23 
24 class QgsWebPage;
25 class QImage;
26 class QgsVectorLayer;
28 class QgsDistanceArea;
29 
33 class CORE_EXPORT QgsComposerHtml: public QgsComposerMultiFrame
34 {
35  Q_OBJECT
36  public:
37 
41  {
42  Url,
43  ManualHtml
44  };
45 
46  QgsComposerHtml( QgsComposition *c, bool createUndoCommands );
47 
48  ~QgsComposerHtml();
49 
57  void setContentMode( ContentMode mode ) { mContentMode = mode; }
58 
66  ContentMode contentMode() const { return mContentMode; }
67 
75  void setUrl( const QUrl &url );
76 
83  QUrl url() const { return mUrl; }
84 
95  void setHtml( const QString &html );
96 
104  QString html() const { return mHtml; }
105 
114  bool evaluateExpressions() const { return mEvaluateExpressions; }
115 
124  void setEvaluateExpressions( bool evaluateExpressions );
125 
131  bool useSmartBreaks() const { return mUseSmartBreaks; }
132 
139  void setUseSmartBreaks( bool useSmartBreaks );
140 
153  void setMaxBreakDistance( double maxBreakDistance );
154 
164  double maxBreakDistance() const { return mMaxBreakDistance; }
165 
176  void setUserStylesheet( const QString &stylesheet );
177 
185  QString userStylesheet() const { return mUserStylesheet; }
186 
193  void setUserStylesheetEnabled( const bool stylesheetEnabled );
194 
201  bool userStylesheetEnabled() const { return mEnableUserStylesheet; }
202 
203  virtual QString displayName() const override;
204  QSizeF totalSize() const override;
205  void render( QPainter *p, const QRectF &renderExtent, const int frameIndex ) override;
206  bool writeXml( QDomElement &elem, QDomDocument &doc, bool ignoreFrames = false ) const override;
207  bool readXml( const QDomElement &itemElem, const QDomDocument &doc, bool ignoreFrames = false ) override;
208  void addFrame( QgsComposerFrame *frame, bool recalcFrameSizes = true ) override;
209  //overridden to break frames without dividing lines of text
210  double findNearbyPageBreak( double yPos ) override;
211 
212  public slots:
213 
221  void loadHtml( const bool useCache = false, const QgsExpressionContext *context = nullptr );
222 
224  void recalculateFrameSizes() override;
225  void refreshExpressionContext();
226 
227  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext *context = nullptr ) override;
228 
229  private slots:
230  void frameLoaded( bool ok = true );
231 
232  private:
233  ContentMode mContentMode;
234  QUrl mUrl;
235  QgsWebPage *mWebPage = nullptr;
236  QString mHtml;
237  QString mFetchedHtml;
238  QString mLastFetchedUrl;
239  QString mActualFetchedUrl; //may be different if page was redirected
240  bool mLoaded;
241  QSizeF mSize; //total size in mm
242  double mHtmlUnitsToMM;
243  QImage *mRenderedPage = nullptr;
244  bool mEvaluateExpressions;
245  bool mUseSmartBreaks;
246  double mMaxBreakDistance;
247 
248  QgsFeature mExpressionFeature;
249  QgsVectorLayer *mExpressionLayer = nullptr;
250  QgsDistanceArea *mDistanceArea = nullptr;
251 
252  QString mUserStylesheet;
253  bool mEnableUserStylesheet;
254 
256  QString mAtlasFeatureJSON;
257 
258  QgsNetworkContentFetcher *mFetcher = nullptr;
259 
260  double htmlUnitsToMM(); //calculate scale factor
261 
262  //renders a snapshot of the page to a cached image
263  void renderCachedImage();
264 
265  //fetches html content from a url and returns it as a string
266  QString fetchHtml( const QUrl &url );
267 
269  void setExpressionContext( const QgsFeature &feature, QgsVectorLayer *layer );
270 
272  double maxFrameWidth() const;
273 };
274 
275 #endif // QGSCOMPOSERHTML_H
virtual void recalculateFrameSizes()
Recalculates the portion of the multiframe item which is shown in each of it&#39;s component frames...
QString userStylesheet() const
Returns the user stylesheet CSS rules used while rendering the HTML content.
virtual double findNearbyPageBreak(double yPos)
Finds the optimal position to break a frame at.
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:136
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.
General purpose distance and area calculator.
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.
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:208
All properties for item.