[kgraphviewer-devel] [KGraphViewer/libkgraphviz] 3ed92c8: Clean up CanvasEdge interface

Kevin Funk krf at electrostorm.net
Thu Dec 16 11:31:14 CET 2010


	A	 src/kgraphviz/canvasedge_p.h	 [License: Trivialfile.]

commit 3ed92c81ab60b7fb2ebb1f584e7630effcc885f0
Author: Kevin Funk <krf at electrostorm.net>
Date:   Thu Dec 16 11:29:14 2010 +0100

    Clean up CanvasEdge interface
    
    Introduce CanvasEdgePrivate, reuse existing elements in CanvasElement
    baseclass where applicable

diff --git a/src/kgraphviz/canvasedge.cpp b/src/kgraphviz/canvasedge.cpp
index 877ef07..0962ec0 100644
--- a/src/kgraphviz/canvasedge.cpp
+++ b/src/kgraphviz/canvasedge.cpp
@@ -26,6 +26,8 @@
 
 
 #include "canvasedge.h"
+#include "canvasedge_p.h"
+
 #include "canvaselement.h"
 #include "dotgraphview.h"
 #include "graphedge.h"
@@ -43,6 +45,14 @@
 
 using namespace KGraphViz;
 
+CanvasEdgePrivate::CanvasEdgePrivate()
+{
+}
+
+CanvasEdgePrivate::~CanvasEdgePrivate()
+{
+}
+
 CanvasEdge::CanvasEdge(DotGraphView* view,
                        GraphEdge* e,
                        QGraphicsScene* scene,
@@ -50,18 +60,17 @@ CanvasEdge::CanvasEdge(DotGraphView* view,
                        qreal xMargin, qreal yMargin, qreal gh,
                        qreal wdhcf, qreal hdvcf,
                        QGraphicsItem* parent) :
-    CanvasElement(view, e, scene, parent),
-    m_scaleX(scaleX), m_scaleY(scaleY),
-    m_xMargin(xMargin), m_yMargin(yMargin),
-    m_gh(/*gh*/0), m_wdhcf(wdhcf), m_hdvcf(hdvcf), m_edge(e),
-    m_font(0), m_view(view)
+  CanvasElement(view, e, scene, parent),
+  d_ptr(new CanvasEdgePrivate)
 {
-  kDebug() << "edge "  << edge()->fromNode()->id() << "->"  << edge()->toNode()->id() << m_gh;
+  kDebug() << "edge "  << edge()->fromNode()->id() << "->"  << edge()->toNode()->id() << CanvasEdge::gh();
   setBoundingRegionGranularity(0.9);
-  m_font = FontsCache::changeable().fromName(e->fontName());
+  setFont(*FontsCache::changeable().fromName(e->fontName()));
+
+  kDebug() << scaleX << scaleY << xMargin << yMargin << /*gh*/0 << wdhcf << hdvcf << font();
 
-  computeBoundingRect();
-//   kDebug() << "boundingRect computed: " << m_boundingRect;
+  initialize(scaleX, scaleY, xMargin, yMargin, /*gh*/0, wdhcf, hdvcf);
+  kDebug() << "boundingRect computed: " << boundingRect();
   
   QString tipStr = i18n("%1 -> %2\nlabel='%3'",
       edge()->fromNode()->id(),edge()->toNode()->id(),e->label());
@@ -70,20 +79,17 @@ CanvasEdge::CanvasEdge(DotGraphView* view,
 
 CanvasEdge::~CanvasEdge()
 {
-}
-
-QRectF CanvasEdge::boundingRect() const
-{
-  return m_boundingRect;
+  delete d_ptr;
 }
 
 QPainterPath CanvasEdge::shape () const
 {
+  Q_D(const CanvasEdge);
 //   kDebug() << edge()->fromNode()->id() << "->" << edge()->toNode()->id();
-  if (m_boundingRegion.isEmpty()) {
-    m_boundingRegion.addRegion(boundingRegion(QTransform()));
+  if (d->m_boundingRegion.isEmpty()) {
+    d->m_boundingRegion.addRegion(boundingRegion(QTransform()));
   }
-  return m_boundingRegion;
+  return d->m_boundingRegion;
   /*
   foreach (const DotRenderOp& dro, edge()->renderOperations())
   {
@@ -123,8 +129,8 @@ QPainterPath CanvasEdge::shape () const
           }
           QPointF p(
               //NOTE: when uncommenting, fix nested comments in here:
-              (dro.integers[2*i+1]/ *%m_wdhcf* /*m_scaleX) +m_xMargin + diffX,
-              (m_gh-dro.integers[2*i+2]/ *%m_hdvcf* /)*m_scaleY + m_yMargin + diffY
+              (dro.integers[2*i+1]/ *%m_wdhcf* /*scaleX()) +marginX() + diffX,
+              (gh()-dro.integers[2*i+2]/ *%m_hdvcf* /)*scaleY() + marginY() + diffY
                   );
           points[i] = p;
         }
@@ -152,11 +158,11 @@ void CanvasEdge::paint(QPainter* p, const QStyleOptionGraphicsItem* option,
   Q_UNUSED(option)
   Q_UNUSED(widget)
 
-  if (m_boundingRect == QRectF())
+  if (boundingRect() == QRectF())
     return;
 
   /// computes the scaling of line width
-  qreal widthScaleFactor = (m_scaleX+m_scaleY)/2;
+  qreal widthScaleFactor = (scaleX()+scaleY())/2;
   if (widthScaleFactor < 1)
   {
     widthScaleFactor = 1;
@@ -209,35 +215,39 @@ void CanvasEdge::paint(QPainter* p, const QStyleOptionGraphicsItem* option,
     {
       const QString& str = dro.str;
     
-      qreal stringWidthGoal = dro.integers[3] * m_scaleX;
+      qreal stringWidthGoal = dro.integers[3] * scaleX();
       int fontSize = edge()->fontSize();
-      m_font->setPointSize(fontSize);
-      QFontMetrics fm(*m_font);
+      QFont font = CanvasElement::font();
+      font.setPointSize(fontSize);
+      QFontMetrics fm(font);
       while (fm.width(str) > stringWidthGoal && fontSize > 1)
       {
         fontSize--;
-        m_font->setPointSize(fontSize);
-        fm = QFontMetrics(*m_font);
+        font.setPointSize(fontSize);
+        fm = QFontMetrics(font);
       }
       p->save();
-      p->setFont(*m_font);
+      p->setFont(font);
       
       p->setPen(Dot2QtConsts::componentData().qtColor(edge()->fontColor()));
 
-      qreal x = (m_scaleX *
+      qreal x = (scaleX() *
                        (
                          (dro.integers[0])
                          + (((-dro.integers[2])*(fm.width(dro.str)))/2)
                          - ( (fm.width(dro.str))/2 )
                        )
                       )
-                      + m_xMargin;
-      qreal y = ((m_gh - (dro.integers[1]))*m_scaleY)+ m_yMargin;
+                      + marginX();
+      qreal y = ((gh() - (dro.integers[1]))*scaleY())+ marginY();
       QPointF point(x,y);
 //       kDebug() << edge()->fromNode()->id() << "->" << edge()->toNode()->id() << "drawText" << edge()->fontColor() << point;
 
       p->drawText(point,str);
       p->restore();
+
+      // save back
+      setFont(font);
     }      
     else if (( dro.renderop == "p" ) || (dro.renderop == "P" ))
     {
@@ -245,8 +255,8 @@ void CanvasEdge::paint(QPainter* p, const QStyleOptionGraphicsItem* option,
       for (int i = 0; i < dro.integers[0]; i++)
       {
         QPointF point(
-            (int(dro.integers[2*i+1])/*%m_wdhcf*/)*m_scaleX +m_xMargin,
-            (int(m_gh-dro.integers[2*i+2])/*%m_hdvcf*/)*m_scaleY + m_yMargin
+            (int(dro.integers[2*i+1])/*%m_wdhcf*/)*scaleX() +marginX(),
+            (int(gh()-dro.integers[2*i+2])/*%m_hdvcf*/)*scaleY() + marginY()
                 );
         polygon[i] = point;
 //         kDebug() << edge()->fromNode()->id() << "->" << edge()->toNode()->id()  << point;
@@ -283,10 +293,10 @@ void CanvasEdge::paint(QPainter* p, const QStyleOptionGraphicsItem* option,
     }
     else if (( dro.renderop == "e" ) || (dro.renderop == "E" ))
     {
-      qreal w = m_scaleX * dro.integers[2] * 2;
-      qreal h = m_scaleY *  dro.integers[3] * 2;
-      qreal x = (m_xMargin + (dro.integers[0]/*%m_wdhcf*/)*m_scaleX) - w/2;
-      qreal y = ((m_gh -  dro.integers[1]/*%m_hdvcf*/)*m_scaleY + m_yMargin) - h/2;
+      qreal w = scaleX() * dro.integers[2] * 2;
+      qreal h = scaleY() *  dro.integers[3] * 2;
+      qreal x = (marginX() + (dro.integers[0]/*%m_wdhcf*/)*scaleX()) - w/2;
+      qreal y = ((gh() -  dro.integers[1]/*%m_hdvcf*/)*scaleY() + marginY()) - h/2;
       p->save();
       if (dro.renderop == "E" )
       {
@@ -378,8 +388,8 @@ void CanvasEdge::paint(QPainter* p, const QStyleOptionGraphicsItem* option,
             }
           }
           QPointF p(
-              (dro.integers[2*i+1]/*%m_wdhcf*/*m_scaleX) +m_xMargin + diffX,
-              (m_gh-dro.integers[2*i+2]/*%m_hdvcf*/)*m_scaleY + m_yMargin + diffY
+              (dro.integers[2*i+1]/*%m_wdhcf*/*scaleX()) +marginX() + diffX,
+              (gh()-dro.integers[2*i+2]/*%m_hdvcf*/)*scaleY() + marginY() + diffY
                   );
           points[i] = p;
 //           kDebug() << edge()->fromNode()->id() << "->" << edge()->toNode()->id()  << p;
@@ -438,16 +448,17 @@ void CanvasEdge::paint(QPainter* p, const QStyleOptionGraphicsItem* option,
 
 void CanvasEdge::computeBoundingRect()
 {
-//   kDebug();
+  Q_D(CanvasEdge);
+  kDebug();
   //invalidate bounding region cache
-  m_boundingRegion = QPainterPath();
+  d->m_boundingRegion = QPainterPath();
   if (edge()->renderOperations().isEmpty())
   {
     if ((edge()->fromNode()->canvasElement()==0)
       || (edge()->toNode()->canvasElement()==0)
       || edge()->style()=="invis")
     {
-      m_boundingRect = QRectF();
+      setBoundingRect(QRectF());
     }
     else
     {
@@ -455,7 +466,7 @@ void CanvasEdge::computeBoundingRect()
       edge()->fromNode()->canvasElement()->boundingRect().center()+edge()->fromNode()->canvasElement()->pos(),
                 edge()->toNode()->canvasElement()->boundingRect().center()+edge()->toNode()->canvasElement()->pos());
 //       kDebug() << edge()->fromNode()->id() << "->" << edge()->toNode()->id() <<br;
-      m_boundingRect = br;
+      setBoundingRect(br);
     }
   }
   else
@@ -470,8 +481,8 @@ void CanvasEdge::computeBoundingRect()
       for (int i = 0; i < dro.integers[0]; i++)
       {
         QPointF p(
-            ((dro.integers[2*i+1]/*%m_wdhcf*/)*m_scaleX) +m_xMargin,
-            ((m_gh-dro.integers[2*i+2]/*%m_hdvcf*/)*m_scaleY) + m_yMargin
+            ((dro.integers[2*i+1]/*%m_wdhcf*/)*scaleX()) +marginX(),
+            ((gh()-dro.integers[2*i+2]/*%m_hdvcf*/)*scaleY()) + marginY()
                 );
         points[previousSize+i] = p;
       }
@@ -490,9 +501,14 @@ void CanvasEdge::computeBoundingRect()
     }
 //     kDebug() << a.size() << "points";
 
-    m_boundingRect = a.boundingRect();
+    setBoundingRect(a.boundingRect());
   }
-  kDebug() << edge()->fromNode()->id() << "->" << edge()->toNode()->id() << "New bounding rect is:" << m_boundingRect;
+  kDebug() << edge()->fromNode()->id() << "->" << edge()->toNode()->id() << "New bounding rect is:" << boundingRect();
+}
+
+GraphEdge* CanvasEdge::edge() const
+{
+  return qobject_cast<GraphEdge*>(element());
 }
 
 qreal CanvasEdge::distance(const QPointF& point1, const QPointF& point2)
diff --git a/src/kgraphviz/canvasedge.h b/src/kgraphviz/canvasedge.h
index f8f5a8d..0f09fbe 100644
--- a/src/kgraphviz/canvasedge.h
+++ b/src/kgraphviz/canvasedge.h
@@ -32,9 +32,6 @@
 
 #include "graphexporter.h"
 
-class QGraphicsScene;
-class QFont;
-
 /*
  * Canvas Items:
  * - CanvasNode       (Rectangular Area)
@@ -45,11 +42,8 @@ class QFont;
 namespace KGraphViz
 {
 
-class CanvasNode;
-class CanvasEdge;
+class CanvasEdgePrivate;
 class GraphEdge;
-class DotGraphView;
-
 
 class CanvasEdge : public CanvasElement
 {
@@ -59,42 +53,29 @@ public:
   explicit CanvasEdge(DotGraphView* v,
                       GraphEdge*,
                       QGraphicsScene* scene,
-             qreal scaleX, qreal scaleY,
-             qreal xMargin, qreal yMargin, qreal gh,
-             qreal wdhcf, qreal hdvcf,
-             QGraphicsItem* parent = 0);
+                      qreal scaleX, qreal scaleY,
+                      qreal xMargin, qreal yMargin, qreal gh,
+                      qreal wdhcf, qreal hdvcf,
+                      QGraphicsItem* parent = 0);
 
   virtual ~CanvasEdge();
-  
-  QRectF boundingRect() const;
+
+  virtual void computeBoundingRect();
 
   QPainterPath shape () const;
 
   void paint(QPainter* p, const QStyleOptionGraphicsItem *option,
         QWidget *widget);
 
-  inline GraphEdge* edge() { return m_edge; }
-  inline const GraphEdge* edge() const { return m_edge; }
+  GraphEdge* edge() const;
 
-  inline void setGh(qreal gh) {m_gh = gh;}
-  
-  void computeBoundingRect();
-  
 private:
+  Q_DECLARE_PRIVATE(CanvasEdge);
+  CanvasEdgePrivate* const d_ptr;
+
   qreal distance(const QPointF& point1, const QPointF& point2);
-  
-  qreal m_scaleX, m_scaleY;
-  qreal m_xMargin, m_yMargin, m_gh, m_wdhcf, m_hdvcf;
-  GraphEdge* m_edge;
-  QRectF m_boundingRect;
-  QFont* m_font;
-  DotGraphView* m_view;
-  mutable QPainterPath m_boundingRegion;
 };
 
 }
 
 #endif
-
-
-
diff --git a/src/kgraphviz/canvasedge_p.h b/src/kgraphviz/canvasedge_p.h
new file mode 100644
index 0000000..148ed94
--- /dev/null
+++ b/src/kgraphviz/canvasedge_p.h
@@ -0,0 +1,42 @@
+/*
+    Copyright (C) 2010 Kevin Funk <krf at electrostorm.net>
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Library General Public
+    License as published by the Free Software Foundation; either
+    version 2 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Library General Public License for more details.
+
+    You should have received a copy of the GNU Library General Public License
+    along with this library; see the file COPYING.LIB.  If not, write to
+    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+    Boston, MA 02110-1301, USA.
+*/
+
+#ifndef CANVAS_EDGE_P_H
+#define CANVAS_EDGE_P_H
+
+#include "canvasedge.h"
+
+#include <QPen>
+#include <QBrush>
+
+namespace KGraphViz
+{
+
+class CanvasEdgePrivate
+{
+public:
+  CanvasEdgePrivate();
+  ~CanvasEdgePrivate();
+
+  mutable QPainterPath m_boundingRegion;
+};
+
+}
+
+#endif
\ No newline at end of file
diff --git a/src/kgraphviz/canvaselement.cpp b/src/kgraphviz/canvaselement.cpp
index 89f0a47..f63740d 100644
--- a/src/kgraphviz/canvaselement.cpp
+++ b/src/kgraphviz/canvaselement.cpp
@@ -107,7 +107,6 @@ CanvasElement::CanvasElement(
 
 CanvasElement::~CanvasElement()
 {
-  Q_D(CanvasElement);
   delete d_ptr;
 }
 
@@ -117,6 +116,11 @@ GraphElement* CanvasElement::element() const
   return d->m_element;
 }
 
+qreal CanvasElement::gh() const
+{
+  Q_D(const CanvasElement);
+  return d->m_gh;
+}
 void CanvasElement::setGh(qreal gh)
 {
   Q_D(CanvasElement);
@@ -178,6 +182,12 @@ void CanvasElement::setFont(const QFont& font)
   d->m_font = font;
 }
 
+void CanvasElement::setBoundingRect(const QRectF& rect)
+{
+  Q_D(CanvasElement);
+  d->m_boundingRect = rect;
+}
+
 void CanvasElement::modelChanged()
 {
   Q_D(CanvasElement);
@@ -216,7 +226,6 @@ QRectF CanvasElement::boundingRect () const
 void CanvasElement::computeBoundingRect()
 {
   Q_D(CanvasElement);
-//   kDebug() << element();
   kDebug() << element()->id() << zValue();
   
   qreal adjust = 0.5;
diff --git a/src/kgraphviz/canvaselement.h b/src/kgraphviz/canvaselement.h
index 24d6c45..7a81614 100644
--- a/src/kgraphviz/canvaselement.h
+++ b/src/kgraphviz/canvaselement.h
@@ -61,6 +61,7 @@ public:
                   qreal gh,
                   qreal wdhcf, qreal hdvcf);
 
+  qreal gh() const;
   void setGh(qreal gh);
 
 protected:
@@ -79,6 +80,8 @@ protected:
   qreal marginY() const;
   void setMarginY(qreal marginY);
 
+  void setBoundingRect(const QRectF& rect);
+
 Q_SIGNALS:
   void selected(CanvasElement*, Qt::KeyboardModifiers);
   void hoverEnter(CanvasElement*);


More information about the kgraphviewer-devel mailing list