Skip to content

Commit 949d9fa

Browse files
author
jef
committed
fix #926
git-svn-id: http://svn.osgeo.org/qgis/trunk@15315 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 9cb502e commit 949d9fa

11 files changed

+347
-44
lines changed

src/app/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ SET(QGIS_APP_SRCS
9191
qgsuniquevaluedialog.cpp
9292
qgsvectorlayerproperties.cpp
9393
qgsquerybuilder.cpp
94+
qgshighlight.cpp
9495

9596
qgsmanageconnectionsdialog.cpp
9697

src/app/qgsattributedialog.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "qgsuniquevaluerenderer.h"
2525
#include "qgssymbol.h"
2626
#include "qgsattributeeditor.h"
27-
#include "qgsrubberband.h"
27+
#include "qgshighlight.h"
2828
#include "qgssearchstring.h"
2929
#include "qgssearchtreenode.h"
3030

@@ -52,7 +52,7 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeat
5252
, mLayer( vl )
5353
, mFeature( thepFeature )
5454
, mFeatureOwner( featureOwner )
55-
, mRubberBand( 0 )
55+
, mHighlight( 0 )
5656
, mFormNr( -1 )
5757
{
5858
if ( !mFeature || !vl->dataProvider() )
@@ -299,10 +299,10 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeat
299299

300300
QgsAttributeDialog::~QgsAttributeDialog()
301301
{
302-
if ( mRubberBand )
302+
if ( mHighlight )
303303
{
304-
mRubberBand->hide();
305-
delete mRubberBand;
304+
mHighlight->hide();
305+
delete mHighlight;
306306
}
307307

308308
if ( mFeatureOwner )
@@ -381,14 +381,14 @@ void QgsAttributeDialog::restoreGeometry()
381381
}
382382
}
383383

384-
void QgsAttributeDialog::setHighlight( QgsRubberBand *rb )
384+
void QgsAttributeDialog::setHighlight( QgsHighlight *h )
385385
{
386-
if ( mRubberBand )
386+
if ( mHighlight )
387387
{
388-
delete mRubberBand;
388+
delete mHighlight;
389389
}
390390

391-
mRubberBand = rb;
391+
mHighlight = h;
392392
}
393393

394394

@@ -410,15 +410,15 @@ void QgsAttributeDialog::dialogDestroyed()
410410

411411
bool QgsAttributeDialog::eventFilter( QObject *obj, QEvent *e )
412412
{
413-
if ( mRubberBand && obj == mDialog )
413+
if ( mHighlight && obj == mDialog )
414414
{
415415
switch ( e->type() )
416416
{
417417
case QEvent::WindowActivate:
418-
mRubberBand->show();
418+
mHighlight->show();
419419
break;
420420
case QEvent::WindowDeactivate:
421-
mRubberBand->hide();
421+
mHighlight->hide();
422422
break;
423423
default:
424424
break;

src/app/qgsattributedialog.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class QgsFeature;
2525
class QLayout;
2626
class QgsField;
2727
class QgsVectorLayer;
28-
class QgsRubberBand;
28+
class QgsHighlight;
2929

3030
class QgsAttributeDialog : public QObject
3131
{
@@ -46,7 +46,7 @@ class QgsAttributeDialog : public QObject
4646
*/
4747
void restoreGeometry();
4848

49-
void setHighlight( QgsRubberBand *rb );
49+
void setHighlight( QgsHighlight *h );
5050

5151
QDialog *dialog() { return mDialog; }
5252

@@ -68,7 +68,7 @@ class QgsAttributeDialog : public QObject
6868
QgsVectorLayer *mLayer;
6969
QgsFeature *mFeature;
7070
bool mFeatureOwner;
71-
QgsRubberBand *mRubberBand;
71+
QgsHighlight *mHighlight;
7272
int mFormNr;
7373
static int smFormCounter;
7474
};

src/app/qgsfeatureaction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ QgsAttributeDialog *QgsFeatureAction::newDialog( bool cloneFeature )
7272
return dialog;
7373
}
7474

75-
bool QgsFeatureAction::viewFeatureForm( QgsRubberBand *rb )
75+
bool QgsFeatureAction::viewFeatureForm( QgsHighlight *h )
7676
{
7777
if ( !mLayer )
7878
return false;
7979

8080
QgsAttributeDialog *dialog = newDialog( true );
81-
dialog->setHighlight( rb );
81+
dialog->setHighlight( h );
8282
dialog->show();
8383

8484
return true;

src/app/qgsfeatureaction.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
class QgsIdentifyResults;
2828
class QgsVectorLayer;
29-
class QgsRubberBand;
29+
class QgsHighlight;
3030
class QgsAttributeDialog;
3131

3232
class QgsFeatureAction : public QAction
@@ -38,7 +38,7 @@ class QgsFeatureAction : public QAction
3838

3939
public slots:
4040
void execute();
41-
bool viewFeatureForm( QgsRubberBand *rb = 0 );
41+
bool viewFeatureForm( QgsHighlight *h = 0 );
4242
bool editFeature();
4343
bool addFeature();
4444

src/app/qgshighlight.cpp

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
/***************************************************************************
2+
qgshighlight.cpp - widget to highlight features on the map
3+
--------------------------------------
4+
Date : 02-03-2011
5+
Copyright : (C) 2011 by Juergen E. Fischer, norBIT GmbH
6+
Email : jef at norbit dot de
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
/* $Id$ */
16+
17+
#include "qgshighlight.h"
18+
#include "qgsgeometry.h"
19+
#include "qgslogger.h"
20+
#include "qgsmapcanvas.h"
21+
#include "qgsmaprenderer.h"
22+
#include "qgsvectorlayer.h"
23+
#include <QPainter>
24+
25+
/*!
26+
\class QgsHighlight
27+
\brief The QgsHighlight class provides a transparent overlay widget
28+
for highlightng features on the map.
29+
*/
30+
QgsHighlight::QgsHighlight( QgsMapCanvas* mapCanvas, QgsGeometry *geom, QgsVectorLayer *layer )
31+
: QgsMapCanvasItem( mapCanvas )
32+
, mLayer( layer )
33+
{
34+
QgsDebugMsg( geom ? "geometry!" : "no geometry" );
35+
mGeometry = geom ? new QgsGeometry( *geom ) : 0;
36+
updateRect();
37+
update();
38+
setColor( QColor( Qt::lightGray ) );
39+
}
40+
41+
QgsHighlight::~QgsHighlight()
42+
{
43+
delete mGeometry;
44+
}
45+
46+
/*!
47+
Set the outline and fill color.
48+
*/
49+
void QgsHighlight::setColor( const QColor & color )
50+
{
51+
mPen.setColor( color );
52+
QColor fillColor( color.red(), color.green(), color.blue(), 63 );
53+
mBrush.setColor( fillColor );
54+
mBrush.setStyle( Qt::SolidPattern );
55+
}
56+
57+
/*!
58+
Set the outline width.
59+
*/
60+
void QgsHighlight::setWidth( int width )
61+
{
62+
mPen.setWidth( width );
63+
}
64+
65+
void QgsHighlight::paintPoint( QPainter *p, QgsPoint point )
66+
{
67+
QPolygonF r( 5 );
68+
69+
double d = mMapCanvas->extent().width() * 0.005;
70+
71+
if ( mLayer )
72+
{
73+
point = mMapCanvas->mapRenderer()->layerToMapCoordinates( mLayer, point );
74+
}
75+
76+
r[0] = toCanvasCoordinates( point + QgsVector( -d, -d ) ) - pos();
77+
r[1] = toCanvasCoordinates( point + QgsVector( d, -d ) ) - pos();
78+
r[2] = toCanvasCoordinates( point + QgsVector( d, d ) ) - pos();
79+
r[3] = toCanvasCoordinates( point + QgsVector( -d, d ) ) - pos();
80+
r[4] = r[0];
81+
82+
p->drawPolygon( r );
83+
}
84+
85+
void QgsHighlight::paintLine( QPainter *p, QgsPolyline line )
86+
{
87+
QPolygonF polygon( line.size() );
88+
89+
for ( int i = 0; i < line.size(); i++ )
90+
{
91+
if ( mLayer )
92+
{
93+
line[i] = mMapCanvas->mapRenderer()->layerToMapCoordinates( mLayer, line[i] );
94+
}
95+
96+
polygon[i] = toCanvasCoordinates( line[i] ) - pos();
97+
}
98+
99+
p->drawPolyline( polygon );
100+
}
101+
102+
void QgsHighlight::paintPolygon( QPainter *p, QgsPolygon polygon )
103+
{
104+
QPolygonF poly;
105+
106+
// just ring outlines, no fill
107+
p->setPen( mPen );
108+
p->setBrush( Qt::NoBrush );
109+
110+
for ( int i = 0; i < polygon.size(); i++ )
111+
{
112+
QPolygonF ring( polygon[i].size() + 1 );
113+
114+
for ( int j = 0; j < polygon[i].size(); j++ )
115+
{
116+
if ( mLayer )
117+
{
118+
polygon[i][j] = mMapCanvas->mapRenderer()->layerToMapCoordinates( mLayer, polygon[i][j] );
119+
}
120+
121+
ring[ j ] = toCanvasCoordinates( polygon[i][j] ) - pos();
122+
}
123+
124+
ring[ polygon[i].size()] = ring[ 0 ];
125+
126+
p->drawPolygon( ring );
127+
128+
if ( i == 0 )
129+
poly = ring;
130+
else
131+
poly = poly.subtracted( ring );
132+
133+
}
134+
135+
// just fill, no outline
136+
p->setPen( Qt::NoPen );
137+
p->setBrush( mBrush );
138+
p->drawPolygon( poly );
139+
}
140+
141+
/*!
142+
Draw the shape in response to an update event.
143+
*/
144+
void QgsHighlight::paint( QPainter* p )
145+
{
146+
QgsDebugMsg( "entered." );
147+
148+
if ( !mGeometry )
149+
{
150+
QgsDebugMsg( "no geometry." );
151+
return;
152+
}
153+
154+
p->setPen( mPen );
155+
p->setBrush( mBrush );
156+
157+
switch ( mGeometry->wkbType() )
158+
{
159+
case QGis::WKBPoint:
160+
case QGis::WKBPoint25D:
161+
{
162+
paintPoint( p, mGeometry->asPoint() );
163+
}
164+
break;
165+
166+
case QGis::WKBMultiPoint:
167+
case QGis::WKBMultiPoint25D:
168+
{
169+
QgsMultiPoint m = mGeometry->asMultiPoint();
170+
for ( int i = 0; i < m.size(); i++ )
171+
{
172+
paintPoint( p, m[i] );
173+
}
174+
}
175+
break;
176+
177+
case QGis::WKBLineString:
178+
case QGis::WKBLineString25D:
179+
{
180+
paintLine( p, mGeometry->asPolyline() );
181+
}
182+
break;
183+
184+
case QGis::WKBMultiLineString:
185+
case QGis::WKBMultiLineString25D:
186+
{
187+
QgsMultiPolyline m = mGeometry->asMultiPolyline();
188+
189+
for ( int i = 0; i < m.size(); i++ )
190+
{
191+
paintLine( p, m[i] );
192+
}
193+
}
194+
break;
195+
196+
case QGis::WKBPolygon:
197+
case QGis::WKBPolygon25D:
198+
{
199+
paintPolygon( p, mGeometry->asPolygon() );
200+
}
201+
break;
202+
203+
case QGis::WKBMultiPolygon:
204+
case QGis::WKBMultiPolygon25D:
205+
{
206+
QgsMultiPolygon m = mGeometry->asMultiPolygon();
207+
for ( int i = 0; i < m.size(); i++ )
208+
{
209+
paintPolygon( p, m[i] );
210+
}
211+
}
212+
break;
213+
214+
case QGis::WKBUnknown:
215+
default:
216+
return;
217+
}
218+
}
219+
220+
void QgsHighlight::updateRect()
221+
{
222+
if ( mGeometry )
223+
{
224+
QgsRectangle r = mGeometry->boundingBox();
225+
226+
if ( r.isEmpty() )
227+
{
228+
double d = mMapCanvas->extent().width() * 0.005;
229+
r.setXMinimum( r.xMinimum() - d );
230+
r.setYMinimum( r.yMinimum() - d );
231+
r.setXMaximum( r.xMaximum() + d );
232+
r.setXMaximum( r.yMaximum() + d );
233+
}
234+
235+
setRect( r );
236+
setVisible( mGeometry );
237+
}
238+
else
239+
{
240+
setRect( QgsRectangle() );
241+
}
242+
243+
}
244+

0 commit comments

Comments
 (0)