[kgraphviewer-devel] [kgraphviewer/libkgraphviz] src: Use single entry point for load from file logic

Kevin Funk krf at electrostorm.net
Fri Jan 28 18:55:24 CET 2011


Git commit 44e88fb371ebfc1388f2d85f234ece49c4e3cf6c by Kevin Funk.
Pushed by kfunk into branch 'libkgraphviz'.

Use single entry point for load from file logic

Required some refactoring in DotGraphView and DotGraph, simplifies logic
for loading files from external command or library.

M  +1    -1    src/kgrapheditor.cpp     
M  +44   -6    src/kgraphviz/dotgraph.cpp     
M  +5    -3    src/kgraphviz/dotgraph.h     
M  +12   -0    src/kgraphviz/dotgraph_p.h     
M  +4    -67   src/kgraphviz/dotgraphview.cpp     
M  +1    -7    src/kgraphviz/dotgraphview.h     
M  +0    -9    src/kgraphviz/dotgraphview_p.h     
M  +3    -18   src/part/kgraphviewer_part.cpp     

http://commits.kde.org/9c7b74a4/44e88fb371ebfc1388f2d85f234ece49c4e3cf6c

diff --git a/src/kgrapheditor.cpp b/src/kgrapheditor.cpp
index 79ea0f1..5ba07f1 100644
--- a/src/kgrapheditor.cpp
+++ b/src/kgrapheditor.cpp
@@ -195,7 +195,7 @@ void KGraphEditor::openUrl(const KUrl& url)
   m_widget-> insertTab(part, QIcon( DesktopIcon("kgraphviewer") ), label);
   m_widget->setCurrentPage(m_widget->indexOf(part));
   m_tabsFilesMap[m_widget->currentPage()] = url.url();
-  part->loadLibrary(url.url());
+  part->loadFromFile(url.url());
 
   m_openedFiles.push_back(url.url());
 }
diff --git a/src/kgraphviz/dotgraph.cpp b/src/kgraphviz/dotgraph.cpp
index 0e97aa3..e65dbf0 100644
--- a/src/kgraphviz/dotgraph.cpp
+++ b/src/kgraphviz/dotgraph.cpp
@@ -80,6 +80,9 @@ DotGraphPrivate::DotGraphPrivate(DotGraph* parent) :
   q->connect(&m_updateTimer, SIGNAL(timeout()), SLOT(doUpdate()));
   m_updateTimer.setInterval(0);
   m_updateTimer.setSingleShot(true);
+
+  q->connect(&m_loadThread, SIGNAL(finished()), SLOT(slotAGraphReadFinished()));
+  q->connect(&m_layoutThread, SIGNAL(finished()), SLOT(slotAGraphLayoutFinished()));
 }
 
 DotGraphPrivate::~DotGraphPrivate()
@@ -101,6 +104,38 @@ void DotGraphPrivate::graphIOError(const QString& error)
   kDebug() << "Error message from graph IO:" << error;
 }
 
+void DotGraphPrivate::slotAGraphReadFinished()
+{
+  Q_Q(DotGraph);
+  QString layoutCommand = (q  ? q->layoutCommand() : "");
+  if (layoutCommand.isEmpty()) {
+      layoutCommand = GraphIO::internalLayoutCommandForFile(m_loadThread.dotFileName());
+  }
+
+  Agraph_t* graph = m_loadThread.g();
+  if (graph)
+    m_layoutThread.layoutGraph(graph, layoutCommand);
+  else
+    kWarning() << "Graph loading failed";
+}
+
+void DotGraphPrivate::slotAGraphLayoutFinished()
+{
+  Q_Q(DotGraph);
+  Agraph_t* graph = m_layoutThread.g();
+  if (!graph) {
+    kWarning() << "Thread failed to layout graph properly, not doing anything.";
+    return;
+  }
+
+  q->updateWithGraph(graph);
+
+  gvFreeLayout(m_layoutThread.gvc(), graph);
+  agclose(graph);
+
+  q->readyToDisplay();
+}
+
 void DotGraphPrivate::doUpdate()
 {
   Q_Q(DotGraph);
@@ -295,14 +330,17 @@ void DotGraph::setUseLibrary(bool value)
   d->m_useLibrary = value;
 }
 
-bool DotGraph::parseDot(const QString& fileName)
+void DotGraph::loadFromFile(const QString& fileName)
 {
-  kDebug() << fileName;
-
   Q_D(DotGraph);
-  d->m_graphIO.loadFromDotFile(fileName, layoutCommand());
-
-  return true;
+  if (!useLibrary()) {
+    kDebug() << "Loading from file, using command line:" << fileName;
+    d->m_graphIO.loadFromDotFile(fileName, layoutCommand());
+  }
+  else {
+    kDebug() << "Loading from file, using library:" << fileName;
+    d->m_loadThread.loadFile(fileName);
+  }
 }
 
 void DotGraph::scheduleUpdate()
diff --git a/src/kgraphviz/dotgraph.h b/src/kgraphviz/dotgraph.h
index 70393cb..efe8728 100644
--- a/src/kgraphviz/dotgraph.h
+++ b/src/kgraphviz/dotgraph.h
@@ -50,8 +50,8 @@ public:
   DotGraph(const QString& layoutCommand, const QString& fileName);
 
   virtual ~DotGraph();
-  
-  bool parseDot(const QString& fileName);
+
+  void loadFromFile(const QString& fileName);
 
   const GraphNodeMap& nodes() const;
   const GraphEdgeMap& edges() const;
@@ -76,7 +76,7 @@ public:
   bool directed() const;
 
   QSet< GraphNode* >& nodesOfCell(unsigned int id);
-  
+
   unsigned int horizCellFactor() const;
   unsigned int vertCellFactor() const;
   double wdhcf() const;
@@ -137,6 +137,8 @@ private:
 
   Q_PRIVATE_SLOT(d_func(), void graphIOFinished());
   Q_PRIVATE_SLOT(d_func(), void graphIOError(QString));
+  Q_PRIVATE_SLOT(d_func(), void slotAGraphReadFinished());
+  Q_PRIVATE_SLOT(d_func(), void slotAGraphLayoutFinished());
 
   Q_PRIVATE_SLOT(d_func(), void doUpdate());
 };
diff --git a/src/kgraphviz/dotgraph_p.h b/src/kgraphviz/dotgraph_p.h
index cbbbf63..c4cc982 100644
--- a/src/kgraphviz/dotgraph_p.h
+++ b/src/kgraphviz/dotgraph_p.h
@@ -22,6 +22,9 @@
 
 #include "graphio.h"
 
+#include "support/loadagraphthread.h"
+#include "support/layoutagraphthread.h"
+
 #include <QVector>
 #include <QTimer>
 
@@ -37,9 +40,18 @@ public:
   unsigned int cellNumber(int x, int y) const;
   void computeCells();
 
+  void slotAGraphReadFinished();
+  void slotAGraphLayoutFinished();
+
   Q_DECLARE_PUBLIC(DotGraph);
   DotGraph* q_ptr;
 
+  /// A thread to load graphviz agraph files
+  LoadAGraphThread m_loadThread;
+
+  /// A thread to layout graphviz agraph files
+  LayoutAGraphThread m_layoutThread;
+
   QTimer m_updateTimer;
 
   GraphIO m_graphIO;
diff --git a/src/kgraphviz/dotgraphview.cpp b/src/kgraphviz/dotgraphview.cpp
index e68bb31..5127709 100644
--- a/src/kgraphviz/dotgraphview.cpp
+++ b/src/kgraphviz/dotgraphview.cpp
@@ -538,9 +538,6 @@ DotGraphView::DotGraphView(KActionCollection* actions, QWidget* parent) :
   setInteractive(true);
   setDragMode(NoDrag);
   setRenderHint(QPainter::Antialiasing);
-
-  connect(&d->m_loadThread, SIGNAL(finished()), this, SLOT(slotAGraphReadFinished()));
-  connect(&d->m_layoutThread, SIGNAL(finished()), this, SLOT(slotAGraphLayoutFinished()));
 }
 
 DotGraphView::~DotGraphView()
@@ -623,47 +620,17 @@ void DotGraphViewPrivate::setupCanvas()
 
 
 
-bool DotGraphView::loadDot(const QString& dotFileName)
+void DotGraphView::loadFromFile(const QString& fileName)
 {
-  kDebug() << "Filename:" << dotFileName;
+  kDebug() << "Filename:" << fileName;
   Q_D(DotGraphView);
   d->setupCanvas();
 
-  QGraphicsSimpleTextItem* loadingLabel = scene()->addSimpleText(i18n("graph %1 is getting loaded...", dotFileName));
-  loadingLabel->setZValue(100);
-  centerOn(loadingLabel);
-
-  if (!d->m_graph->parseDot(dotFileName)) {
-    kError() << "NOT successfully parsed!" << endl;
-    loadingLabel->setText(i18n("error parsing file %1", dotFileName));
-    return false;
-  }
-  return true;
-}
-
-bool DotGraphView::loadLibrary(const QString& dotFileName)
-{
-  kDebug() << "Load file:" << dotFileName;
-  
-  Q_D(DotGraphView);
-  // TODO: Clear canvas
-  QGraphicsSimpleTextItem* loadingLabel = scene()->addSimpleText(i18n("graph %1 is getting loaded...", dotFileName));
+  QGraphicsSimpleTextItem* loadingLabel = scene()->addSimpleText(i18n("Graph %1 is getting loaded...", fileName));
   loadingLabel->setZValue(100);
   centerOn(loadingLabel);
 
-  d->m_loadThread.loadFile(dotFileName);
-  
-  return true;
-}
-
-bool DotGraphView::loadLibrary(graph_t* graph, const QString& layoutCommand)
-{
-  kDebug() << "Agraph_t:" << graph << "- Layout command:" << layoutCommand;
-
-  Q_D(DotGraphView);
-  d->setupCanvas();
-  d->m_graph->updateWithGraph(graph);
-  return true;
+  graph()->loadFromFile(fileName);
 }
 
 void DotGraphView::slotSelectionChanged()
@@ -1672,36 +1639,6 @@ void DotGraphView::enterEvent ( QEvent * /*event*/ )
   }
 }
 
-void DotGraphView::slotAGraphReadFinished()
-{
-  Q_D(DotGraphView);
-  QString layoutCommand = (d->m_graph!=0?d->m_graph->layoutCommand():"");
-  if (layoutCommand.isEmpty()) {
-      layoutCommand = GraphIO::internalLayoutCommandForFile(d->m_loadThread.dotFileName());
-  }
-
-  Agraph_t* graph = d->m_loadThread.g();
-  if (graph)
-    d->m_layoutThread.layoutGraph(graph, layoutCommand);
-  else
-    kWarning() << "Graph loading failed";
-}
-
-void DotGraphView::slotAGraphLayoutFinished()
-{
-  Q_D(DotGraphView);
-  Agraph_t* graph = d->m_layoutThread.g();
-  if (!graph) {
-    kWarning() << "Thread failed to layout graph properly, not doing anything.";
-    return;
-  }
-  
-  loadLibrary(graph, d->m_layoutThread.layoutCommand());
-
-  gvFreeLayout(d->m_layoutThread.gvc(), graph);
-  agclose(d->m_layoutThread.g());
-}
-
 void DotGraphView::slotSelectNode(const QString& nodeName)
 {
   kDebug() << nodeName;
diff --git a/src/kgraphviz/dotgraphview.h b/src/kgraphviz/dotgraphview.h
index 0f05d90..3eced9f 100644
--- a/src/kgraphviz/dotgraphview.h
+++ b/src/kgraphviz/dotgraphview.h
@@ -71,9 +71,7 @@ public:
   static DotGraphView::PannerPosition zoomPos(const QString&);
   static QString toString(DotGraphView::PannerPosition);
 
-  bool loadDot(const QString& dotFileName);
-  bool loadLibrary(const QString& dotFileName);
-  bool loadLibrary(Agraph_t* graph, const QString& layoutCommand = "dot");
+  void loadFromFile(const QString& fileName);
 
   /// multiplies current zoom factor with @p factor
   void applyZoom(double factor);
@@ -193,10 +191,6 @@ Q_SIGNALS:
   void hoverEnter(const QString&);
   void hoverLeave(const QString&);
 
-private Q_SLOTS:
-  void slotAGraphReadFinished();
-  void slotAGraphLayoutFinished();
-
 private:
   Q_DECLARE_PRIVATE(DotGraphView);
 
diff --git a/src/kgraphviz/dotgraphview_p.h b/src/kgraphviz/dotgraphview_p.h
index 537225b..13e472c 100644
--- a/src/kgraphviz/dotgraphview_p.h
+++ b/src/kgraphviz/dotgraphview_p.h
@@ -22,9 +22,6 @@
 
 #include "dotgraphview.h"
 
-#include "support/loadagraphthread.h"
-#include "support/layoutagraphthread.h"
-
 #include <QMap>
 #include <QSet>
 
@@ -108,12 +105,6 @@ public:
   /// true if elements should be highlighted on hover; false otherwise
   bool m_highlighting;
 
-  /// A thread to load graphviz agraph files
-  LoadAGraphThread m_loadThread;
-
-  /// A thread to layout graphviz agraph files
-  LayoutAGraphThread m_layoutThread;
-
   /// The graph background color
   QColor m_backgroundColor;
 };
diff --git a/src/part/kgraphviewer_part.cpp b/src/part/kgraphviewer_part.cpp
index f6849d8..dd79c58 100644
--- a/src/part/kgraphviewer_part.cpp
+++ b/src/part/kgraphviewer_part.cpp
@@ -47,7 +47,7 @@ namespace KGraphViewer
 class KGraphViewerPartPrivate
 {
 public:
-  KGraphViewerPartPrivate() : m_watch(new KDirWatch()), m_layoutMethod(KGraphViewerInterface::InternalLibrary)
+  KGraphViewerPartPrivate() : m_watch(new KDirWatch())
   {
     
   }
@@ -59,8 +59,6 @@ public:
   
   KGraphViz::DotGraphView *m_widget;
   KDirWatch* m_watch;
-  KGraphViewerPart::LayoutMethod m_layoutMethod;
-  
 };
 
 KGraphViewerPart::KGraphViewerPart( QWidget *parentWidget, QObject *parent)
@@ -188,20 +186,7 @@ KGraphViewerPart::~KGraphViewerPart()
 bool KGraphViewerPart::openFile()
 {
   kDebug() << " " << localFilePath();
-  //    d->m_widget->loadedDot( localFilePath() );
-  switch (d->m_layoutMethod)
-  {
-    case ExternalProgram:
-      if (!d->m_widget->loadDot(localFilePath()))
-        return false;
-      break;
-    case InternalLibrary:
-      if (!d->m_widget->loadLibrary(localFilePath()))
-        return false;
-      break;
-    default:
-      kError() << "Unsupported layout method " << d->m_layoutMethod;
-  }
+  d->m_widget->loadFromFile(localFilePath());
   
   // deletes the existing file watcher because we have no way know here the name of the
   // previously watched file and thus we cannot use removeFile... :-(
@@ -364,7 +349,7 @@ void KGraphViewerPart::slotSetLayoutMethod(LayoutMethod method)
 
 void KGraphViewerPart::setLayoutMethod(LayoutMethod method)
 {
-  d->m_layoutMethod = method;
+  d->m_widget->graph()->setUseLibrary(method == InternalLibrary);
 }
 
 void KGraphViewerPart::centerOnNode(const QString& nodeId)



More information about the kgraphviewer-devel mailing list