|
| 1 | +/*************************************************************************** |
| 2 | + qgslayoutitemhtml.h |
| 3 | + ------------------------------------------------------------ |
| 4 | + begin : October 2017 |
| 5 | + copyright : (C) 2017 by Nyall Dawson |
| 6 | + email : nyall dot dawson at gmail dot com |
| 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 QGSLAYOUTITEMHTML_H |
| 17 | +#define QGSLAYOUTITEMHTML_H |
| 18 | + |
| 19 | +#include "qgis_core.h" |
| 20 | +#include "qgis.h" |
| 21 | +#include "qgslayoutmultiframe.h" |
| 22 | +#include "qgsfeature.h" |
| 23 | +#include <QUrl> |
| 24 | + |
| 25 | +class QgsWebPage; |
| 26 | +class QImage; |
| 27 | +class QgsVectorLayer; |
| 28 | +class QgsNetworkContentFetcher; |
| 29 | +class QgsDistanceArea; |
| 30 | + |
| 31 | +/** |
| 32 | + * \ingroup core |
| 33 | + * A layout multiframe subclass for HTML content. |
| 34 | + * \since QGIS 3.0 |
| 35 | + */ |
| 36 | +class CORE_EXPORT QgsLayoutItemHtml: public QgsLayoutMultiFrame |
| 37 | +{ |
| 38 | + Q_OBJECT |
| 39 | + |
| 40 | + public: |
| 41 | + |
| 42 | + //! Source modes for the HTML content to render in the item |
| 43 | + enum ContentMode |
| 44 | + { |
| 45 | + Url, //!< Using this mode item fetches its content via a url |
| 46 | + ManualHtml //!< HTML content is manually set for the item |
| 47 | + }; |
| 48 | + |
| 49 | + /** |
| 50 | + * Constructor for QgsLayoutItemHtml, with the specified parent \a layout. |
| 51 | + */ |
| 52 | + QgsLayoutItemHtml( QgsLayout *layout ); |
| 53 | + |
| 54 | + ~QgsLayoutItemHtml(); |
| 55 | + |
| 56 | + /** |
| 57 | + * Sets the source \a mode for item's HTML content. |
| 58 | + * \see contentMode() |
| 59 | + * \see setUrl() |
| 60 | + * \see setHtml() |
| 61 | + */ |
| 62 | + void setContentMode( ContentMode mode ) { mContentMode = mode; } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns the source mode for item's HTML content. |
| 66 | + * \see setContentMode() |
| 67 | + * \see url() |
| 68 | + * \see html() |
| 69 | + */ |
| 70 | + ContentMode contentMode() const { return mContentMode; } |
| 71 | + |
| 72 | + /** |
| 73 | + * Sets the \a url for content to display in the item when the item is using |
| 74 | + * the QgsLayoutItemHtml::Url mode. Content is automatically fetched and the |
| 75 | + * HTML item refreshed after calling this function. |
| 76 | + * \see url() |
| 77 | + * \see contentMode() |
| 78 | + */ |
| 79 | + void setUrl( const QUrl &url ); |
| 80 | + |
| 81 | + /** |
| 82 | + * Returns the URL of the content displayed in the item if the item is using |
| 83 | + * the QgsLayoutItemHtml::Url mode. |
| 84 | + * \see setUrl() |
| 85 | + * \see contentMode() |
| 86 | + */ |
| 87 | + QUrl url() const { return mUrl; } |
| 88 | + |
| 89 | + /** |
| 90 | + * Sets the \a html to display in the item when the item is using |
| 91 | + * the QgsLayoutItemHtml::ManualHtml mode. Setting the HTML using this function |
| 92 | + * does not automatically refresh the item's contents. Call loadHtml to trigger |
| 93 | + * a refresh of the item after setting the HTML content. |
| 94 | + * \see html() |
| 95 | + * \see contentMode() |
| 96 | + * \see loadHtml() |
| 97 | + */ |
| 98 | + void setHtml( const QString &html ); |
| 99 | + |
| 100 | + /** |
| 101 | + * Returns the HTML source displayed in the item if the item is using |
| 102 | + * the QgsLayoutItemHtml::ManualHtml mode. |
| 103 | + * \see setHtml() |
| 104 | + * \see contentMode() |
| 105 | + */ |
| 106 | + QString html() const { return mHtml; } |
| 107 | + |
| 108 | + /** |
| 109 | + * Returns whether html item will evaluate QGIS expressions prior to rendering |
| 110 | + * the HTML content. If set, any content inside [% %] tags will be |
| 111 | + * treated as a QGIS expression and evaluated against the current atlas |
| 112 | + * feature. |
| 113 | + * \see setEvaluateExpressions() |
| 114 | + */ |
| 115 | + bool evaluateExpressions() const { return mEvaluateExpressions; } |
| 116 | + |
| 117 | + /** |
| 118 | + * Sets whether the html item will evaluate QGIS expressions prior to rendering |
| 119 | + * the HTML content. If set, any content inside [% %] tags will be |
| 120 | + * treated as a QGIS expression and evaluated against the current atlas |
| 121 | + * feature. |
| 122 | + * \see evaluateExpressions() |
| 123 | + */ |
| 124 | + void setEvaluateExpressions( bool evaluateExpressions ); |
| 125 | + |
| 126 | + /** |
| 127 | + * Returns whether html item is using smart breaks. Smart breaks prevent |
| 128 | + * the html frame contents from breaking mid-way though a line of text. |
| 129 | + * \see setUseSmartBreaks() |
| 130 | + */ |
| 131 | + bool useSmartBreaks() const { return mUseSmartBreaks; } |
| 132 | + |
| 133 | + /** |
| 134 | + * Sets whether the html item should use smart breaks. Smart breaks prevent |
| 135 | + * the html frame contents from breaking mid-way though a line of text. |
| 136 | + * \see useSmartBreaks() |
| 137 | + */ |
| 138 | + void setUseSmartBreaks( bool useSmartBreaks ); |
| 139 | + |
| 140 | + /** |
| 141 | + * Sets the maximum \a distance allowed when calculating where to place page breaks |
| 142 | + * in the html. This distance is the maximum amount of empty space allowed |
| 143 | + * at the bottom of a frame after calculating the optimum break location. Setting |
| 144 | + * a larger value will result in better choice of page break location, but more |
| 145 | + * wasted space at the bottom of frames. This setting is only effective if |
| 146 | + * useSmartBreaks is true. |
| 147 | + * \see maxBreakDistance() |
| 148 | + * \see setUseSmartBreaks() |
| 149 | + */ |
| 150 | + void setMaxBreakDistance( double distance ); |
| 151 | + |
| 152 | + /** |
| 153 | + * Returns the maximum distance allowed when calculating where to place page breaks |
| 154 | + * in the html. This distance is the maximum amount of empty space allowed |
| 155 | + * at the bottom of a frame after calculating the optimum break location. This setting |
| 156 | + * is only effective if useSmartBreaks is true. |
| 157 | + * \see setMaxBreakDistance() |
| 158 | + * \see useSmartBreaks() |
| 159 | + */ |
| 160 | + double maxBreakDistance() const { return mMaxBreakDistance; } |
| 161 | + |
| 162 | + /** |
| 163 | + * Sets the user \a stylesheet CSS rules to use while rendering the HTML content. These |
| 164 | + * allow for overriding the styles specified within the HTML source. Setting the stylesheet |
| 165 | + * using this function does not automatically refresh the item's contents. Call loadHtml |
| 166 | + * to trigger a refresh of the item after setting the stylesheet rules. |
| 167 | + * \see userStylesheet() |
| 168 | + * \see setUserStylesheetEnabled() |
| 169 | + * \see loadHtml() |
| 170 | + */ |
| 171 | + void setUserStylesheet( const QString &stylesheet ); |
| 172 | + |
| 173 | + /** |
| 174 | + * Returns the user stylesheet CSS rules used while rendering the HTML content. These |
| 175 | + * overriding the styles specified within the HTML source. |
| 176 | + * \see setUserStylesheet() |
| 177 | + * \see userStylesheetEnabled() |
| 178 | + */ |
| 179 | + QString userStylesheet() const { return mUserStylesheet; } |
| 180 | + |
| 181 | + /** |
| 182 | + * Sets whether user stylesheets are \a enabled for the HTML content. |
| 183 | + * \see userStylesheetEnabled() |
| 184 | + * \see setUserStylesheet() |
| 185 | + */ |
| 186 | + void setUserStylesheetEnabled( const bool enabled ); |
| 187 | + |
| 188 | + /** |
| 189 | + * Returns whether user stylesheets are enabled for the HTML content. |
| 190 | + * \see setUserStylesheetEnabled() |
| 191 | + * \see userStylesheet() |
| 192 | + */ |
| 193 | + bool userStylesheetEnabled() const { return mEnableUserStylesheet; } |
| 194 | + |
| 195 | + QString displayName() const override; |
| 196 | + QSizeF totalSize() const override; |
| 197 | + void render( QgsRenderContext &context, const QRectF &renderExtent, const int frameIndex, |
| 198 | + const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; |
| 199 | + |
| 200 | + bool writeXml( QDomElement &elem, QDomDocument &doc, bool ignoreFrames = false ) const override; |
| 201 | + bool readXml( const QDomElement &itemElem, const QDomDocument &doc, bool ignoreFrames = false ) override; |
| 202 | + //overridden to break frames without dividing lines of text |
| 203 | + double findNearbyPageBreak( double yPos ) override; |
| 204 | + |
| 205 | + public slots: |
| 206 | + |
| 207 | + /** |
| 208 | + * Reloads the html source from the url and redraws the item. |
| 209 | + * \param useCache set to true to use a cached copy of remote html |
| 210 | + * content |
| 211 | + * \param context expression context for evaluating data defined urls and expressions in html |
| 212 | + * \see setUrl |
| 213 | + * \see url |
| 214 | + */ |
| 215 | + void loadHtml( const bool useCache = false, const QgsExpressionContext *context = nullptr ); |
| 216 | + |
| 217 | + //! Recalculates the frame sizes for the current viewport dimensions |
| 218 | + void recalculateFrameSizes() override; |
| 219 | + void refreshExpressionContext(); |
| 220 | + |
| 221 | + void refreshDataDefinedProperty( const QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::AllProperties ); |
| 222 | + |
| 223 | + private slots: |
| 224 | + void frameLoaded( bool ok = true ); |
| 225 | + |
| 226 | + private: |
| 227 | + ContentMode mContentMode; |
| 228 | + QUrl mUrl; |
| 229 | + QgsWebPage *mWebPage = nullptr; |
| 230 | + QString mHtml; |
| 231 | + QString mFetchedHtml; |
| 232 | + QString mLastFetchedUrl; |
| 233 | + QString mActualFetchedUrl; //may be different if page was redirected |
| 234 | + bool mLoaded; |
| 235 | + QSizeF mSize; //total size in mm |
| 236 | + double mHtmlUnitsToLayoutUnits; |
| 237 | + QImage *mRenderedPage = nullptr; |
| 238 | + bool mEvaluateExpressions; |
| 239 | + bool mUseSmartBreaks; |
| 240 | + double mMaxBreakDistance; |
| 241 | + |
| 242 | + QgsFeature mExpressionFeature; |
| 243 | + QgsVectorLayer *mExpressionLayer = nullptr; |
| 244 | + QgsDistanceArea *mDistanceArea = nullptr; |
| 245 | + |
| 246 | + QString mUserStylesheet; |
| 247 | + bool mEnableUserStylesheet; |
| 248 | + |
| 249 | + //! JSON string representation of current atlas feature |
| 250 | + QString mAtlasFeatureJSON; |
| 251 | + |
| 252 | + QgsNetworkContentFetcher *mFetcher = nullptr; |
| 253 | + |
| 254 | + double htmlUnitsToLayoutUnits(); //calculate scale factor |
| 255 | + |
| 256 | + //renders a snapshot of the page to a cached image |
| 257 | + void renderCachedImage(); |
| 258 | + |
| 259 | + //fetches html content from a url and returns it as a string |
| 260 | + QString fetchHtml( const QUrl &url ); |
| 261 | + |
| 262 | + //! Sets the current feature, the current layer and a list of local variable substitutions for evaluating expressions |
| 263 | + void setExpressionContext( const QgsFeature &feature, QgsVectorLayer *layer ); |
| 264 | + |
| 265 | + //! Calculates the max width of frames in the html multiframe |
| 266 | + double maxFrameWidth() const; |
| 267 | +}; |
| 268 | + |
| 269 | +#endif // QGSLAYOUTITEMHTML_H |
0 commit comments