[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    extragear/graphics/kgraphviewer/src/part
From:       Gaƫl de Chalendar <kleag () free ! fr>
Date:       2007-11-02 12:24:06
Message-ID: 1194006246.291439.29897.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 731967 by kleag:

Allow to remove edges

 M  +51 -5     canvasedge.cpp  
 M  +8 -1      canvasedge.h  
 M  +21 -0     dotgraph.cpp  
 M  +2 -1      dotgraph.h  
 M  +52 -0     dotgraphview.cpp  
 M  +7 -0      dotgraphview.h  
 M  +1 -1      graphedge.h  
 M  +9 -1      kgraphviewer_part.cpp  
 M  +5 -2      kgraphviewer_part.h  


--- trunk/extragear/graphics/kgraphviewer/src/part/canvasedge.cpp #731966:731967
@@ -33,9 +33,14 @@
 #include "dotgraphview.h"
 #include "FontsCache.h"
 
-#include <iostream>
+#include <KAction>
+
 #include <QPainter>
+#include <QGraphicsSceneMouseEvent>
+#include <QMenu>
 
+#include <iostream>
+
 //
 // CanvasEdge
 //
@@ -49,7 +54,7 @@
     m_scaleX(scaleX),
     m_scaleY(scaleY), m_xMargin(xMargin), m_yMargin(yMargin),
     m_gh(gh), m_wdhcf(wdhcf), m_hdvcf(hdvcf), m_edge(e),
-    m_font(0), m_view(view)
+    m_font(0), m_view(view), m_popup(new QMenu())
 {
   m_font = FontsCache::changeable().fromName(e->fontName());
 
@@ -62,9 +67,23 @@
       edge()->fromNode()->id(),edge()->toNode()->id(),e->label());
   setToolTip(tipStr);
 
+  // the message should be given (or possible to be given) by the part user
+  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))); +  
 } 
 
+CanvasEdge::~CanvasEdge()
+{
+  delete m_popup;
+}
+
+
 QRectF CanvasEdge::boundingRect() const
 {
   return m_boundingRect;
@@ -393,12 +412,39 @@
 
 void CanvasEdge::mousePressEvent(QGraphicsSceneMouseEvent * event)
 {
-//   kDebug() << event;
-  edge()->setSelected(!edge()->isSelected());
-  update();
+  kDebug() << event;
+  if (event->button() == Qt::LeftButton)
+  {
+    edge()->setSelected(!edge()->isSelected());
+    if (edge()->isSelected())
+    {
+      emit(selected(this,event->modifiers()));
+    }
+    update();
+  }
+  else if (event->button() == Qt::RightButton)
+  {
+    // opens the selected edge contextual menu and if necessary select the edge
+    if (!edge()->isSelected())
+    {
+      edge()->setSelected(true);
+      emit(selected(this,event->modifiers()));
+      update();
+    }
+    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));
 }
+
+void CanvasEdge::slotRemoveEdge()
+{
+  kDebug();
+  m_view->removeSelectedEdges();
+}
--- trunk/extragear/graphics/kgraphviewer/src/part/canvasedge.h #731966:731967
@@ -43,6 +43,8 @@
 class GraphEdge;
 class DotGraphView;
 
+class QMenu;
+
 /*
  * Canvas Items:
  * - CanvasNode       (Rectangular Area)
@@ -61,7 +63,7 @@
              qreal xMargin, qreal yMargin, qreal gh,
              qreal wdhcf, qreal hdvcf, QGraphicsItem* parent = 0);
 
-  virtual ~CanvasEdge() {}
+  virtual ~CanvasEdge();
   
   QRectF boundingRect() const;
 
@@ -73,8 +75,12 @@
 
   void computeBoundingRect();
 
+Q_SIGNALS:
+  void selected(CanvasEdge*, Qt::KeyboardModifiers);
+  
 public Q_SLOTS:
   void modelChanged();
+  void slotRemoveEdge();
 
 protected:
   virtual void mousePressEvent ( QGraphicsSceneMouseEvent * event );
@@ -88,6 +94,7 @@
   QRectF m_boundingRect;
   QFont* m_font;
   DotGraphView* m_view;
+  QMenu* m_popup;
 };
 
 
--- trunk/extragear/graphics/kgraphviewer/src/part/dotgraph.cpp #731966:731967
@@ -425,4 +425,25 @@
 
 }
 
+void DotGraph::removeEdge(const QString& id)
+{
+  kDebug();
+  foreach (const QString& eid, edges().keys())
+  {
+    GraphEdge* edge = edges()[eid];
+    if (edge->id() ==id)
+    {
+      if (edge->canvasEdge() != 0)
+      {
+        edge->canvasEdge()->hide();
+        delete edge->canvasEdge();
+        delete edge;
+      }
+      edges().remove(eid);
+      break;
+    }
+  }
+}
+
+                                       
 #include "dotgraph.moc"
--- trunk/extragear/graphics/kgraphviewer/src/part/dotgraph.h #731966:731967
@@ -99,7 +99,8 @@
   virtual void updateWith(const DotGraph& graph);
 
   void removeNodeNamed(const QString& nodeName);
-  
+  void removeEdge(const QString& id);
+                             
 Q_SIGNALS:
   void readyToDisplay();
 
--- trunk/extragear/graphics/kgraphviewer/src/part/dotgraphview.cpp #731966:731967
@@ -760,6 +760,15 @@
   }
   else
   {
+    QGraphicsItem *item = itemAt(e->pos());
+    if (item == 0) // click outside any item: unselect all
+    {
+      foreach(GraphEdge* e, m_graph->edges())
+      {
+        e->setSelected(false);
+        e->canvasEdge()->update();
+      }
+    }
     QGraphicsView::mousePressEvent(e);
   }
   
@@ -816,6 +825,7 @@
 
 void DotGraphView::contextMenuEvent(QContextMenuEvent* e)
 {
+  kDebug();
 //   QList<QGraphicsItem *> l = scene()->collidingItems(scene()->itemAt(e->pos()));
 
   m_popup->exec(e->globalPos());
@@ -1309,6 +1319,22 @@
 void DotGraphView::prepareAddNewEdge(QMap<QString,QString> attribs)
 {
   kDebug() ;
+  bool anySelected = false;
+  foreach (GraphEdge* edge, m_graph->edges())
+  {
+    if (edge->isSelected())
+    {
+      anySelected = true;
+      foreach(const QString& k, attribs.keys())
+      {
+        edge->attributes()[k] = attribs[k];
+      }
+    }
+  }
+  if (anySelected)
+  {
+    return;
+  }
   m_editingMode = AddNewEdge;
   m_newElementAttributes = attribs;
   unsetCursor();
@@ -1388,7 +1414,33 @@
   }
 }
 
+void DotGraphView::slotEdgeSelected(CanvasEdge* edge, Qt::KeyboardModifiers \
modifiers) +{
+  if (!modifiers.testFlag(Qt::ControlModifier))
+  {
+    foreach(GraphEdge* e, m_graph->edges())
+    {
+      if (e->canvasEdge() != edge)
+      {
+        e->setSelected(false);
+        e->canvasEdge()->update();
+      }
+    }
+  }
+}
 
+void DotGraphView::removeSelectedEdges()
+{
+  kDebug();
+  foreach(GraphEdge* e, m_graph->edges())
+  {
+    if (e->isSelected())
+    {
+      kDebug() << "emiting removeEdge " << e->id();
+      emit removeEdge(e->id());
+    }
+  }
+}
 
 #include "dotgraphview.moc"
 
--- trunk/extragear/graphics/kgraphviewer/src/part/dotgraphview.h #731966:731967
@@ -44,6 +44,7 @@
 
 class GraphElement;
 class CanvasNode;
+class CanvasEdge;
 class PannerView;
 class DotGraph;
 class KGVSimplePrintingCommand;
@@ -127,12 +128,16 @@
   void setReadOnly();
   void setReadWrite();
 
+  void removeSelectedEdges();
+  
 signals:
   void zoomed(double factor);
   void sigViewBevEnabledToggled(bool value);
   void sigViewBevActivated(int newPos);
   void graphLoaded();
   void newEdgeAdded(QString,QString);
+  /** signals that the user as activated a remove edge command */
+  void removeEdge(const QString&);
 
 public slots:
   void zoomIn();
@@ -164,6 +169,8 @@
   void slotBevAutomatic();
   void slotUpdate();
   bool displayGraph();
+  void slotEdgeSelected(CanvasEdge*, Qt::KeyboardModifiers);
+  
 protected:
   void resizeEvent(QResizeEvent*);
   void mousePressEvent(QMouseEvent*);
--- trunk/extragear/graphics/kgraphviewer/src/part/graphedge.h #731966:731967
@@ -102,7 +102,7 @@
 };
 
 
-/** A multi map associating the bounds nodes of a graph's edges to these edges */
+/** A map associating the bounds nodes of a graph's edges to these edges */
 typedef QMap<QString, GraphEdge*> GraphEdgeMap;
 
 QTextStream& operator<<(QTextStream& s, const GraphEdge& e);
--- trunk/extragear/graphics/kgraphviewer/src/part/kgraphviewer_part.cpp \
#731966:731967 @@ -61,7 +61,9 @@
            this, SIGNAL( graphLoaded() ) );
   connect( m_widget, SIGNAL( newEdgeAdded(QString, QString) ),
            this, SIGNAL( newEdgeAdded(QString, QString) ) );
-
+  connect( m_widget, SIGNAL( removeEdge(const QString&) ),
+           this, SIGNAL( removeEdge(const QString&) ) );
+                    
   // notify the part that this is our internal widget
   setWidget(m_widget);
 
@@ -253,6 +255,12 @@
   m_widget->graph()->nodes()[nodeName]->removeAttribute(attribName);
 }
 
+void kgraphviewerPart::slotRemoveEdge(const QString& id)
+{
+  kDebug();
+  m_widget->graph()->removeEdge(id);
+}
+
 /*It's usually safe to leave the factory code alone.. with the
 notable exception of the KAboutData data*/
 #include <kaboutdata.h>
--- trunk/extragear/graphics/kgraphviewer/src/part/kgraphviewer_part.h #731966:731967
@@ -60,7 +60,9 @@
 Q_SIGNALS:
   void graphLoaded();
   void newEdgeAdded(QString, QString);
-
+  /** signals that the user as activated a remove edge command */
+  void removeEdge(const QString&);
+    
 public slots:
   void slotHide(KParts::Part* part);
   void slotUpdate();
@@ -77,7 +79,8 @@
   void slotAddNewNodeToSubgraph(QMap<QString,QString> attribs,QString subgraph);
   void slotAddNewSubgraph(QMap<QString,QString> attribs);
   void slotAddNewEdge(QString src, QString tgt, QMap<QString,QString> attribs);
-
+  void slotRemoveEdge(const QString& id);
+                       
 protected:
     /**
      * This must be implemented by each part


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic