From 15bfb31ea5162414d1065250a89058922e402508 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C5=82awomir=20Lach?= <slawek@lach.art.pl>
Date: Mon, 2 Jan 2023 15:13:04 +0100
Subject: [PATCH] =?UTF-8?q?!OSDN:=20#TICKET:=2045892:=20S=C5=82awomir=20La?=
 =?UTF-8?q?ch=20<slawek@lach.art.pl>?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Implementing ruledit tab for counters

diff --git a/tools/ruledit/tab_counters.cpp b/tools/ruledit/tab_counters.cpp
new file mode 100644
index 0000000000..13a578bf0f
--- /dev/null
+++ b/tools/ruledit/tab_counters.cpp
@@ -0,0 +1,345 @@
+/***********************************************************************
+ 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 <QCheckBox>
+#include <QGridLayout>
+#include <QLineEdit>
+#include <QListWidget>
+#include <QMenu>
+#include <QPushButton>
+#include <QToolButton>
+
+// utility
+#include "fcintl.h"
+#include "log.h"
+
+// common
+#include "counters.h"
+#include "fc_types.h"
+#include "game.h"
+
+// ruledit
+#include "ruledit.h"
+#include "ruledit_qt.h"
+#include "validity.h"
+
+#include "tab_counters.h"
+
+/**********************************************************************//**
+  Setup tab_counter object
+**************************************************************************/
+tab_counter::tab_counter(ruledit_gui *ui_in) : QWidget()
+{
+  QVBoxLayout *main_layout = new QVBoxLayout(this);
+  QGridLayout *counter_layout = new QGridLayout();
+  QLabel *label;
+  QPushButton *add_button;
+  QPushButton *delete_button;
+
+  ui = ui_in;
+  selected = 0;
+
+  counter_list = new QListWidget(this);
+
+  connect(counter_list, SIGNAL(itemSelectionChanged()), this, SLOT(select_counter()));
+  main_layout->addWidget(counter_list);
+
+  counter_layout->setSizeConstraint(QLayout::SetMaximumSize);
+
+  label = new QLabel(QString::fromUtf8(R__("Rule Name")));
+  label->setParent(this);
+  rname = new QLineEdit(this);
+  rname->setText(R__("None"));
+  connect(rname, SIGNAL(returnPressed()), this, SLOT(name_given()));
+  counter_layout->addWidget(label, 0, 0);
+  counter_layout->addWidget(rname, 0, 1);
+
+  label = new QLabel(QString::fromUtf8(R__("Name")));
+  label->setParent(this);
+
+  name = new QLineEdit(this);
+  name->setText(R__("None"));
+  connect(name, SIGNAL(returnPressed()), this, SLOT(name_given()));
+  counter_layout->addWidget(label, 1, 0);
+  counter_layout->addWidget(name, 1, 1);
+
+
+  add_button = new QPushButton(QString::fromUtf8(R__("Add Counter")), this);
+  connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
+  counter_layout->addWidget(add_button, 6, 0);
+  show_experimental(add_button);
+
+  delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Counter")), this);
+  connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
+  counter_layout->addWidget(delete_button, 6, 1);
+  show_experimental(delete_button);
+
+  label = new QLabel(QString::fromUtf8(R__("Default Value")), this);
+  label->setParent(this);
+  counter_layout->addWidget(label, 8, 0);
+  show_experimental(label);
+
+  def = new QLineEdit(this);
+  def->setValidator(new QRegExpValidator(QRegExp("[0-9]*"), def));
+  counter_layout->addWidget(def, 8, 1);
+  show_experimental(def);
+
+  label = new QLabel(QString::fromUtf8(R__("Checkpoint")), this);
+  label->setParent(this);
+  counter_layout->addWidget(label, 9, 0);
+  show_experimental(label);
+
+  checkpoint = new QLineEdit(this);
+  checkpoint->setValidator(new QRegExpValidator(QRegExp("[0-9]*"), checkpoint));
+  counter_layout->addWidget(checkpoint, 9, 1);
+  show_experimental(checkpoint);
+
+  {
+    QVariant CITY_OWNED_COUNTER(CB_CITY_OWNED_TURNS);
+    QVariant CB_CITY_CELEBRATION_COUNTER(CB_CITY_CELEBRATION_TURNS);
+    type = new QComboBox(this);
+
+    label = new QLabel(QString::fromUtf8(R__("Checkpoint")));
+    label->setParent(this);
+    counter_layout->addWidget(label, 7, 0);
+    show_experimental(label);
+
+    type->addItem(counter_behaviour_name(CB_CITY_OWNED_TURNS), CITY_OWNED_COUNTER);
+    type->addItem(counter_behaviour_name(CB_CITY_CELEBRATION_TURNS), CB_CITY_CELEBRATION_COUNTER);
+
+    connect(type, SIGNAL(activated), this, SLOT(counter_behaviour_selected));
+
+    counter_layout->addWidget(type, 7, 1);
+    show_experimental(type);
+  }
+  refresh();
+  update_counter_info(nullptr);
+
+  main_layout->addLayout(counter_layout);
+
+  setLayout(main_layout);
+}
+
+/**********************************************************************//**
+  Called when counter behaviour is set by user
+**************************************************************************/
+void tab_counter::counter_behaviour_selected(int item)
+{
+  if (nullptr == selected) {
+
+    return;
+  }
+  selected->type = (enum counter_behaviour) item;
+
+  update_counter_info(selected);
+  refresh();
+}
+
+/**********************************************************************//**
+  Refresh the information.
+**************************************************************************/
+void tab_counter::refresh()
+{
+  counter_list->clear();
+
+  city_counters_iterate(pcount) {
+      QListWidgetItem *item = new QListWidgetItem(counter_rule_name(pcount));
+
+      counter_list->insertItem(counter_index(pcount), item);
+  } city_counters_iterate_end;
+}
+
+/**********************************************************************//**
+  Display name of the counter
+**************************************************************************/
+QString tab_counter::counter_name(struct counter *pcounter)
+{
+
+  return QString::fromUtf8(counter_rule_name(pcounter));
+}
+
+/**********************************************************************//**
+  Update info of the counter
+**************************************************************************/
+void tab_counter::update_counter_info(struct counter *counter)
+{
+  selected = counter;
+
+  if (selected != nullptr) {
+    QString dispn = QString::fromUtf8(untranslated_name(&(counter->name)));
+    QString rulen = QString::fromUtf8(rule_name_get(&(counter->name)));
+
+    name->setText(dispn);
+    rname->setText(rulen);
+
+    if (dispn == rulen) {
+      name->setEnabled(false);
+    } else {
+      name->setEnabled(true);
+    }
+
+  } else {
+    name->setText(R__("None"));
+    rname->setText(R__("None"));
+    name->setEnabled(false);
+  }
+}
+
+/**********************************************************************//**
+  User selected counter from the list.
+**************************************************************************/
+void tab_counter::select_counter()
+{
+  char *cname;
+  QList<QListWidgetItem *> select_list = counter_list->selectedItems();
+
+  if (!select_list.isEmpty()) {
+    QByteArray tn_bytes;
+
+    tn_bytes = select_list.at(0)->text().toUtf8();
+
+    cname = tn_bytes.data();
+    update_counter_info(counter_by_rule_name(cname));
+  }
+}
+
+
+/**********************************************************************//**
+  User entered name for counter
+**************************************************************************/
+void tab_counter::name_given()
+{
+  if (selected != nullptr) {
+    QByteArray name_bytes;
+    QByteArray rname_bytes;
+
+
+    city_counters_iterate(pcounter) {
+      if (pcounter != selected) {
+        rname_bytes = rname->text().toUtf8();
+        if (!strcmp(counter_rule_name(pcounter), rname_bytes.data())) {
+          ui->display_msg(R__("A counter with that rule name already exists!"));
+          return;
+        }
+      }
+    } city_counters_iterate_end;
+
+    name_bytes = name->text().toUtf8();
+    rname_bytes = rname->text().toUtf8();
+    names_set(&(selected->name), 0,
+              name_bytes.data(),
+              rname_bytes.data());
+    refresh();
+  }
+}
+
+/**********************************************************************//**
+  User requested counter deletion
+**************************************************************************/
+void tab_counter::delete_now()
+{
+  if (selected != 0) {
+
+    initialize_new_counter(selected);
+    selected->type = COUNTER_BEHAVIOUR_LAST;
+
+    selected->ruledit_disabled = TRUE;
+
+    refresh();
+    update_counter_info(nullptr);
+  }
+}
+
+/**********************************************************************//**
+  Initialize new counter for use.
+**************************************************************************/
+bool tab_counter::initialize_new_counter(struct counter *counter)
+{
+  counter->checkpoint = 0;
+  counter->def = 0;
+  counter->target = CTGT_CITY;
+  counter->type = CB_CITY_OWNED_TURNS;
+  counter->index = counters_get_city_counters_count() - 1;
+  counter->ruledit_disabled = FALSE;
+
+  name_set(&counter->name, 0, "New counter");
+
+  return true;
+}
+
+/**********************************************************************//**
+  User requested new counter
+**************************************************************************/
+void tab_counter::add_now()
+{
+   struct counter *new_counter;
+
+  // Try to reuse freed counter slot
+  city_counters_iterate(pcount) {
+    if (pcount->type == COUNTER_BEHAVIOUR_LAST) {
+      if (initialize_new_counter(pcount)) {
+        update_counter_info(pcount);
+        refresh();
+      }
+      return;
+    }
+  } city_counters_iterate_end;
+
+  // Try to add completely new counter
+  if (counters_get_city_counters_count() >= MAX_COUNTERS) {
+    return;
+  }
+
+  // num_counter_types must be big enough to hold new counter or
+  // counter_by_number() fails.
+
+  game.control.num_counters++;
+  new_counter = counter_by_id(counters_get_city_counters_count());
+  if (initialize_new_counter(new_counter)) {
+    update_counter_info(new_counter);
+    attach_city_counter(new_counter);
+    refresh();
+  }
+}
+
+/**********************************************************************//**
+  Toggled whether rule_name and name should be kept identical
+**************************************************************************/
+void tab_counter::same_name_toggle(bool checked)
+{
+  name->setEnabled(!checked);
+  if (checked) {
+    name->setText(rname->text());
+  }
+}
+
+/**********************************************************************//**
+  User wants to edit effects
+**************************************************************************/
+void tab_counter::edit_effects()
+{
+  if (selected != nullptr) {
+    struct universal uni;
+
+    uni.value.counter = selected;
+    uni.kind = VUT_COUNTER;
+
+    ui->open_effect_edit(QString::fromUtf8(counter_rule_name(selected)),
+                         &uni, EFMC_NORMAL);
+  }
+}
diff --git a/tools/ruledit/tab_counters.h b/tools/ruledit/tab_counters.h
index 6e96ed3d06..9703cad6aa 100644
--- a/tools/ruledit/tab_counters.h
+++ b/tools/ruledit/tab_counters.h
@@ -20,6 +20,7 @@
 
 // Qt
 #include <QWidget>
+#include <QComboBox>
 
 // common
 #include "counters.h"
@@ -49,6 +50,9 @@ class tab_counter : public QWidget
 
     QLineEdit *name;
     QLineEdit *rname;
+    QLineEdit *checkpoint;
+    QLineEdit *def;
+    QComboBox *type;
 
     QListWidget *counter_list;
 
@@ -63,6 +67,7 @@ class tab_counter : public QWidget
     void same_name_toggle(bool checked);
     void edit_effects();
     bool initialize_new_counter(struct counter *padv);
+    void counter_behaviour_selected(int item);
     //QMenu *prepare_counter_button(QToolButton *button, enum tech_req rn);
 };
 
-- 
2.39.0

