Skip to content

Instantly share code, notes, and snippets.

@luis-l
Last active May 2, 2024 04:25
Show Gist options
  • Save luis-l/5c78488906dc98ce9316 to your computer and use it in GitHub Desktop.
Save luis-l/5c78488906dc98ce9316 to your computer and use it in GitHub Desktop.
Qt Interactable QGraphicsView with proper Panning and Zooming.
#include "interactiveview.h"
#define VIEW_CENTER viewport()->rect().center()
#define VIEW_WIDTH viewport()->rect().width()
#define VIEW_HEIGHT viewport()->rect().height()
InteractiveView::InteractiveView()
{
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setMaxSize();
centerOn(0, 0);
zoomDelta = 0.1;
panSpeed = 4;
_doMousePanning = false;
_doKeyZoom = false;
_scale = 1.0;
panButton = Qt::MiddleButton;
zoomKey = Qt::Key_Z;
}
qreal InteractiveView::getScale() const
{
return _scale;
}
void InteractiveView::keyPressEvent(QKeyEvent * event)
{
qint32 key = event->key();
if(key == zoomKey){
_doKeyZoom = true;
}
if (_doKeyZoom){
if (key == Qt::Key_Up)
zoomIn();
else if (key == Qt::Key_Down)
zoomOut();
}
else{
QGraphicsView::keyPressEvent(event);
}
}
void InteractiveView::keyReleaseEvent(QKeyEvent * event)
{
if (event->key() == zoomKey){
_doKeyZoom = false;
}
QGraphicsView::keyReleaseEvent(event);
}
void InteractiveView::mouseMoveEvent(QMouseEvent * event)
{
if (_doMousePanning){
QPointF mouseDelta = mapToScene(event->pos()) - mapToScene(_lastMousePos);
pan(mouseDelta);
}
QGraphicsView::mouseMoveEvent(event);
_lastMousePos = event->pos();
}
void InteractiveView::mousePressEvent(QMouseEvent * event)
{
if (event->button() == panButton){
_lastMousePos = event->pos();
_doMousePanning = true;
}
QGraphicsView::mousePressEvent(event);
}
void InteractiveView::mouseReleaseEvent(QMouseEvent * event)
{
if (event->button() == panButton){
_doMousePanning = false;
}
QGraphicsView::mouseReleaseEvent(event);
}
void InteractiveView::wheelEvent(QWheelEvent *event)
{
QPoint scrollAmount = event->angleDelta();
// Apply zoom.
scrollAmount.y() > 0 ? zoomIn() : zoomOut();
}
void InteractiveView::setMaxSize()
{
setSceneRect(INT_MIN/2, INT_MIN/2, INT_MAX, INT_MAX);
}
void InteractiveView::zoom(float scaleFactor)
{
scale(scaleFactor, scaleFactor);
_scale *= scaleFactor;
}
void InteractiveView::zoomIn()
{
zoom(1 + zoomDelta);
}
void InteractiveView::zoomOut()
{
zoom (1 - zoomDelta);
}
void InteractiveView::pan(QPointF delta)
{
// Scale the pan amount by the current zoom.
delta *= _scale;
delta *= panSpeed;
// Have panning be anchored from the mouse.
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
QPoint newCenter(VIEW_WIDTH / 2 - delta.x(), VIEW_HEIGHT / 2 - delta.y());
centerOn(mapToScene(newCenter));
// For zooming to anchor from the view center.
setTransformationAnchor(QGraphicsView::AnchorViewCenter);
}
#ifndef INTERACTIVE_VIEW_H
#define INTERACTIVE_VIEW_H
#include <QGraphicsView>
#include <QWheelEvent>
#include <QKeyEvent>
class InteractiveView : public QGraphicsView
{
public:
InteractiveView();
qreal panSpeed;
qreal zoomDelta;
qreal zoomKey;
Qt::MouseButton panButton;
void pan(QPointF delta);
void zoom(float scaleFactor);
void zoomIn();
void zoomOut();
qreal getScale() const;
protected:
void keyPressEvent(QKeyEvent*) override;
void keyReleaseEvent(QKeyEvent*) override;
void mouseMoveEvent(QMouseEvent*) override;
void mousePressEvent(QMouseEvent*) override;
void mouseReleaseEvent(QMouseEvent*) override;
void wheelEvent(QWheelEvent*) override;
private:
// Flags to determine if zooming or panning should be done.
bool _doMousePanning;
bool _doKeyZoom;
QPoint _lastMousePos;
qreal _scale;
void setMaxSize();
};
#endif // INTERACTIVE_VIEW_H
Copyright (c) 2012-2024 Scott Chacon and others
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@BlackStar-EoP
Copy link

Thank you for this, I am using this in my node editor as the main graphics view, I have credited you in the header and made some modifications, what is the license for this thing? I haven't selected a license for my node editor yet :)

https://github.com/BlackStar-EoP/eop-node-editor

@bkmgit
Copy link

bkmgit commented Jan 5, 2024

@luis-l Thanks for this. Can you add licensing information (e.g. MIT, public domain)? It is used as a dependency in Kristall.

@luis-l
Copy link
Author

luis-l commented May 1, 2024

@bkmgit sure. Added MIT license

@bkmgit
Copy link

bkmgit commented May 2, 2024

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment