[QGIS Commit] r13328 - in trunk/qgis: python/core src/core/composer

svn_qgis at osgeo.org svn_qgis at osgeo.org
Mon Apr 19 10:31:00 EDT 2010


Author: mhugent
Date: 2010-04-19 10:30:59 -0400 (Mon, 19 Apr 2010)
New Revision: 13328

Modified:
   trunk/qgis/python/core/qgscomposeritem.sip
   trunk/qgis/src/core/composer/qgscomposerarrow.cpp
   trunk/qgis/src/core/composer/qgscomposerarrow.h
   trunk/qgis/src/core/composer/qgscomposeritem.cpp
   trunk/qgis/src/core/composer/qgscomposeritem.h
Log:
Move code to draw arrow head from composer arrow to composer item

Modified: trunk/qgis/python/core/qgscomposeritem.sip
===================================================================
--- trunk/qgis/python/core/qgscomposeritem.sip	2010-04-19 13:20:26 UTC (rev 13327)
+++ trunk/qgis/python/core/qgscomposeritem.sip	2010-04-19 14:30:59 UTC (rev 13328)
@@ -181,6 +181,12 @@
     /**Draw background*/
     virtual void drawBackground( QPainter* p );
 
+    /**Draws arrowhead*/
+    void drawArrowHead( QPainter* p, double x, double y, double angle, double arrowHeadWidth ) const;
+
+    /**Returns angle of the line from p1 to p2 (clockwise, starting at N)*/
+    double angle( const QPointF& p1, const QPointF& p2 ) const;
+
     /**Returns the current (zoom level dependent) tolerance to decide if mouse position is close enough to the \
     item border for resizing*/
     double rectHandlerBorderTolerance() const;

Modified: trunk/qgis/src/core/composer/qgscomposerarrow.cpp
===================================================================
--- trunk/qgis/src/core/composer/qgscomposerarrow.cpp	2010-04-19 13:20:26 UTC (rev 13327)
+++ trunk/qgis/src/core/composer/qgscomposerarrow.cpp	2010-04-19 14:30:59 UTC (rev 13328)
@@ -105,43 +105,15 @@
 
 void QgsComposerArrow::drawHardcodedMarker( QPainter* p, MarkerType type )
 {
-  double angle = arrowAngle();
-  //qWarning(QString::number(angle).toLocal8Bit().data());
-  double angleRad = angle / 180.0 * M_PI;
-  QPointF middlePoint = QPointF( mStopPoint.x() - transform().dx(), mStopPoint.y() - transform().dy() );
-
-  //rotate both arrow points
-  QPointF p1 = QPointF( -mArrowHeadWidth / 2.0, mArrowHeadWidth );
-  QPointF p2 = QPointF( mArrowHeadWidth / 2.0, mArrowHeadWidth );
-
-  QPointF p1Rotated, p2Rotated;
-  p1Rotated.setX( p1.x() * cos( angleRad ) + p1.y() * -sin( angleRad ) );
-  p1Rotated.setY( p1.x() * sin( angleRad ) + p1.y() * cos( angleRad ) );
-  p2Rotated.setX( p2.x() * cos( angleRad ) + p2.y() * -sin( angleRad ) );
-  p2Rotated.setY( p2.x() * sin( angleRad ) + p2.y() * cos( angleRad ) );
-
-  QPolygonF arrowHeadPoly;
-  arrowHeadPoly << middlePoint;
-  arrowHeadPoly << QPointF( middlePoint.x() + p1Rotated.x(), middlePoint.y() + p1Rotated.y() );
-  arrowHeadPoly << QPointF( middlePoint.x() + p2Rotated.x(), middlePoint.y() + p2Rotated.y() );
-
-  p->save();
-
-  QPen arrowPen = p->pen();
-  arrowPen.setJoinStyle( Qt::RoundJoin );
   QBrush arrowBrush = p->brush();
   arrowBrush.setColor( mArrowColor );
-  arrowBrush.setStyle( Qt::SolidPattern );
-  p->setPen( arrowPen );
   p->setBrush( arrowBrush );
-  p->drawPolygon( arrowHeadPoly );
-
-  p->restore();
+  drawArrowHead( p, mStopPoint.x() - transform().dx(), mStopPoint.y() - transform().dy(), angle( mStartPoint, mStopPoint ), mArrowHeadWidth );
 }
 
 void QgsComposerArrow::drawSVGMarker( QPainter* p, MarkerType type, const QString& markerPath )
 {
-  double angle = arrowAngle();
+  double ang = angle( mStartPoint, mStopPoint );
 
   double arrowHeadHeight;
   if ( type == StartMarker )
@@ -211,7 +183,7 @@
     fixPoint.setX( 0 ); fixPoint.setY( -arrowHeadHeight / 2.0 );
   }
   QPointF rotatedFixPoint;
-  double angleRad = angle / 180 * M_PI;
+  double angleRad = ang / 180 * M_PI;
   rotatedFixPoint.setX( fixPoint.x() * cos( angleRad ) + fixPoint.y() * -sin( angleRad ) );
   rotatedFixPoint.setY( fixPoint.x() * sin( angleRad ) + fixPoint.y() * cos( angleRad ) );
 
@@ -221,7 +193,7 @@
 
   p->save();
   p->translate( canvasPoint.x() - rotatedFixPoint.x() , canvasPoint.y() - rotatedFixPoint.y() );
-  p->rotate( angle );
+  p->rotate( ang );
   p->translate( -mArrowHeadWidth / 2.0, -arrowHeadHeight / 2.0 );
 
   p->drawImage( QRectF( 0, 0, mArrowHeadWidth, arrowHeadHeight ), markerImage, QRectF( 0, 0, imageWidth, imageHeight ) );
@@ -230,20 +202,6 @@
   return;
 }
 
-double QgsComposerArrow::arrowAngle() const
-{
-  double xDiff = mStopPoint.x() - mStartPoint.x();
-  double yDiff = mStopPoint.y() - mStartPoint.y();
-  double length = sqrt( xDiff * xDiff + yDiff * yDiff );
-
-  double angle = acos(( -yDiff * length ) / ( length * length ) ) * 180 / M_PI;
-  if ( xDiff < 0 )
-  {
-    return ( 360 - angle );
-  }
-  return angle;
-}
-
 void QgsComposerArrow::setStartMarker( const QString& svgPath )
 {
   QSvgRenderer r;

Modified: trunk/qgis/src/core/composer/qgscomposerarrow.h
===================================================================
--- trunk/qgis/src/core/composer/qgscomposerarrow.h	2010-04-19 13:20:26 UTC (rev 13327)
+++ trunk/qgis/src/core/composer/qgscomposerarrow.h	2010-04-19 14:30:59 UTC (rev 13328)
@@ -108,8 +108,6 @@
     void drawHardcodedMarker( QPainter* p, MarkerType type );
     /**Draws a user-defined marker (must be an svg file)*/
     void drawSVGMarker( QPainter* p, MarkerType type, const QString& markerPath );
-    /**Calculates arrow angle (for marker rotation)*/
-    double arrowAngle() const;
     /**Apply default graphics settings*/
     void initGraphicsSettings();
 };

Modified: trunk/qgis/src/core/composer/qgscomposeritem.cpp
===================================================================
--- trunk/qgis/src/core/composer/qgscomposeritem.cpp	2010-04-19 13:20:26 UTC (rev 13327)
+++ trunk/qgis/src/core/composer/qgscomposeritem.cpp	2010-04-19 14:30:59 UTC (rev 13328)
@@ -721,6 +721,43 @@
   p->restore();
 }
 
+void QgsComposerItem::drawArrowHead( QPainter* p, double x, double y, double angle, double arrowHeadWidth ) const
+{
+  if ( !p )
+  {
+    return;
+  }
+  double angleRad = angle / 180.0 * M_PI;
+  QPointF middlePoint( x, y );
+  //rotate both arrow points
+  QPointF p1 = QPointF( -arrowHeadWidth / 2.0, arrowHeadWidth );
+  QPointF p2 = QPointF( arrowHeadWidth / 2.0, arrowHeadWidth );
+
+  QPointF p1Rotated, p2Rotated;
+  p1Rotated.setX( p1.x() * cos( angleRad ) + p1.y() * -sin( angleRad ) );
+  p1Rotated.setY( p1.x() * sin( angleRad ) + p1.y() * cos( angleRad ) );
+  p2Rotated.setX( p2.x() * cos( angleRad ) + p2.y() * -sin( angleRad ) );
+  p2Rotated.setY( p2.x() * sin( angleRad ) + p2.y() * cos( angleRad ) );
+
+  QPolygonF arrowHeadPoly;
+  arrowHeadPoly << middlePoint;
+  arrowHeadPoly << QPointF( middlePoint.x() + p1Rotated.x(), middlePoint.y() + p1Rotated.y() );
+  arrowHeadPoly << QPointF( middlePoint.x() + p2Rotated.x(), middlePoint.y() + p2Rotated.y() );
+
+  p->save();
+
+  QPen arrowPen = p->pen();
+  arrowPen.setJoinStyle( Qt::RoundJoin );
+  QBrush arrowBrush = p->brush();
+  arrowBrush.setStyle( Qt::SolidPattern );
+  p->setPen( arrowPen );
+  p->setBrush( arrowBrush );
+  arrowBrush.setStyle( Qt::SolidPattern );
+  p->drawPolygon( arrowHeadPoly );
+
+  p->restore();
+}
+
 double QgsComposerItem::textWidthMillimeters( const QFont& font, const QString& text ) const
 {
   QFont metricsFont = scaledFontPixelSize( font );
@@ -748,6 +785,24 @@
   return scaledFont;
 }
 
+double QgsComposerItem::angle( const QPointF& p1, const QPointF& p2 ) const
+{
+  double xDiff = p2.x() - p1.x();
+  double yDiff = p2.y() - p1.y();
+  double length = sqrt( xDiff * xDiff + yDiff * yDiff );
+  if ( length <= 0 )
+  {
+    return 0;
+  }
+
+  double angle = acos(( -yDiff * length ) / ( length * length ) ) * 180 / M_PI;
+  if ( xDiff < 0 )
+  {
+    return ( 360 - angle );
+  }
+  return angle;
+}
+
 double QgsComposerItem::horizontalViewScaleFactor() const
 {
   double result = -1;

Modified: trunk/qgis/src/core/composer/qgscomposeritem.h
===================================================================
--- trunk/qgis/src/core/composer/qgscomposeritem.h	2010-04-19 13:20:26 UTC (rev 13327)
+++ trunk/qgis/src/core/composer/qgscomposeritem.h	2010-04-19 14:30:59 UTC (rev 13328)
@@ -233,6 +233,12 @@
     /**Draw background*/
     virtual void drawBackground( QPainter* p );
 
+    /**Draws arrowhead*/
+    void drawArrowHead( QPainter* p, double x, double y, double angle, double arrowHeadWidth ) const;
+
+    /**Returns angle of the line from p1 to p2 (clockwise, starting at N)*/
+    double angle( const QPointF& p1, const QPointF& p2 ) const;
+
     /**Returns the current (zoom level dependent) tolerance to decide if mouse position is close enough to the \
     item border for resizing*/
     double rectHandlerBorderTolerance() const;



More information about the QGIS-commit mailing list