QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
30 #include <QDomElement>
31 #include <QDir>
32 #include <QFile>
33 #include <QFileInfo>
34 #include <QGraphicsProxyWidget>
35 #include <QPainter>
36 #include <QSettings>
37 #include <QWidget>
38 
39 
41  : QgsAnnotation( parent )
42 {
43  mWebPage = new QgsWebPage();
44  mWebPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
45  mWebPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
46  mWebPage->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
47 
48  connect( mWebPage->mainFrame(), &QWebFrame::javaScriptWindowObjectCleared, this, &QgsHtmlAnnotation::javascript );
49 }
50 
52 {
53  std::unique_ptr< QgsHtmlAnnotation > c( new QgsHtmlAnnotation() );
54  copyCommonProperties( c.get() );
55  c->setSourceFile( mHtmlFile );
56  return c.release();
57 }
58 
59 void QgsHtmlAnnotation::setSourceFile( const QString &htmlFile )
60 {
61  QFile file( htmlFile );
62  mHtmlFile = htmlFile;
63  if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
64  {
65  mHtmlSource.clear();
66  }
67  else
68  {
69  QTextStream in( &file );
70  in.setCodec( "UTF-8" );
71  mHtmlSource = in.readAll();
72  }
73 
74  file.close();
76  emit appearanceChanged();
77 }
78 
79 void QgsHtmlAnnotation::renderAnnotation( QgsRenderContext &context, QSizeF size ) const
80 {
81  if ( !context.painter() )
82  {
83  return;
84  }
85 
86  // scale painter back to 96 dpi, so layout prints match screen rendering
87  context.painter()->save();
88  const double scaleFactor = context.painter()->device()->logicalDpiX() / 96.0;
89  context.painter()->scale( scaleFactor, scaleFactor );
90  size /= scaleFactor;
91 
92  mWebPage->setViewportSize( size.toSize() );
93  mWebPage->mainFrame()->render( context.painter() );
94 
95  context.painter()->restore();
96 }
97 
99 {
100  if ( mWebPage )
101  {
102  QSizeF widgetMinSize = QSizeF( 0, 0 ); // mWebPage->minimumSize();
103  return QSizeF( contentsMargin().left() + contentsMargin().right() + widgetMinSize.width(),
104  contentsMargin().top() + contentsMargin().bottom() + widgetMinSize.height() );
105  }
106  else
107  {
108  return QSizeF( 0, 0 );
109  }
110 }
111 
112 void QgsHtmlAnnotation::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
113 {
114  QDomElement formAnnotationElem = doc.createElement( QStringLiteral( "HtmlAnnotationItem" ) );
115  formAnnotationElem.setAttribute( QStringLiteral( "htmlfile" ), sourceFile() );
116 
117  _writeXml( formAnnotationElem, doc, context );
118  elem.appendChild( formAnnotationElem );
119 }
120 
121 void QgsHtmlAnnotation::readXml( const QDomElement &itemElem, const QgsReadWriteContext &context )
122 {
123  mHtmlFile = itemElem.attribute( QStringLiteral( "htmlfile" ), QString() );
124  QDomElement annotationElem = itemElem.firstChildElement( QStringLiteral( "AnnotationItem" ) );
125  if ( !annotationElem.isNull() )
126  {
127  _readXml( annotationElem, context );
128  }
129 
130  // upgrade old layer
131  if ( !mapLayer() && itemElem.hasAttribute( QStringLiteral( "vectorLayer" ) ) )
132  {
133  setMapLayer( QgsProject::instance()->mapLayer( itemElem.attribute( QStringLiteral( "vectorLayer" ) ) ) );
134  }
135 
136  if ( mWebPage )
137  {
138  setSourceFile( mHtmlFile );
139  }
140 }
141 
143 {
145  QString newText;
146  QgsVectorLayer *vectorLayer = qobject_cast< QgsVectorLayer * >( mapLayer() );
147  if ( feature.isValid() && vectorLayer )
148  {
150  context.setFeature( feature );
151  newText = QgsExpression::replaceExpressionText( mHtmlSource, &context );
152  }
153  else
154  {
155  newText = mHtmlSource;
156  }
157  mWebPage->mainFrame()->setHtml( newText );
158  emit appearanceChanged();
159 }
160 
161 void QgsHtmlAnnotation::javascript()
162 {
163  QWebFrame *frame = mWebPage->mainFrame();
164  frame->addToJavaScriptWindowObject( QStringLiteral( "layer" ), mapLayer() );
165 }
166 
167 
168 
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
qgsexpressioncontextutils.h
QgsHtmlAnnotation::QgsHtmlAnnotation
QgsHtmlAnnotation(QObject *parent=nullptr)
Constructor for QgsHtmlAnnotation.
Definition: qgshtmlannotation.cpp:40
QgsAnnotation::_readXml
void _readXml(const QDomElement &annotationElem, const QgsReadWriteContext &context)
Reads common annotation properties from a DOM element.
Definition: qgsannotation.cpp:445
QgsAnnotation::contentsMargin
QgsMargins contentsMargin() const
Returns the margins (in millimeters) between the outside of the frame and the annotation content.
Definition: qgsannotation.h:231
QgsReadWriteContext
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:51
QgsHtmlAnnotation::setSourceFile
void setSourceFile(const QString &htmlFile)
Sets the file path for the source HTML file.
Definition: qgshtmlannotation.cpp:59
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:79
qgsfeature.h
QgsRenderContext
Definition: qgsrendercontext.h:57
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:458
QgsExpressionContextUtils::globalProjectLayerScopes
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Definition: qgsexpressioncontextutils.cpp:306
QgsAnnotation::mapLayer
QgsMapLayer * mapLayer() const
Returns the map layer associated with the annotation.
Definition: qgsannotation.h:286
QgsWebPage
QWebPage subclass which redirects JavaScript errors and console output to the QGIS message log.
Definition: qgswebpage.h:216
qgswebpage.h
QgsAnnotation
Abstract base class for annotation items which are drawn over a map.
Definition: qgsannotation.h:49
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:183
QgsHtmlAnnotation::readXml
void readXml(const QDomElement &itemElem, const QgsReadWriteContext &context) override
Restores the annotation's state from a DOM element.
Definition: qgshtmlannotation.cpp:121
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:301
QgsHtmlAnnotation::minimumFrameSize
QSizeF minimumFrameSize() const override
Returns the minimum frame size for the annotation.
Definition: qgshtmlannotation.cpp:98
QgsAnnotation::_writeXml
void _writeXml(QDomElement &itemElem, QDomDocument &doc, const QgsReadWriteContext &context) const
Writes common annotation properties to a DOM element.
Definition: qgsannotation.cpp:400
QgsHtmlAnnotation
Definition: qgshtmlannotation.h:35
QgsAnnotation::setAssociatedFeature
virtual void setAssociatedFeature(const QgsFeature &feature)
Sets the feature associated with the annotation.
Definition: qgsannotation.cpp:180
QgsNetworkAccessManager::instance
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
Definition: qgsnetworkaccessmanager.cpp:121
qgshtmlannotation.h
qgsvectorlayer.h
QgsHtmlAnnotation::setAssociatedFeature
void setAssociatedFeature(const QgsFeature &feature) override
Sets the feature associated with the annotation.
Definition: qgshtmlannotation.cpp:142
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
Definition: qgsvectorlayer.h:385
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:112
QgsFeature
Definition: qgsfeature.h:55
qgslogger.h
QgsRenderContext::painter
QPainter * painter()
Returns the destination QPainter for the render operation.
Definition: qgsrendercontext.h:174
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:430
qgsproject.h
QgsAnnotation::copyCommonProperties
void copyCommonProperties(QgsAnnotation *target) const
Copies common annotation properties to the targe annotation.
Definition: qgsannotation.cpp:547
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:521
QgsAnnotation::setMapLayer
void setMapLayer(QgsMapLayer *layer)
Sets the map layer associated with the annotation.
Definition: qgsannotation.cpp:174