Author: cazfi
Date: Tue Aug 23 08:09:29 2016
New Revision: 33677

URL: http://svn.gna.org/viewcvs/freeciv?rev=33677&view=rev
Log:
Added governments tab to ruledit

See patch #7628

Added:
    trunk/tools/ruledit/tab_gov.cpp
    trunk/tools/ruledit/tab_gov.h
Modified:
    trunk/tools/ruledit/Makefile.am
    trunk/tools/ruledit/ruledit_qt.cpp
    trunk/tools/ruledit/ruledit_qt.h
    trunk/tools/ruledit/validity.c
    trunk/tools/ruledit/validity.h

Modified: trunk/tools/ruledit/Makefile.am
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/Makefile.am?rev=33677&r1=33676&r2=33677&view=diff
==============================================================================
--- trunk/tools/ruledit/Makefile.am     (original)
+++ trunk/tools/ruledit/Makefile.am     Tue Aug 23 08:09:29 2016
@@ -33,6 +33,7 @@
             meta_tab_tech.cpp  \
             meta_tab_building.cpp \
            meta_tab_good.cpp   \
+           meta_tab_gov.cpp    \
             meta_tab_unit.cpp
 
 freeciv_ruledit_SOURCES =      \
@@ -42,6 +43,8 @@
                tab_building.h  \
                tab_good.cpp    \
                tab_good.h      \
+               tab_gov.cpp     \
+               tab_gov.h       \
                tab_misc.cpp    \
                tab_misc.h      \
                tab_nation.cpp  \

Modified: trunk/tools/ruledit/ruledit_qt.cpp
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/ruledit_qt.cpp?rev=33677&r1=33676&r2=33677&view=diff
==============================================================================
--- trunk/tools/ruledit/ruledit_qt.cpp  (original)
+++ trunk/tools/ruledit/ruledit_qt.cpp  Tue Aug 23 08:09:29 2016
@@ -43,6 +43,7 @@
 #include "ruledit.h"
 #include "tab_building.h"
 #include "tab_good.h"
+#include "tab_gov.h"
 #include "tab_misc.h"
 #include "tab_nation.h"
 #include "tab_tech.h"
@@ -162,6 +163,8 @@
   stack->addTab(unit, QString::fromUtf8(R__("Units")));
   good = new tab_good(this);
   stack->addTab(good, QString::fromUtf8(R__("Goods")));
+  gov = new tab_gov(this);
+  stack->addTab(gov, QString::fromUtf8(R__("Governments")));
   nation = new tab_nation(this);
   stack->addTab(nation, QString::fromUtf8(R__("Nations")));
 
@@ -205,6 +208,7 @@
     tech->refresh();
     unit->refresh();
     good->refresh();
+    gov->refresh();
     main_layout->setCurrentIndex(1);
   } else {
     display_msg(R__("Ruleset loading failed!"));

Modified: trunk/tools/ruledit/ruledit_qt.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/ruledit_qt.h?rev=33677&r1=33676&r2=33677&view=diff
==============================================================================
--- trunk/tools/ruledit/ruledit_qt.h    (original)
+++ trunk/tools/ruledit/ruledit_qt.h    Tue Aug 23 08:09:29 2016
@@ -30,6 +30,7 @@
 class requirers_dlg;
 class tab_building;
 class tab_good;
+class tab_gov;
 class tab_misc;
 class tab_tech;
 class tab_unit;
@@ -76,6 +77,7 @@
     tab_tech *tech;
     tab_unit *unit;
     tab_good *good;
+    tab_gov *gov;
     tab_nation *nation;
 
   private slots:

Added: trunk/tools/ruledit/tab_gov.cpp
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/tab_gov.cpp?rev=33677&view=auto
==============================================================================
--- trunk/tools/ruledit/tab_gov.cpp     (added)
+++ trunk/tools/ruledit/tab_gov.cpp     Tue Aug 23 08:09:29 2016
@@ -0,0 +1,282 @@
+/***********************************************************************
+ 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 <QListWidget>
+#include <QMenu>
+#include <QPushButton>
+#include <QRadioButton>
+#include <QToolButton>
+
+// utility
+#include "fcintl.h"
+#include "log.h"
+
+// common
+#include "game.h"
+#include "government.h"
+
+// ruledit
+#include "req_edit.h"
+#include "ruledit.h"
+#include "ruledit_qt.h"
+#include "validity.h"
+
+#include "tab_gov.h"
+
+/**************************************************************************
+  Setup tab_good object
+**************************************************************************/
+tab_gov::tab_gov(ruledit_gui *ui_in) : QWidget()
+{
+  QVBoxLayout *main_layout = new QVBoxLayout(this);
+  QGridLayout *gov_layout = new QGridLayout();
+  QLabel *label;
+  QPushButton *add_button;
+  QPushButton *delete_button;
+  QPushButton *reqs_button;
+
+  ui = ui_in;
+  selected = 0;
+
+  gov_list = new QListWidget(this);
+
+  connect(gov_list, SIGNAL(itemSelectionChanged()), this, SLOT(select_gov()));
+  main_layout->addWidget(gov_list);
+
+  gov_layout->setSizeConstraint(QLayout::SetMaximumSize);
+
+  label = new QLabel(QString::fromUtf8(R__("Rule Name")));
+  label->setParent(this);
+  rname = new QLineEdit(this);
+  rname->setText("None");
+  connect(rname, SIGNAL(returnPressed()), this, SLOT(name_given()));
+  gov_layout->addWidget(label, 0, 0);
+  gov_layout->addWidget(rname, 0, 2);
+
+  label = new QLabel(QString::fromUtf8(R__("Name")));
+  label->setParent(this);
+  same_name = new QRadioButton();
+  connect(same_name, SIGNAL(toggled(bool)), this, 
SLOT(same_name_toggle(bool)));
+  name = new QLineEdit(this);
+  name->setText("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);
+
+  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);
+  show_experimental(reqs_button);
+
+  add_button = new QPushButton(QString::fromUtf8(R__("Add Government")), this);
+  connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
+  gov_layout->addWidget(add_button, 5, 0);
+  show_experimental(add_button);
+
+  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, 5, 2);
+  show_experimental(delete_button);
+
+  refresh();
+
+  main_layout->addLayout(gov_layout);
+
+  setLayout(main_layout);
+}
+
+/**************************************************************************
+  Refresh the information.
+**************************************************************************/
+void tab_gov::refresh()
+{
+  gov_list->clear();
+
+  governments_iterate(pgov) {
+    if (!pgov->disabled) {
+      QListWidgetItem *item =
+        new QListWidgetItem(QString::fromUtf8(government_rule_name(pgov)));
+
+      gov_list->insertItem(government_index(pgov), item);
+    }
+  } governments_iterate_end;
+}
+
+/**************************************************************************
+  Update info of the government
+**************************************************************************/
+void tab_gov::update_gov_info(struct government *pgov)
+{
+  selected = pgov;
+
+  if (selected != 0) {
+    QString dispn = QString::fromUtf8(untranslated_name(&(pgov->name)));
+    QString rulen = QString::fromUtf8(government_rule_name(pgov));
+
+    name->setText(dispn);
+    rname->setText(rulen);
+    if (dispn == rulen) {
+      name->setEnabled(false);
+      same_name->setChecked(true);
+    } else {
+      same_name->setChecked(false);
+      name->setEnabled(true);
+    }
+  } else {
+    name->setText("None");
+    rname->setText("None");
+    same_name->setChecked(true);
+    name->setEnabled(false);
+  }
+}
+
+/**************************************************************************
+  User selected government from the list.
+**************************************************************************/
+void tab_gov::select_gov()
+{
+  QList<QListWidgetItem *> select_list = gov_list->selectedItems();
+
+  if (!select_list.isEmpty()) {
+    
update_gov_info(government_by_rule_name(select_list.at(0)->text().toUtf8().data()));
+  }
+}
+
+/**************************************************************************
+  User entered name for the government
+**************************************************************************/
+void tab_gov::name_given()
+{
+  if (selected != nullptr) {
+    governments_iterate(pgov) {
+      if (pgov != selected && !pgov->disabled) {
+        if (!strcmp(government_rule_name(pgov), 
rname->text().toUtf8().data())) {
+          ui->display_msg(R__("Government with that rule name already 
exist!"));
+          return;
+        }
+      }
+    } governments_iterate_end;
+
+    if (same_name->isChecked()) {
+      name->setText(rname->text());
+    }
+
+    names_set(&(selected->name), 0,
+              name->text().toUtf8().data(),
+              rname->text().toUtf8().data());
+    refresh();
+  }
+}
+
+/**************************************************************************
+  User requested government deletion 
+**************************************************************************/
+void tab_gov::delete_now()
+{
+  if (selected != 0) {
+    requirers_dlg *requirers;
+
+    requirers = ui->create_requirers(government_rule_name(selected));
+    if (is_government_needed(selected, &ruledit_qt_display_requirers, 
requirers)) {
+      return;
+    }
+
+    selected->disabled = true;
+
+    refresh();
+    update_gov_info(0);
+  }
+}
+
+/**************************************************************************
+  Initialize new government for use.
+**************************************************************************/
+bool tab_gov::initialize_new_gov(struct government *pgov)
+{
+  if (government_by_rule_name("New Government") != nullptr) {
+    return false;
+  }
+
+  name_set(&(pgov->name), 0, "New Government");
+
+  return true;
+}
+
+/**************************************************************************
+  User requested new government
+**************************************************************************/
+void tab_gov::add_now()
+{
+  struct government *new_gov;
+
+  // Try to reuse freed government slot
+  governments_iterate(pgov) {
+    if (pgov->disabled) {
+      if (initialize_new_gov(pgov)) {
+        pgov->disabled = false;
+        update_gov_info(pgov);
+        refresh();
+      }
+      return;
+    }
+  } governments_iterate_end;
+
+  // Try to add completely new government
+  if (game.control.num_goods_types >= MAX_GOODS_TYPES) {
+    return;
+  }
+
+  // government_count must be big enough to hold new government or
+  // government_by_number() fails.
+  game.control.government_count++;
+  new_gov = government_by_number(game.control.government_count - 1);
+  if (initialize_new_gov(new_gov)) {
+    update_gov_info(new_gov);
+
+    refresh();
+  } else {
+    game.control.government_count--; // Restore
+  }
+}
+
+/**************************************************************************
+  Toggled whether rule_name and name should be kept identical
+**************************************************************************/
+void tab_gov::same_name_toggle(bool checked)
+{
+  name->setEnabled(!checked);
+  if (checked) {
+    name->setText(rname->text());
+  }
+}
+
+/**************************************************************************
+  User wants to edit reqs
+**************************************************************************/
+void tab_gov::edit_reqs()
+{
+  if (selected != nullptr) {
+    req_edit *redit = new req_edit(ui, 
QString::fromUtf8(government_rule_name(selected)),
+                                   &selected->reqs);
+
+    redit->show();
+  }
+}

Added: trunk/tools/ruledit/tab_gov.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/tab_gov.h?rev=33677&view=auto
==============================================================================
--- trunk/tools/ruledit/tab_gov.h       (added)
+++ trunk/tools/ruledit/tab_gov.h       Tue Aug 23 08:09:29 2016
@@ -0,0 +1,60 @@
+/***********************************************************************
+ 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__TAB_GOV_H
+#define FC__TAB_GOV_H
+
+#ifdef HAVE_CONFIG_H
+#include <fc_config.h>
+#endif
+
+// Qt
+#include <QWidget>
+
+class QLineEdit;
+class QListWidget;
+class QRadioButton;
+
+class ruledit_gui;
+
+class tab_gov : public QWidget
+{
+  Q_OBJECT
+
+  public:
+    explicit tab_gov(ruledit_gui *ui_in);
+    void refresh();
+
+  private:
+    ruledit_gui *ui;
+    void update_gov_info(struct government *pgov);
+    bool initialize_new_gov(struct government *pgov);
+
+    QLineEdit *name;
+    QLineEdit *rname;
+    QListWidget *gov_list;
+    QRadioButton *same_name;
+
+    struct government *selected;
+
+  private slots:
+    void name_given();
+    void select_gov();
+    void add_now();
+    void delete_now();
+    void same_name_toggle(bool checked);
+    void edit_reqs();
+};
+
+
+#endif // FC__TAB_GOV_H

Modified: trunk/tools/ruledit/validity.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/validity.c?rev=33677&r1=33676&r2=33677&view=diff
==============================================================================
--- trunk/tools/ruledit/validity.c      (original)
+++ trunk/tools/ruledit/validity.c      Tue Aug 23 08:09:29 2016
@@ -224,7 +224,7 @@
 }
 
 /**************************************************************************
-  Check if anything in ruleset needs unit type
+  Check if anything in ruleset needs goods type
 **************************************************************************/
 bool is_good_needed(struct goods_type *pgood, requirers_cb cb,
                     void *data)
@@ -236,3 +236,16 @@
 
   return needed;
 }
+
+/**************************************************************************
+  Check if anything in ruleset needs government
+**************************************************************************/
+bool is_government_needed(struct government *pgov, requirers_cb cb, void *data)
+{
+  struct universal uni = { .value.govern = pgov, .kind = VUT_GOVERNMENT };
+  bool needed = FALSE;
+
+  needed |= is_universal_needed(&uni, cb, data);
+
+  return needed;
+}

Modified: trunk/tools/ruledit/validity.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/validity.h?rev=33677&r1=33676&r2=33677&view=diff
==============================================================================
--- trunk/tools/ruledit/validity.h      (original)
+++ trunk/tools/ruledit/validity.h      Tue Aug 23 08:09:29 2016
@@ -22,7 +22,8 @@
 bool is_tech_needed(struct advance *padv, requirers_cb cb, void *data);
 bool is_building_needed(struct impr_type *pimpr, requirers_cb cb, void *data);
 bool is_utype_needed(struct unit_type *ptype, requirers_cb cb, void *data);
-  bool is_good_needed(struct goods_type *pgood, requirers_cb cb, void *data);
+bool is_good_needed(struct goods_type *pgood, requirers_cb cb, void *data);
+bool is_government_needed(struct government *pgov, requirers_cb cb, void 
*data);
 
 #ifdef __cplusplus
 }


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to