From 4d73a9a39fc4c74ac2adeafb40f88a6858b730d5 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Wed, 15 May 2024 14:11:43 +0300
Subject: [PATCH 85/85] Ruledit: Add edit_gov dialog

See RM #618

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 common/government.c        |   1 +
 common/government.h        |   1 +
 meson.build                |   2 +
 tools/ruledit/Makefile.am  |   3 +
 tools/ruledit/edit_gov.cpp | 193 +++++++++++++++++++++++++++++++++++++
 tools/ruledit/edit_gov.h   |  62 ++++++++++++
 tools/ruledit/tab_gov.cpp  |  70 +++++++++-----
 tools/ruledit/tab_gov.h    |   1 +
 tools/ruledit/tab_unit.cpp |  22 ++---
 9 files changed, 321 insertions(+), 34 deletions(-)
 create mode 100644 tools/ruledit/edit_gov.cpp
 create mode 100644 tools/ruledit/edit_gov.h

diff --git a/common/government.c b/common/government.c
index 534ae11714..39254770d8 100644
--- a/common/government.c
+++ b/common/government.c
@@ -504,6 +504,7 @@ static inline void government_init(struct government *pgovern)
   requirement_vector_init(&pgovern->reqs);
   pgovern->changed_to_times = 0;
   pgovern->ruledit_disabled = FALSE;
+  pgovern->ruledit_dlg = NULL;
 }
 
 /**********************************************************************//**
diff --git a/common/government.h b/common/government.h
index ec2bae001b..a7e0161378 100644
--- a/common/government.h
+++ b/common/government.h
@@ -53,6 +53,7 @@ struct government {
   Government_type_id item_number;
   struct name_translation name;
   bool ruledit_disabled;
+  void *ruledit_dlg;
   char graphic_str[MAX_LEN_NAME];
   char graphic_alt[MAX_LEN_NAME];
   char sound_str[MAX_LEN_NAME];
diff --git a/meson.build b/meson.build
index 1f2b068354..44e8b3c244 100644
--- a/meson.build
+++ b/meson.build
@@ -4300,6 +4300,7 @@ mocced_ruledit = qt_mod.preprocess(
   moc_headers: [
    'tools/ruledit/conversion_log.h',
    'tools/ruledit/edit_extra.h',
+   'tools/ruledit/edit_gov.h',
    'tools/ruledit/edit_impr.h',
    'tools/ruledit/edit_tech.h',
    'tools/ruledit/edit_terrain.h',
@@ -4329,6 +4330,7 @@ mocced_ruledit = qt_mod.preprocess(
 executable('freeciv-ruledit',
   'tools/ruledit/conversion_log.cpp',
   'tools/ruledit/edit_extra.cpp',
+  'tools/ruledit/edit_gov.cpp',
   'tools/ruledit/edit_impr.cpp',
   'tools/ruledit/edit_tech.cpp',
   'tools/ruledit/edit_terrain.cpp',
diff --git a/tools/ruledit/Makefile.am b/tools/ruledit/Makefile.am
index 51898293e0..39c64618c6 100644
--- a/tools/ruledit/Makefile.am
+++ b/tools/ruledit/Makefile.am
@@ -28,6 +28,7 @@ freeciv_ruledit_CXXFLAGS = $(ruledit_cxxflags)
 MOC_FILES = \
             meta_conversion_log.cpp \
             meta_edit_extra.cpp	\
+            meta_edit_gov.cpp	\
             meta_edit_impr.cpp	\
             meta_edit_tech.cpp	\
             meta_edit_terrain.cpp	\
@@ -58,6 +59,8 @@ freeciv_ruledit_SOURCES =	\
 		conversion_log.h \
 		edit_extra.cpp	\
 		edit_extra.h	\
+		edit_gov.cpp	\
+		edit_gov.h	\
 		edit_impr.cpp	\
 		edit_impr.h	\
 		edit_tech.cpp	\
diff --git a/tools/ruledit/edit_gov.cpp b/tools/ruledit/edit_gov.cpp
new file mode 100644
index 0000000000..5f6dd69432
--- /dev/null
+++ b/tools/ruledit/edit_gov.cpp
@@ -0,0 +1,193 @@
+/***********************************************************************
+ 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
+
+// Qt
+#include <QGridLayout>
+#include <QLineEdit>
+#include <QPushButton>
+
+// common
+#include "government.h"
+
+// ruledit
+#include "helpeditor.h"
+#include "ruledit.h"
+#include "ruledit_qt.h"
+
+#include "edit_gov.h"
+
+
+/**********************************************************************//**
+  Setup edit_gov object
+**************************************************************************/
+edit_gov::edit_gov(ruledit_gui *ui_in, struct government *gov_in) : values_dlg()
+{
+  QHBoxLayout *main_layout = new QHBoxLayout(this);
+  QGridLayout *gov_layout = new QGridLayout();
+  QLabel *label;
+  QPushButton *button;
+  int row;
+
+  ui = ui_in;
+  gov = gov_in;
+
+  setWindowTitle(QString::fromUtf8(government_rule_name(gov)));
+
+  row = 0;
+
+  label = new QLabel(QString::fromUtf8(R__("Graphics tag")));
+  label->setParent(this);
+
+  gfx_tag = new QLineEdit(this);
+  connect(gfx_tag, SIGNAL(returnPressed()), this, SLOT(gfx_tag_given()));
+
+  gov_layout->addWidget(label, row, 0);
+  gov_layout->addWidget(gfx_tag, row++, 1);
+
+  label = new QLabel(QString::fromUtf8(R__("Alt graphics tag")));
+  label->setParent(this);
+
+  gfx_tag_alt = new QLineEdit(this);
+  connect(gfx_tag_alt, SIGNAL(returnPressed()), this, SLOT(gfx_tag_alt_given()));
+
+  gov_layout->addWidget(label, row, 0);
+  gov_layout->addWidget(gfx_tag_alt, row++, 1);
+
+  label = new QLabel(QString::fromUtf8(R__("Sound tag")));
+  label->setParent(this);
+
+  sound_tag = new QLineEdit(this);
+  connect(sound_tag, SIGNAL(returnPressed()), this, SLOT(sound_tag_given()));
+
+  gov_layout->addWidget(label, row, 0);
+  gov_layout->addWidget(sound_tag, row++, 1);
+
+  label = new QLabel(QString::fromUtf8(R__("Alt sound tag")));
+  label->setParent(this);
+
+  sound_tag_alt = new QLineEdit(this);
+  connect(sound_tag_alt, SIGNAL(returnPressed()), this, SLOT(sound_tag_alt_given()));
+
+  gov_layout->addWidget(label, row, 0);
+  gov_layout->addWidget(sound_tag_alt, row++, 1);
+
+  label = new QLabel(QString::fromUtf8(R__("Second alt sound tag")));
+  label->setParent(this);
+
+  sound_tag_alt2 = new QLineEdit(this);
+  connect(sound_tag_alt2, SIGNAL(returnPressed()), this, SLOT(sound_tag_alt2_given()));
+
+  gov_layout->addWidget(label, row, 0);
+  gov_layout->addWidget(sound_tag_alt2, row++, 1);
+
+  button = new QPushButton(QString::fromUtf8(R__("Helptext")), this);
+  connect(button, SIGNAL(pressed()), this, SLOT(helptext()));
+  gov_layout->addWidget(button, row++, 1);
+
+  refresh();
+
+  main_layout->addLayout(gov_layout);
+
+  setLayout(main_layout);
+}
+
+/**********************************************************************//**
+  User is closing dialog.
+**************************************************************************/
+void edit_gov::closeEvent(QCloseEvent *cevent)
+{
+  close_help();
+
+  // Save values from text fields.
+  gfx_tag_given();
+  gfx_tag_alt_given();
+  sound_tag_given();
+  sound_tag_alt_given();
+  sound_tag_alt2_given();
+
+  gov->ruledit_dlg = nullptr;
+}
+
+/**********************************************************************//**
+  Refresh the information.
+**************************************************************************/
+void edit_gov::refresh()
+{
+  gfx_tag->setText(gov->graphic_str);
+  gfx_tag_alt->setText(gov->graphic_alt);
+  sound_tag->setText(gov->sound_str);
+  sound_tag_alt->setText(gov->sound_alt);
+  sound_tag_alt2->setText(gov->sound_alt2);
+}
+
+/**********************************************************************//**
+  User entered new graphics tag.
+**************************************************************************/
+void edit_gov::gfx_tag_given()
+{
+  QByteArray tag_bytes = gfx_tag->text().toUtf8();
+
+  sz_strlcpy(gov->graphic_str, tag_bytes);
+}
+
+/**********************************************************************//**
+  User entered new alternative graphics tag.
+**************************************************************************/
+void edit_gov::gfx_tag_alt_given()
+{
+  QByteArray tag_bytes = gfx_tag_alt->text().toUtf8();
+
+  sz_strlcpy(gov->graphic_alt, tag_bytes);
+}
+
+/**********************************************************************//**
+  User entered new sound tag.
+**************************************************************************/
+void edit_gov::sound_tag_given()
+{
+  QByteArray tag_bytes = sound_tag->text().toUtf8();
+
+  sz_strlcpy(gov->sound_str, tag_bytes);
+}
+
+/**********************************************************************//**
+  User entered new alternative sound tag.
+**************************************************************************/
+void edit_gov::sound_tag_alt_given()
+{
+  QByteArray tag_bytes = sound_tag_alt->text().toUtf8();
+
+  sz_strlcpy(gov->sound_alt, tag_bytes);
+}
+
+/**********************************************************************//**
+  User entered new second alternative sound tag.
+**************************************************************************/
+void edit_gov::sound_tag_alt2_given()
+{
+  QByteArray tag_bytes = sound_tag_alt2->text().toUtf8();
+
+  sz_strlcpy(gov->sound_alt2, tag_bytes);
+}
+
+/**********************************************************************//**
+  User pressed helptext button
+**************************************************************************/
+void edit_gov::helptext()
+{
+  open_help(&gov->helptext);
+}
diff --git a/tools/ruledit/edit_gov.h b/tools/ruledit/edit_gov.h
new file mode 100644
index 0000000000..1211d99663
--- /dev/null
+++ b/tools/ruledit/edit_gov.h
@@ -0,0 +1,62 @@
+/***********************************************************************
+ 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__EDIT_GOV_H
+#define FC__EDIT_GOV_H
+
+#ifdef HAVE_CONFIG_H
+#include <fc_config.h>
+#endif
+
+// Qt
+#include <QDialog>
+
+// ruledit
+#include "values_dlg.h"
+
+class QGridLayout;
+class QLineEdit;
+
+class ruledit_gui;
+
+class edit_gov : public values_dlg
+{
+  Q_OBJECT
+
+  public:
+    explicit edit_gov(ruledit_gui *ui_in, struct government *gov_in);
+    void refresh();
+
+  private:
+    ruledit_gui *ui;
+    struct government *gov;
+    QLineEdit *gfx_tag;
+    QLineEdit *gfx_tag_alt;
+    QLineEdit *sound_tag;
+    QLineEdit *sound_tag_alt;
+    QLineEdit *sound_tag_alt2;
+
+  protected:
+    void closeEvent(QCloseEvent *cevent);
+
+  private slots:
+    void gfx_tag_given();
+    void gfx_tag_alt_given();
+    void sound_tag_given();
+    void sound_tag_alt_given();
+    void sound_tag_alt2_given();
+    void helptext();
+};
+
+
+#endif // FC__EDIT_GOV_H
diff --git a/tools/ruledit/tab_gov.cpp b/tools/ruledit/tab_gov.cpp
index 7f5086c6eb..7a0723f744 100644
--- a/tools/ruledit/tab_gov.cpp
+++ b/tools/ruledit/tab_gov.cpp
@@ -33,6 +33,7 @@
 #include "government.h"
 
 // ruledit
+#include "edit_gov.h"
 #include "ruledit.h"
 #include "ruledit_qt.h"
 #include "validity.h"
@@ -47,10 +48,8 @@ tab_gov::tab_gov(ruledit_gui *ui_in) : QWidget()
   QVBoxLayout *main_layout = new QVBoxLayout(this);
   QGridLayout *gov_layout = new QGridLayout();
   QLabel *label;
-  QPushButton *effects_button;
-  QPushButton *add_button;
-  QPushButton *delete_button;
-  QPushButton *reqs_button;
+  QPushButton *button;
+  int row = 0;
 
   ui = ui_in;
   selected = 0;
@@ -67,8 +66,8 @@ tab_gov::tab_gov(ruledit_gui *ui_in) : QWidget()
   rname = new QLineEdit(this);
   rname->setText(R__("None"));
   connect(rname, SIGNAL(returnPressed()), this, SLOT(name_given()));
-  gov_layout->addWidget(label, 0, 0);
-  gov_layout->addWidget(rname, 0, 2);
+  gov_layout->addWidget(label, row, 0);
+  gov_layout->addWidget(rname, row++, 2);
 
   label = new QLabel(QString::fromUtf8(R__("Name")));
   label->setParent(this);
@@ -77,27 +76,31 @@ tab_gov::tab_gov(ruledit_gui *ui_in) : QWidget()
   name = new QLineEdit(this);
   name->setText(R__("None"));
   connect(name, SIGNAL(returnPressed()), this, SLOT(name_given()));
-  gov_layout->addWidget(label, 1, 0);
-  gov_layout->addWidget(same_name, 1, 1);
-  gov_layout->addWidget(name, 1, 2);
+  gov_layout->addWidget(label, row, 0);
+  gov_layout->addWidget(same_name, row, 1);
+  gov_layout->addWidget(name, row++, 2);
 
-  reqs_button = new QPushButton(QString::fromUtf8(R__("Requirements")), this);
-  connect(reqs_button, SIGNAL(pressed()), this, SLOT(edit_reqs()));
-  gov_layout->addWidget(reqs_button, 2, 2);
+  button = new QPushButton(QString::fromUtf8(R__("Edit Values")), this);
+  connect(button, SIGNAL(pressed()), this, SLOT(edit_now()));
+  gov_layout->addWidget(button, row++, 2);
 
-  effects_button = new QPushButton(QString::fromUtf8(R__("Effects")), this);
-  connect(effects_button, SIGNAL(pressed()), this, SLOT(edit_effects()));
-  gov_layout->addWidget(effects_button, 3, 2);
+  button = new QPushButton(QString::fromUtf8(R__("Requirements")), this);
+  connect(button, SIGNAL(pressed()), this, SLOT(edit_reqs()));
+  gov_layout->addWidget(button, row++, 2);
 
-  add_button = new QPushButton(QString::fromUtf8(R__("Add Government")), this);
-  connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
-  gov_layout->addWidget(add_button, 4, 0);
-  show_experimental(add_button);
+  button = new QPushButton(QString::fromUtf8(R__("Effects")), this);
+  connect(button, SIGNAL(pressed()), this, SLOT(edit_effects()));
+  gov_layout->addWidget(button, row++, 2);
 
-  delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Government")), this);
-  connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
-  gov_layout->addWidget(delete_button, 4, 2);
-  show_experimental(delete_button);
+  button = new QPushButton(QString::fromUtf8(R__("Add Government")), this);
+  connect(button, SIGNAL(pressed()), this, SLOT(add_now()));
+  gov_layout->addWidget(button, row, 0);
+  show_experimental(button);
+
+  button = new QPushButton(QString::fromUtf8(R__("Remove this Government")), this);
+  connect(button, SIGNAL(pressed()), this, SLOT(delete_now()));
+  gov_layout->addWidget(button, row++, 2);
+  show_experimental(button);
 
   refresh();
   update_gov_info(nullptr);
@@ -213,11 +216,32 @@ void tab_gov::delete_now()
 
     selected->ruledit_disabled = true;
 
+    if (selected->ruledit_dlg != nullptr) {
+      ((edit_gov *)selected->ruledit_dlg)->done(0);
+    }
+
     refresh();
     update_gov_info(nullptr);
   }
 }
 
+/**********************************************************************//**
+  User requested government edit dialog
+**************************************************************************/
+void tab_gov::edit_now()
+{
+  if (selected != nullptr) {
+    if (selected->ruledit_dlg == nullptr) {
+      edit_gov *edit = new edit_gov(ui, selected);
+
+      edit->show();
+      selected->ruledit_dlg = edit;
+    } else {
+      ((edit_gov *)selected->ruledit_dlg)->raise();
+    }
+  }
+}
+
 /**********************************************************************//**
   Initialize new government for use.
 **************************************************************************/
diff --git a/tools/ruledit/tab_gov.h b/tools/ruledit/tab_gov.h
index 9875a230f5..e63192119f 100644
--- a/tools/ruledit/tab_gov.h
+++ b/tools/ruledit/tab_gov.h
@@ -52,6 +52,7 @@ class tab_gov : public QWidget
     void select_gov();
     void add_now();
     void delete_now();
+    void edit_now();
     void same_name_toggle(bool checked);
     void edit_reqs();
     void edit_effects();
diff --git a/tools/ruledit/tab_unit.cpp b/tools/ruledit/tab_unit.cpp
index 57fc3d8137..ff059d9af2 100644
--- a/tools/ruledit/tab_unit.cpp
+++ b/tools/ruledit/tab_unit.cpp
@@ -49,7 +49,7 @@ tab_unit::tab_unit(ruledit_gui *ui_in) : QWidget()
   QGridLayout *unit_layout = new QGridLayout();
   QLabel *label;
   QPushButton *button;
-  int but_row = 0;
+  int row = 0;
 
   ui = ui_in;
   selected = 0;
@@ -66,8 +66,8 @@ tab_unit::tab_unit(ruledit_gui *ui_in) : QWidget()
   rname = new QLineEdit(this);
   rname->setText(R__("None"));
   connect(rname, SIGNAL(returnPressed()), this, SLOT(name_given()));
-  unit_layout->addWidget(label, but_row, 0);
-  unit_layout->addWidget(rname, but_row++, 2);
+  unit_layout->addWidget(label, row, 0);
+  unit_layout->addWidget(rname, row++, 2);
 
   label = new QLabel(QString::fromUtf8(R__("Name")));
   label->setParent(this);
@@ -76,30 +76,30 @@ tab_unit::tab_unit(ruledit_gui *ui_in) : QWidget()
   name = new QLineEdit(this);
   name->setText(R__("None"));
   connect(name, SIGNAL(returnPressed()), this, SLOT(name_given()));
-  unit_layout->addWidget(label, but_row, 0);
-  unit_layout->addWidget(same_name, but_row, 1);
-  unit_layout->addWidget(name, but_row++, 2);
+  unit_layout->addWidget(label, row, 0);
+  unit_layout->addWidget(same_name, row, 1);
+  unit_layout->addWidget(name, row++, 2);
 
   button = new QPushButton(QString::fromUtf8(R__("Edit Values")), this);
   connect(button, SIGNAL(pressed()), this, SLOT(edit_now()));
-  unit_layout->addWidget(button, but_row++, 2);
+  unit_layout->addWidget(button, row++, 2);
 
   button = new QPushButton(QString::fromUtf8(R__("Requirements")), this);
   connect(button, SIGNAL(pressed()), this, SLOT(edit_reqs()));
-  unit_layout->addWidget(button, but_row++, 2);
+  unit_layout->addWidget(button, row++, 2);
 
   button = new QPushButton(QString::fromUtf8(R__("Effects")), this);
   connect(button, SIGNAL(pressed()), this, SLOT(edit_effects()));
-  unit_layout->addWidget(button, but_row++, 2);
+  unit_layout->addWidget(button, row++, 2);
 
   button = new QPushButton(QString::fromUtf8(R__("Add Unit")), this);
   connect(button, SIGNAL(pressed()), this, SLOT(add_now()));
-  unit_layout->addWidget(button, but_row, 0);
+  unit_layout->addWidget(button, row, 0);
   show_experimental(button);
 
   button = new QPushButton(QString::fromUtf8(R__("Remove this Unit")), this);
   connect(button, SIGNAL(pressed()), this, SLOT(delete_now()));
-  unit_layout->addWidget(button, but_row++, 2);
+  unit_layout->addWidget(button, row++, 2);
   show_experimental(button);
 
   refresh();
-- 
2.43.0

