29 #include <QDomElement> 33 #include <QGraphicsProxyWidget> 40 :
QgsAnnotationItem( canvas ), mWidgetContainer( 0 ), mWebView( 0 ), mVectorLayer( vlayer ),
41 mHasAssociatedFeature( hasFeature ), mFeatureId( feature )
43 mWebView =
new QWebView();
46 mWidgetContainer =
new QGraphicsProxyWidget(
this );
47 mWidgetContainer->setWidget( mWebView );
49 QObject::connect( mWebView->page()->mainFrame(), SIGNAL( javaScriptWindowObjectCleared() ),
this, SLOT( javascript() ) );
53 QObject::connect( mVectorLayer, SIGNAL( layerModified() ),
this, SLOT( setFeatureForMapPosition() ) );
54 QObject::connect(
mMapCanvas, SIGNAL( renderComplete( QPainter* ) ),
this, SLOT( setFeatureForMapPosition() ) );
55 QObject::connect(
mMapCanvas, SIGNAL( layersChanged() ),
this, SLOT( updateVisibility() ) );
58 setFeatureForMapPosition();
68 QFile file( htmlFile );
70 if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
76 QTextStream in( &file );
77 in.setCodec(
"UTF-8" );
78 mHtmlSource = in.readAll();
82 setFeatureForMapPosition();
88 setFeatureForMapPosition();
100 if ( !painter || !mWidgetContainer )
114 if ( data( 1 ).toString() ==
"composer" )
129 QSizeF widgetMinSize = mWebView->minimumSize();
134 return QSizeF( 0, 0 );
140 QDomElement documentElem = doc.documentElement();
141 if ( documentElem.isNull() )
146 QDomElement formAnnotationElem = doc.createElement(
"HtmlAnnotationItem" );
149 formAnnotationElem.setAttribute(
"vectorLayer", mVectorLayer->
id() );
151 formAnnotationElem.setAttribute(
"hasFeature", mHasAssociatedFeature );
152 formAnnotationElem.setAttribute(
"feature", mFeatureId );
153 formAnnotationElem.setAttribute(
"htmlfile",
htmlPage() );
156 documentElem.appendChild( formAnnotationElem );
162 if ( itemElem.hasAttribute(
"vectorLayer" ) )
167 QObject::connect( mVectorLayer, SIGNAL( layerModified() ),
this, SLOT( setFeatureForMapPosition() ) );
168 QObject::connect(
mMapCanvas, SIGNAL( renderComplete( QPainter* ) ),
this, SLOT( setFeatureForMapPosition() ) );
169 QObject::connect(
mMapCanvas, SIGNAL( layersChanged() ),
this, SLOT( updateVisibility() ) );
172 mHasAssociatedFeature = itemElem.attribute(
"hasFeature",
"0" ).toInt();
173 mFeatureId = itemElem.attribute(
"feature",
"0" ).toInt();
174 mHtmlFile = itemElem.attribute(
"htmlfile",
"" );
175 QDomElement annotationElem = itemElem.firstChildElement(
"AnnotationItem" );
176 if ( !annotationElem.isNull() )
188 void QgsHtmlAnnotationItem::setFeatureForMapPosition()
204 bool featureFound =
false;
206 while ( fit.nextFeature( currentFeature ) )
208 currentFeatureId = currentFeature.
id();
213 mHasAssociatedFeature = featureFound;
214 mFeatureId = currentFeatureId;
215 mFeature = currentFeature;
218 mWebView->setHtml( newtext );
221 void QgsHtmlAnnotationItem::updateVisibility()
228 setVisible( visible );
231 void QgsHtmlAnnotationItem::javascript()
233 QWebFrame *frame = mWebView->page()->mainFrame();
234 frame->addToJavaScriptWindowObject(
"canvas",
mMapCanvas );
235 frame->addToJavaScriptWindowObject(
"layer", mVectorLayer );
QgsFeatureId id() const
Get the feature id for this feature.
void readXML(const QDomDocument &doc, const QDomElement &itemElem) override
Wrapper for iterator of features from vector data provider or vector layer.
A rectangle specified with double values.
QgsVectorLayer * vectorLayer() const
void _readXML(const QDomDocument &doc, const QDomElement &annotationElem)
double mFrameBorderWidth
Width of the frame.
Use exact geometry intersection (slower) instead of bounding boxes.
void setMapPosition(const QgsPoint &pos) override
Reimplemented from QgsAnnotationItem.
QPointF mOffsetFromReferencePoint
Describes the shift of the item content box to the reference point.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
static QgsNetworkAccessManager * instance()
QList< QgsMapLayer * > layers() const
return list of layers within map canvas.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Map canvas is a class for displaying all GIS data types on a canvas.
QgsHtmlAnnotationItem(QgsMapCanvas *canvas, QgsVectorLayer *vlayer=0, bool hasFeature=false, int feature=0)
void drawSelectionBoxes(QPainter *p)
virtual void setMapPosition(const QgsPoint &pos)
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
void drawMarkerSymbol(QPainter *p)
A class to represent a point.
An annotation item can be either placed either on screen corrdinates or on map coordinates.
bool mMapPositionFixed
True: the item stays at the same map position, False: the item stays on same screen position...
void _writeXML(QDomDocument &doc, QDomElement &itemElem) const
void paint(QPainter *painter) override
function to be implemented by derived classes
void setHTMLPage(const QString &htmlFile)
QgsMapCanvas * mMapCanvas
pointer to map canvas
void drawFrame(QPainter *p)
void writeXML(QDomDocument &doc) const override
QSizeF minimumFrameSize() const override
QgsMapLayer * mapLayer(QString theLayerId)
Retrieve a pointer to a loaded layer by id.
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Represents a vector layer which manages a vector based data sets.
QgsPoint mMapPosition
Map position (in case mMapPositionFixed is true)
QSizeF mFrameSize
Size of the frame (without balloon)
static QString replaceExpressionText(const QString &action, const QgsFeature *feat, QgsVectorLayer *layer, const QMap< QString, QVariant > *substitutionMap=0, const QgsDistanceArea *distanceArea=0)
This function currently replaces each expression between [% and %] in the string with the result of i...