From 373d1a161b3f4e15ec115f47148e49e89f990d9b Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sat, 2 Nov 2024 07:37:33 +0200
Subject: [PATCH 55/55] Ruledit: Add enabler comment editing

See RM #1043

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 tools/ruledit/tab_enablers.cpp | 54 ++++++++++++++++++++++++++++++----
 tools/ruledit/tab_enablers.h   |  2 ++
 2 files changed, 50 insertions(+), 6 deletions(-)

diff --git a/tools/ruledit/tab_enablers.cpp b/tools/ruledit/tab_enablers.cpp
index 44a1d2dba5..10cf2955f4 100644
--- a/tools/ruledit/tab_enablers.cpp
+++ b/tools/ruledit/tab_enablers.cpp
@@ -96,6 +96,8 @@ tab_enabler::tab_enabler(ruledit_gui *ui_in) : QWidget()
   QGridLayout *enabler_layout = new QGridLayout();
   QLabel *label;
   QPushButton *add_button;
+  QLabel *lbl;
+  int row = 0;
 
   ui = ui_in;
   connect(ui, SIGNAL(req_vec_may_have_changed(const requirement_vector *)),
@@ -131,14 +133,14 @@ tab_enabler::tab_enabler(ruledit_gui *ui_in) : QWidget()
   type_button->setText(R__("None"));
 
   type_button->setEnabled(false);
-  enabler_layout->addWidget(type_button, 0, 2);
+  enabler_layout->addWidget(type_button, row++, 2);
 
   act_reqs_button
       = new QPushButton(QString::fromUtf8(R__("Actor Requirements")), this);
   connect(act_reqs_button, SIGNAL(pressed()),
           this, SLOT(edit_actor_reqs()));
   act_reqs_button->setEnabled(false);
-  enabler_layout->addWidget(act_reqs_button, 1, 2);
+  enabler_layout->addWidget(act_reqs_button, row++, 2);
 
   tgt_reqs_button
       = new QPushButton(QString::fromUtf8(R__("Target Requirements")),
@@ -146,24 +148,30 @@ tab_enabler::tab_enabler(ruledit_gui *ui_in) : QWidget()
   connect(tgt_reqs_button, SIGNAL(pressed()),
           this, SLOT(edit_target_reqs()));
   tgt_reqs_button->setEnabled(false);
-  enabler_layout->addWidget(tgt_reqs_button, 2, 2);
+  enabler_layout->addWidget(tgt_reqs_button, row++, 2);
+
+  lbl = new QLabel(QString::fromUtf8(R__("Comment")));
+  enabler_layout->addWidget(lbl, row, 0);
+  comment = new QLineEdit(this);
+  connect(comment, SIGNAL(returnPressed()), this, SLOT(comment_given()));
+  enabler_layout->addWidget(comment, row++, 1);
 
   add_button = new QPushButton(QString::fromUtf8(R__("Add Enabler")), this);
   connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
-  enabler_layout->addWidget(add_button, 3, 0);
+  enabler_layout->addWidget(add_button, row, 0);
   show_experimental(add_button);
 
   delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Enabler")), this);
   connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
   delete_button->setEnabled(false);
-  enabler_layout->addWidget(delete_button, 3, 2);
+  enabler_layout->addWidget(delete_button, row, 2);
   show_experimental(delete_button);
 
   repair_button = new QPushButton(this);
   connect(repair_button, SIGNAL(pressed()), this, SLOT(repair_now()));
   repair_button->setEnabled(false);
   repair_button->setText(QString::fromUtf8(R__("Enabler Issues")));
-  enabler_layout->addWidget(repair_button, 3, 1);
+  enabler_layout->addWidget(repair_button, row++, 1);
 
   refresh();
 
@@ -218,6 +226,12 @@ void tab_enabler::update_enabler_info(struct action_enabler *enabler)
     act_reqs_button->setEnabled(true);
     tgt_reqs_button->setEnabled(true);
 
+    if (selected->rulesave.comment != nullptr) {
+      comment->setText(selected->rulesave.comment);
+    } else {
+      comment->setText("");
+    }
+
     delete_button->setEnabled(true);
 
     switch (enabler_problem_level(selected)) {
@@ -247,6 +261,8 @@ void tab_enabler::update_enabler_info(struct action_enabler *enabler)
     repair_button->setEnabled(false);
     repair_button->setText(QString::fromUtf8(R__("Enabler Issues")));
 
+    comment->setText("");
+
     delete_button->setEnabled(false);
   }
 
@@ -269,6 +285,9 @@ void tab_enabler::select_enabler()
 {
   int i = 0;
 
+  // Save previously selected one's comment.
+  comment_given();
+
   action_enablers_iterate(enabler) {
     QListWidgetItem *item = enabler_list->item(i++);
 
@@ -296,6 +315,11 @@ void tab_enabler::delete_now()
 **************************************************************************/
 bool tab_enabler::initialize_new_enabler(struct action_enabler *enabler)
 {
+  if (enabler->rulesave.comment != nullptr) {
+    free(enabler->rulesave.comment);
+    enabler->rulesave.comment = nullptr;
+  }
+
   return true;
 }
 
@@ -402,6 +426,24 @@ void tab_enabler::edit_actor_reqs()
   }
 }
 
+/**********************************************************************//**
+  User entered comment for the enabler
+**************************************************************************/
+void tab_enabler::comment_given()
+{
+  if (selected != nullptr) {
+    if (selected->rulesave.comment != nullptr) {
+      free(selected->rulesave.comment);
+    }
+
+    if (!comment->text().isEmpty()) {
+      selected->rulesave.comment = fc_strdup(comment->text().toUtf8());
+    } else {
+      selected->rulesave.comment = nullptr;
+    }
+  }
+}
+
 /**********************************************************************//**
   Construct fix_enabler_item to help req_vec_fix with the action enabler
   unique stuff.
diff --git a/tools/ruledit/tab_enablers.h b/tools/ruledit/tab_enablers.h
index 04f26ac37b..75ae074988 100644
--- a/tools/ruledit/tab_enablers.h
+++ b/tools/ruledit/tab_enablers.h
@@ -53,6 +53,7 @@ class tab_enabler : public QWidget
     QPushButton *delete_button;
     QPushButton *repair_button;
     QListWidget *enabler_list;
+    QLineEdit *comment;
 
     struct action_enabler *selected;
 
@@ -65,6 +66,7 @@ class tab_enabler : public QWidget
     void edit_type(QAction *action);
     void edit_target_reqs();
     void edit_actor_reqs();
+    void comment_given();
 };
 
 #endif // FC__TAB_ENABLERS_H
-- 
2.45.2

