QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgscomposermultiframe.h"
20 #include "qgsfeature.h"
21 #include <QUrl>
22 
23 class QgsWebPage;
24 class QImage;
25 class QgsVectorLayer;
27 class QgsDistanceArea;
28 
32 class CORE_EXPORT QgsComposerHtml: public QgsComposerMultiFrame
33 {
34  Q_OBJECT
35  public:
36 
40  {
41  Url,
42  ManualHtml
43  };
44 
45  QgsComposerHtml( QgsComposition* c, bool createUndoCommands );
46 
47  ~QgsComposerHtml();
48 
56  void setContentMode( ContentMode mode ) { mContentMode = mode; }
57 
65  ContentMode contentMode() const { return mContentMode; }
66 
74  void setUrl( const QUrl& url );
75 
82  const QUrl& url() const { return mUrl; }
83 
94  void setHtml( const QString& html );
95 
103  QString html() const { return mHtml; }
104 
113  bool evaluateExpressions() const { return mEvaluateExpressions; }
114 
123  void setEvaluateExpressions( bool evaluateExpressions );
124 
130  bool useSmartBreaks() const { return mUseSmartBreaks; }
131 
138  void setUseSmartBreaks( bool useSmartBreaks );
139 
152  void setMaxBreakDistance( double maxBreakDistance );
153 
163  double maxBreakDistance() const { return mMaxBreakDistance; }
164 
175  void setUserStylesheet( const QString& stylesheet );
176 
184  QString userStylesheet() const { return mUserStylesheet; }
185 
192  void setUserStylesheetEnabled( const bool stylesheetEnabled );
193 
200  bool userStylesheetEnabled() const { return mEnableUserStylesheet; }
201 
202  virtual QString displayName() const override;
203  QSizeF totalSize() const override;
204  void render( QPainter* p, const QRectF& renderExtent, const int frameIndex ) override;
205  bool writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames = false ) const override;
206  bool readXML( const QDomElement& itemElem, const QDomDocument& doc, bool ignoreFrames = false ) override;
207  void addFrame( QgsComposerFrame* frame, bool recalcFrameSizes = true ) override;
208  //overridden to break frames without dividing lines of text
209  double findNearbyPageBreak( double yPos ) override;
210 
211  public slots:
212 
220  void loadHtml( const bool useCache = false, const QgsExpressionContext* context = nullptr );
221 
223  void recalculateFrameSizes() override;
224  void refreshExpressionContext();
225 
226  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext* context = nullptr ) override;
227 
228  private slots:
229  void frameLoaded( bool ok = true );
230 
231  private:
232  ContentMode mContentMode;
233  QUrl mUrl;
234  QgsWebPage* mWebPage;
235  QString mHtml;
236  QString mFetchedHtml;
237  QString mLastFetchedUrl;
238  QString mActualFetchedUrl; //may be different if page was redirected
239  bool mLoaded;
240  QSizeF mSize; //total size in mm
241  double mHtmlUnitsToMM;
242  QImage* mRenderedPage;
243  bool mEvaluateExpressions;
244  bool mUseSmartBreaks;
245  double mMaxBreakDistance;
246 
247  QgsFeature mExpressionFeature;
248  QgsVectorLayer* mExpressionLayer;
249  QgsDistanceArea* mDistanceArea;
250 
251  QString mUserStylesheet;
252  bool mEnableUserStylesheet;
253 
255  QString mAtlasFeatureJSON;
256 
257  QgsNetworkContentFetcher* mFetcher;
258 
259  double htmlUnitsToMM(); //calculate scale factor
260 
261  //renders a snapshot of the page to a cached image
262  void renderCachedImage();
263 
264  //fetches html content from a url and returns it as a string
265  QString fetchHtml( const QUrl& url );
266 
268  void setExpressionContext( const QgsFeature& feature, QgsVectorLayer* layer );
269 
271  double maxFrameWidth() const;
272 };
273 
274 #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...
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:187
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.
const QUrl & url() const
Returns the URL of the content displayed in the item if the item is using the QgsComposerHtml::Url mo...
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 Q_DECL_DEPRECATED void render(QPainter *p, const QRectF &renderExtent)
Renders a portion of the multiframe&#39;s content into a painter.
virtual bool readXML(const QDomElement &itemElem, const QDomDocument &doc, bool ignoreFrames=false)=0
Reads multiframe state information from a DOM element.
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:211