Skip to content

Commit

Permalink
+ fixes #1501: Add Ability to Export Dependency Graph as SVG and PNG
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Apr 7, 2014
1 parent 397ee6e commit f62ff52
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 54 deletions.
52 changes: 26 additions & 26 deletions src/App/Document.cpp
Expand Up @@ -201,6 +201,32 @@ void Document::writeDependencyGraphViz(std::ostream &out)
out << "}" << endl;
}

void Document::exportGraphviz(std::ostream& out)
{
std::vector<std::string> names;
names.reserve(d->objectMap.size());
DependencyList DepList;
std::map<DocumentObject*,Vertex> VertexObjectList;

// Filling up the adjacency List
for (std::map<std::string,DocumentObject*>::const_iterator It = d->objectMap.begin(); It != d->objectMap.end();++It) {
// add the object as Vertex and remember the index
VertexObjectList[It->second] = add_vertex(DepList);
names.push_back(It->second->Label.getValue());
}
// add the edges
for (std::map<std::string,DocumentObject*>::const_iterator It = d->objectMap.begin(); It != d->objectMap.end();++It) {
std::vector<DocumentObject*> OutList = It->second->getOutList();
for (std::vector<DocumentObject*>::const_iterator It2=OutList.begin();It2!=OutList.end();++It2) {
if (*It2)
add_edge(VertexObjectList[It->second],VertexObjectList[*It2],DepList);
}
}

if (!names.empty())
boost::write_graphviz(out, DepList, boost::make_label_writer(&(names[0])));
}

//bool _has_cycle_dfs(const DependencyList & g, vertex_t u, default_color_type * color)
//{
// color[u] = gray_color;
Expand Down Expand Up @@ -906,32 +932,6 @@ unsigned int Document::getMemSize (void) const
return size;
}

void Document::exportGraphviz(std::ostream& out)
{
std::vector<std::string> names;
names.reserve(d->objectMap.size());
DependencyList DepList;
std::map<DocumentObject*,Vertex> VertexObjectList;

// Filling up the adjacency List
for (std::map<std::string,DocumentObject*>::const_iterator It = d->objectMap.begin(); It != d->objectMap.end();++It) {
// add the object as Vertex and remember the index
VertexObjectList[It->second] = add_vertex(DepList);
names.push_back(It->second->Label.getValue());
}
// add the edges
for (std::map<std::string,DocumentObject*>::const_iterator It = d->objectMap.begin(); It != d->objectMap.end();++It) {
std::vector<DocumentObject*> OutList = It->second->getOutList();
for (std::vector<DocumentObject*>::const_iterator It2=OutList.begin();It2!=OutList.end();++It2) {
if (*It2)
add_edge(VertexObjectList[It->second],VertexObjectList[*It2],DepList);
}
}

if (!names.empty())
boost::write_graphviz(out, DepList, boost::make_label_writer(&(names[0])));
}

bool Document::saveAs(const char* file)
{
Base::FileInfo fi(file);
Expand Down
2 changes: 1 addition & 1 deletion src/App/DocumentPyImp.cpp
Expand Up @@ -472,7 +472,7 @@ Py::List DocumentPy::getRedoNames(void) const
Py::String DocumentPy::getDependencyGraph(void) const
{
std::stringstream out;
getDocumentPtr()->writeDependencyGraphViz(out);
getDocumentPtr()->exportGraphviz(out);
return Py::String(out.str());
}

Expand Down
19 changes: 17 additions & 2 deletions src/Gui/CMakeLists.txt
Expand Up @@ -167,6 +167,7 @@ set(Gui_MOC_HDRS
EditorView.h
FileDialog.h
Flag.h
GraphvizView.h
GuiApplicationNativeEventAware.h
HelpView.h
InputVector.h
Expand Down Expand Up @@ -748,7 +749,6 @@ SOURCE_GROUP("View3D\\Inventor" FILES ${Inventor_SRCS})
SET(Widget_CPP_SRCS
FileDialog.cpp
MainWindow.cpp
MDIView.cpp
PrefWidgets.cpp
InputField.cpp
ProgressBar.cpp
Expand All @@ -761,7 +761,6 @@ SET(Widget_CPP_SRCS
SET(Widget_HPP_SRCS
FileDialog.h
MainWindow.h
MDIView.h
PrefWidgets.h
InputField.h
ProgressBar.h
Expand All @@ -777,6 +776,21 @@ SET(Widget_SRCS
)
SOURCE_GROUP("Widget" FILES ${Widget_SRCS})

# The view sources
SET(View_CPP_SRCS
MDIView.cpp
GraphvizView.cpp
)
SET(View_HPP_SRCS
MDIView.h
GraphvizView.h
)
SET(View_SRCS
${View_CPP_SRCS}
${View_HPP_SRCS}
)
SOURCE_GROUP("View" FILES ${View_SRCS})

# The workbench sources
SET(Workbench_CPP_SRCS
DockWindowManager.cpp
Expand Down Expand Up @@ -879,6 +893,7 @@ SET(FreeCADGui_SRCS
${View3D_SRCS}
${Viewprovider_SRCS}
${Widget_SRCS}
${View_SRCS}
${Workbench_SRCS}
${Selection_SRCS}
${FreeCADGui_SRCS}
Expand Down
28 changes: 3 additions & 25 deletions src/Gui/CommandDoc.cpp
Expand Up @@ -27,8 +27,6 @@
# include <QClipboard>
# include <QEventLoop>
# include <QFileDialog>
# include <QGraphicsScene>
# include <QGraphicsView>
# include <QLabel>
# include <QStatusBar>
# include <QPointer>
Expand Down Expand Up @@ -67,6 +65,7 @@
#include <Gui/View3DInventorViewer.h>
#include "MergeDocuments.h"
#include "NavigationStyle.h"
#include "GraphvizView.h"

using namespace Gui;

Expand Down Expand Up @@ -326,28 +325,6 @@ bool StdCmdMergeProjects::isActive(void)
// Std_ExportGraphviz
//===========================================================================

namespace Gui {
class ImageView : public MDIView
{
public:
ImageView(const QPixmap& p, QWidget* parent=0) : MDIView(0, parent)
{
scene = new QGraphicsScene();
scene->addPixmap(p);
view = new QGraphicsView(scene, this);
view->show();
setCentralWidget(view);
}
~ImageView()
{
delete scene;
delete view;
}
QGraphicsScene* scene;
QGraphicsView* view;
};
}

DEF_STD_CMD_A(StdCmdExportGraphviz);

StdCmdExportGraphviz::StdCmdExportGraphviz()
Expand Down Expand Up @@ -420,7 +397,8 @@ void StdCmdExportGraphviz::activated(int iMsg)

QPixmap px;
if (px.loadFromData(proc.readAll(), "PNG")) {
Gui::ImageView* view = new Gui::ImageView(px);
Gui::GraphvizView* view = new Gui::GraphvizView(px);
view->setDependencyGraph(str.str());
view->setWindowTitle(qApp->translate("Std_ExportGraphviz","Dependency graph"));
getMainWindow()->addWindow(view);
}
Expand Down
208 changes: 208 additions & 0 deletions src/Gui/GraphvizView.cpp
@@ -0,0 +1,208 @@
/***************************************************************************
* Copyright (c) 2014 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* 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., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/


#include "PreCompiled.h"

#ifndef _PreComp_
# include <QFile>
# include <QPrinter>
# include <QPrintDialog>
# include <QPrintPreviewDialog>
# include <QProcess>
# include <QSvgRenderer>
#endif
# include <QGraphicsScene>
# include <QGraphicsView>
#include "FileDialog.h"


#include "GraphvizView.h"
#include <App/Application.h>

using namespace Gui;


GraphvizView::GraphvizView(const QPixmap& p, QWidget* parent)
: MDIView(0, parent)
{
scene = new QGraphicsScene();
scene->addPixmap(p);
view = new QGraphicsView(scene, this);
view->show();
setCentralWidget(view);
}

GraphvizView::~GraphvizView()
{
delete scene;
delete view;
}

void GraphvizView::setDependencyGraph(const std::string& s)
{
graphCode = s;
}

QByteArray GraphvizView::exportGraph(const QString& filter)
{
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Paths");
QProcess proc;
QStringList args;
if (filter.indexOf(QLatin1String("png")) > 0)
args << QLatin1String("-Tpng");
else if (filter.indexOf(QLatin1String("svg")) > 0)
args << QLatin1String("-Tsvg");
else if (filter.indexOf(QLatin1String("pdf")) > 0)
args << QLatin1String("-Tpdf");
else
return QByteArray();

#ifdef FC_OS_LINUX
QString path = QString::fromUtf8(hGrp->GetASCII("Graphviz", "/usr/bin").c_str());
#else
QString path = QString::fromUtf8(hGrp->GetASCII("Graphviz").c_str());
#endif

#ifdef FC_OS_WIN32
QString exe = QString::fromAscii("\"%1/dot\"").arg(path);
#else
QString exe = QString::fromAscii("%1/dot").arg(path);
#endif
proc.setEnvironment(QProcess::systemEnvironment());
proc.start(exe, args);
if (!proc.waitForStarted()) {
return QByteArray();
}

proc.write(graphCode.c_str(), graphCode.size());
proc.closeWriteChannel();
if (!proc.waitForFinished())
return QByteArray();

return proc.readAll();
}

bool GraphvizView::onMsg(const char* pMsg,const char** ppReturn)
{
if (strcmp("Save",pMsg) == 0 || strcmp("SaveAs",pMsg) == 0) {
QStringList filter;
filter << tr("PNG format (*.png)");
filter << tr("SVG format (*.svg)");
filter << tr("PDF format (*.pdf)");

QString selectedFilter;
QString fn = Gui::FileDialog::getSaveFileName(this, tr("Export graph"), QString(), filter.join(QLatin1String(";;")), &selectedFilter);
if (!fn.isEmpty()) {
QByteArray buffer = exportGraph(selectedFilter);
if (buffer.isEmpty())
return false;
QFile file(fn);
if (file.open(QFile::WriteOnly)) {
file.write(buffer);
file.close();
return true;
}
}
}
else if (strcmp("Print",pMsg) == 0) {
return true;
}
else if (strcmp("PrintPreview",pMsg) == 0) {
return true;
}
else if (strcmp("PrintPdf",pMsg) == 0) {
return true;
}

return false;
}

bool GraphvizView::onHasMsg(const char* pMsg) const
{
if (strcmp("Save",pMsg) == 0)
return true;
else if (strcmp("SaveAs",pMsg) == 0)
return true;
else if (strcmp("Print",pMsg) == 0)
return true;
else if (strcmp("PrintPreview",pMsg) == 0)
return true;
else if (strcmp("PrintPdf",pMsg) == 0)
return true;
return false;
}

void GraphvizView::print(QPrinter* printer)
{
QPainter p(printer);
QRect rect = printer->pageRect();
view->scene()->render(&p, rect);
//QByteArray buffer = exportGraph(QString::fromLatin1("(*.svg)"));
//QSvgRenderer svg(buffer);
//svg.render(&p, rect);
p.end();
}

void GraphvizView::print()
{
QPrinter printer(QPrinter::HighResolution);
printer.setFullPage(true);
printer.setOrientation(QPrinter::Landscape);
QPrintDialog dlg(&printer, this);
if (dlg.exec() == QDialog::Accepted) {
print(&printer);
}
}

void GraphvizView::printPdf()
{
QStringList filter;
filter << tr("PDF format (*.pdf)");

QString selectedFilter;
QString fn = Gui::FileDialog::getSaveFileName(this, tr("Export graph"), QString(), filter.join(QLatin1String(";;")), &selectedFilter);
if (!fn.isEmpty()) {
QByteArray buffer = exportGraph(selectedFilter);
if (buffer.isEmpty())
return;
QFile file(fn);
if (file.open(QFile::WriteOnly)) {
file.write(buffer);
file.close();
}
}
}

void GraphvizView::printPreview()
{
QPrinter printer(QPrinter::HighResolution);
printer.setFullPage(true);
printer.setOrientation(QPrinter::Landscape);

QPrintPreviewDialog dlg(&printer, this);
connect(&dlg, SIGNAL(paintRequested (QPrinter *)),
this, SLOT(print(QPrinter *)));
dlg.exec();
}

#include "moc_GraphvizView.cpp"

0 comments on commit f62ff52

Please sign in to comment.