QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgshtmlannotation.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgshtmlannotation.h
3 ------------------------
4 begin : February 26, 2010
5 copyright : (C) 2010 by Marco Hugentobler
6 email : marco dot hugentobler at hugis dot net
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgshtmlannotation.h"
19
20#include "qgsexpression.h"
22#include "qgsfeature.h"
23#include "qgsfeatureiterator.h"
24#include "qgslogger.h"
26#include "qgsproject.h"
27#include "qgsrendercontext.h"
28#include "qgsvectorlayer.h"
29#include "qgswebframe.h"
30#include "qgswebpage.h"
31
32#include <QDir>
33#include <QDomElement>
34#include <QFile>
35#include <QFileInfo>
36#include <QGraphicsProxyWidget>
37#include <QPainter>
38#include <QSettings>
39#include <QTextStream>
40#include <QWidget>
41
42#include "moc_qgshtmlannotation.cpp"
43
45 : QgsAnnotation( parent )
46{
47 mWebPage = new QgsWebPage();
48 mWebPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
49 mWebPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
50 mWebPage->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
51
52 // Make QWebPage transparent so that the background color of the annotation frame is used
53 QPalette palette = mWebPage->palette();
54 palette.setBrush( QPalette::Base, Qt::transparent );
55 mWebPage->setPalette( palette );
56
57 connect( mWebPage->mainFrame(), &QWebFrame::javaScriptWindowObjectCleared, this, &QgsHtmlAnnotation::javascript );
58}
59
61{
62 auto c = std::make_unique<QgsHtmlAnnotation>();
63 copyCommonProperties( c.get() );
64 c->setSourceFile( mHtmlFile );
65 return c.release();
66}
67
68void QgsHtmlAnnotation::setSourceFile( const QString &htmlFile )
69{
70 QFile file( htmlFile );
71 mHtmlFile = htmlFile;
72 if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
73 {
74 mHtmlSource.clear();
75 }
76 else
77 {
78 QTextStream in( &file );
79#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
80 in.setCodec( "UTF-8" );
81#endif
82 mHtmlSource = in.readAll();
83 }
84
85 file.close();
87 emit appearanceChanged();
88}
89
91{
92 mHtmlFile.clear();
93 mHtmlSource = htmlSource;
95 emit appearanceChanged();
96}
97
98void QgsHtmlAnnotation::renderAnnotation( QgsRenderContext &context, QSizeF size ) const
99{
100 if ( !context.painter() || ( context.feedback() && context.feedback()->isCanceled() ) )
101 {
102 return;
103 }
104
105 // scale painter back to 96 dpi, so layout prints match screen rendering
106 const QgsScopedQPainterState painterState( context.painter() );
107 const double scaleFactor = context.painter()->device()->logicalDpiX() / 96.0;
108 context.painter()->scale( scaleFactor, scaleFactor );
109 size /= scaleFactor;
110
111 mWebPage->setViewportSize( size.toSize() );
112 mWebPage->mainFrame()->render( context.painter() );
113}
114
116{
117 if ( mWebPage )
118 {
119 const QSizeF widgetMinSize = QSizeF( 0, 0 ); // mWebPage->minimumSize();
120 return QSizeF( contentsMargin().left() + contentsMargin().right() + widgetMinSize.width(),
121 contentsMargin().top() + contentsMargin().bottom() + widgetMinSize.height() );
122 }
123 else
124 {
125 return QSizeF( 0, 0 );
126 }
127}
128
129void QgsHtmlAnnotation::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
130{
131 QDomElement formAnnotationElem = doc.createElement( QStringLiteral( "HtmlAnnotationItem" ) );
132 if ( !mHtmlFile.isEmpty() )
133 {
134 formAnnotationElem.setAttribute( QStringLiteral( "htmlfile" ), sourceFile() );
135 }
136 else
137 {
138 formAnnotationElem.setAttribute( QStringLiteral( "htmlsource" ), mHtmlSource );
139 }
140
141 _writeXml( formAnnotationElem, doc, context );
142 elem.appendChild( formAnnotationElem );
143}
144
145void QgsHtmlAnnotation::readXml( const QDomElement &itemElem, const QgsReadWriteContext &context )
146{
147 const QDomElement annotationElem = itemElem.firstChildElement( QStringLiteral( "AnnotationItem" ) );
148 if ( !annotationElem.isNull() )
149 {
150 _readXml( annotationElem, context );
151 }
152
153 // upgrade old layer
154 if ( !mapLayer() && itemElem.hasAttribute( QStringLiteral( "vectorLayer" ) ) )
155 {
156 setMapLayer( QgsProject::instance()->mapLayer( itemElem.attribute( QStringLiteral( "vectorLayer" ) ) ) ); // skip-keyword-check
157 }
158
159 if ( mWebPage )
160 {
161 mHtmlFile = itemElem.attribute( QStringLiteral( "htmlfile" ), QString() );
162 if ( !mHtmlFile.isEmpty() )
163 {
164 setSourceFile( mHtmlFile );
165 }
166 else
167 {
168 setHtmlSource( itemElem.attribute( QStringLiteral( "htmlsource" ), QString() ) );
169 }
170 }
171}
172
174{
176 QString newText;
177 QgsVectorLayer *vectorLayer = qobject_cast< QgsVectorLayer * >( mapLayer() );
178 if ( feature.isValid() && vectorLayer )
179 {
181 context.setFeature( feature );
182 newText = QgsExpression::replaceExpressionText( mHtmlSource, &context );
183 }
184 else
185 {
186 newText = mHtmlSource;
187 }
188 mWebPage->mainFrame()->setHtml( newText );
189 emit appearanceChanged();
190}
191
192void QgsHtmlAnnotation::javascript()
193{
194 QWebFrame *frame = mWebPage->mainFrame();
195 frame->addToJavaScriptWindowObject( QStringLiteral( "layer" ), mapLayer() );
196}
A collection of stubs to mimic the API of a QWebFrame on systems where QtWebkit is not available.
Definition qgswebframe.h:38
void appearanceChanged()
Emitted whenever the annotation's appearance changes.
QgsMargins contentsMargin() const
Returns the margins (in millimeters) between the outside of the frame and the annotation content.
void _writeXml(QDomElement &itemElem, QDomDocument &doc, const QgsReadWriteContext &context) const
Writes common annotation properties to a DOM element.
QgsMapLayer * mapLayer() const
Returns the map layer associated with the annotation.
virtual void setAssociatedFeature(const QgsFeature &feature)
Sets the feature associated with the annotation.
QgsFeature associatedFeature() const
Returns the feature associated with the annotation, or an invalid feature if none has been set.
void _readXml(const QDomElement &annotationElem, const QgsReadWriteContext &context)
Reads common annotation properties from a DOM element.
void copyCommonProperties(QgsAnnotation *target) const
Copies common annotation properties to the targe annotation.
QgsAnnotation(QObject *parent=nullptr)
Constructor for QgsAnnotation.
void setMapLayer(QgsMapLayer *layer)
Sets the map layer associated with the annotation.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
static QString replaceExpressionText(const QString &action, const QgsExpressionContext *context, const QgsDistanceArea *distanceArea=nullptr)
This function replaces each expression between [% and %] in the string with the result of its evaluat...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
bool isValid() const
Returns the validity of this feature.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
QgsHtmlAnnotation * clone() const override
Clones the annotation, returning a new copy of the annotation reflecting the annotation's current sta...
QString htmlSource() const
Returns html source text.
QgsHtmlAnnotation(QObject *parent=nullptr)
Constructor for QgsHtmlAnnotation.
void writeXml(QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context) const override
Writes the annotation state to a DOM element.
void readXml(const QDomElement &itemElem, const QgsReadWriteContext &context) override
Restores the annotation's state from a DOM element.
void setSourceFile(const QString &htmlFile)
Sets the file path for the source HTML file.
QSizeF minimumFrameSize() const override
Returns the minimum frame size for the annotation.
void setHtmlSource(const QString &htmlSource)
Sets the html source directly (not coming from a file).
void setAssociatedFeature(const QgsFeature &feature) override
Sets the feature associated with the annotation.
void renderAnnotation(QgsRenderContext &context, QSizeF size) const override
Renders the annotation's contents to a target /a context at the specified /a size.
QString sourceFile() const
Returns the file path for the source HTML file.
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
static QgsProject * instance()
Returns the QgsProject singleton instance.
A container for the context for various read/write operations on objects.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
QgsFeedback * feedback() const
Returns the feedback object that can be queried regularly during rendering to check if rendering shou...
Scoped object for saving and restoring a QPainter object's state.
Represents a vector layer which manages a vector based dataset.
QWebPage subclass which redirects JavaScript errors and console output to the QGIS message log.
Definition qgswebpage.h:218
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c