|
| 1 | +#include "qgsresidualplotitem.h" |
| 2 | +#include "qgsgeorefdatapoint.h" |
| 3 | +#include <QPainter> |
| 4 | +#include <cfloat> |
| 5 | +#ifndef Q_OS_MACX |
| 6 | +#include <cmath> |
| 7 | +#else |
| 8 | +#include <math.h> |
| 9 | +#endif |
| 10 | + |
| 11 | +QgsResidualPlotItem::QgsResidualPlotItem( QgsComposition* c ): QgsComposerItem( c ), mConvertScaleToMapUnits( false ), mPixelToMapUnits( 1.0 ) |
| 12 | +{ |
| 13 | + |
| 14 | +} |
| 15 | + |
| 16 | +QgsResidualPlotItem::~QgsResidualPlotItem() |
| 17 | +{ |
| 18 | + |
| 19 | +} |
| 20 | + |
| 21 | +void QgsResidualPlotItem::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget ) |
| 22 | +{ |
| 23 | + if ( mGCPList.size() < 1 || !painter ) |
| 24 | + { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + double widthMM = rect().width(); |
| 29 | + double heightMM = rect().height(); |
| 30 | + |
| 31 | + QPen enabledPen( QColor( 0, 0, 255, 255 ) ); |
| 32 | + enabledPen.setWidthF( 0.4 ); |
| 33 | + QPen disabledPen( QColor( 0, 0, 255, 127 ) ); |
| 34 | + disabledPen.setWidthF( 0.3 ); |
| 35 | + QBrush enabledBrush( QColor( 255, 0, 0, 255 ) ); |
| 36 | + QBrush disabledBrush( QColor( 255, 0, 0, 127 ) ); |
| 37 | + |
| 38 | + //draw all points and collect minimal mm/pixel ratio |
| 39 | + double minMMPixelRatio = DBL_MAX; |
| 40 | + double mmPixelRatio = 1; |
| 41 | + |
| 42 | + painter->setRenderHint( QPainter::Antialiasing, true ); |
| 43 | + |
| 44 | + QgsGCPList::const_iterator gcpIt = mGCPList.constBegin(); |
| 45 | + for ( ; gcpIt != mGCPList.constEnd(); ++gcpIt ) |
| 46 | + { |
| 47 | + QgsPoint gcpCoords = ( *gcpIt )->pixelCoords(); |
| 48 | + double gcpItemMMX = ( gcpCoords.x() - mExtent.xMinimum() ) / mExtent.width() * widthMM; |
| 49 | + double gcpItemMMY = ( 1 - ( gcpCoords.y() - mExtent.yMinimum() ) / mExtent.height() ) * heightMM; |
| 50 | + |
| 51 | + if (( *gcpIt )->isEnabled() ) |
| 52 | + { |
| 53 | + painter->setPen( enabledPen ); |
| 54 | + painter->setBrush( enabledBrush ); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + painter->setPen( disabledPen ); |
| 59 | + painter->setBrush( disabledBrush ); |
| 60 | + } |
| 61 | + painter->drawRect( QRectF( gcpItemMMX - 1, gcpItemMMY - 1, 2, 2 ) ); |
| 62 | + drawText( painter, gcpItemMMX + 2, gcpItemMMY + 2, QString::number(( *gcpIt )->id() ), QFont() ); |
| 63 | + |
| 64 | + mmPixelRatio = maxMMToPixelRatioForGCP( *gcpIt, gcpItemMMX, gcpItemMMY ); |
| 65 | + if ( mmPixelRatio < minMMPixelRatio ) |
| 66 | + { |
| 67 | + minMMPixelRatio = mmPixelRatio; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + //draw residual arrows |
| 72 | + gcpIt = mGCPList.constBegin(); |
| 73 | + for ( ; gcpIt != mGCPList.constEnd(); ++gcpIt ) |
| 74 | + { |
| 75 | + QgsPoint gcpCoords = ( *gcpIt )->pixelCoords(); |
| 76 | + double gcpItemMMX = ( gcpCoords.x() - mExtent.xMinimum() ) / mExtent.width() * widthMM; |
| 77 | + double gcpItemMMY = ( 1 - ( gcpCoords.y() - mExtent.yMinimum() ) / mExtent.height() ) * heightMM; |
| 78 | + if (( *gcpIt )->isEnabled() ) |
| 79 | + { |
| 80 | + painter->setPen( enabledPen ); |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + painter->setPen( disabledPen ); |
| 85 | + } |
| 86 | + |
| 87 | + QPointF p1( gcpItemMMX, gcpItemMMY ); |
| 88 | + QPointF p2( gcpItemMMX + ( *gcpIt )->residual().x() * minMMPixelRatio, gcpItemMMY + ( *gcpIt )->residual().y() * minMMPixelRatio ); |
| 89 | + painter->drawLine( p1, p2 ); |
| 90 | + painter->setBrush( QBrush( painter->pen().color() ) ); |
| 91 | + drawArrowHead( painter, p2.x(), p2.y(), angle( p1, p2 ), 1 ); |
| 92 | + } |
| 93 | + |
| 94 | + //draw scale bar |
| 95 | + double initialScaleBarWidth = rect().width() / 5; |
| 96 | + int nUnits; |
| 97 | + double scaleBarWidth; |
| 98 | + if ( mConvertScaleToMapUnits ) //map units |
| 99 | + { |
| 100 | + nUnits = initialScaleBarWidth / minMMPixelRatio * mPixelToMapUnits; |
| 101 | + scaleBarWidth = nUnits * minMMPixelRatio / mPixelToMapUnits; |
| 102 | + } |
| 103 | + else //pixels |
| 104 | + { |
| 105 | + nUnits = initialScaleBarWidth / minMMPixelRatio; |
| 106 | + scaleBarWidth = nUnits * minMMPixelRatio; |
| 107 | + } |
| 108 | + |
| 109 | + painter->setPen( QColor( 0, 0, 0 ) ); |
| 110 | + painter->drawLine( QPointF( 5, rect().height() - 5 ), QPointF( 5 + scaleBarWidth, rect().height() - 5 ) ); |
| 111 | + painter->drawLine( QPointF( 5, rect().height() - 5 ), QPointF( 5, rect().height() - 7 ) ); |
| 112 | + painter->drawLine( QPointF( 5 + scaleBarWidth, rect().height() - 5 ), QPointF( 5 + scaleBarWidth, rect().height() - 7 ) ); |
| 113 | + QFont scaleBarFont; |
| 114 | + if ( mConvertScaleToMapUnits ) |
| 115 | + { |
| 116 | + drawText( painter, 5, rect().height() - 4 + fontAscentMillimeters( scaleBarFont ), QString( "%1 map units" ).arg( nUnits ), QFont() ); |
| 117 | + } |
| 118 | + else |
| 119 | + { |
| 120 | + drawText( painter, 5, rect().height() - 4 + fontAscentMillimeters( scaleBarFont ), QString( "%1 pixels" ).arg( nUnits ), QFont() ); |
| 121 | + } |
| 122 | + |
| 123 | + drawFrame( painter ); |
| 124 | + if ( isSelected() ) |
| 125 | + { |
| 126 | + drawSelectionBoxes( painter ); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +double QgsResidualPlotItem::maxMMToPixelRatioForGCP( const QgsGeorefDataPoint* p, double pixelXMM, double pixelYMM ) |
| 131 | +{ |
| 132 | + if ( !p ) |
| 133 | + { |
| 134 | + return 0; |
| 135 | + } |
| 136 | + |
| 137 | + //calculate intersections with upper / lower frame edge depending on the residual y sign |
| 138 | + double upDownDist = DBL_MAX; //distance to frame intersection with lower or upper frame |
| 139 | + double leftRightDist = DBL_MAX; //distance to frame intersection with left or right frame |
| 140 | + |
| 141 | + QPointF residual = p->residual(); |
| 142 | + QLineF residualLine( pixelXMM, pixelYMM, pixelXMM + residual.x(), pixelYMM + residual.y() ); |
| 143 | + QPointF intersectionPoint; |
| 144 | + double dx, dy; |
| 145 | + |
| 146 | + if ( residual.y() > 0 ) |
| 147 | + { |
| 148 | + QLineF lowerFrameLine( 0, rect().height(), rect().width(), rect().height() ); |
| 149 | + if ( residualLine.intersect( lowerFrameLine, &intersectionPoint ) != QLineF::NoIntersection ) |
| 150 | + { |
| 151 | + upDownDist = dist( QPointF( pixelXMM, pixelYMM ) , intersectionPoint ); |
| 152 | + } |
| 153 | + } |
| 154 | + else if ( residual.y() < 0 ) |
| 155 | + { |
| 156 | + QLineF upperFrameLine( 0, 0, mExtent.xMaximum(), 0 ); |
| 157 | + if ( residualLine.intersect( upperFrameLine, &intersectionPoint ) != QLineF::NoIntersection ) |
| 158 | + { |
| 159 | + upDownDist = dist( QPointF( pixelXMM, pixelYMM ) , intersectionPoint ); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + //calculate intersection with left / right frame edge depending on the residual x sign |
| 164 | + if ( residual.x() > 0 ) |
| 165 | + { |
| 166 | + QLineF rightFrameLine( rect().width(), 0, rect().width(), rect().height() ); |
| 167 | + if ( residualLine.intersect( rightFrameLine, &intersectionPoint ) != QLineF::NoIntersection ) |
| 168 | + { |
| 169 | + leftRightDist = dist( QPointF( pixelXMM, pixelYMM ) , intersectionPoint ); |
| 170 | + } |
| 171 | + } |
| 172 | + else if ( residual.x() < 0 ) |
| 173 | + { |
| 174 | + QLineF leftFrameLine( 0, 0 , 0, rect().height() ); |
| 175 | + if ( residualLine.intersect( leftFrameLine, &intersectionPoint ) != QLineF::NoIntersection ) |
| 176 | + { |
| 177 | + leftRightDist = dist( QPointF( pixelXMM, pixelYMM ) , intersectionPoint ); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + double resTot = sqrt( residual.x() * residual.x() + residual.y() * residual.y() ); |
| 182 | + if ( leftRightDist <= upDownDist ) |
| 183 | + { |
| 184 | + return leftRightDist / resTot; |
| 185 | + } |
| 186 | + else |
| 187 | + { |
| 188 | + return upDownDist / resTot; |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +bool QgsResidualPlotItem::writeXML( QDomElement& elem, QDomDocument & doc ) const |
| 193 | +{ |
| 194 | + return false; |
| 195 | +} |
| 196 | + |
| 197 | +bool QgsResidualPlotItem::readXML( const QDomElement& itemElem, const QDomDocument& doc ) |
| 198 | +{ |
| 199 | + return false; |
| 200 | +} |
| 201 | + |
| 202 | +double QgsResidualPlotItem::dist( const QPointF& p1, const QPointF& p2 ) const |
| 203 | +{ |
| 204 | + double dx = p2.x() - p1.x(); |
| 205 | + double dy = p2.y() - p1.y(); |
| 206 | + return sqrt( dx * dx + dy * dy ); |
| 207 | +} |
0 commit comments