Skip to content

Commit

Permalink
Added zoom and automatic refresh of dependency graph view.
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindkv authored and wwmayer committed Jun 15, 2015
1 parent 1749ff2 commit f99fd10
Show file tree
Hide file tree
Showing 9 changed files with 428 additions and 74 deletions.
4 changes: 3 additions & 1 deletion src/App/Document.cpp
Expand Up @@ -200,7 +200,7 @@ void Document::writeDependencyGraphViz(std::ostream &out)
out << "}" << endl;
}

void Document::exportGraphviz(std::ostream& out)
void Document::exportGraphviz(std::ostream& out) const
{
std::vector<std::string> names;
names.reserve(d->objectMap.size());
Expand Down Expand Up @@ -1437,6 +1437,8 @@ void Document::recompute()
it->second->purgeTouched();
}
d->vertexMap.clear();

signalRecomputed(*this);
}

const char * Document::getErrorDescription(const App::DocumentObject*Obj) const
Expand Down
3 changes: 2 additions & 1 deletion src/App/Document.h
Expand Up @@ -129,6 +129,7 @@ class AppExport Document : public App::PropertyContainer
Base::XMLReader&)> signalImportObjects;
boost::signal<void (const std::vector<App::DocumentObject*>&, Base::Reader&,
const std::map<std::string, std::string>&)> signalImportViewObjects;
boost::signal<void (const App::Document&)> signalRecomputed;
//@}

/** @name File handling of the document */
Expand All @@ -141,7 +142,7 @@ class AppExport Document : public App::PropertyContainer
/// Restore the document from the file in Property Path
void restore (void);
void exportObjects(const std::vector<App::DocumentObject*>&, std::ostream&);
void exportGraphviz(std::ostream&);
void exportGraphviz(std::ostream&) const;
std::vector<App::DocumentObject*> importObjects(Base::XMLReader& reader);
/// Opens the document from its file name
//void open (void);
Expand Down
9 changes: 9 additions & 0 deletions src/Gui/CMakeLists.txt
Expand Up @@ -201,6 +201,7 @@ set(Gui_MOC_HDRS
EditorView.h
FileDialog.h
Flag.h
GraphicsViewZoom.h
GraphvizView.h
GuiApplicationNativeEventAware.h
HelpView.h
Expand Down Expand Up @@ -253,6 +254,12 @@ set(Gui_MOC_HDRS
fc_wrap_cpp(Gui_MOC_SRCS ${Gui_MOC_HDRS})
#SOURCE_GROUP("Moc" FILES ${Gui_MOC_SRCS})

add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/moc_GraphvizView-internal.cpp
COMMAND ${QT_MOC_EXECUTABLE} -o ${CMAKE_CURRENT_BINARY_DIR}/moc_GraphvizView-internal.cpp ${CMAKE_CURRENT_SOURCE_DIR}/GraphvizView.cpp
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/GraphvizView.cpp)

set_property(SOURCE GraphvizView.cpp APPEND PROPERTY OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/moc_GraphvizView-internal.cpp)

SET(Gui_UIC_SRCS
AboutApplication.ui
Clipping.ui
Expand Down Expand Up @@ -915,6 +922,7 @@ SET(FreeCADGui_CPP_SRCS
Document.cpp
DocumentModel.cpp
DocumentPyImp.cpp
GraphicsViewZoom.cpp
GuiApplicationNativeEventAware.cpp
GuiConsole.cpp
Macro.cpp
Expand All @@ -934,6 +942,7 @@ SET(FreeCADGui_SRCS
Document.h
DocumentModel.h
FreeCADGuiInit.py
GraphicsViewZoom.h
GuiApplicationNativeEventAware.h
GuiConsole.h
InventorAll.h
Expand Down
68 changes: 3 additions & 65 deletions src/Gui/CommandDoc.cpp
Expand Up @@ -361,71 +361,9 @@ StdCmdExportGraphviz::StdCmdExportGraphviz()
void StdCmdExportGraphviz::activated(int iMsg)
{
App::Document* doc = App::GetApplication().getActiveDocument();
std::stringstream str;
doc->exportGraphviz(str);

ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Paths");
QProcess proc;
QStringList args;
args << QLatin1String("-Tpng");
#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
bool pathChanged = false;
#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());
do {
proc.start(exe, args);
if (!proc.waitForStarted()) {
int ret = QMessageBox::warning(getMainWindow(),
qApp->translate("Std_ExportGraphviz","Graphviz not found"),
qApp->translate("Std_ExportGraphviz","Graphviz couldn't be found on your system.\n"
"Do you want to specify its installation path if it's already installed?"),
QMessageBox::Yes, QMessageBox::No);
if (ret == QMessageBox::No)
return;
path = QFileDialog::getExistingDirectory(Gui::getMainWindow(),
qApp->translate("Std_ExportGraphviz","Graphviz installation path"));
if (path.isEmpty())
return;
pathChanged = true;
#ifdef FC_OS_WIN32
exe = QString::fromAscii("\"%1/dot\"").arg(path);
#else
exe = QString::fromAscii("%1/dot").arg(path);
#endif
}
else {
if (pathChanged)
hGrp->SetASCII("Graphviz", (const char*)path.toUtf8());
break;
}
}
while(true);

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

QPixmap px;
if (px.loadFromData(proc.readAll(), "PNG")) {
Gui::GraphvizView* view = new Gui::GraphvizView(px);
view->setDependencyGraph(str.str());
view->setWindowTitle(qApp->translate("Std_ExportGraphviz","Dependency graph"));
getMainWindow()->addWindow(view);
}
else {
QMessageBox::warning(getMainWindow(),
qApp->translate("Std_ExportGraphviz","Graphviz failed"),
qApp->translate("Std_ExportGraphviz","Graphviz failed to create an image file"));
}
Gui::GraphvizView* view = new Gui::GraphvizView(*doc);
view->setWindowTitle(qApp->translate("Std_ExportGraphviz","Dependency graph"));
getMainWindow()->addWindow(view);
}

bool StdCmdExportGraphviz::isActive(void)
Expand Down
94 changes: 94 additions & 0 deletions src/Gui/GraphicsViewZoom.cpp
@@ -0,0 +1,94 @@
/***************************************************************************
* Copyright (c) 2015 Pavel Strakhov <ri@idzaaus.org> *
* Copyright (c) 2015 Eivind Kvedalen <eivind@kvedalen.name> *
* *
* 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 *
* *
***************************************************************************/

/*
* Based on
*
* http://stackoverflow.com/questions/19113532/qgraphicsview-zooming-in-and-out-under-mouse-position-using-mouse-wheel
*
* Re-licensed to LGPL after having contacted original author by e-mail.
*
*/

#include "PreCompiled.h"
#ifndef _PreComp_
# include <QGraphicsView>
# include <QMouseEvent>
# include <QApplication>
# include <QScrollBar>
# include <qmath.h>
#endif

#include <GraphicsViewZoom.h>

GraphicsViewZoom::GraphicsViewZoom(QGraphicsView* view)
: QObject(view), _view(view)
{
_view->viewport()->installEventFilter(this);
_view->setMouseTracking(true);
_modifiers = Qt::ControlModifier;
_zoom_factor_base = 1.0015;
}

void GraphicsViewZoom::gentle_zoom(double factor) {
_view->scale(factor, factor);
_view->centerOn(target_scene_pos);
QPointF delta_viewport_pos = target_viewport_pos - QPointF(_view->viewport()->width() / 2.0,
_view->viewport()->height() / 2.0);
QPointF viewport_center = _view->mapFromScene(target_scene_pos) - delta_viewport_pos;
_view->centerOn(_view->mapToScene(viewport_center.toPoint()));
}

void GraphicsViewZoom::set_modifiers(Qt::KeyboardModifiers modifiers) {
_modifiers = modifiers;

}

void GraphicsViewZoom::set_zoom_factor_base(double value) {
_zoom_factor_base = value;
}

bool GraphicsViewZoom::eventFilter(QObject *object, QEvent *event) {
if (event->type() == QEvent::MouseMove) {
QMouseEvent* mouse_event = static_cast<QMouseEvent*>(event);
QPointF delta = target_viewport_pos - mouse_event->pos();
if (qAbs(delta.x()) > 5 || qAbs(delta.y()) > 5) {
target_viewport_pos = mouse_event->pos();
target_scene_pos = _view->mapToScene(mouse_event->pos());
}
} else if (event->type() == QEvent::Wheel) {
QWheelEvent* wheel_event = static_cast<QWheelEvent*>(event);
if (QApplication::keyboardModifiers() == _modifiers) {
if (wheel_event->orientation() == Qt::Vertical) {
double angle = wheel_event->delta();
double factor = qPow(_zoom_factor_base, angle);
gentle_zoom(factor);
return true;
}
}
}
Q_UNUSED(object);
return false;
}

#include "moc_GraphicsViewZoom.cpp"
90 changes: 90 additions & 0 deletions src/Gui/GraphicsViewZoom.h
@@ -0,0 +1,90 @@
/***************************************************************************
* Copyright (c) 2015 Pavel Strakhov <ri@idzaaus.org> *
* Copyright (c) 2015 Eivind Kvedalen <eivind@kvedalen.name> *
* *
* 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 *
* *
***************************************************************************/

/*
* Based on
*
* http://stackoverflow.com/questions/19113532/qgraphicsview-zooming-in-and-out-under-mouse-position-using-mouse-wheel
*
* Re-licensed to LGPL after having contacted original author by e-mail.
*
*/

#ifndef GRAPHICSVIEWZOOM_H
#define GRAPHICSVIEWZOOM_H

#include <QObject>
#include <QPointF>



/*!
* This class adds ability to zoom QGraphicsView using mouse wheel. The point under cursor
* remains motionless while it's possible.
*
* Note that it becomes not possible when the scene's
* size is not large enough comparing to the viewport size. QGraphicsView centers the picture
* when it's smaller than the view. And QGraphicsView's scrolls boundaries don't allow to
* put any picture point at any viewport position.
*
* When the user starts scrolling, this class remembers original scene position and
* keeps it until scrolling is completed. It's better than getting original scene position at
* each scrolling step because that approach leads to position errors due to before-mentioned
* positioning restrictions.
*
* When zommed using scroll, this class emits zoomed() signal.
*
* Usage:
*
* new GraphicsViewZoom(view);
*
* The object will be deleted automatically when the view is deleted.
*
* You can set keyboard modifiers used for zooming using set_modified(). Zooming will be
* performed only on exact match of modifiers combination. The default modifier is Ctrl.
*
* You can change zoom velocity by calling set_zoom_factor_base().
* Zoom coefficient is calculated as zoom_factor_base^angle_delta
* (see QWheelEvent::angleDelta).
* The default zoom factor base is 1.0015.
*/

class QGraphicsView;

class GraphicsViewZoom : public QObject {
Q_OBJECT
public:
GraphicsViewZoom(QGraphicsView* view);
void gentle_zoom(double factor);
void set_modifiers(Qt::KeyboardModifiers modifiers);
void set_zoom_factor_base(double value);

private:
QGraphicsView* _view;
Qt::KeyboardModifiers _modifiers;
double _zoom_factor_base;
QPointF target_scene_pos, target_viewport_pos;
bool eventFilter(QObject* object, QEvent* event);
};

#endif // GRAPHICSVIEWZOOM_H

0 comments on commit f99fd10

Please sign in to comment.