diff options
author | Karel Kočí <cynerd@email.cz> | 2018-04-15 13:01:40 +0200 |
---|---|---|
committer | Karel Kočí <cynerd@email.cz> | 2018-04-15 13:01:40 +0200 |
commit | 3ce0bbb92731f67aa09b20cd21208505daea5e6b (patch) | |
tree | 88097451a413ef39fffdbbee397cb7d4d1530676 /qtmips_gui/graphicsview.cpp | |
parent | d7ca8071777cc1b11572dbb0d79dcf8a677a0bcd (diff) | |
download | qtmips-3ce0bbb92731f67aa09b20cd21208505daea5e6b.tar.gz qtmips-3ce0bbb92731f67aa09b20cd21208505daea5e6b.tar.bz2 qtmips-3ce0bbb92731f67aa09b20cd21208505daea5e6b.zip |
Rename CoreView to GraphicsView
This generalizes CoreView to be used with other parts of the project
too.
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); +} |