From a392316f892322623ba15b8618216b2ca0bda52d Mon Sep 17 00:00:00 2001 From: Louis Moureaux Date: Mon, 21 Oct 2024 02:11:59 +0200 Subject: [PATCH] Save less state in minimap_view The widget needs to compute horizontal and vertical scaling factors to map between the displayed minimap (in pixel units) and the computed one (essentially in tile units). It was computing them on resize events and storing them, which was causing issues when switching maps. Compute them when they are needed instead, it's just two float divisions. Also remove a bunch of unused variables from the class and don't try to move() it when shown (it's in a layout). Closes #1657. --- client/minimap.cpp | 45 +++++++++------------------------------------ client/minimap.h | 9 +-------- 2 files changed, 10 insertions(+), 44 deletions(-) diff --git a/client/minimap.cpp b/client/minimap.cpp index e924f0a57f..7005e47e4a 100644 --- a/client/minimap.cpp +++ b/client/minimap.cpp @@ -32,26 +32,10 @@ const auto always_visible_margin = 15; /** Constructor for minimap */ -minimap_view::minimap_view(QWidget *parent) : fcwidget() +minimap_view::minimap_view(QWidget *parent) : fcwidget(parent) { - setParent(parent); setAttribute(Qt::WA_OpaquePaintEvent, true); - w_ratio = 0.0; - h_ratio = 0.0; - // Dark magic: This call is required for the widget to work. - resize(0, 0); - background = QBrush(QColor(0, 0, 0)); setCursor(Qt::CrossCursor); - pix = new QPixmap; -} - -/** - Minimap_view destructor - */ -minimap_view::~minimap_view() -{ - delete pix; - pix = nullptr; } /** @@ -74,20 +58,6 @@ void minimap_view::update_menu() ::king()->menu_bar->minimap_status->setChecked(false); } -/** - Minimap is being moved, position is being remembered - */ -void minimap_view::moveEvent(QMoveEvent *event) { position = event->pos(); } - -/** - Minimap is just unhidden, old position is restored - */ -void minimap_view::showEvent(QShowEvent *event) -{ - move(position); - event->setAccepted(true); -} - namespace { void overview_pos_nowrap(const struct tileset *t, int *ovr_x, int *ovr_y, @@ -134,6 +104,10 @@ void minimap_view::draw_viewport(QPainter *painter) painter->setPen(QColor(Qt::white)); + auto w_ratio = static_cast(width()) / gui_options->overview.width; + auto h_ratio = + static_cast(height()) / gui_options->overview.height; + QVector lines; for (int i = 0; i < 4; i++) { lines.append(QLineF(x[i] * w_ratio, y[i] * h_ratio, @@ -239,8 +213,6 @@ void minimap_view::resizeEvent(QResizeEvent *event) if (C_S_RUNNING <= client_state() && size.width() > 0 && size.height() > 0) { - w_ratio = static_cast(width()) / gui_options->overview.width; - h_ratio = static_cast(height()) / gui_options->overview.height; king()->qt_settings.minimap_width = static_cast(size.width()) / mapview.width; king()->qt_settings.minimap_height = @@ -258,11 +230,12 @@ void minimap_view::resizeEvent(QResizeEvent *event) void minimap_view::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::RightButton) { - cursor = event->pos(); auto fx = event->pos().x(); auto fy = event->pos().y(); - fx = qRound(fx / w_ratio); - fy = qRound(fy / h_ratio); + fx = qRound(fx * gui_options->overview.width + / static_cast(width())); + fy = qRound(fy * gui_options->overview.height + / static_cast(height())); fx = qMax(fx, 1); fy = qMax(fy, 1); fx = qMin(fx, gui_options->overview.width - 1); diff --git a/client/minimap.h b/client/minimap.h index eaefe6e7a3..9a1c943fb3 100644 --- a/client/minimap.h +++ b/client/minimap.h @@ -38,7 +38,7 @@ class minimap_view : public fcwidget { public: minimap_view(QWidget *parent); - ~minimap_view() override; + void paint(QPainter *painter, QPaintEvent *event); void update_menu() override; void update_image(); @@ -51,14 +51,7 @@ class minimap_view : public fcwidget { void paintEvent(QPaintEvent *event) override; void resizeEvent(QResizeEvent *event) override; void mousePressEvent(QMouseEvent *event) override; - void moveEvent(QMoveEvent *event) override; - void showEvent(QShowEvent *event) override; private: void draw_viewport(QPainter *painter); - float w_ratio, h_ratio; - QBrush background; - QPixmap *pix; - QPoint cursor; - QPoint position; };