From 58a7d8c9cf50d4b2a9ae321684ca9d4ce4504260 Mon Sep 17 00:00:00 2001
From: Chris Xiong <chirs241097@gmail.com>
Date: Fri, 10 May 2024 00:04:04 -0400
Subject: Disable automatic hidpi scaling on maps. Add zoom option.

---
 mapman/src/groupview.cpp | 10 +++++++++-
 mapman/src/groupview.hpp |  2 ++
 mapman/src/painter.cpp   | 28 +++++++++++++++++-----------
 mapman/src/painter.hpp   |  4 +++-
 mapman/src/sliceview.cpp |  2 +-
 mapman/src/utils.cpp     |  3 ++-
 mapman/src/utils.hpp     |  2 +-
 7 files changed, 35 insertions(+), 16 deletions(-)

(limited to 'mapman')

diff --git a/mapman/src/groupview.cpp b/mapman/src/groupview.cpp
index 136607b..ea4225a 100644
--- a/mapman/src/groupview.cpp
+++ b/mapman/src/groupview.cpp
@@ -11,6 +11,7 @@
 #include <QLineEdit>
 #include <QSpinBox>
 #include <QLabel>
+#include <QComboBox>
 #include <QSplitter>
 #include <QHBoxLayout>
 #include <QVBoxLayout>
@@ -79,7 +80,7 @@ group_view::group_view() : QMdiSubWindow()
     auto rightpane = new QWidget();
     auto l3 = new QVBoxLayout();
     rightpane->setLayout(l3);
-    p = new map_painter();
+    p = new map_painter(this);
     l3->addWidget(p->view());
     connect(p, &map_painter::map_id_changed, this, &group_view::painter_drop);
     tetitle = new QLineEdit();
@@ -103,6 +104,13 @@ group_view::group_view() : QMdiSubWindow()
     l4->addWidget(new QLabel("x"));
     l4->addWidget(sbv);
     l4->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding));
+    l4->addWidget(new QLabel("Zoom"));
+    cbscale = new QComboBox();
+    cbscale->addItems({QStringLiteral("100%"), QStringLiteral("200%"), QStringLiteral("300%"), QStringLiteral("400%")});
+    connect(cbscale, &QComboBox::currentIndexChanged, this, [this](int idx) {
+       p->set_scaling(idx + 1);
+    });
+    l4->addWidget(cbscale);
     pbapply = new QPushButton("Save");
     connect(pbapply, &QPushButton::pressed, this, &group_view::update_library);
 
diff --git a/mapman/src/groupview.hpp b/mapman/src/groupview.hpp
index 52c1bd0..6db8838 100644
--- a/mapman/src/groupview.hpp
+++ b/mapman/src/groupview.hpp
@@ -10,6 +10,7 @@ class QPushButton;
 class QLineEdit;
 class QSpinBox;
 class QLabel;
+class QComboBox;
 class QStandardItemModel;
 class QSortFilterProxyModel;
 class map_library;
@@ -42,6 +43,7 @@ private:
     QLineEdit *tefilter;
     QSpinBox *sbh;
     QSpinBox *sbv;
+    QComboBox *cbscale;
     QPushButton *pbapply;
     bool dirty;
     map_library *l;
diff --git a/mapman/src/painter.cpp b/mapman/src/painter.cpp
index 3dafee6..bfd2f79 100644
--- a/mapman/src/painter.cpp
+++ b/mapman/src/painter.cpp
@@ -14,7 +14,7 @@
 class drop_rect : public QGraphicsRectItem
 {
 public:
-    drop_rect(int pos, map_painter *painter);
+    drop_rect(int pos, double dim, map_painter *painter);
 signals:
     void dropped(int pos, int id);
 protected:
@@ -25,8 +25,8 @@ private:
     map_painter *pt;
 };
 
-drop_rect::drop_rect(int pos, map_painter *painter) :
-    QGraphicsRectItem(QRectF(0, 0, 128, 128), nullptr),
+drop_rect::drop_rect(int pos, double dim, map_painter *painter) :
+    QGraphicsRectItem(QRectF(0, 0, dim, dim), nullptr),
     p(pos),
     pt(painter)
 {
@@ -51,10 +51,11 @@ void drop_rect::dropEvent(QGraphicsSceneDragDropEvent *e)
     else e->setDropAction(Qt::DropAction::IgnoreAction);
 }
 
-map_painter::map_painter() : l(nullptr)
+map_painter::map_painter(QWidget *parent) : l(nullptr)
 {
     s = new QGraphicsScene(0, 0, 0, 0);
-    v = new QGraphicsView(s);
+    v = new QGraphicsView(s, parent);
+    slice_dim = 128 / parent->devicePixelRatio();
     hc = vc = 0;
 }
 
@@ -68,7 +69,7 @@ void map_painter::set_dimension(int h, int v)
 {
     hc = h;
     vc = v;
-    s->setSceneRect(0, 0, hc * 128, vc * 128);
+    s->setSceneRect(0, 0, hc * slice_dim, vc * slice_dim);
     s->clear();
     slices.clear();
     slices.resize(hc * vc, nullptr);
@@ -78,11 +79,11 @@ void map_painter::set_dimension(int h, int v)
         for (int j = 0; j < hc; ++j)
         {
             auto t = s->addSimpleText(QString::number(i * hc + j));
-            t->setPos(j * 128 + 64 - t->boundingRect().width() / 2, i * 128 + 64 - t->boundingRect().height() / 2);
-            auto r = new drop_rect(i * hc + j, this);
+            t->setPos(j * slice_dim + slice_dim / 2 - t->boundingRect().width() / 2, i * slice_dim + slice_dim / 2 - t->boundingRect().height() / 2);
+            auto r = new drop_rect(i * hc + j, slice_dim, this);
             s->addItem(r);
             bgspr[i * hc + j] = {t, r};
-            r->setPos(j * 128, i * 128);
+            r->setPos(j * slice_dim, i * slice_dim);
         }
 }
 
@@ -101,7 +102,7 @@ void map_painter::set_map_id(int pos, bool populated, int id, bool user_input)
         QPixmap pm;
         if (!QPixmapCache::find(QString("map_%1").arg(id), &pm))
         {
-            pm = pixmap_of_map_data(l->get_map(id).map_data);
+            pm = pixmap_of_map_data(l->get_map(id).map_data, this->v->devicePixelRatio());
             QPixmapCache::insert(QString("map_%1").arg(id), pm);
         }
         auto p = s->addPixmap(pm);
@@ -112,7 +113,7 @@ void map_painter::set_map_id(int pos, bool populated, int id, bool user_input)
             .arg(m.locked ? "\nLocked" : ""));
         int x = pos / hc;
         int y = pos % hc;
-        p->setPos(y * 128, x * 128);
+        p->setPos(y * slice_dim, x * slice_dim);
         slices[pos] = p;
         static_cast<QAbstractGraphicsShapeItem*>(t)->setPen(QColor(Qt::GlobalColor::transparent));
         static_cast<QAbstractGraphicsShapeItem*>(r)->setPen(QColor(Qt::GlobalColor::transparent));
@@ -121,4 +122,9 @@ void map_painter::set_map_id(int pos, bool populated, int id, bool user_input)
         emit map_id_changed(pos, populated, id);
 }
 
+void map_painter::set_scaling(double scale)
+{
+    v->setTransform(QTransform::fromScale(scale, scale));
+}
+
 void map_painter::set_map_library(map_library *lib) { l = lib; }
diff --git a/mapman/src/painter.hpp b/mapman/src/painter.hpp
index f7822a9..a5611ea 100644
--- a/mapman/src/painter.hpp
+++ b/mapman/src/painter.hpp
@@ -14,11 +14,12 @@ class map_painter : public QObject
 {
     Q_OBJECT
 public:
-    map_painter(); 
+    map_painter(QWidget *parent = nullptr);
     ~map_painter();
     void set_dimension(int h, int v);
     void set_map_library(map_library *lib);
     void set_map_id(int pos, bool populated, int id, bool user_input = false);
+    void set_scaling(double scale);
 
     QGraphicsView* view() { return v; }
 signals:
@@ -29,6 +30,7 @@ private:
     map_library *l;
     int hc;
     int vc;
+    double slice_dim;
     std::vector<QGraphicsItem*> slices;
     std::vector<std::tuple<QGraphicsItem*, QGraphicsItem*>> bgspr;
 };
diff --git a/mapman/src/sliceview.cpp b/mapman/src/sliceview.cpp
index db24a26..1083028 100644
--- a/mapman/src/sliceview.cpp
+++ b/mapman/src/sliceview.cpp
@@ -29,7 +29,7 @@ slice_view::slice_view() : l(nullptr)
     lv->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection);
     lv->setDragDropMode(QAbstractItemView::DragDropMode::NoDragDrop);
     lv->viewport()->installEventFilter(this);
-    p = new map_painter();
+    p = new map_painter(this);
     p->set_dimension(1, 1);
     auto leftcontainer = new QWidget();
     auto leftlayout = new QVBoxLayout();
diff --git a/mapman/src/utils.cpp b/mapman/src/utils.cpp
index f01d946..e6f658c 100644
--- a/mapman/src/utils.cpp
+++ b/mapman/src/utils.cpp
@@ -21,9 +21,10 @@ rgb_t modify_color(rgb_t c, uint8_t variant)
 
 QColor rgb2qcolor(rgb_t c) {return QColor(c.r, c.g, c.b);}
 
-QPixmap pixmap_of_map_data(const std::array<uint8_t, 128 * 128> &map_data)
+QPixmap pixmap_of_map_data(const std::array<uint8_t, 128 * 128> &map_data, double scaling)
 {
     QImage ret(128, 128, QImage::Format_ARGB32);
+    ret.setDevicePixelRatio(scaling);
     for (size_t i = 0; i < 128; ++i)
         for (size_t j = 0; j < 128; ++j)
         {
diff --git a/mapman/src/utils.hpp b/mapman/src/utils.hpp
index 47df676..76978f2 100644
--- a/mapman/src/utils.hpp
+++ b/mapman/src/utils.hpp
@@ -78,5 +78,5 @@ const rgb_t MAP_COLORS[62] = {
     {127, 167, 150}
 };
 
-QPixmap pixmap_of_map_data(const map_data_t &map_data);
+QPixmap pixmap_of_map_data(const map_data_t &map_data, double scaling = 1.);
 #endif
-- 
cgit v1.2.3