From 81f09646bb40a2e63012d1801e0464adf0738ff3 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sat, 23 Mar 2024 07:59:02 +0200
Subject: [PATCH 46/46] Ruledit: Add values_dlg

This is a parent class for individual ruleset object
editor dialogs, handling helpeditor bookkeeping.
Currently set for building editor only.

See RM #327

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 meson.build                  |  4 ++-
 tools/ruledit/Makefile.am    |  7 +++--
 tools/ruledit/edit_impr.cpp  |  8 +++---
 tools/ruledit/edit_impr.h    |  5 +++-
 tools/ruledit/helpeditor.cpp | 17 ++++++++++--
 tools/ruledit/helpeditor.h   |  5 +++-
 tools/ruledit/values_dlg.cpp | 54 ++++++++++++++++++++++++++++++++++++
 tools/ruledit/values_dlg.h   | 45 ++++++++++++++++++++++++++++++
 8 files changed, 134 insertions(+), 11 deletions(-)
 create mode 100644 tools/ruledit/values_dlg.cpp
 create mode 100644 tools/ruledit/values_dlg.h

diff --git a/meson.build b/meson.build
index 5317478acd..f97830574d 100644
--- a/meson.build
+++ b/meson.build
@@ -4282,7 +4282,8 @@ mocced_ruledit = qt_mod.preprocess(
    'tools/ruledit/tab_building.h',
    'tools/ruledit/tab_good.h',
    'tools/ruledit/tab_gov.h',
-   'tools/ruledit/tab_unit.h']
+   'tools/ruledit/tab_unit.h',
+   'tools/ruledit/values_dlg.h']
   )
 
 executable('freeciv-ruledit',
@@ -4313,6 +4314,7 @@ executable('freeciv-ruledit',
   'tools/ruledit/tab_unit.cpp',
   'tools/ruledit/univ_value.c',
   'tools/ruledit/validity.c',
+  'tools/ruledit/values_dlg.cpp',
   mocced_ruledit, rulediticon,
   include_directories: tool_inc,
   dependencies: [qt_dep, m_dep, net_dep, readline_dep, gettext_dep,
diff --git a/tools/ruledit/Makefile.am b/tools/ruledit/Makefile.am
index afe7be5a26..376c70d4b8 100644
--- a/tools/ruledit/Makefile.am
+++ b/tools/ruledit/Makefile.am
@@ -49,7 +49,8 @@ MOC_FILES = \
             meta_tab_nation.cpp	\
             meta_tab_tech.cpp	\
 	    meta_tab_terrains.cpp \
-            meta_tab_unit.cpp
+            meta_tab_unit.cpp	\
+	    meta_values_dlg.cpp
 
 freeciv_ruledit_SOURCES =	\
 		conversion_log.cpp \
@@ -104,7 +105,9 @@ freeciv_ruledit_SOURCES =	\
 		univ_value.c	\
 		univ_value.h	\
 		validity.c	\
-		validity.h
+		validity.h	\
+		values_dlg.cpp	\
+		values_dlg.h
 
 nodist_freeciv_ruledit_SOURCES = $(MOC_FILES)
 
diff --git a/tools/ruledit/edit_impr.cpp b/tools/ruledit/edit_impr.cpp
index 8252c8f33d..fed25f58a1 100644
--- a/tools/ruledit/edit_impr.cpp
+++ b/tools/ruledit/edit_impr.cpp
@@ -38,7 +38,7 @@
 /**********************************************************************//**
   Setup edit_impr object
 **************************************************************************/
-edit_impr::edit_impr(ruledit_gui *ui_in, struct impr_type *impr_in) : QDialog()
+edit_impr::edit_impr(ruledit_gui *ui_in, struct impr_type *impr_in) : values_dlg()
 {
   QHBoxLayout *main_layout = new QHBoxLayout(this);
   QGridLayout *impr_layout = new QGridLayout();
@@ -175,6 +175,8 @@ edit_impr::edit_impr(ruledit_gui *ui_in, struct impr_type *impr_in) : QDialog()
 **************************************************************************/
 void edit_impr::closeEvent(QCloseEvent *cevent)
 {
+  close_help();
+
   // Save values from text fields.
   gfx_tag_given();
   gfx_tag_alt_given();
@@ -312,7 +314,5 @@ void edit_impr::sound_tag_alt2_given()
 **************************************************************************/
 void edit_impr::helptext()
 {
-  helpeditor *editor = new helpeditor(impr->helptext);
-
-  editor->show();
+  open_help(impr->helptext);
 }
diff --git a/tools/ruledit/edit_impr.h b/tools/ruledit/edit_impr.h
index 386757053b..a04a15bf32 100644
--- a/tools/ruledit/edit_impr.h
+++ b/tools/ruledit/edit_impr.h
@@ -21,6 +21,9 @@
 // Qt
 #include <QDialog>
 
+// ruledit
+#include "values_dlg.h"
+
 class QGridLayout;
 class QLineEdit;
 class QSpinBox;
@@ -28,7 +31,7 @@ class QToolButton;
 
 class ruledit_gui;
 
-class edit_impr : public QDialog
+class edit_impr : public values_dlg
 {
   Q_OBJECT
 
diff --git a/tools/ruledit/helpeditor.cpp b/tools/ruledit/helpeditor.cpp
index f417537a81..9082b0181e 100644
--- a/tools/ruledit/helpeditor.cpp
+++ b/tools/ruledit/helpeditor.cpp
@@ -23,17 +23,21 @@
 #include "fcintl.h"
 #include "string_vector.h"
 
+// ruledit
+#include "values_dlg.h"
+
 #include "helpeditor.h"
 
 /**********************************************************************//**
   Setup helpeditor object
 **************************************************************************/
-helpeditor::helpeditor(struct strvec *helptext_in) : QDialog()
+helpeditor::helpeditor(values_dlg *parent_dlg, struct strvec *helptext_in) : QDialog()
 {
   QGridLayout *main_layout = new QGridLayout(this);
   QPushButton *close_button;
   int row = 0;
 
+  pdlg = parent_dlg;
   helptext = helptext_in;
 
   area = new QTextEdit();
@@ -53,10 +57,19 @@ helpeditor::helpeditor(struct strvec *helptext_in) : QDialog()
   setLayout(main_layout);
 }
 
+/**********************************************************************//**
+  Close helpeditor
+**************************************************************************/
+void helpeditor::close()
+{
+  done(0);
+}
+
 /**********************************************************************//**
   User pushed close button
 **************************************************************************/
 void helpeditor::close_now()
 {
-  done(0);
+  // Both closes this dialog, and handles parent's own bookkeeping
+  pdlg->close_help();
 }
diff --git a/tools/ruledit/helpeditor.h b/tools/ruledit/helpeditor.h
index 366323f147..6c9d8b00c9 100644
--- a/tools/ruledit/helpeditor.h
+++ b/tools/ruledit/helpeditor.h
@@ -23,16 +23,19 @@
 #include <QTextEdit>
 
 class ruledit_gui;
+class values_dlg;
 
 class helpeditor : public QDialog
 {
   Q_OBJECT
 
   public:
-    explicit helpeditor(struct strvec *helptext_in);
+    explicit helpeditor(values_dlg *parent_dlg, struct strvec *helptext_in);
+    void close();
 
   private:
     struct strvec *helptext;
+    values_dlg *pdlg;
 
     QTextEdit *area;
 
diff --git a/tools/ruledit/values_dlg.cpp b/tools/ruledit/values_dlg.cpp
new file mode 100644
index 0000000000..eec2f8cf5d
--- /dev/null
+++ b/tools/ruledit/values_dlg.cpp
@@ -0,0 +1,54 @@
+/***********************************************************************
+ Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+***********************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include <fc_config.h>
+#endif
+
+// ruledit
+#include "helpeditor.h"
+
+#include "values_dlg.h"
+
+
+/**********************************************************************//**
+  values_dlg constructor
+**************************************************************************/
+values_dlg::values_dlg() : QDialog()
+{
+  help = nullptr;
+}
+
+/**********************************************************************//**
+  Open help editor
+**************************************************************************/
+void values_dlg::open_help(struct strvec *helptext)
+{
+  if (help == nullptr) {
+    help = new helpeditor(this, helptext);
+
+    help->show();
+  }
+}
+
+/**********************************************************************//**
+  Help editor is closing.
+**************************************************************************/
+void values_dlg::close_help()
+{
+  if (help != nullptr) {
+    help->close();
+
+    help = nullptr;
+  }
+}
diff --git a/tools/ruledit/values_dlg.h b/tools/ruledit/values_dlg.h
new file mode 100644
index 0000000000..00aadf5cfb
--- /dev/null
+++ b/tools/ruledit/values_dlg.h
@@ -0,0 +1,45 @@
+/***********************************************************************
+ Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+***********************************************************************/
+
+#ifndef FC__VALUES_DLG_H
+#define FC__VALUES_DLG_H
+
+#ifdef HAVE_CONFIG_H
+#include <fc_config.h>
+#endif
+
+// Qt
+#include <QDialog>
+
+class helpeditor;
+struct strvec;
+
+class values_dlg : public QDialog
+{
+  Q_OBJECT
+
+  public:
+    values_dlg();
+    void open_help(struct strvec *help);
+    void close_help();
+
+  private:
+    helpeditor *help;
+
+  protected:
+
+  private slots:
+};
+
+
+#endif // FC__VALUES_DLG_H
-- 
2.43.0

