[kgraphviewer-devel] [KGraphViewer/libkgraphviz] e5bd717: Remove duplicated code in CanvasEdge

Kevin Funk krf at electrostorm.net
Wed Dec 15 15:41:51 CET 2010


commit e5bd717c3fafdfeb2f737849da3a7907149509ca
branch libkgraphviz
Author: Kevin Funk <krf at electrostorm.net>
Date:   Wed Dec 15 13:19:58 2010 +0100

    Remove duplicated code in CanvasEdge
    
    Most of the code (if not all) is already implemented in CanvasEdge's base class
    (CanvasElement).
    
    Dropped methods in CanvasEdge:
    * slotElementHoverEnter
    * slotElementHoverLeave
    * slotEdgeSelected

diff --git a/src/kgraphviz/canvasedge.cpp b/src/kgraphviz/canvasedge.cpp
index bded6e2..0e7f7cd 100644
--- a/src/kgraphviz/canvasedge.cpp
+++ b/src/kgraphviz/canvasedge.cpp
@@ -74,19 +74,6 @@ CanvasEdge::CanvasEdge(DotGraphView* view,
   KAction* removeEdgeAction = new KAction(i18n("Remove selected edge(s)"), this);
   m_popup->addAction(removeEdgeAction);
   connect(removeEdgeAction,SIGNAL(triggered(bool)),this,SLOT(slotRemoveEdge()));
-  
-  
-  connect(e,SIGNAL(changed()),this,SLOT(modelChanged()));
-  connect(this, SIGNAL(selected(CanvasEdge*, Qt::KeyboardModifiers)), view, SLOT(slotEdgeSelected(CanvasEdge*, Qt::KeyboardModifiers)));
-  
-  connect(this, SIGNAL(edgeContextMenuEvent(const QString&, const QPoint&)), view, SLOT(slotContextMenuEvent(const QString&, const QPoint&)));
-
-  setAcceptHoverEvents ( true );
-
-  kDebug() << "connect slotElementHoverEnter";
-  connect(this, SIGNAL(hoverEnter(CanvasEdge*)), view, SLOT(slotElementHoverEnter(CanvasEdge*)));
-  connect(this, SIGNAL(hoverLeave(CanvasEdge*)), view, SLOT(slotElementHoverLeave(CanvasEdge*)));
-  
 } 
 
 CanvasEdge::~CanvasEdge()
@@ -172,13 +159,12 @@ QPainterPath CanvasEdge::shape () const
 void CanvasEdge::paint(QPainter* p, const QStyleOptionGraphicsItem* option,
                    QWidget* widget)
 {
-//   kDebug();
-Q_UNUSED(option)
-Q_UNUSED(widget)
+  Q_UNUSED(option)
+  Q_UNUSED(widget)
+
   if (m_boundingRect == QRectF())
-  {
     return;
-  }
+
   /// computes the scaling of line width
   qreal widthScaleFactor = (m_scaleX+m_scaleY)/2;
   if (widthScaleFactor < 1)
@@ -460,13 +446,6 @@ Q_UNUSED(widget)
   }
 }
 
-void CanvasEdge::modelChanged()
-{
-//   kDebug() << edge()->fromNode()->id() << "->" << edge()->toNode()->id();
-  prepareGeometryChange();
-  computeBoundingRect();
-}
-
 void CanvasEdge::computeBoundingRect()
 {
 //   kDebug();
@@ -526,38 +505,6 @@ void CanvasEdge::computeBoundingRect()
   kDebug() << edge()->fromNode()->id() << "->" << edge()->toNode()->id() << "New bounding rect is:" << m_boundingRect;
 }
 
-void CanvasEdge::mousePressEvent(QGraphicsSceneMouseEvent * event)
-{
-  kDebug() << event;
-  if (m_view->isReadOnly())
-  {
-    return;
-  }
-  if (event->button() == Qt::LeftButton)
-  {
-    edge()->setSelected(!edge()->isSelected());
-    if (edge()->isSelected())
-    {
-      emit(selected(this,event->modifiers()));
-    }
-    update();
-  }
-  else if (event->button() == Qt::RightButton)
-  {
-    if (!edge()->isSelected())
-    {
-      edge()->setSelected(true);
-      emit(selected(this,event->modifiers()));
-      update();
-    }
-    kDebug() << "emiting edgeContextMenuEvent("<<m_edge->id()<<","<<event->screenPos()<<")";
-    emit(edgeContextMenuEvent(m_edge->id(), event->screenPos() ));
-// opens the selected edge contextual menu and if necessary select the edge
-/*    kDebug() << "opens the contextual menu";
-    m_popup->exec(event->screenPos());*/
-  }
-}
-
 qreal CanvasEdge::distance(const QPointF& point1, const QPointF& point2)
 {
   return sqrt(pow(point1.x()-point2.x(),2)+pow(point1.y()-point2.y(),2));
@@ -569,18 +516,4 @@ void CanvasEdge::slotRemoveEdge()
   m_view->removeSelectedElements();
 }
 
-void CanvasEdge::hoverEnterEvent( QGraphicsSceneHoverEvent * event )
-{
-  Q_UNUSED(event)
-  kDebug() << edge()->id();
-  emit hoverEnter(this);
-}
-
-void CanvasEdge::hoverLeaveEvent( QGraphicsSceneHoverEvent * event )
-{
-  Q_UNUSED(event)
-  kDebug() << edge()->id();
-  emit hoverLeave(this);
-}
-
 #include "canvasedge.moc"
diff --git a/src/kgraphviz/canvasedge.h b/src/kgraphviz/canvasedge.h
index f4f0afe..9ce46e8 100644
--- a/src/kgraphviz/canvasedge.h
+++ b/src/kgraphviz/canvasedge.h
@@ -81,20 +81,8 @@ public:
   
   void computeBoundingRect();
 
-Q_SIGNALS:
-  void selected(CanvasEdge*, Qt::KeyboardModifiers);
-  void edgeContextMenuEvent(const QString&, const QPoint&);
-  void hoverEnter(CanvasEdge*);
-  void hoverLeave(CanvasEdge*);
-  
 public Q_SLOTS:
-  void modelChanged();
   void slotRemoveEdge();
-
-protected:
-  virtual void mousePressEvent ( QGraphicsSceneMouseEvent * event );
-  virtual void hoverEnterEvent ( QGraphicsSceneHoverEvent * event );
-  virtual void hoverLeaveEvent ( QGraphicsSceneHoverEvent * event );
   
 private:
   qreal distance(const QPointF& point1, const QPointF& point2);
diff --git a/src/kgraphviz/canvaselement.cpp b/src/kgraphviz/canvaselement.cpp
index fed0af5..bea5185 100644
--- a/src/kgraphviz/canvaselement.cpp
+++ b/src/kgraphviz/canvaselement.cpp
@@ -507,6 +507,7 @@ void CanvasElement::mousePressEvent(QGraphicsSceneMouseEvent* event)
     m_view->finishNewEdgeTo(this);
     return;
   }
+
   if (event->button() == Qt::LeftButton)
   {
     m_element->setSelected(!m_element->isSelected());
@@ -553,7 +554,7 @@ void CanvasElement::slotRemoveElement()
 void CanvasElement::hoverEnterEvent( QGraphicsSceneHoverEvent * event )
 {
   Q_UNUSED(event)
-//   kDebug();
+  kDebug() << "Element:" << element()->id();
   m_hovered = true;
   update();
   emit hoverEnter(this);
@@ -562,7 +563,7 @@ void CanvasElement::hoverEnterEvent( QGraphicsSceneHoverEvent * event )
 void CanvasElement::hoverLeaveEvent( QGraphicsSceneHoverEvent * event )
 {
   Q_UNUSED(event)
-//   kDebug();
+  kDebug() << "Element:" << element()->id();
   m_hovered = false;
   update();
   emit hoverLeave(this);
diff --git a/src/kgraphviz/dotgraphview.cpp b/src/kgraphviz/dotgraphview.cpp
index 2337ed8..bcb9b1d 100644
--- a/src/kgraphviz/dotgraphview.cpp
+++ b/src/kgraphviz/dotgraphview.cpp
@@ -1216,22 +1216,6 @@ void DotGraphView::slotElementHoverLeave(CanvasElement* element)
   emit (hoverLeave(element->element()->id()));
 }
 
-void DotGraphView::slotElementHoverEnter(CanvasEdge* element)
-{
-  kDebug() << element->edge()->id();
-  //   QList<QGraphicsItem *> l = scene()->collidingItems(scene()->itemAt(e->pos()));
-  
-  emit (hoverEnter(element->edge()->id()));
-}
-
-void DotGraphView::slotElementHoverLeave(CanvasEdge* element)
-{
-  kDebug() << element->edge()->id();
-  //   QList<QGraphicsItem *> l = scene()->collidingItems(scene()->itemAt(e->pos()));
-  
-  emit (hoverLeave(element->edge()->id()));
-}
-
 void DotGraphView::setLayoutCommand(const QString& command)
 {
   Q_D(DotGraphView);
@@ -1594,66 +1578,10 @@ void DotGraphView::setReadWrite()
   }
 }
 
-void DotGraphView::slotEdgeSelected(CanvasEdge* edge, Qt::KeyboardModifiers modifiers)
-{
-  Q_D(DotGraphView);
-  kDebug() << edge->edge()->id();
-  QList<QString> selection;
-  selection.push_back(edge->edge()->id());
-  if (!modifiers.testFlag(Qt::ControlModifier))
-  {
-    foreach(GraphEdge* e, d->m_graph->edges())
-    {
-      if (e->canvasElement() != edge)
-      {
-        e->setSelected(false);
-        e->canvasElement()->update();
-      }
-    }
-    foreach(GraphNode* n, d->m_graph->nodes())
-    {
-      n->setSelected(false);
-      n->canvasElement()->update();
-    }
-    foreach(GraphSubgraph* s, d->m_graph->subgraphs())
-    {
-      s->setElementSelected(0, false, true);
-    }
-  }
-  else
-  {
-    foreach(GraphEdge* e, d->m_graph->edges())
-    {
-      if (e->canvasElement() != edge)
-      {
-        if (e->isSelected())
-        {
-          selection.push_back(e->id());
-        }
-      }
-    }
-    foreach(GraphNode* n, d->m_graph->nodes())
-    {
-      if (n->isSelected())
-      {
-        selection.push_back(n->id());
-      }
-    }
-    foreach(GraphSubgraph* s, d->m_graph->subgraphs())
-    {
-      if (s->isSelected())
-      {
-        selection.push_back(s->id());
-      }
-    }
-  }
-  emit selectionIs(selection, QPoint());
-}
-
 void DotGraphView::slotElementSelected(CanvasElement* element, Qt::KeyboardModifiers modifiers)
 {
   Q_D(DotGraphView);
-  kDebug();
+  kDebug() << "Element:" << element->element()->id();
   QList<QString> selection;
   selection.push_back(element->element()->id());
   if (!modifiers.testFlag(Qt::ControlModifier))
diff --git a/src/kgraphviz/dotgraphview.h b/src/kgraphviz/dotgraphview.h
index eafda6d..b46327c 100644
--- a/src/kgraphviz/dotgraphview.h
+++ b/src/kgraphviz/dotgraphview.h
@@ -171,14 +171,11 @@ public Q_SLOTS:
   void slotBevAutomatic();
   void slotUpdate();
   bool displayGraph();
-  void slotEdgeSelected(CanvasEdge*, Qt::KeyboardModifiers);
   void slotElementSelected(CanvasElement*, Qt::KeyboardModifiers);
   void slotSelectionChanged();
   void slotContextMenuEvent(const QString&, const QPoint&);
   void slotElementHoverEnter(CanvasElement*);
   void slotElementHoverLeave(CanvasElement*);
-  void slotElementHoverEnter(CanvasEdge*);
-  void slotElementHoverLeave(CanvasEdge*);
   void slotSelectNode(const QString& nodeName);
   void centerOnNode(const QString& nodeId);
 


More information about the kgraphviewer-devel mailing list