QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgshtmlannotationitem.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgshtmlannotationitem.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 "qgshtmlannotationitem.h"
19 #include "qgsattributeeditor.h"
20 #include "qgsfeature.h"
21 #include "qgslogger.h"
22 #include "qgsmapcanvas.h"
23 #include "qgsmaplayerregistry.h"
24 #include "qgsvectorlayer.h"
25 #include "qgsexpression.h"
26 
27 #include <QDomElement>
28 #include <QDir>
29 #include <QFile>
30 #include <QFileInfo>
31 #include <QGraphicsProxyWidget>
32 #include <QPainter>
33 #include <QSettings>
34 #include <QWidget>
35 
36 
37 QgsHtmlAnnotationItem::QgsHtmlAnnotationItem( QgsMapCanvas* canvas, QgsVectorLayer* vlayer, bool hasFeature, int feature )
38  : QgsAnnotationItem( canvas ), mWidgetContainer( 0 ), mWebView( 0 ), mVectorLayer( vlayer ),
39  mHasAssociatedFeature( hasFeature ), mFeatureId( feature )
40 {
41  mWebView = new QWebView();
42  mWidgetContainer = new QGraphicsProxyWidget( this );
43  mWidgetContainer->setWidget( mWebView );
44 
45  QObject::connect( mWebView->page()->mainFrame(), SIGNAL( javaScriptWindowObjectCleared() ), this, SLOT( javascript() ) );
46 
47  if ( mVectorLayer && mMapCanvas )
48  {
49  QObject::connect( mVectorLayer, SIGNAL( layerModified() ), this, SLOT( setFeatureForMapPosition() ) );
50  QObject::connect( mMapCanvas, SIGNAL( renderComplete( QPainter* ) ), this, SLOT( setFeatureForMapPosition() ) );
51  QObject::connect( mMapCanvas, SIGNAL( layersChanged() ), this, SLOT( updateVisibility() ) );
52  }
53 
55 }
56 
58 {
59  delete mWebView;
60 }
61 
62 void QgsHtmlAnnotationItem::setHTMLPage( const QString& htmlFile )
63 {
64  QFile file( htmlFile );
65  mHtmlFile = htmlFile;
66  if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
67  {
68  mHtmlSource = "";
69  }
70  else
71  {
72  QTextStream in( &file );
73  in.setCodec( "UTF-8" );
74  mHtmlSource = in.readAll();
75  }
76 
77  file.close();
79 }
80 
82 {
85 }
86 
87 void QgsHtmlAnnotationItem::paint( QPainter * painter )
88 {
89  Q_UNUSED( painter );
90 }
91 
92 void QgsHtmlAnnotationItem::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget )
93 {
94  Q_UNUSED( option );
95  Q_UNUSED( widget );
96  if ( !painter || !mWidgetContainer )
97  {
98  return;
99  }
100 
101  drawFrame( painter );
102  if ( mMapPositionFixed )
103  {
104  drawMarkerSymbol( painter );
105  }
106 
108  + mFrameBorderWidth / 2.0, mFrameSize.width() - mFrameBorderWidth, mFrameSize.height()
109  - mFrameBorderWidth ) );
110  if ( data( 1 ).toString() == "composer" )
111  {
112  mWidgetContainer->widget()->render( painter, mOffsetFromReferencePoint.toPoint() );
113  }
114 
115  if ( isSelected() )
116  {
117  drawSelectionBoxes( painter );
118  }
119 }
120 
122 {
123  if ( mWebView )
124  {
125  QSizeF widgetMinSize = mWebView->minimumSize();
126  return QSizeF( 2 * mFrameBorderWidth + widgetMinSize.width(), 2 * mFrameBorderWidth + widgetMinSize.height() );
127  }
128  else
129  {
130  return QSizeF( 0, 0 );
131  }
132 }
133 
134 void QgsHtmlAnnotationItem::writeXML( QDomDocument& doc ) const
135 {
136  QDomElement documentElem = doc.documentElement();
137  if ( documentElem.isNull() )
138  {
139  return;
140  }
141 
142  QDomElement formAnnotationElem = doc.createElement( "HtmlAnnotationItem" );
143  if ( mVectorLayer )
144  {
145  formAnnotationElem.setAttribute( "vectorLayer", mVectorLayer->id() );
146  }
147  formAnnotationElem.setAttribute( "hasFeature", mHasAssociatedFeature );
148  formAnnotationElem.setAttribute( "feature", mFeatureId );
149  formAnnotationElem.setAttribute( "htmlfile", htmlPage() );
150 
151  _writeXML( doc, formAnnotationElem );
152  documentElem.appendChild( formAnnotationElem );
153 }
154 
155 void QgsHtmlAnnotationItem::readXML( const QDomDocument& doc, const QDomElement& itemElem )
156 {
157  mVectorLayer = 0;
158  if ( itemElem.hasAttribute( "vectorLayer" ) )
159  {
160  mVectorLayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( itemElem.attribute( "vectorLayer", "" ) ) );
161  if ( mVectorLayer )
162  {
163  QObject::connect( mVectorLayer, SIGNAL( layerModified() ), this, SLOT( setFeatureForMapPosition() ) );
164  QObject::connect( mMapCanvas, SIGNAL( renderComplete( QPainter* ) ), this, SLOT( setFeatureForMapPosition() ) );
165  QObject::connect( mMapCanvas, SIGNAL( layersChanged() ), this, SLOT( updateVisibility() ) );
166  }
167  }
168  mHasAssociatedFeature = itemElem.attribute( "hasFeature", "0" ).toInt();
169  mFeatureId = itemElem.attribute( "feature", "0" ).toInt();
170  mHtmlFile = itemElem.attribute( "htmlfile", "" );
171  QDomElement annotationElem = itemElem.firstChildElement( "AnnotationItem" );
172  if ( !annotationElem.isNull() )
173  {
174  _readXML( doc, annotationElem );
175  }
176 
177  if ( mWebView )
178  {
180  }
182 }
183 
185 {
186  if ( !mVectorLayer || !mMapCanvas )
187  {
188  return;
189  }
190 
191  QSettings settings;
192  double identifyValue = settings.value( "/Map/identifyRadius", QGis::DEFAULT_IDENTIFY_RADIUS ).toDouble();
193  double halfIdentifyWidth = mMapCanvas->extent().width() / 100 / 2 * identifyValue;
194  QgsRectangle searchRect( mMapPosition.x() - halfIdentifyWidth, mMapPosition.y() - halfIdentifyWidth,
195  mMapPosition.x() + halfIdentifyWidth, mMapPosition.y() + halfIdentifyWidth );
196 
198 
199  QgsFeature currentFeature;
200  QgsFeatureId currentFeatureId = 0;
201  bool featureFound = false;
202 
203  while ( fit.nextFeature( currentFeature ) )
204  {
205  currentFeatureId = currentFeature.id();
206  featureFound = true;
207  break;
208  }
209 
210  mHasAssociatedFeature = featureFound;
211  mFeatureId = currentFeatureId;
212  mFeature = currentFeature;
213 
215  mWebView->setHtml( newtext );
216 }
217 
219 {
220  bool visible = true;
221  if ( mVectorLayer && mMapCanvas )
222  {
223  visible = mMapCanvas->layers().contains( mVectorLayer );
224  }
225  setVisible( visible );
226 }
227 
229 {
230  QWebFrame *frame = mWebView->page()->mainFrame();
231  frame->addToJavaScriptWindowObject( "canvas", mMapCanvas );
232  frame->addToJavaScriptWindowObject( "layer", mVectorLayer );
233 }
234 
235 
236