[Kst] extragear/graphics/kst/kst

Andrew Walker arwalker at sumusltd.com
Sun Dec 18 01:58:39 CET 2005


SVN commit 489315 by arwalker:

provide better looking arrows in a much simpler way

 M  +32 -70    kstviewarrow.cpp  
 M  +1 -0      kstviewarrow.h  


--- trunk/extragear/graphics/kst/kst/kstviewarrow.cpp #489314:489315
@@ -16,6 +16,7 @@
  ***************************************************************************/
 
 #include "kstgfxarrowmousehandler.h"
+#include "kstmath.h"
 #include "kstviewarrow.h"
 #include "kstviewobjectfactory.h"
 
@@ -29,8 +30,8 @@
 
 KstViewArrow::KstViewArrow()
 : KstViewLine("Arrow") {
-  _fromArrowScaling = 1;
-  _toArrowScaling = 1;
+  _fromArrowScaling = 1.0;
+  _toArrowScaling = 1.0;
 }
 
 
@@ -56,6 +57,27 @@
 }
 
 
+void KstViewArrow::paintArrow(KstPainter& p, const QPoint& to, const QPoint &from, int w) {
+  double deltax = _toArrowScaling * 2.0 * double(w);
+  double theta = atan2(double(from.y() - to.y()), double(from.x() - to.x())) - M_PI / 2.0;
+  double sina = sin(theta);
+  double cosa = cos(theta);
+  double yin = sqrt(3.0) * deltax;
+  double x1, y1, x2, y2;
+  QWMatrix m(cosa, sina, -sina, cosa, 0.0, 0.0);
+  
+  m.map( deltax, yin, &x1, &y1);
+  m.map(-deltax, yin, &x2, &y2);
+  
+  QPointArray pts(3);
+  pts[0] = to;
+  pts[1] = to + QPoint(d2i(x1), d2i(y1));
+  pts[2] = to + QPoint(d2i(x2), d2i(y2));
+  
+  p.drawPolygon(pts);
+}
+
+
 void KstViewArrow::paint(KstPainter& p, const QRegion& bounds) {
   p.save();
   if (p.type() != KstPainter::P_PRINT && p.type() != KstPainter::P_EXPORT) {
@@ -65,80 +87,20 @@
   }
   
   if (hasArrow()) {
+    QPoint to = KstViewLine::to();
+    QPoint from = KstViewLine::from();    
     int w = width();
     QPen pen(_foregroundColor, w);
+    
     pen.setCapStyle(capStyle());
     p.setPen(pen);
     p.setBrush(_foregroundColor);
-    QPointArray pts(3);
-    if (_hasToArrow) {
-      QPoint to = KstViewLine::to();
-      QPoint from = KstViewLine::from();    
-      double rise = double(from.y() - to.y());
-      double run = double(from.x() - to.x());
-      double theta = 0.0;
-      if (run == 0) {
-        if (rise < 0) {
-          theta = 0.5*M_PI;
-        } else {
-          theta = 1.5*M_PI;
-        }
-      }
-      else {
-        theta = atan(rise/run);
-        if (run > 0) {
-          theta += M_PI;  
-        }
-        if (rise > 0) {
-          theta += 2*M_PI;  
-        }
-      }
- 
-      double sina = sin(theta + M_PI / 2);
-      double cosa = cos(theta + M_PI / 2);
-      int l = (int)(_toArrowScaling * 2.0 * w);
-      pts[0] = to;
-      pts[1] = QPoint(to.x() + l, to.y() + int(sqrt(3) * l));
-      pts[2] = QPoint(to.x() - l, to.y() + int(sqrt(3) * l));
-      QWMatrix m(cosa, sina, -sina, cosa, 0, 0);
-      pts.translate(-to.x(), -to.y());
-      pts = m.map(pts);
-      pts.translate(to.x(), to.y());
-      p.drawPolygon(pts);
+    
+    if (_hasToArrow) {      
+      paintArrow(p, to, from, w);
     }
-    if (_hasFromArrow) {
-      QPoint to = KstViewLine::from();
-      QPoint from = KstViewLine::to();    
-      double rise = double(from.y() - to.y());
-      double run = double(from.x() - to.x());
-      double theta = 0.0;
-      if (run == 0) {
-        if (rise < 0) {
-          theta = 0.5*M_PI;
-        } else {
-          theta = 1.5*M_PI;
-        }
-      } else {
-        theta = atan(rise/run);
-        if (run > 0) {
-          theta += M_PI;  
-        }
-        if (rise > 0) {
-          theta += 2*M_PI;  
-        }
-      }
-      
-      double sina = sin(theta + M_PI / 2);
-      double cosa = cos(theta + M_PI / 2);
-      int l = (int)(_fromArrowScaling * 2.0 * w);
-      pts[0] = to;
-      pts[1] = QPoint(to.x() + l, to.y() + int(sqrt(3) * l));
-      pts[2] = QPoint(to.x() - l, to.y() + int(sqrt(3) * l));
-      QWMatrix m(cosa, sina, -sina, cosa, 0, 0);
-      pts.translate(-to.x(), -to.y());
-      pts = m.map(pts);
-      pts.translate(to.x(), to.y());
-      p.drawPolygon(pts);
+    if (_hasFromArrow) {      
+      paintArrow(p, from, to, w);
     }
   }
   KstViewLine::paint(p, bounds);
--- trunk/extragear/graphics/kst/kst/kstviewarrow.h #489314:489315
@@ -39,6 +39,7 @@
     QMap<QString, QVariant > widgetHints(const QString& propertyName) const;
 
     void paint(KstPainter& p, const QRegion& bounds);
+    void paintArrow(KstPainter& p, const QPoint& to, const QPoint &from, int w);
     // true if either end has an arrow 
     bool hasArrow() const;
     


More information about the Kst mailing list