Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added zoom and automatic refresh of dependency graph view.
- Loading branch information
Showing
9 changed files
with
428 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.