QGIS API Documentation 3.41.0-Master (3440c17df1d)
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#include "moc_qgshtmlannotation.cpp"
20#include "qgsfeature.h"
21#include "qgsfeatureiterator.h"
22#include "qgslogger.h"
23#include "qgsproject.h"
24#include "qgsvectorlayer.h"
25#include "qgsexpression.h"
27#include "qgswebpage.h"
28#include "qgswebframe.h"
30#include "qgsrendercontext.h"
31
32#include <QDomElement>
33#include <QDir>
34#include <QFile>
35#include <QFileInfo>
36#include <QGraphicsProxyWidget>
37#include <QPainter>
38#include <QSettings>
39#include <QWidget>
40#include <QTextStream>
41
43 : QgsAnnotation( parent )
44{
45 mWebPage = new QgsWebPage();
46 mWebPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
47 mWebPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
48 mWebPage->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
49
50 // Make QWebPage transparent so that the background color of the annotation frame is used
51 QPalette palette = mWebPage->palette();
52 palette.setBrush( QPalette::Base, Qt::transparent );
53 mWebPage->setPalette( palette );
54
55 connect( mWebPage->mainFrame(), &QWebFrame::javaScriptWindowObjectCleared, this, &QgsHtmlAnnotation::javascript );
56}
57
59{
60 std::unique_ptr< QgsHtmlAnnotation > c( new QgsHtmlAnnotation() );
61 copyCommonProperties( c.get() );
62 c->setSourceFile( mHtmlFile );
63 return c.release();
64}
65
66void QgsHtmlAnnotation::setSourceFile( const QString &htmlFile )
67{
68 QFile file( htmlFile );
69 mHtmlFile = htmlFile;
70 if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
71 {
72 mHtmlSource.clear();
73 }
74 else
75 {
76 QTextStream in( &file );
77#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
78 in.setCodec( "UTF-8" );
79#endif
80 mHtmlSource = in.readAll();
81 }
82
83 file.close();
85 emit appearanceChanged();
86}
87
88void QgsHtmlAnnotation::setHtmlSource( const QString &htmlSource )
89{
90 mHtmlFile.clear();
91 mHtmlSource = htmlSource;
93 emit appearanceChanged();
94}
95
96void QgsHtmlAnnotation::renderAnnotation( QgsRenderContext &context, QSizeF size ) const
97{
98 if ( !context.painter() || ( context.feedback() && context.feedback()->isCanceled() ) )
99 {
100 return;
101 }
102
103 // scale painter back to 96 dpi, so layout prints match screen rendering
104 const QgsScopedQPainterState painterState( context.painter() );
105 const double scaleFactor = context.painter()->device()->logicalDpiX() / 96.0;
106 context.painter()->scale( scaleFactor, scaleFactor );
107 size /= scaleFactor;
108
109 mWebPage->setViewportSize( size.toSize() );
110 mWebPage->mainFrame()->render( context.painter() );
111}
112
114{
115 if ( mWebPage )
116 {
117 const QSizeF widgetMinSize = QSizeF( 0, 0 ); // mWebPage->minimumSize();
118 return QSizeF( contentsMargin().left() + contentsMargin().right() + widgetMinSize.width(),
119 contentsMargin().top() + contentsMargin().bottom() + widgetMinSize.height() );
120 }
121 else
122 {
123 return QSizeF( 0, 0 );
124 }
125}
126
127void QgsHtmlAnnotation::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
128{
129 QDomElement formAnnotationElem = doc.createElement( QStringLiteral( "HtmlAnnotationItem" ) );
130 if ( !mHtmlFile.isEmpty() )
131 {
132 formAnnotationElem.setAttribute( QStringLiteral( "htmlfile" ), sourceFile() );
133 }
134 else
135 {
136 formAnnotationElem.setAttribute( QStringLiteral( "htmlsource" ), mHtmlSource );
137 }
138
139 _writeXml( formAnnotationElem, doc, context );
140 elem.appendChild( formAnnotationElem );
141}
142
143void QgsHtmlAnnotation::readXml( const QDomElement &itemElem, const QgsReadWriteContext &context )
144{
145 const QDomElement annotationElem = itemElem.firstChildElement( QStringLiteral( "AnnotationItem" ) );
146 if ( !annotationElem.isNull() )
147 {
148 _readXml( annotationElem, context );
149 }
150
151 // upgrade old layer
152 if ( !mapLayer() && itemElem.hasAttribute( QStringLiteral( "vectorLayer" ) ) )
153 {
154 setMapLayer( QgsProject::instance()->mapLayer( itemElem.attribute( QStringLiteral( "vectorLayer" ) ) ) ); // skip-keyword-check
155 }
156
157 if ( mWebPage )
158 {
159 mHtmlFile = itemElem.attribute( QStringLiteral( "htmlfile" ), QString() );
160 if ( !mHtmlFile.isEmpty() )
161 {
162 setSourceFile( mHtmlFile );
163 }
164 else
165 {
166 setHtmlSource( itemElem.attribute( QStringLiteral( "htmlsource" ), QString() ) );
167 }
168 }
169}
170
172{
174 QString newText;
175 QgsVectorLayer *vectorLayer = qobject_cast< QgsVectorLayer * >( mapLayer() );
176 if ( feature.isValid() && vectorLayer )
177 {
179 context.setFeature( feature );
180 newText = QgsExpression::replaceExpressionText( mHtmlSource, &context );
181 }
182 else
183 {
184 newText = mHtmlSource;
185 }
186 mWebPage->mainFrame()->setHtml( newText );
187 emit appearanceChanged();
188}
189
190void QgsHtmlAnnotation::javascript()
191{
192 QWebFrame *frame = mWebPage->mainFrame();
193 frame->addToJavaScriptWindowObject( QStringLiteral( "layer" ), mapLayer() );
194}
The QWebFrame class is a collection of stubs to mimic the API of a QWebFrame on systems where QtWebki...
Definition qgswebframe.h:38
Abstract base class for annotation items which are drawn over a map.
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.
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
An annotation item that embeds HTML content.
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.
The class is used as a container of context for various read/write operations on other 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 data sets.
QWebPage subclass which redirects JavaScript errors and console output to the QGIS message log.
Definition qgswebpage.h:217
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