15
15
/* $Id$ */
16
16
17
17
#include " qgsgcpcanvasitem.h"
18
+ #include " qgsgeorefdatapoint.h"
19
+ #include " qgsmaplayerregistry.h"
20
+ #include " qgsmaprenderer.h"
21
+ #include " qgsrasterlayer.h"
18
22
19
- QgsGCPCanvasItem::QgsGCPCanvasItem ( QgsMapCanvas* mapCanvas, const QgsPoint& rasterCoords,
20
- const QgsPoint& worldCoords, bool isGCPSource )
21
- : QgsMapCanvasItem( mapCanvas )
23
+ QgsGCPCanvasItem::QgsGCPCanvasItem ( QgsMapCanvas* mapCanvas, const QgsGeorefDataPoint* dataPoint, bool isGCPSource )
24
+ : QgsMapCanvasItem( mapCanvas ), mDataPoint( dataPoint )
22
25
, mPointBrush( Qt::red )
23
26
, mLabelBrush( Qt::yellow )
24
- , mRasterCoords( rasterCoords )
25
- , mWorldCoords( worldCoords )
26
- , mId( -1 )
27
27
, mIsGCPSource( isGCPSource )
28
- , mEnabled( true )
29
28
{
30
29
setFlags ( QGraphicsItem::ItemIsMovable );
30
+ mResidualPen .setColor ( QColor ( 255 , 0 , 0 ) );
31
+ mResidualPen .setWidthF ( 2.0 );
31
32
32
33
updatePosition ();
33
34
}
34
35
35
36
void QgsGCPCanvasItem::paint ( QPainter* p )
36
37
{
37
38
p->setRenderHint ( QPainter::Antialiasing );
38
- p->setOpacity ( mEnabled ? 1.0 : 0.3 );
39
+
40
+ bool enabled = true ;
41
+ QgsPoint worldCoords;
42
+ int id = -1 ;
43
+
44
+ if ( mDataPoint )
45
+ {
46
+ enabled = mDataPoint ->isEnabled ();
47
+ worldCoords = mDataPoint ->mapCoords ();
48
+ id = mDataPoint ->id ();
49
+ }
50
+
51
+ p->setOpacity ( enabled ? 1.0 : 0.3 );
39
52
40
53
// draw the point
41
54
p->setPen ( Qt::black );
@@ -46,8 +59,8 @@ void QgsGCPCanvasItem::paint( QPainter* p )
46
59
bool showIDs = s.value ( " /Plugin-GeoReferencer/Config/ShowId" ).toBool ();
47
60
if ( !showIDs && mIsGCPSource )
48
61
{
49
- QString msg = QString ( " X %1\n Y %2" ).arg ( QString::number ( mWorldCoords .x (), ' f' ) ).
50
- arg ( QString::number ( mWorldCoords .y (), ' f' ) );
62
+ QString msg = QString ( " X %1\n Y %2" ).arg ( QString::number ( worldCoords .x (), ' f' ) ).
63
+ arg ( QString::number ( worldCoords .y (), ' f' ) );
51
64
p->setFont ( QFont ( " helvetica" , 9 ) );
52
65
QRect textBounds = p->boundingRect ( 6 , 6 , 10 , 10 , Qt::AlignLeft, msg );
53
66
p->setBrush ( mLabelBrush );
@@ -58,20 +71,52 @@ void QgsGCPCanvasItem::paint( QPainter* p )
58
71
else if ( showIDs )
59
72
{
60
73
p->setFont ( QFont ( " helvetica" , 12 ) );
61
- QString msg = QString::number ( mId );
74
+ QString msg = QString::number ( id );
62
75
p->setBrush ( mLabelBrush );
63
76
p->drawRect ( 5 , 4 , p->fontMetrics ().width ( msg ) + 2 , 14 );
64
77
p->drawText ( 6 , 16 , msg );
65
78
QFontMetrics fm = p->fontMetrics ();
66
79
mTextBounds = QSize ( fm.width ( msg ) + 4 , fm.height () + 4 );
67
80
}
68
- // else
69
- // mTextBounds = QSizeF(0, 0 );
81
+
82
+ drawResidualArrow ( p );
70
83
}
71
84
72
85
QRectF QgsGCPCanvasItem::boundingRect () const
73
86
{
74
- return QRectF ( -2 , -2 , mTextBounds .width () + 6 , mTextBounds .height () + 6 );
87
+ double residualLeft, residualRight, residualTop, residualBottom;
88
+
89
+ QPointF residual;
90
+ if ( mDataPoint )
91
+ {
92
+ residual = mDataPoint ->residual ();
93
+ }
94
+ double rf = residualToScreenFactor ();
95
+
96
+ if ( residual.x () > 0 )
97
+ {
98
+ residualRight = residual.x () * rf + mResidualPen .widthF ();
99
+ residualLeft = -mResidualPen .widthF ();
100
+ }
101
+ else
102
+ {
103
+ residualLeft = residual.x () * rf - mResidualPen .widthF ();
104
+ residualRight = mResidualPen .widthF ();
105
+ }
106
+ if ( residual.y () > 0 )
107
+ {
108
+ residualBottom = residual.y () * rf + mResidualPen .widthF ();
109
+ residualTop = -mResidualPen .widthF ();
110
+ }
111
+ else
112
+ {
113
+ residualBottom = mResidualPen .widthF ();
114
+ residualTop = residual.y () * rf - mResidualPen .widthF ();
115
+ }
116
+
117
+ QRectF residualArrowRect ( QPointF ( residualLeft, residualTop ), QPointF ( residualRight, residualBottom ) );
118
+ QRectF markerRect ( -2 , -2 , mTextBounds .width () + 6 , mTextBounds .height () + 6 );
119
+ return residualArrowRect.united ( markerRect );
75
120
}
76
121
77
122
QPainterPath QgsGCPCanvasItem::shape () const
@@ -83,31 +128,63 @@ QPainterPath QgsGCPCanvasItem::shape() const
83
128
return p;
84
129
}
85
130
86
- void QgsGCPCanvasItem::setEnabled ( bool enabled )
131
+ void QgsGCPCanvasItem::updatePosition ( )
87
132
{
88
- mEnabled = enabled;
89
- mPointBrush = enabled ? QBrush ( Qt::red ) : QBrush ( Qt::gray );
90
- mLabelBrush = enabled ? QBrush ( Qt::yellow ) : QBrush ( Qt::gray );
91
- update ();
92
- }
133
+ if ( !mDataPoint )
134
+ {
135
+ return ;
136
+ }
93
137
94
- void QgsGCPCanvasItem::setRasterCoords ( QgsPoint p )
95
- {
96
- mRasterCoords = p;
138
+ setPos ( toCanvasCoordinates ( mIsGCPSource ? mDataPoint ->pixelCoords () : mDataPoint ->mapCoords () ) );
97
139
}
98
140
99
- void QgsGCPCanvasItem::setWorldCoords ( QgsPoint p )
141
+ void QgsGCPCanvasItem::drawResidualArrow ( QPainter* p )
100
142
{
101
- mWorldCoords = p;
143
+ if ( !mDataPoint || !mIsGCPSource )
144
+ {
145
+ return ;
146
+ }
147
+
148
+ QPointF residual = mDataPoint ->residual ();
149
+
150
+ double rf = residualToScreenFactor ();
151
+ p->setPen ( mResidualPen );
152
+ p->drawLine ( QPointF ( 0 , 0 ), QPointF ( residual.rx () * rf, residual.ry () * rf ) );
153
+
102
154
}
103
155
104
- void QgsGCPCanvasItem::setId ( int id )
156
+ double QgsGCPCanvasItem::residualToScreenFactor () const
105
157
{
106
- mId = id;
107
- update ();
158
+ if ( !mMapCanvas )
159
+ {
160
+ return 1 ;
161
+ }
162
+
163
+ double mapUnitsPerScreenPixel = mMapCanvas ->mapUnitsPerPixel ();
164
+ double mapUnitsPerRasterPixel = 1.0 ;
165
+
166
+ if ( mMapCanvas ->mapRenderer () )
167
+ {
168
+ QStringList canvasLayers = mMapCanvas ->mapRenderer ()->layerSet ();
169
+ if ( canvasLayers.size () > 0 )
170
+ {
171
+ QString layerId = canvasLayers.at ( 0 );
172
+ QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance ()->mapLayer ( layerId );
173
+ if ( mapLayer )
174
+ {
175
+ QgsRasterLayer* rasterLayer = dynamic_cast <QgsRasterLayer*>( mapLayer );
176
+ if ( rasterLayer )
177
+ {
178
+ mapUnitsPerRasterPixel = rasterLayer->rasterUnitsPerPixel ();
179
+ }
180
+ }
181
+ }
182
+ }
183
+
184
+ return 1.0 / ( mapUnitsPerScreenPixel * mapUnitsPerRasterPixel );
108
185
}
109
186
110
- void QgsGCPCanvasItem::updatePosition ()
187
+ void QgsGCPCanvasItem::checkBoundingRectChange ()
111
188
{
112
- setPos ( toCanvasCoordinates ( mIsGCPSource ? mRasterCoords : mWorldCoords ) );
189
+ prepareGeometryChange ( );
113
190
}
0 commit comments