|
15 | 15 |
|
16 | 16 | #include "qgsresidualplotitem.h"
|
17 | 17 | #include "qgsgeorefdatapoint.h"
|
18 |
| -#include "qgscomposerutils.h" |
| 18 | +#include "qgslayoututils.h" |
19 | 19 | #include <QPainter>
|
20 | 20 | #include <cfloat>
|
21 | 21 | #include <cmath>
|
22 | 22 |
|
23 |
| -QgsResidualPlotItem::QgsResidualPlotItem( QgsComposition *c ) |
24 |
| - : QgsComposerItem( c ) |
| 23 | +QgsResidualPlotItem::QgsResidualPlotItem( QgsLayout *layout ) |
| 24 | + : QgsLayoutItem( layout ) |
25 | 25 | , mConvertScaleToMapUnits( false )
|
26 | 26 | {
|
27 |
| - |
| 27 | + setBackgroundEnabled( false ); |
28 | 28 | }
|
29 | 29 |
|
30 | 30 | void QgsResidualPlotItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget )
|
@@ -68,7 +68,7 @@ void QgsResidualPlotItem::paint( QPainter *painter, const QStyleOptionGraphicsIt
|
68 | 68 | painter->setBrush( disabledBrush );
|
69 | 69 | }
|
70 | 70 | painter->drawRect( QRectF( gcpItemMMX - 0.5, gcpItemMMY - 0.5, 1, 1 ) );
|
71 |
| - QgsComposerUtils::drawText( painter, QPointF( gcpItemMMX + 2, gcpItemMMY + 2 ), QString::number( ( *gcpIt )->id() ), QFont() ); |
| 71 | + QgsLayoutUtils::drawText( painter, QPointF( gcpItemMMX + 2, gcpItemMMY + 2 ), QString::number( ( *gcpIt )->id() ), QFont() ); |
72 | 72 |
|
73 | 73 | mmPixelRatio = maxMMToPixelRatioForGCP( *gcpIt, gcpItemMMX, gcpItemMMY );
|
74 | 74 | if ( mmPixelRatio < minMMPixelRatio )
|
@@ -97,7 +97,7 @@ void QgsResidualPlotItem::paint( QPainter *painter, const QStyleOptionGraphicsIt
|
97 | 97 | QPointF p2( gcpItemMMX + ( *gcpIt )->residual().x() * minMMPixelRatio, gcpItemMMY + ( *gcpIt )->residual().y() * minMMPixelRatio );
|
98 | 98 | painter->drawLine( p1, p2 );
|
99 | 99 | painter->setBrush( QBrush( painter->pen().color() ) );
|
100 |
| - QgsComposerUtils::drawArrowHead( painter, p2.x(), p2.y(), QgsComposerUtils::angle( p1, p2 ), 1 ); |
| 100 | + drawArrowHead( painter, p2.x(), p2.y(), angle( p1, p2 ), 1 ); |
101 | 101 | }
|
102 | 102 |
|
103 | 103 | //draw scale bar
|
@@ -132,20 +132,29 @@ void QgsResidualPlotItem::paint( QPainter *painter, const QStyleOptionGraphicsIt
|
132 | 132 | scaleBarFont.setPointSize( 9 );
|
133 | 133 | if ( mConvertScaleToMapUnits )
|
134 | 134 | {
|
135 |
| - QgsComposerUtils::drawText( painter, QPointF( 5, rect().height() - 4 + QgsComposerUtils::fontAscentMM( scaleBarFont ) ), QStringLiteral( "%1 map units" ).arg( scaleBarWidthUnits ), QFont() ); |
| 135 | + QgsLayoutUtils::drawText( painter, QPointF( 5, rect().height() - 4 + QgsLayoutUtils::fontAscentMM( scaleBarFont ) ), QStringLiteral( "%1 map units" ).arg( scaleBarWidthUnits ), QFont() ); |
136 | 136 | }
|
137 | 137 | else
|
138 | 138 | {
|
139 |
| - QgsComposerUtils::drawText( painter, QPointF( 5, rect().height() - 4 + QgsComposerUtils::fontAscentMM( scaleBarFont ) ), QStringLiteral( "%1 pixels" ).arg( scaleBarWidthUnits ), QFont() ); |
| 139 | + QgsLayoutUtils::drawText( painter, QPointF( 5, rect().height() - 4 + QgsLayoutUtils::fontAscentMM( scaleBarFont ) ), QStringLiteral( "%1 pixels" ).arg( scaleBarWidthUnits ), QFont() ); |
140 | 140 | }
|
141 | 141 |
|
142 |
| - drawFrame( painter ); |
143 |
| - if ( isSelected() ) |
| 142 | + if ( frameEnabled() ) |
144 | 143 | {
|
145 |
| - drawSelectionBoxes( painter ); |
| 144 | + painter->save(); |
| 145 | + painter->setPen( pen() ); |
| 146 | + painter->setBrush( Qt::NoBrush ); |
| 147 | + painter->setRenderHint( QPainter::Antialiasing, true ); |
| 148 | + painter->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) ); |
| 149 | + painter->restore(); |
146 | 150 | }
|
147 | 151 | }
|
148 | 152 |
|
| 153 | +void QgsResidualPlotItem::draw( QgsRenderContext &, const QStyleOptionGraphicsItem * ) |
| 154 | +{ |
| 155 | + |
| 156 | +} |
| 157 | + |
149 | 158 | double QgsResidualPlotItem::maxMMToPixelRatioForGCP( const QgsGeorefDataPoint *p, double pixelXMM, double pixelYMM )
|
150 | 159 | {
|
151 | 160 | if ( !p )
|
@@ -207,23 +216,65 @@ double QgsResidualPlotItem::maxMMToPixelRatioForGCP( const QgsGeorefDataPoint *p
|
207 | 216 | }
|
208 | 217 | }
|
209 | 218 |
|
210 |
| -bool QgsResidualPlotItem::writeXml( QDomElement &elem, QDomDocument &doc ) const |
| 219 | +double QgsResidualPlotItem::dist( QPointF p1, QPointF p2 ) const |
211 | 220 | {
|
212 |
| - Q_UNUSED( elem ); |
213 |
| - Q_UNUSED( doc ); |
214 |
| - return false; |
| 221 | + double dx = p2.x() - p1.x(); |
| 222 | + double dy = p2.y() - p1.y(); |
| 223 | + return std::sqrt( dx * dx + dy * dy ); |
215 | 224 | }
|
216 | 225 |
|
217 |
| -bool QgsResidualPlotItem::readXml( const QDomElement &itemElem, const QDomDocument &doc ) |
| 226 | +void QgsResidualPlotItem::drawArrowHead( QPainter *p, const double x, const double y, const double angle, const double arrowHeadWidth ) |
218 | 227 | {
|
219 |
| - Q_UNUSED( itemElem ); |
220 |
| - Q_UNUSED( doc ); |
221 |
| - return false; |
| 228 | + if ( !p ) |
| 229 | + { |
| 230 | + return; |
| 231 | + } |
| 232 | + |
| 233 | + double angleRad = angle / 180.0 * M_PI; |
| 234 | + QPointF middlePoint( x, y ); |
| 235 | + //rotate both arrow points |
| 236 | + QPointF p1 = QPointF( -arrowHeadWidth / 2.0, arrowHeadWidth ); |
| 237 | + QPointF p2 = QPointF( arrowHeadWidth / 2.0, arrowHeadWidth ); |
| 238 | + |
| 239 | + QPointF p1Rotated, p2Rotated; |
| 240 | + p1Rotated.setX( p1.x() * std::cos( angleRad ) + p1.y() * -std::sin( angleRad ) ); |
| 241 | + p1Rotated.setY( p1.x() * std::sin( angleRad ) + p1.y() * std::cos( angleRad ) ); |
| 242 | + p2Rotated.setX( p2.x() * std::cos( angleRad ) + p2.y() * -std::sin( angleRad ) ); |
| 243 | + p2Rotated.setY( p2.x() * std::sin( angleRad ) + p2.y() * std::cos( angleRad ) ); |
| 244 | + |
| 245 | + QPolygonF arrowHeadPoly; |
| 246 | + arrowHeadPoly << middlePoint; |
| 247 | + arrowHeadPoly << QPointF( middlePoint.x() + p1Rotated.x(), middlePoint.y() + p1Rotated.y() ); |
| 248 | + arrowHeadPoly << QPointF( middlePoint.x() + p2Rotated.x(), middlePoint.y() + p2Rotated.y() ); |
| 249 | + |
| 250 | + p->save(); |
| 251 | + |
| 252 | + QPen arrowPen = p->pen(); |
| 253 | + arrowPen.setJoinStyle( Qt::RoundJoin ); |
| 254 | + QBrush arrowBrush = p->brush(); |
| 255 | + arrowBrush.setStyle( Qt::SolidPattern ); |
| 256 | + p->setPen( arrowPen ); |
| 257 | + p->setBrush( arrowBrush ); |
| 258 | + arrowBrush.setStyle( Qt::SolidPattern ); |
| 259 | + p->drawPolygon( arrowHeadPoly ); |
| 260 | + |
| 261 | + p->restore(); |
222 | 262 | }
|
223 | 263 |
|
224 |
| -double QgsResidualPlotItem::dist( QPointF p1, QPointF p2 ) const |
| 264 | +double QgsResidualPlotItem::angle( QPointF p1, QPointF p2 ) |
225 | 265 | {
|
226 |
| - double dx = p2.x() - p1.x(); |
227 |
| - double dy = p2.y() - p1.y(); |
228 |
| - return std::sqrt( dx * dx + dy * dy ); |
| 266 | + double xDiff = p2.x() - p1.x(); |
| 267 | + double yDiff = p2.y() - p1.y(); |
| 268 | + double length = std::sqrt( xDiff * xDiff + yDiff * yDiff ); |
| 269 | + if ( length <= 0 ) |
| 270 | + { |
| 271 | + return 0; |
| 272 | + } |
| 273 | + |
| 274 | + double angle = std::acos( ( -yDiff * length ) / ( length * length ) ) * 180 / M_PI; |
| 275 | + if ( xDiff < 0 ) |
| 276 | + { |
| 277 | + return ( 360 - angle ); |
| 278 | + } |
| 279 | + return angle; |
229 | 280 | }
|
0 commit comments