diff options
Diffstat (limited to 'qtmips_gui/graphicsview.cpp')
-rw-r--r-- | qtmips_gui/graphicsview.cpp | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/qtmips_gui/graphicsview.cpp b/qtmips_gui/graphicsview.cpp new file mode 100644 index 0000000..6ef118d --- /dev/null +++ b/qtmips_gui/graphicsview.cpp @@ -0,0 +1,36 @@ +#include "graphicsview.h" + +GraphicsView::GraphicsView(QWidget *parent) : QGraphicsView(parent) { + setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); +} + +void GraphicsView::setScene(QGraphicsScene *scene) { + QGraphicsView::setScene(scene); + update_scale(); +} + +void GraphicsView::resizeEvent(QResizeEvent *event) { + QGraphicsView::resizeEvent(event); + update_scale(); +} + +void GraphicsView::update_scale() { + if (scene() == nullptr) + return; // Skip if we have no scene + + // Note: there is somehow three pixels error when viewing so we have to always compensate + const int w = scene()->width() + 3; + const int h = scene()->height() + 3; + + qreal scale = 1; + if (height() > h && width() > w) { + if (height() > width()) { + scale = (qreal)width() / w; + } else { + scale = (qreal)height() / h; + } + } + QTransform t; + t.scale(scale, scale); + setTransform(t, false); +} |