24 #include "qgsexpression.h" 29 #include <QDomElement> 33 #include <QGraphicsProxyWidget> 43 mWebPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
44 mWebPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
47 connect( mWebPage->mainFrame(), &QWebFrame::javaScriptWindowObjectCleared,
this, &QgsHtmlAnnotation::javascript );
54 c->setSourceFile( mHtmlFile );
60 QFile file( htmlFile );
62 if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
68 QTextStream in( &file );
69 in.setCodec(
"UTF-8" );
70 mHtmlSource = in.readAll();
85 mWebPage->setViewportSize( size.toSize() );
86 mWebPage->mainFrame()->render( context.
painter() );
93 QSizeF widgetMinSize = QSizeF( 0, 0 );
99 return QSizeF( 0, 0 );
105 QDomElement formAnnotationElem = doc.createElement( QStringLiteral(
"HtmlAnnotationItem" ) );
106 formAnnotationElem.setAttribute( QStringLiteral(
"htmlfile" ),
sourceFile() );
108 _writeXml( formAnnotationElem, doc, context );
109 elem.appendChild( formAnnotationElem );
114 mHtmlFile = itemElem.attribute( QStringLiteral(
"htmlfile" ), QLatin1String(
"" ) );
115 QDomElement annotationElem = itemElem.firstChildElement( QStringLiteral(
"AnnotationItem" ) );
116 if ( !annotationElem.isNull() )
118 _readXml( annotationElem, context );
122 if ( !
mapLayer() && itemElem.hasAttribute( QStringLiteral(
"vectorLayer" ) ) )
138 if ( feature.
isValid() && vectorLayer )
142 newText = QgsExpression::replaceExpressionText( mHtmlSource, &context );
146 newText = mHtmlSource;
148 mWebPage->mainFrame()->setHtml( newText );
152 void QgsHtmlAnnotation::javascript()
154 QWebFrame *frame = mWebPage->mainFrame();
155 frame->addToJavaScriptWindowObject( QStringLiteral(
"layer" ),
mapLayer() );
bool isValid() const
Returns the validity of this feature.
The class is used as a container of context for various read/write operations on other objects...
QgsHtmlAnnotation(QObject *parent=nullptr)
Constructor for QgsHtmlAnnotation.
void appearanceChanged()
Emitted whenever the annotation's appearance changes.
void _writeXml(QDomElement &itemElem, QDomDocument &doc, const QgsReadWriteContext &context) const
Writes common annotation properties to a DOM element.
void setMapLayer(QgsMapLayer *layer)
Sets the map layer associated with the annotation.
QgsHtmlAnnotation * clone() const override
Clones the annotation, returning a new copy of the annotation reflecting the annotation's current sta...
void setSourceFile(const QString &htmlFile)
Sets the file path for the source HTML file.
void renderAnnotation(QgsRenderContext &context, QSizeF size) const override
Renders the annotation's contents to a target /a context at the specified /a size.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
An annotation item that embeds HTML content.
void copyCommonProperties(QgsAnnotation *target) const
Copies common annotation properties to the targe annotation.
Abstract base class for annotation items which are drawn over a map.
QString sourceFile() const
Returns the file path for the source HTML file.
void readXml(const QDomElement &itemElem, const QgsReadWriteContext &context) override
Restores the annotation's state from a DOM element.
QgsMargins contentsMargin() const
Returns the margins (in millimeters) between the outside of the frame and the annotation content...
double bottom() const
Returns the bottom margin.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
double top() const
Returns the top margin.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
QSizeF minimumFrameSize() const override
Returns the minimum frame size for the annotation.
void _readXml(const QDomElement &annotationElem, const QgsReadWriteContext &context)
Reads common annotation properties from a DOM element.
QgsMapLayer * mapLayer() const
Returns the map layer associated with the annotation.
void setAssociatedFeature(const QgsFeature &feature) override
Sets the feature associated with the annotation.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
void writeXml(QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context) const override
Writes the annotation state to a DOM element.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsFeature associatedFeature() const
Returns the feature associated with the annotation, or an invalid feature if none has been set...
Represents a vector layer which manages a vector based data sets.
virtual void setAssociatedFeature(const QgsFeature &feature)
Sets the feature associated with the annotation.
QWebPage subclass which redirects JavaScript errors and console output to the QGIS message log...
The QWebFrame class is a collection of stubs to mimic the API of a QWebFrame on systems where QtWebki...