From ca97f1bd709a154f73c9e28f8a50c5b409baab3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Fri, 5 Jan 2018 23:42:36 +0100 Subject: Fix how we handle scale of GraphicView That widget is seriously buggy.. grr --- qtmips_gui/coreview.cpp | 29 +++++++++++++++++++++++------ qtmips_gui/coreview.h | 5 ++++- 2 files changed, 27 insertions(+), 7 deletions(-) (limited to 'qtmips_gui') diff --git a/qtmips_gui/coreview.cpp b/qtmips_gui/coreview.cpp index 7210f1c..2546c2b 100644 --- a/qtmips_gui/coreview.cpp +++ b/qtmips_gui/coreview.cpp @@ -1,16 +1,33 @@ #include "coreview.h" CoreView::CoreView(QWidget *parent) : QGraphicsView(parent) { - setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); - // TODO fitInView doesn't work as I want so reimplement or do something with it - //fitInView(0, 0, 201, 201, Qt::KeepAspectRatioByExpanding); + setSceneRect(0, 0, scene_width, scene_height); + update_scale(); } -void CoreView::resizeEvent(QResizeEvent *event __attribute__((unused))) { - // fitInView(0, 0, 201, 201, Qt::KeepAspectRatioByExpanding); +void CoreView::resizeEvent(QResizeEvent *event) { + QGraphicsView::resizeEvent(event); + update_scale(); +} + +void CoreView::update_scale() { + // 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); } CoreViewScene::CoreViewScene(CoreView *view, machine::QtMipsMachine *machine) : QGraphicsScene(view) { diff --git a/qtmips_gui/coreview.h b/qtmips_gui/coreview.h index fddcafe..52b98ee 100644 --- a/qtmips_gui/coreview.h +++ b/qtmips_gui/coreview.h @@ -14,8 +14,11 @@ class CoreView : public QGraphicsView { public: CoreView(QWidget *parent); -private: +protected: void resizeEvent(QResizeEvent *event); +private: + const int scene_width = 800, scene_height = 800; + void update_scale(); }; class CoreViewScene : public QGraphicsScene { -- cgit v1.2.3