QGIS API Documentation  2.14.0-Essen
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 QWebPage;
24 class QImage;
25 class QgsVectorLayer;
27 class QgsDistanceArea;
28 
29 class CORE_EXPORT QgsComposerHtml: public QgsComposerMultiFrame
30 {
31  Q_OBJECT
32  public:
33 
37  {
38  Url,
39  ManualHtml
40  };
41 
42  QgsComposerHtml( QgsComposition* c, bool createUndoCommands );
43 
44  ~QgsComposerHtml();
45 
53  void setContentMode( ContentMode mode ) { mContentMode = mode; }
54 
62  ContentMode contentMode() const { return mContentMode; }
63 
71  void setUrl( const QUrl& url );
72 
79  const QUrl& url() const { return mUrl; }
80 
91  void setHtml( const QString& html );
92 
100  QString html() const { return mHtml; }
101 
110  bool evaluateExpressions() const { return mEvaluateExpressions; }
111 
120  void setEvaluateExpressions( bool evaluateExpressions );
121 
127  bool useSmartBreaks() const { return mUseSmartBreaks; }
128 
135  void setUseSmartBreaks( bool useSmartBreaks );
136 
149  void setMaxBreakDistance( double maxBreakDistance );
150 
160  double maxBreakDistance() const { return mMaxBreakDistance; }
161 
172  void setUserStylesheet( const QString& stylesheet );
173 
181  QString userStylesheet() const { return mUserStylesheet; }
182 
189  void setUserStylesheetEnabled( const bool stylesheetEnabled );
190 
197  bool userStylesheetEnabled() const { return mEnableUserStylesheet; }
198 
199  virtual QString displayName() const override;
200  QSizeF totalSize() const override;
201  void render( QPainter* p, const QRectF& renderExtent, const int frameIndex ) override;
202  bool writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames = false ) const override;
203  bool readXML( const QDomElement& itemElem, const QDomDocument& doc, bool ignoreFrames = false ) override;
204  void addFrame( QgsComposerFrame* frame, bool recalcFrameSizes = true ) override;
205  //overridden to break frames without dividing lines of text
206  double findNearbyPageBreak( double yPos ) override;
207 
208  public slots:
209 
217  void loadHtml( const bool useCache = false, const QgsExpressionContext* context = nullptr );
218 
220  void recalculateFrameSizes() override;
221  void refreshExpressionContext();
222 
223  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext* context = nullptr ) override;
224 
225  private slots:
226  void frameLoaded( bool ok = true );
227 
228  private:
229  ContentMode mContentMode;
230  QUrl mUrl;
231  QWebPage* mWebPage;
232  QString mHtml;
233  QString mFetchedHtml;
234  QString mLastFetchedUrl;
235  QString mActualFetchedUrl; //may be different if page was redirected
236  bool mLoaded;
237  QSizeF mSize; //total size in mm
238  double mHtmlUnitsToMM;
239  QImage* mRenderedPage;
240  bool mEvaluateExpressions;
241  bool mUseSmartBreaks;
242  double mMaxBreakDistance;
243 
244  QgsFeature mExpressionFeature;
245  QgsVectorLayer* mExpressionLayer;
246  QgsDistanceArea* mDistanceArea;
247 
248  QString mUserStylesheet;
249  bool mEnableUserStylesheet;
250 
251  QgsNetworkContentFetcher* mFetcher;
252 
253  double htmlUnitsToMM(); //calculate scale factor
254 
255  //renders a snapshot of the page to a cached image
256  void renderCachedImage();
257 
258  //fetches html content from a url and returns it as a string
259  QString fetchHtml( const QUrl& url );
260 
262  void setExpressionContext( const QgsFeature& feature, QgsVectorLayer* layer );
263 
265  double maxFrameWidth() const;
266 };
267 
268 #endif // QGSCOMPOSERHTML_H
virtual void recalculateFrameSizes()
Recalculates the portion of the multiframe item which is shown in each of it&#39;s component frames...
ContentMode contentMode() const
Returns the source mode for item&#39;s HTML content.
virtual QString displayName() const
Get multiframe display name.
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 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
DataDefinedProperty
Data defined properties for different item types.
QString userStylesheet() const
Returns the user stylesheet CSS rules used while rendering the HTML content.
bool useSmartBreaks() const
Returns whether html item is using smart breaks.
QString html() const
Returns the HTML source displayed in the item if the item is using the QgsComposerHtml::ManualHtml mo...
virtual void addFrame(QgsComposerFrame *frame, bool recalcFrameSizes=true)=0
Adds a frame to the multiframe.
HTTP network content fetcher.
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...
The QWebPage class is a collection of stubs to mimic the API of a QWebPage on systems where QtWebkit ...
Definition: qgswebpage.h:99
Graphics scene for map printing.
Frame item for a composer multiframe item.
const QUrl & url() const
Returns the URL of the content displayed in the item if the item is using the QgsComposerHtml::Url mo...
void setContentMode(ContentMode mode)
Sets the source mode for item&#39;s HTML content.
General purpose distance and area calculator.
bool userStylesheetEnabled() const
Returns whether user stylesheets are enabled for the HTML content.
virtual bool writeXML(QDomElement &elem, QDomDocument &doc, bool ignoreFrames=false) const =0
Stores state information about multiframe in DOM element.
double maxBreakDistance() const
Returns the maximum distance allowed when calculating where to place page breaks in the html...
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.