32 #include <QCoreApplication>
35 #include <QNetworkReply>
42 , mHtmlUnitsToMM( 1.0 )
44 , mEvaluateExpressions( true )
45 , mUseSmartBreaks( true )
46 , mMaxBreakDistance( 10 )
47 , mExpressionFeature( 0 )
48 , mExpressionLayer( 0 )
50 , mEnableUserStylesheet( false )
54 mHtmlUnitsToMM = htmlUnitsToMM();
61 palette.
setBrush( QPalette::Base, Qt::transparent );
65 QObject::connect( mWebPage, SIGNAL( loadFinished(
bool ) ),
this, SLOT( frameLoaded(
bool ) ) );
86 connect( mFetcher, SIGNAL( finished() ),
this, SLOT( frameLoaded() ) );
134 switch ( mContentMode )
152 if ( !( useCache && currentUrl == mLastFetchedUrl ) )
154 loadedHtml = fetchHtml(
QUrl( currentUrl ) );
155 mLastFetchedUrl = currentUrl;
159 loadedHtml = mFetchedHtml;
170 if ( mEvaluateExpressions )
185 if ( mEnableUserStylesheet && ! mUserStylesheet.
isEmpty() )
189 QUrl cssFileURL =
QUrl(
"data:text/css;charset=utf-8;base64," + ba.
toBase64() );
199 qApp->processEvents();
207 void QgsComposerHtml::frameLoaded(
bool ok )
213 double QgsComposerHtml::maxFrameWidth()
const
219 maxWidth = qMax( maxWidth, (
double )(( *frameIt )->boundingRect().width() ) );
232 double maxWidth = maxFrameWidth();
234 contentsSize.
setWidth( maxWidth * mHtmlUnitsToMM );
247 void QgsComposerHtml::renderCachedImage()
252 delete mRenderedPage;
255 if ( mRenderedPage->
isNull() )
259 mRenderedPage->
fill( Qt::transparent );
261 painter.
begin( mRenderedPage );
274 qApp->processEvents();
289 Q_UNUSED( frameIndex );
298 p->
scale( 1.0 / mHtmlUnitsToMM, 1.0 / mHtmlUnitsToMM );
299 p->
translate( 0.0, -renderExtent.
top() * mHtmlUnitsToMM );
304 double QgsComposerHtml::htmlUnitsToMM()
323 if ( recalcFrameSizes )
331 if ( c1.second < c2.second )
333 else if ( c1.second > c2.second )
335 else if ( c1.first > c2.first )
343 if ( !mWebPage || !mRenderedPage || !mUseSmartBreaks )
349 int idealPos = yPos * htmlUnitsToMM();
352 if ( idealPos >= mRenderedPage->
height() )
357 int maxSearchDistance = mMaxBreakDistance * htmlUnitsToMM();
363 bool currentPixelTransparent =
false;
364 bool previousPixelTransparent =
false;
367 int minRow = qMax( idealPos - maxSearchDistance, 0 );
368 for (
int candidateRow = idealPos; candidateRow >= minRow; --candidateRow )
371 currentColor = qRgba( 0, 0, 0, 0 );
373 for (
int col = 0; col < mRenderedPage->
width(); ++col )
379 pixelColor = mRenderedPage->
pixel( col, candidateRow );
380 currentPixelTransparent = qAlpha( pixelColor ) == 0;
381 if ( pixelColor != currentColor && !( currentPixelTransparent && previousPixelTransparent ) )
384 currentColor = pixelColor;
387 previousPixelTransparent = currentPixelTransparent;
389 candidates.
append( qMakePair( candidateRow, changes ) );
400 int maxCandidateRow = candidates[0].
first;
401 int minCandidateRow = maxCandidateRow + 1;
402 int minCandidateChanges = candidates[0].second;
405 for ( it = candidates.
begin(); it != candidates.
end(); ++it )
407 if (( *it ).second != minCandidateChanges || ( *it ).first != minCandidateRow - 1 )
412 return ( minCandidateRow + ( maxCandidateRow - minCandidateRow ) / 2 ) / htmlUnitsToMM();
414 minCandidateRow = ( *it ).
first;
419 return candidates[0].
first / htmlUnitsToMM();
438 mUserStylesheet = stylesheet;
447 if ( mEnableUserStylesheet != stylesheetEnabled )
449 mEnableUserStylesheet = stylesheetEnabled;
457 return tr(
"<html frame>" );
466 htmlElem.
setAttribute(
"evaluateExpressions", mEvaluateExpressions ?
"true" :
"false" );
467 htmlElem.
setAttribute(
"useSmartBreaks", mUseSmartBreaks ?
"true" :
"false" );
470 htmlElem.
setAttribute(
"stylesheetEnabled", mEnableUserStylesheet ?
"true" :
"false" );
472 bool state =
_writeXML( htmlElem, doc, ignoreFrames );
485 if ( !
_readXML( itemElem, doc, ignoreFrames ) )
492 if ( !contentModeOK )
496 mEvaluateExpressions = itemElem.
attribute(
"evaluateExpressions",
"true" ) ==
"true" ?
true :
false;
497 mUseSmartBreaks = itemElem.
attribute(
"useSmartBreaks",
"true" ) ==
"true" ?
true :
false;
500 mUserStylesheet = itemElem.
attribute(
"stylesheet" );
501 mEnableUserStylesheet = itemElem.
attribute(
"stylesheetEnabled",
"false" ) ==
"true" ?
true :
false;
518 mExpressionFeature = feature;
519 mExpressionLayer = layer;
552 setExpressionContext( feature, vl );
void setBrush(ColorRole role, const QBrush &brush)
virtual void recalculateFrameSizes()
Recalculates the portion of the multiframe item which is shown in each of it's component frames...
QgsComposition::AtlasMode atlasMode() const
Returns the current atlas mode of the composition.
QString contentAsString() const
Returns the fetched content as a string.
void setRenderHint(RenderHint hint, bool on)
QDomNode appendChild(const QDomNode &newChild)
void push_back(const T &value)
QString attribute(const QString &name, const QString &defValue) const
bool writeXML(QDomElement &elem, QDomDocument &doc, bool ignoreFrames=false) const override
Stores state information about multiframe in DOM element.
virtual QString displayName() const override
Get multiframe display name.
const QgsMapSettings & mapSettings() const
Return setting of QGIS map canvas.
void setSourceCrs(long srsid)
sets source spatial reference system (by QGIS CRS)
void setHtml(const QString html)
Sets the HTML to display in the item when the item is using the QgsComposerHtml::ManualHtml mode...
void scale(qreal sx, qreal sy)
void setUserStylesheet(const QString stylesheet)
Sets the user stylesheet CSS rules to use while rendering the HTML content.
QMap< QgsComposerObject::DataDefinedProperty, QString > mDataDefinedNames
Map of data defined properties for the item to string name to use when exporting item to xml...
A item that forms part of a map composition.
virtual void refreshDataDefinedProperty(const QgsComposerObject::DataDefinedProperty property=QgsComposerObject::AllProperties) override
bool enabled() const
Returns whether the atlas generation is enabled.
void fetchContent(const QUrl url)
Fetches content from a remote URL and handles redirects.
bool hasCrsTransformEnabled() const
returns true if projections are enabled for this layer set
QgsComposerHtml(QgsComposition *c, bool createUndoCommands)
QNetworkReply * reply()
Returns a reference to the network reply.
bool setEllipsoid(const QString &ellipsoid)
sets ellipsoid by its acronym
void setEvaluateExpressions(bool evaluateExpressions)
Sets whether the html item will evaluate QGIS expressions prior to rendering the HTML content...
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
double toDouble(bool *ok) const
QString toString(QFlags< QUrl::FormattingOption > options) const
QString tr(const char *sourceText, const char *disambiguation, int n)
DataDefinedProperty
Data defined properties for different item types.
QSizeF totalSize() const override
Returns the total size of the multiframe's content.
const QString GEO_NONE
Constant that holds the string representation for "No ellips/No CRS".
bool dataDefinedEvaluate(const QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue)
Evaluate a data defined property and return the calculated value.
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...
bool _readXML(const QDomElement &itemElem, const QDomDocument &doc, bool ignoreFrames=false)
Restores state information about base multiframe object from a DOM element.
QRgb pixel(int x, int y) const
QString number(int n, int base)
void append(const T &value)
void setHtml(const QString &html, const QUrl &baseUrl)
void setUserStyleSheetUrl(const QUrl &location)
void fill(uint pixelValue)
HTTP network content fetcher.
int printResolution() const
void setAttribute(const QString &name, const QString &value)
void setWidth(qreal width)
void recalculateFrameSizes() override
Recalculates the frame sizes for the current viewport dimensions.
const QgsCoordinateReferenceSystem & destinationCrs() const
returns CRS of destination coordinate reference system
int toInt(bool *ok, int base) const
Abstract base class for composer items with the ability to distribute the content to several frames (...
void addFrame(QgsComposerFrame *frame, bool recalcFrameSizes=true) override
Adds a frame to the multiframe.
QList< QgsComposerFrame * > mFrameItems
QWebFrame * mainFrame() const
bool _writeXML(QDomElement &elem, QDomDocument &doc, bool ignoreFrames=false) const
Stores state information about base multiframe object in DOM element.
void setMaxBreakDistance(double maxBreakDistance)
Sets the maximum distance allowed when calculating where to place page breaks in the html...
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 ...
void setUseSmartBreaks(bool useSmartBreaks)
Sets whether the html item should use smart breaks.
int frameCount() const
Returns the number of frames associated with this multiframe.
void loadHtml(const bool useCache=false)
Reloads the html source from the url and redraws the item.
Graphics scene for map printing.
QgsFeature * currentFeature()
Returns the current atlas feature.
Frame item for a composer multiframe item.
QByteArray & append(char ch)
const QUrl & url() const
Returns the URL of the content displayed in the item if the item is using the QgsComposerHtml::Url mo...
QWebSettings * settings() const
General purpose distance and area calculator.
QgsComposition * mComposition
void deleteFrames()
Removes and deletes all child frames.
void refreshExpressionContext()
void render(QPainter *painter)
void contentsChanged()
Emitted when the contents of the multi frame have changed and the frames must be redrawn.
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
double maxBreakDistance() const
Returns the maximum distance allowed when calculating where to place page breaks in the html...
void addComposerHtmlFrame(QgsComposerHtml *html, QgsComposerFrame *frame)
Adds composer html frame and advises composer to create a widget for it (through signal) ...
void setUrl(const QUrl &url)
Sets the URL for content to display in the item when the item is using the QgsComposerHtml::Url mode...
static QgsProject * instance()
access to canonical QgsProject instance
void translate(const QPointF &offset)
const QgsCoordinateReferenceSystem & crs() const
Returns layer's spatial reference system.
QgsAtlasComposition & atlasComposition()
iterator insert(const Key &key, const T &value)
QgsVectorLayer * coverageLayer() const
Returns the coverage layer used for the atlas features.
QByteArray toBase64() const
void setUserStylesheetEnabled(const bool stylesheetEnabled)
Sets whether user stylesheets are enabled for the HTML content.
bool readXML(const QDomElement &itemElem, const QDomDocument &doc, bool ignoreFrames=false) override
Reads multiframe state information from a DOM element.
const_iterator constEnd() const
QDomElement createElement(const QString &tagName)
const_iterator constBegin() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Represents a vector layer which manages a vector based data sets.
bool begin(QPaintDevice *device)
ContentMode
Source modes for the HTML content to render in the item.
void setHeight(qreal height)
void setViewportSize(const QSize &size) const
double findNearbyPageBreak(double yPos) override
Finds the optimal position to break a frame at.
void setNetworkAccessManager(QNetworkAccessManager *manager)
void handleFrameRemoval(QgsComposerItem *item)
Called before a frame is going to be removed.
bool candidateSort(const QPair< int, int > &c1, const QPair< int, int > &c2)
virtual void refreshDataDefinedProperty(const DataDefinedProperty property=AllProperties)
Refreshes a data defined property for the item by reevaluating the property's value and redrawing the...
void setEllipsoidalMode(bool flag)
sets whether coordinates must be projected to ellipsoid before measuring
The QWebSettings class is a collection of stubs to mimic the API of a QWebSettings on systems where Q...
void render(QPainter *p, const QRectF &renderExtent, const int frameIndex) override
Renders a portion of the multiframe's content into a painter.
void changed()
Emitted when the properties of a multi frame have changed, and the GUI item widget must be updated...
QByteArray toUtf8() const
static QString replaceExpressionText(const QString &action, const QgsFeature *feat, QgsVectorLayer *layer, const QMap< QString, QVariant > *substitutionMap=0, const QgsDistanceArea *distanceArea=0)
This function currently replaces each expression between [% and %] in the string with the result of i...