From e41cd179fb65852739df0ae97248b649e5f807ba Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Fri, 28 Jun 2024 14:16:20 +0300
Subject: [PATCH 5/5] Qt: Avoid deprecated QMouseEvent::globalPos() in Qt6-mode

See RM #735

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 client/gui-qt/citydlg.cpp    | 10 +++++++---
 client/gui-qt/dialogs.cpp    | 10 ++++++----
 client/gui-qt/gui_main.h     |  8 ++++++++
 client/gui-qt/mapview.cpp    | 34 +++++++++++++++++++++-------------
 client/gui-qt/messagewin.cpp | 11 +++++++----
 client/gui-qt/repodlgs.cpp   |  3 ++-
 6 files changed, 51 insertions(+), 25 deletions(-)

diff --git a/client/gui-qt/citydlg.cpp b/client/gui-qt/citydlg.cpp
index f41f5290a4..f9fe2dbeef 100644
--- a/client/gui-qt/citydlg.cpp
+++ b/client/gui-qt/citydlg.cpp
@@ -60,7 +60,7 @@
 #include "text.h"
 #include "tilespec.h"
 
-//gui-qt
+// gui-qt
 #include "citydlg.h"
 #include "colors.h"
 #include "fc_client.h"
@@ -3838,6 +3838,7 @@ bool fc_tooltip::eventFilter(QObject *obj, QEvent *ev)
     help_event = static_cast<QHelpEvent *>(ev);
     QPoint pos = help_event->pos();
     QModelIndex index = view->indexAt(pos);
+    QPoint hpos;
 
     if (!index.isValid()) {
       return false;
@@ -3845,8 +3846,11 @@ bool fc_tooltip::eventFilter(QObject *obj, QEvent *ev)
 
     item_tooltip = view->model()->data(index, Qt::ToolTipRole).toString();
     rect = view->visualRect(index);
-    rect.setX(rect.x() + help_event->globalPos().x());
-    rect.setY(rect.y() + help_event->globalPos().y());
+
+    hpos = help_event->globalPos();
+
+    rect.setX(rect.x() + hpos.x());
+    rect.setY(rect.y() + hpos.y());
 
     if (!item_tooltip.isEmpty()) {
       QToolTip::showText(help_event->globalPos(), item_tooltip, view, rect);
diff --git a/client/gui-qt/dialogs.cpp b/client/gui-qt/dialogs.cpp
index e1d55d7fdc..a283321bdb 100644
--- a/client/gui-qt/dialogs.cpp
+++ b/client/gui-qt/dialogs.cpp
@@ -371,7 +371,7 @@ void qfc_dialog::paintEvent(QPaintEvent *event)
 void qfc_dialog::mouseMoveEvent(QMouseEvent *event)
 {
   if (moving_now) {
-    move(event->globalPos() - point);
+    move(mevent_gpos(event) - point);
   }
 }
 
@@ -386,7 +386,7 @@ void qfc_dialog::mousePressEvent(QMouseEvent *event)
 
   if (y <= titlebar_height
       && x <= width() - close_pix.width()) {
-    point = event->globalPos() - geometry().topLeft();
+    point = mevent_gpos(event) - geometry().topLeft();
     moving_now = true;
     setCursor(Qt::SizeAllCursor);
   } else if (y <= titlebar_height
@@ -960,7 +960,8 @@ void notify_dialog::paintEvent(QPaintEvent * paint_event)
 ***************************************************************************/
 void notify_dialog::mousePressEvent(QMouseEvent *event)
 {
-  cursor = event->globalPos() - geometry().topLeft();
+  cursor = mevent_gpos(event) - geometry().topLeft();
+
   if (event->button() == Qt::RightButton) {
     was_destroyed = true;
     close();
@@ -972,7 +973,8 @@ void notify_dialog::mousePressEvent(QMouseEvent *event)
 ***************************************************************************/
 void notify_dialog::mouseMoveEvent(QMouseEvent *event)
 {
-  move(event->globalPos() - cursor);
+  move(mevent_gpos(event) - cursor);
+
   setCursor(Qt::SizeAllCursor);
 }
 
diff --git a/client/gui-qt/gui_main.h b/client/gui-qt/gui_main.h
index d825cba768..33ca146a93 100644
--- a/client/gui-qt/gui_main.h
+++ b/client/gui-qt/gui_main.h
@@ -23,4 +23,12 @@ QApplication *current_app();
 #define CAPTURE_DEFAULT_THIS [=]
 #endif // FREECIV_HAVE_CXX20_CAPTURE_THIS
 
+// Compatibility layer between Qt5 and Qt6 while we support
+// also the former.
+#ifndef FC_QT5_MODE
+#define mevent_gpos(__ev__) (__ev__)->globalPosition().toPoint()
+#else  // FC_QT5_MODE
+#define mevent_gpos(__ev__) (__ev__)->globalPos()
+#endif // FC_QT5_MODE
+
 #endif // FC__GUI_MAIN_H
diff --git a/client/gui-qt/mapview.cpp b/client/gui-qt/mapview.cpp
index b7dddb8bb6..7030d68f06 100644
--- a/client/gui-qt/mapview.cpp
+++ b/client/gui-qt/mapview.cpp
@@ -46,8 +46,9 @@
 // gui-qt
 #include "colors.h"
 #include "fc_client.h"
-#include "qtg_cxxside.h"
+#include "gui_main.h"
 #include "mapview.h"
+#include "qtg_cxxside.h"
 #include "sidebar.h"
 
 const char *get_timeout_label_text();
@@ -408,18 +409,19 @@ void move_widget::put_to_corner()
 void move_widget::mouseMoveEvent(QMouseEvent *event)
 {
   if (!gui()->interface_locked) {
-    parentWidget()->move(event->globalPos() - point);
+    parentWidget()->move(mevent_gpos(event) - point);
   }
 }
 
 /**********************************************************************//**
-  Sets moving point for move widget;
+  Sets moving point for move widget
 **************************************************************************/
-void move_widget::mousePressEvent(QMouseEvent* event)
+void move_widget::mousePressEvent(QMouseEvent *event)
 {
   if (!gui()->interface_locked) {
-    point = event->globalPos() - parentWidget()->geometry().topLeft();
+    point = mevent_gpos(event) - parentWidget()->geometry().topLeft();
   }
+
   update();
 }
 
@@ -449,14 +451,15 @@ void resize_widget::put_to_corner()
 /**********************************************************************//**
   Mouse handler for resize widget (resizes parent widget)
 **************************************************************************/
-void resize_widget::mouseMoveEvent(QMouseEvent * event)
+void resize_widget::mouseMoveEvent(QMouseEvent *event)
 {
   QPoint qp, np;
 
   if (gui()->interface_locked) {
     return;
   }
-  qp = event->globalPos();
+
+  qp = mevent_gpos(event);
   np.setX(qp.x() - point.x());
   np.setY(qp.y() - point.y());
   np.setX(qMax(np.x(), 32));
@@ -467,14 +470,15 @@ void resize_widget::mouseMoveEvent(QMouseEvent * event)
 /**********************************************************************//**
   Sets moving point for resize widget;
 **************************************************************************/
-void resize_widget::mousePressEvent(QMouseEvent* event)
+void resize_widget::mousePressEvent(QMouseEvent *event)
 {
   QPoint qp;
 
   if (gui()->interface_locked) {
     return;
   }
-  qp = event->globalPos();
+
+  qp = mevent_gpos(event);
   point.setX(qp.x() - parentWidget()->width());
   point.setY(qp.y() - parentWidget()->height());
   update();
@@ -915,7 +919,7 @@ void minimap_view::zoom_out()
   Right button - recenters on some point
   For wheel look mouseWheelEvent
 **************************************************************************/
-void minimap_view::mousePressEvent(QMouseEvent * event)
+void minimap_view::mousePressEvent(QMouseEvent *event)
 {
   int fx, fy;
   int x, y;
@@ -924,8 +928,9 @@ void minimap_view::mousePressEvent(QMouseEvent * event)
     if (gui()->interface_locked) {
       return;
     }
-    cursor = event->globalPos() - geometry().topLeft();
+    cursor = mevent_gpos(event) - geometry().topLeft();
   }
+
   if (event->button() == Qt::RightButton) {
     cursor = event->pos();
     fx = event->pos().x();
@@ -943,23 +948,26 @@ void minimap_view::mousePressEvent(QMouseEvent * event)
     center_tile_mapcanvas(map_pos_to_tile(&(wld.map), x, y));
     update_image();
   }
+
   event->setAccepted(true);
 }
 
 /**********************************************************************//**
   Called when mouse button was pressed. Used to moving minimap.
 **************************************************************************/
-void minimap_view::mouseMoveEvent(QMouseEvent* event)
+void minimap_view::mouseMoveEvent(QMouseEvent *event)
 {
   if (gui()->interface_locked) {
     return;
   }
+
   if (event->buttons() & Qt::LeftButton) {
     QPoint p, r;
+
     p = event->pos();
     r = mapTo(gui()->mapview_wdg, p);
     p = r - p;
-    move(event->globalPos() - cursor);
+    move(mevent_gpos(event) - cursor);
     setCursor(Qt::SizeAllCursor);
     gui()->qt_settings.minimap_x = static_cast<float>(p.x()) / mapview.width;
     gui()->qt_settings.minimap_y = static_cast<float>(p.y())
diff --git a/client/gui-qt/messagewin.cpp b/client/gui-qt/messagewin.cpp
index 3a16c43c2f..3c1facf26a 100644
--- a/client/gui-qt/messagewin.cpp
+++ b/client/gui-qt/messagewin.cpp
@@ -25,6 +25,7 @@
 
 // gui-qt
 #include "fc_client.h"
+#include "gui_main.h"
 #include "messagewin.h"
 #include "qtg_cxxside.h"
 #include "sprite.h"
@@ -94,7 +95,7 @@ void info_tab::mousePressEvent(QMouseEvent *event)
     int x = pos.x();
     int y = pos.y();
 
-    cursor = event->globalPos() - geometry().topLeft();
+    cursor = mevent_gpos(event) - geometry().topLeft();
     if (y > 0 && y < 25 && x > width() - 25 && x < width()) {
       resize_mode = true;
       resxy = true;
@@ -115,12 +116,14 @@ void info_tab::mousePressEvent(QMouseEvent *event)
 /***********************************************************************//**
   Restores cursor when resizing is done
 ***************************************************************************/
-void info_tab::mouseReleaseEvent(QMouseEvent* event)
+void info_tab::mouseReleaseEvent(QMouseEvent *event)
 {
   QPoint p;
+
   if (gui()->interface_locked) {
     return;
   }
+
   if (resize_mode) {
     resize_mode = false;
     resx = false;
@@ -161,7 +164,7 @@ void info_tab::mouseMoveEvent(QMouseEvent *event)
     int newheight = event->globalY() - cursor.y() - geometry().y();
 
     resize(width(), this->geometry().height()-newheight);
-    to_move = event->globalPos() - cursor;
+    to_move = mevent_gpos(event) - cursor;
     move(this->x(), to_move.y());
     setCursor(Qt::SizeVerCursor);
     restore_chat();
@@ -179,7 +182,7 @@ void info_tab::mouseMoveEvent(QMouseEvent *event)
     int newheight = event->globalY() - cursor.y() - geometry().y();
 
     resize(ex, this->geometry().height()- newheight);
-    to_move = event->globalPos() - cursor;
+    to_move = mevent_gpos(event) - cursor;
     move(this->x(), to_move.y());
     setCursor(Qt::SizeBDiagCursor);
     restore_chat();
diff --git a/client/gui-qt/repodlgs.cpp b/client/gui-qt/repodlgs.cpp
index 9e869ed39a..73e09e3a63 100644
--- a/client/gui-qt/repodlgs.cpp
+++ b/client/gui-qt/repodlgs.cpp
@@ -38,6 +38,7 @@
 
 // client
 #include "client_main.h"
+#include "gui_main.h"
 #include "helpdata.h"
 #include "options.h"
 #include "repodlgs_common.h"
@@ -803,7 +804,7 @@ void research_diagram::mouseMoveEvent(QMouseEvent *event)
       tt_text = def_str + tt_text.toHtmlEscaped();
       tooltip_text = tt_text.trimmed();
       tooltip_rect = rttp->rect;
-      tooltip_pos = event->globalPos();
+      tooltip_pos = mevent_gpos(event);
       if (!QToolTip::isVisible() && !timer_active) {
         timer_active = true;
         QTimer::singleShot(500, this, SLOT(show_tooltip()));
-- 
2.43.0

