1414 ***************************************************************************/
1515/* $Id$ */
1616
17+ #include " qgsmessageviewer.h"
1718#include " qgsmaptoolidentify.h"
1819#include " qgsmapcanvas.h"
1920#include " qgsvectordataprovider.h"
3031#include < QMessageBox>
3132#include < QCursor>
3233#include < QPixmap>
34+ #include < QObject>
3335
3436
3537QgsMapToolIdentify::QgsMapToolIdentify (QgsMapCanvas* canvas)
36- : QgsMapTool(canvas), mResults(NULL )
38+ : QgsMapTool(canvas),
39+ mResults(0 ),
40+ mViewer(0 )
3741{
3842 // set cursor
3943 QPixmap myIdentifyQPixmap = QPixmap ((const char **) identify_cursor);
@@ -42,7 +46,15 @@ QgsMapToolIdentify::QgsMapToolIdentify(QgsMapCanvas* canvas)
4246
4347QgsMapToolIdentify::~QgsMapToolIdentify ()
4448{
45- delete mResults ;
49+ if (mResults )
50+ {
51+ delete mResults ;
52+ }
53+
54+ if (mViewer )
55+ {
56+ delete mViewer ;
57+ }
4658}
4759
4860void QgsMapToolIdentify::canvasMoveEvent (QMouseEvent * e)
@@ -61,24 +73,38 @@ void QgsMapToolIdentify::canvasReleaseEvent(QMouseEvent * e)
6173
6274 if (layer)
6375 {
64- // convert screen coordinates to map coordinates
65- QgsPoint idPoint = mCanvas ->getCoordinateTransform ()->toMapCoordinates (e->x (), e->y ());
66-
67- if (layer->type () == QgsMapLayer::VECTOR)
76+ // In the special case of the WMS provider,
77+ // coordinates are sent back to the server as pixel coordinates
78+ // not the layer's native CRS. So identify on screen coordinates!
79+ if (
80+ (layer->type () == QgsMapLayer::RASTER)
81+ &&
82+ (dynamic_cast <QgsRasterLayer*>(layer)->providerKey () == " wms" )
83+ )
6884 {
69- identifyVectorLayer (dynamic_cast <QgsVectorLayer*>(layer), idPoint);
70- }
71- else if (layer->type () == QgsMapLayer::RASTER)
72- {
73- identifyRasterLayer (dynamic_cast <QgsRasterLayer*>(layer), idPoint);
85+ identifyRasterWmsLayer (dynamic_cast <QgsRasterLayer*>(layer), QgsPoint (e->x (), e->y ()) );
7486 }
7587 else
7688 {
89+ // convert screen coordinates to map coordinates
90+ QgsPoint idPoint = mCanvas ->getCoordinateTransform ()->toMapCoordinates (e->x (), e->y ());
91+
92+ if (layer->type () == QgsMapLayer::VECTOR)
93+ {
94+ identifyVectorLayer (dynamic_cast <QgsVectorLayer*>(layer), idPoint);
95+ }
96+ else if (layer->type () == QgsMapLayer::RASTER)
97+ {
98+ identifyRasterLayer (dynamic_cast <QgsRasterLayer*>(layer), idPoint);
99+ }
100+ else
101+ {
77102#ifdef QGISDEBUG
78- std::cout << " identify : unknown layer type!" << std::endl;
103+ std::cout << " QgsMapToolIdentify::canvasReleaseEvent : unknown layer type!" << std::endl;
79104#endif
105+ }
80106 }
81-
107+
82108 }
83109 else
84110 {
@@ -124,6 +150,34 @@ void QgsMapToolIdentify::identifyRasterLayer(QgsRasterLayer* layer, const QgsPoi
124150}
125151
126152
153+ void QgsMapToolIdentify::identifyRasterWmsLayer (QgsRasterLayer* layer, const QgsPoint& point)
154+ {
155+ if (!layer)
156+ {
157+ return ;
158+ }
159+
160+ QString html = layer->identifyAsHtml (point);
161+
162+ if (html.isEmpty ())
163+ {
164+ showError (layer);
165+ return ;
166+ }
167+
168+ if (!mViewer )
169+ {
170+ mViewer = new QgsMessageViewer ();
171+ }
172+
173+ mViewer ->setCaption ( layer->name () );
174+ mViewer ->setMessageAsPlainText ( html );
175+ // mViewer->setMessageAsHtml( html );
176+
177+ // mViewer->exec();
178+ mViewer ->show ();
179+ }
180+
127181
128182void QgsMapToolIdentify::identifyVectorLayer (QgsVectorLayer* layer, const QgsPoint& point)
129183{
@@ -313,3 +367,26 @@ void QgsMapToolIdentify::identifyVectorLayer(QgsVectorLayer* layer, const QgsPoi
313367 }
314368 dataProvider->reset ();
315369}
370+
371+
372+ void QgsMapToolIdentify::showError (QgsMapLayer * mapLayer)
373+ {
374+ // QMessageBox::warning(
375+ // this,
376+ // mapLayer->errorCaptionString(),
377+ // tr("Could not draw") + " " + mapLayer->name() + " " + tr("because") + ":\n" +
378+ // mapLayer->errorString()
379+ // );
380+
381+ QgsMessageViewer * mv = new QgsMessageViewer ();
382+ mv->setCaption ( mapLayer->errorCaptionString () );
383+ mv->setMessageAsPlainText (
384+ QObject::tr (" Could not identify objects on" ) + " " + mapLayer->name () + " " + QObject::tr (" because" ) + " :\n " +
385+ mapLayer->errorString ()
386+ );
387+ mv->exec ();
388+ delete mv;
389+
390+ }
391+
392+ // ENDS
0 commit comments