QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 "qgsfeature.h"
20 #include "qgsfeatureiterator.h"
21 #include "qgslogger.h"
22 #include "qgsproject.h"
23 #include "qgsvectorlayer.h"
24 #include "qgsexpression.h"
26 #include "qgswebpage.h"
27 #include "qgswebframe.h"
29 #include "qgsrendercontext.h"
30 
31 #include <QDomElement>
32 #include <QDir>
33 #include <QFile>
34 #include <QFileInfo>
35 #include <QGraphicsProxyWidget>
36 #include <QPainter>
37 #include <QSettings>
38 #include <QWidget>
39 #include <QTextStream>
40 
42  : QgsAnnotation( parent )
43 {
44  mWebPage = new QgsWebPage();
45  mWebPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
46  mWebPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
47  mWebPage->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
48 
49  connect( mWebPage->mainFrame(), &QWebFrame::javaScriptWindowObjectCleared, this, &QgsHtmlAnnotation::javascript );
50 }
51 
53 {
54  std::unique_ptr< QgsHtmlAnnotation > c( new QgsHtmlAnnotation() );
55  copyCommonProperties( c.get() );
56  c->setSourceFile( mHtmlFile );
57  return c.release();
58 }
59 
60 void QgsHtmlAnnotation::setSourceFile( const QString &htmlFile )
61 {
62  QFile file( htmlFile );
63  mHtmlFile = htmlFile;
64  if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
65  {
66  mHtmlSource.clear();
67  }
68  else
69  {
70  QTextStream in( &file );
71 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
72  in.setCodec( "UTF-8" );
73 #endif
74  mHtmlSource = in.readAll();
75  }
76 
77  file.close();
79  emit appearanceChanged();
80 }
81 
82 void QgsHtmlAnnotation::setHtmlSource( const QString &htmlSource )
83 {
84  mHtmlFile.clear();
85  mHtmlSource = htmlSource;
87  emit appearanceChanged();
88 }
89 
90 void QgsHtmlAnnotation::renderAnnotation( QgsRenderContext &context, QSizeF size ) const
91 {
92  if ( !context.painter() )
93  {
94  return;
95  }
96 
97  // scale painter back to 96 dpi, so layout prints match screen rendering
98  const QgsScopedQPainterState painterState( context.painter() );
99  const double scaleFactor = context.painter()->device()->logicalDpiX() / 96.0;
100  context.painter()->scale( scaleFactor, scaleFactor );
101  size /= scaleFactor;
102 
103  mWebPage->setViewportSize( size.toSize() );
104  mWebPage->mainFrame()->render( context.painter() );
105 }
106 
108 {
109  if ( mWebPage )
110  {
111  const QSizeF widgetMinSize = QSizeF( 0, 0 ); // mWebPage->minimumSize();
112  return QSizeF( contentsMargin().left() + contentsMargin().right() + widgetMinSize.width(),
113  contentsMargin().top() + contentsMargin().bottom() + widgetMinSize.height() );
114  }
115  else
116  {
117  return QSizeF( 0, 0 );
118  }
119 }
120 
121 void QgsHtmlAnnotation::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
122 {
123  QDomElement formAnnotationElem = doc.createElement( QStringLiteral( "HtmlAnnotationItem" ) );
124  if ( !mHtmlFile.isEmpty() )
125  {
126  formAnnotationElem.setAttribute( QStringLiteral( "htmlfile" ), sourceFile() );
127  }
128  else
129  {
130  formAnnotationElem.setAttribute( QStringLiteral( "htmlsource" ), mHtmlSource );
131  }
132 
133  _writeXml( formAnnotationElem, doc, context );
134  elem.appendChild( formAnnotationElem );
135 }
136 
137 void QgsHtmlAnnotation::readXml( const QDomElement &itemElem, const QgsReadWriteContext &context )
138 {
139  const QDomElement annotationElem = itemElem.firstChildElement( QStringLiteral( "AnnotationItem" ) );
140  if ( !annotationElem.isNull() )
141  {
142  _readXml( annotationElem, context );
143  }
144 
145  // upgrade old layer
146  if ( !mapLayer() && itemElem.hasAttribute( QStringLiteral( "vectorLayer" ) ) )
147  {
148  setMapLayer( QgsProject::instance()->mapLayer( itemElem.attribute( QStringLiteral( "vectorLayer" ) ) ) );
149  }
150 
151  if ( mWebPage )
152  {
153  mHtmlFile = itemElem.attribute( QStringLiteral( "htmlfile" ), QString() );
154  if ( !mHtmlFile.isEmpty() )
155  {
156  setSourceFile( mHtmlFile );
157  }
158  else
159  {
160  setHtmlSource( itemElem.attribute( QStringLiteral( "htmlsource" ), QString() ) );
161  }
162  }
163 }
164 
166 {
168  QString newText;
169  QgsVectorLayer *vectorLayer = qobject_cast< QgsVectorLayer * >( mapLayer() );
170  if ( feature.isValid() && vectorLayer )
171  {
173  context.setFeature( feature );
174  newText = QgsExpression::replaceExpressionText( mHtmlSource, &context );
175  }
176  else
177  {
178  newText = mHtmlSource;
179  }
180  mWebPage->mainFrame()->setHtml( newText );
181  emit appearanceChanged();
182 }
183 
184 void QgsHtmlAnnotation::javascript()
185 {
186  QWebFrame *frame = mWebPage->mainFrame();
187  frame->addToJavaScriptWindowObject( QStringLiteral( "layer" ), mapLayer() );
188 }
189 
190 
191 
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
qgsexpressioncontextutils.h
QgsHtmlAnnotation::QgsHtmlAnnotation
QgsHtmlAnnotation(QObject *parent=nullptr)
Constructor for QgsHtmlAnnotation.
Definition: qgshtmlannotation.cpp:41
QgsAnnotation::_readXml
void _readXml(const QDomElement &annotationElem, const QgsReadWriteContext &context)
Reads common annotation properties from a DOM element.
Definition: qgsannotation.cpp:309
QgsAnnotation::contentsMargin
QgsMargins contentsMargin() const
Returns the margins (in millimeters) between the outside of the frame and the annotation content.
Definition: qgsannotation.h:237
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
qgsexpression.h
qgswebframe.h
qgsfeatureiterator.h
QgsHtmlAnnotation::clone
QgsHtmlAnnotation * clone() const override
Clones the annotation, returning a new copy of the annotation reflecting the annotation's current sta...
Definition: qgshtmlannotation.cpp:52
QgsHtmlAnnotation::setSourceFile
void setSourceFile(const QString &htmlFile)
Sets the file path for the source HTML file.
Definition: qgshtmlannotation.cpp:60
QgsHtmlAnnotation::renderAnnotation
void renderAnnotation(QgsRenderContext &context, QSizeF size) const override
Renders the annotation's contents to a target /a context at the specified /a size.
Definition: qgshtmlannotation.cpp:90
qgsfeature.h
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:480
QgsExpressionContextUtils::globalProjectLayerScopes
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Definition: qgsexpressioncontextutils.cpp:377
QgsAnnotation::mapLayer
QgsMapLayer * mapLayer() const
Returns the map layer associated with the annotation.
Definition: qgsannotation.h:292
QgsWebPage
QWebPage subclass which redirects JavaScript errors and console output to the QGIS message log.
Definition: qgswebpage.h:217
qgswebpage.h
QgsAnnotation
Abstract base class for annotation items which are drawn over a map.
Definition: qgsannotation.h:53
QgsHtmlAnnotation::sourceFile
QString sourceFile() const
Returns the file path for the source HTML file.
Definition: qgshtmlannotation.h:59
QgsFeature::isValid
bool isValid() const
Returns the validity of this feature.
Definition: qgsfeature.cpp:216
QgsHtmlAnnotation::readXml
void readXml(const QDomElement &itemElem, const QgsReadWriteContext &context) override
Restores the annotation's state from a DOM element.
Definition: qgshtmlannotation.cpp:137
qgsnetworkaccessmanager.h
QgsAnnotation::associatedFeature
QgsFeature associatedFeature() const
Returns the feature associated with the annotation, or an invalid feature if none has been set.
Definition: qgsannotation.h:307
QgsHtmlAnnotation::minimumFrameSize
QSizeF minimumFrameSize() const override
Returns the minimum frame size for the annotation.
Definition: qgshtmlannotation.cpp:107
qgsrendercontext.h
QgsAnnotation::_writeXml
void _writeXml(QDomElement &itemElem, QDomDocument &doc, const QgsReadWriteContext &context) const
Writes common annotation properties to a DOM element.
Definition: qgsannotation.cpp:264
QgsHtmlAnnotation::setHtmlSource
void setHtmlSource(const QString &htmlSource)
Sets the html source directly (not coming from a file)
Definition: qgshtmlannotation.cpp:82
QgsHtmlAnnotation
An annotation item that embeds HTML content.
Definition: qgshtmlannotation.h:35
QgsScopedQPainterState
Scoped object for saving and restoring a QPainter object's state.
Definition: qgsrendercontext.h:1336
QgsAnnotation::setAssociatedFeature
virtual void setAssociatedFeature(const QgsFeature &feature)
Sets the feature associated with the annotation.
Definition: qgsannotation.cpp:189
QgsNetworkAccessManager::instance
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
Definition: qgsnetworkaccessmanager.cpp:202
qgshtmlannotation.h
QgsHtmlAnnotation::htmlSource
QString htmlSource() const
Returns html source text.
Definition: qgshtmlannotation.h:70
qgsvectorlayer.h
QgsHtmlAnnotation::setAssociatedFeature
void setAssociatedFeature(const QgsFeature &feature) override
Sets the feature associated with the annotation.
Definition: qgshtmlannotation.cpp:165
c
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
Definition: porting_processing.dox:1
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsAnnotation::appearanceChanged
void appearanceChanged()
Emitted whenever the annotation's appearance changes.
QgsHtmlAnnotation::writeXml
void writeXml(QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context) const override
Writes the annotation state to a DOM element.
Definition: qgshtmlannotation.cpp:121
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
qgslogger.h
QgsRenderContext::painter
QPainter * painter()
Returns the destination QPainter for the render operation.
Definition: qgsrendercontext.h:112
QgsExpression::replaceExpressionText
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...
Definition: qgsexpression.cpp:434
qgsproject.h
QgsAnnotation::copyCommonProperties
void copyCommonProperties(QgsAnnotation *target) const
Copies common annotation properties to the targe annotation.
Definition: qgsannotation.cpp:410
QWebFrame
The QWebFrame class is a collection of stubs to mimic the API of a QWebFrame on systems where QtWebki...
Definition: qgswebframe.h:37
QgsExpressionContext::setFeature
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Definition: qgsexpressioncontext.cpp:525
QgsAnnotation::setMapLayer
void setMapLayer(QgsMapLayer *layer)
Sets the map layer associated with the annotation.
Definition: qgsannotation.cpp:183