From 46aef58a4916e99147e01914044432148d8430d3 Mon Sep 17 00:00:00 2001
From: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
Date: Sat, 28 Nov 2026 13:48:09 -0800
Subject: [PATCH] Qt: Keep diplomacy gold limit in step with players' gold

The gold spin boxes of a diplomacy meeting got their maximum from each
player's gold when the meeting widget was built, and never again. Once
the player's gold grew, say at the next turn, the spin box still
clamped the offer to the old amount.

Fill the update_diplomacy_gold() stub added in RM #2506: keep the two
spin boxes in diplo_wdg and set the maximum of the one for that player
in every open meeting. Signals are blocked meanwhile, so that clamping
to a lower maximum does not offer the clamped amount by itself.

See RM #2507

Signed-off-by: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
---
 client/gui-qt/diplodlg.cpp | 53 +++++++++++++++++++++++++++++++++++---
 client/gui-qt/diplodlg.h   |  4 +++
 2 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/client/gui-qt/diplodlg.cpp b/client/gui-qt/diplodlg.cpp
index 3174914a5e..0b188cd6f5 100644
--- a/client/gui-qt/diplodlg.cpp
+++ b/client/gui-qt/diplodlg.cpp
@@ -174,8 +174,6 @@ diplo_wdg::diplo_wdg(struct treaty *ptreaty,
   if (clause_enabled(CLAUSE_GOLD)) {
     QLabel *goldlab1;
     QLabel *goldlab2;
-    QSpinBox *gold_edit1;
-    QSpinBox *gold_edit2;
 
     goldlab1 = new QLabel(_("Gold:"));
     goldlab1->setAlignment(Qt::AlignRight);
@@ -197,6 +195,8 @@ diplo_wdg::diplo_wdg(struct treaty *ptreaty,
     layout->addWidget(gold_edit2, 3, 5);
     clause_column = 6;
   } else {
+    gold_edit1 = nullptr;
+    gold_edit2 = nullptr;
     clause_column = 5;
   }
 
@@ -294,6 +294,29 @@ void diplo_wdg::closeEvent(QCloseEvent *event)
   event->accept();
 }
 
+/************************************************************************//**
+  Keep the maximum of the gold spinners in step with the players' gold
+****************************************************************************/
+void diplo_wdg::update_gold(struct player *pplayer)
+{
+  QSpinBox *spin;
+
+  if (pplayer == plr1) {
+    spin = gold_edit2;
+  } else if (pplayer == plr2) {
+    spin = gold_edit1;
+  } else {
+    return;
+  }
+
+  if (spin != nullptr) {
+    // Clamping the value to a lower maximum must not offer that amount
+    spin->blockSignals(true);
+    spin->setMaximum(pplayer->economic.gold);
+    spin->blockSignals(false);
+  }
+}
+
 /************************************************************************//**
   Gold changed on first spinner
 ****************************************************************************/
@@ -904,6 +927,19 @@ void diplo_dlg::update_dlg()
   }
 }
 
+/************************************************************************//**
+  Updates gold spinners of all diplomacy widgets in current dialog
+****************************************************************************/
+void diplo_dlg::update_gold(struct player *pplayer)
+{
+  QMapIterator <struct player *, diplo_wdg *>i(treaty_list);
+
+  while (i.hasNext()) {
+    i.next();
+    i.value()->update_gold(pplayer);
+  }
+}
+
 /************************************************************************//**
   Existing dialog requested again.
 ****************************************************************************/
@@ -1127,5 +1163,16 @@ void close_all_diplomacy_dialogs(void)
 ****************************************************************************/
 void update_diplomacy_gold(struct player *pplayer)
 {
-  /* PORTME */
+  int i;
+  diplo_dlg *dd;
+  QWidget *w;
+
+  if (!gui()->is_repo_dlg_open("DDI")) {
+    return;
+  }
+  i = gui()->gimme_index_of("DDI");
+  fc_assert(i != -1);
+  w = gui()->game_tab_widget->widget(i);
+  dd = qobject_cast<diplo_dlg *>(w);
+  dd->update_gold(pplayer);
 }
diff --git a/client/gui-qt/diplodlg.h b/client/gui-qt/diplodlg.h
index 6b9115d154..ed88bb29d8 100644
--- a/client/gui-qt/diplodlg.h
+++ b/client/gui-qt/diplodlg.h
@@ -61,6 +61,7 @@ public:
             struct player *they, struct player *initiator);
   ~diplo_wdg();
   void update_wdg();
+  void update_gold(struct player *pplayer);
   void set_index(int ind);
   int get_index();
   struct treaty *treaty;
@@ -91,6 +92,8 @@ protected:
 private:
   struct player *plr1;
   struct player *plr2;
+  QSpinBox *gold_edit1;
+  QSpinBox *gold_edit2;
   int active_menu;
   struct player *curr_player;
   bool p1_accept;
@@ -112,6 +115,7 @@ public:
   ~diplo_dlg();
   void reactivate();
   void update_dlg();
+  void update_gold(struct player *pplayer);
   bool init(bool raise);
   diplo_wdg *find_widget(struct player *they);
   void close_widget(struct player *they);
-- 
2.55.0

