Author: cazfi
Date: Fri Jun 24 19:07:20 2016
New Revision: 33015

URL: http://svn.gna.org/viewcvs/freeciv?rev=33015&view=rev
Log:
Added Goods tab for ruledit

See patch #7308

Added:
    trunk/tools/ruledit/tab_good.cpp
    trunk/tools/ruledit/tab_good.h
Modified:
    trunk/common/improvement.h
    trunk/common/traderoutes.c
    trunk/common/traderoutes.h
    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/common/improvement.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/improvement.h?rev=33015&r1=33014&r2=33015&view=diff
==============================================================================
--- trunk/common/improvement.h  (original)
+++ trunk/common/improvement.h  Fri Jun 24 19:07:20 2016
@@ -68,7 +68,7 @@
 struct impr_type {
   Impr_type_id item_number;
   struct name_translation name;
-  bool disabled;                        /* Does not really exist - hole in 
improvments array */
+  bool disabled;                        /* Does not really exist - hole in 
improvements array */
   char graphic_str[MAX_LEN_NAME];      /* city icon of improv. */
   char graphic_alt[MAX_LEN_NAME];      /* city icon of improv. */
   struct requirement_vector reqs;

Modified: trunk/common/traderoutes.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/traderoutes.c?rev=33015&r1=33014&r2=33015&view=diff
==============================================================================
--- trunk/common/traderoutes.c  (original)
+++ trunk/common/traderoutes.c  Fri Jun 24 19:07:20 2016
@@ -410,6 +410,7 @@
     goods[i].id = i;
 
     requirement_vector_init(&(goods[i].reqs));
+    goods[i].disabled = FALSE;
   }
 }
 

Modified: trunk/common/traderoutes.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/traderoutes.h?rev=33015&r1=33014&r2=33015&view=diff
==============================================================================
--- trunk/common/traderoutes.h  (original)
+++ trunk/common/traderoutes.h  Fri Jun 24 19:07:20 2016
@@ -156,6 +156,7 @@
 {
   int id;
   struct name_translation name;
+  bool disabled; /* Does not really exist - hole in goods array */
 
   struct requirement_vector reqs;
 };

Modified: trunk/tools/ruledit/Makefile.am
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/Makefile.am?rev=33015&r1=33014&r2=33015&view=diff
==============================================================================
--- trunk/tools/ruledit/Makefile.am     (original)
+++ trunk/tools/ruledit/Makefile.am     Fri Jun 24 19:07:20 2016
@@ -31,6 +31,7 @@
             meta_tab_nation.cpp        \
             meta_tab_tech.cpp  \
             meta_tab_building.cpp \
+           meta_tab_good.cpp   \
             meta_tab_unit.cpp
 
 freeciv_ruledit_SOURCES =      \
@@ -38,6 +39,8 @@
                edit_utype.h    \
                tab_building.cpp \
                tab_building.h  \
+               tab_good.cpp    \
+               tab_good.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=33015&r1=33014&r2=33015&view=diff
==============================================================================
--- trunk/tools/ruledit/ruledit_qt.cpp  (original)
+++ trunk/tools/ruledit/ruledit_qt.cpp  Fri Jun 24 19:07:20 2016
@@ -1,4 +1,4 @@
-/********************************************************************** 
+/***********************************************************************
  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
@@ -42,6 +42,7 @@
 #include "requirers_dlg.h"
 #include "ruledit.h"
 #include "tab_building.h"
+#include "tab_good.h"
 #include "tab_misc.h"
 #include "tab_nation.h"
 #include "tab_tech.h"
@@ -172,6 +173,8 @@
   stack->addTab(bldg, QString::fromUtf8(R__("Buildings")));
   unit = new tab_unit(this);
   stack->addTab(unit, QString::fromUtf8(R__("Units")));
+  good = new tab_good(this);
+  stack->addTab(good, QString::fromUtf8(R__("Goods")));
   nation = new tab_nation(this);
   stack->addTab(nation, QString::fromUtf8(R__("Nations")));
 
@@ -214,6 +217,7 @@
     nation->refresh();
     tech->refresh();
     unit->refresh();
+    good->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=33015&r1=33014&r2=33015&view=diff
==============================================================================
--- trunk/tools/ruledit/ruledit_qt.h    (original)
+++ trunk/tools/ruledit/ruledit_qt.h    Fri Jun 24 19:07:20 2016
@@ -1,4 +1,4 @@
-/********************************************************************** 
+/*********************************************************************** 
  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
@@ -29,6 +29,7 @@
 
 class requirers_dlg;
 class tab_building;
+class tab_good;
 class tab_misc;
 class tab_tech;
 class tab_unit;
@@ -74,6 +75,7 @@
     tab_misc *misc;
     tab_tech *tech;
     tab_unit *unit;
+    tab_good *good;
     tab_nation *nation;
 
   private slots:

Added: trunk/tools/ruledit/tab_good.cpp
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/tab_good.cpp?rev=33015&view=auto
==============================================================================
--- trunk/tools/ruledit/tab_good.cpp    (added)
+++ trunk/tools/ruledit/tab_good.cpp    Fri Jun 24 19:07:20 2016
@@ -0,0 +1,261 @@
+/***********************************************************************
+ 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 "improvement.h"
+
+// ruledit
+#include "ruledit.h"
+#include "ruledit_qt.h"
+#include "validity.h"
+
+#include "tab_good.h"
+
+/**************************************************************************
+  Setup tab_good object
+**************************************************************************/
+tab_good::tab_good(ruledit_gui *ui_in) : QWidget()
+{
+  QVBoxLayout *main_layout = new QVBoxLayout(this);
+  QGridLayout *good_layout = new QGridLayout();
+  QLabel *label;
+  QPushButton *add_button;
+  QPushButton *delete_button;
+
+  ui = ui_in;
+  selected = 0;
+
+  good_list = new QListWidget(this);
+
+  connect(good_list, SIGNAL(itemSelectionChanged()), this, 
SLOT(select_good()));
+  main_layout->addWidget(good_list);
+
+  good_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()));
+  good_layout->addWidget(label, 0, 0);
+  good_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()));
+  good_layout->addWidget(label, 1, 0);
+  good_layout->addWidget(same_name, 1, 1);
+  good_layout->addWidget(name, 1, 2);
+
+  add_button = new QPushButton(QString::fromUtf8(R__("Add Good")), this);
+  connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
+  good_layout->addWidget(add_button, 5, 0);
+  show_experimental(add_button);
+
+  delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Good")), 
this);
+  connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
+  good_layout->addWidget(delete_button, 5, 2);
+  show_experimental(delete_button);
+
+  refresh();
+
+  main_layout->addLayout(good_layout);
+
+  setLayout(main_layout);
+}
+
+/**************************************************************************
+  Refresh the information.
+**************************************************************************/
+void tab_good::refresh()
+{
+  good_list->clear();
+
+  goods_type_iterate(pgood) {
+    if (!pgood->disabled) {
+      QListWidgetItem *item = new QListWidgetItem(goods_rule_name(pgood));
+
+      good_list->insertItem(goods_index(pgood), item);
+    }
+  } goods_type_iterate_end;
+}
+
+/**************************************************************************
+  Update info of the good
+**************************************************************************/
+void tab_good::update_good_info(struct goods_type *pgood)
+{
+  selected = pgood;
+
+  if (selected != 0) {
+    QString dispn = QString::fromUtf8(untranslated_name(&(pgood->name)));
+    QString rulen = QString::fromUtf8(goods_rule_name(pgood));
+
+    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 good from the list.
+**************************************************************************/
+void tab_good::select_good()
+{
+  QList<QListWidgetItem *> select_list = good_list->selectedItems();
+
+  if (!select_list.isEmpty()) {
+    
update_good_info(goods_by_rule_name(select_list.at(0)->text().toUtf8().data()));
+  }
+}
+
+/**************************************************************************
+  User entered name for the good
+**************************************************************************/
+void tab_good::name_given()
+{
+  if (selected != nullptr) {
+    goods_type_iterate(pgood) {
+      if (pgood != selected && !pgood->disabled) {
+        if (!strcmp(goods_rule_name(pgood), rname->text().toUtf8().data())) {
+          ui->display_msg(R__("Good with that rule name already exist!"));
+          return;
+        }
+      }
+    } goods_type_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 good deletion 
+**************************************************************************/
+void tab_good::delete_now()
+{
+  if (selected != 0) {
+    requirers_dlg *requirers;
+
+    requirers = ui->create_requirers(goods_rule_name(selected));
+    if (is_good_needed(selected, &ruledit_qt_display_requirers, requirers)) {
+      return;
+    }
+
+    selected->disabled = true;
+
+    refresh();
+    update_good_info(0);
+  }
+}
+
+/**************************************************************************
+  Initialize new good for use.
+**************************************************************************/
+bool tab_good::initialize_new_good(struct goods_type *pgood)
+{
+  if (goods_by_rule_name("New Good") != nullptr) {
+    return false;
+  }
+
+  name_set(&(pgood->name), 0, "New Good");
+
+  return true;
+}
+
+/**************************************************************************
+  User requested new good
+**************************************************************************/
+void tab_good::add_now()
+{
+  struct goods_type *new_good;
+
+  // Try to reuse freed good slot
+  goods_type_iterate(pgood) {
+    if (pgood->disabled) {
+      if (initialize_new_good(pgood)) {
+        pgood->disabled = false;
+        update_good_info(pgood);
+        refresh();
+      }
+      return;
+    }
+  } goods_type_iterate_end;
+
+  // Try to add completely new good
+  if (game.control.num_goods_types >= MAX_GOODS_TYPES) {
+    return;
+  }
+
+  // num_good_types must be big enough to hold new good or
+  // good_by_number() fails.
+  game.control.num_goods_types++;
+  new_good = goods_by_number(game.control.num_goods_types - 1);
+  if (initialize_new_good(new_good)) {
+    update_good_info(new_good);
+
+    refresh();
+  } else {
+    game.control.num_goods_types--; // Restore
+  }
+}
+
+/**************************************************************************
+  Toggled whether rule_name and name should be kept identical
+**************************************************************************/
+void tab_good::same_name_toggle(bool checked)
+{
+  name->setEnabled(!checked);
+  if (checked) {
+    name->setText(rname->text());
+  }
+}

Added: trunk/tools/ruledit/tab_good.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/tab_good.h?rev=33015&view=auto
==============================================================================
--- trunk/tools/ruledit/tab_good.h      (added)
+++ trunk/tools/ruledit/tab_good.h      Fri Jun 24 19:07:20 2016
@@ -0,0 +1,59 @@
+/***********************************************************************
+ 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_GOOD_H
+#define FC__TAB_GOOD_H
+
+#ifdef HAVE_CONFIG_H
+#include <fc_config.h>
+#endif
+
+// Qt
+#include <QWidget>
+
+class QLineEdit;
+class QListWidget;
+class QRadioButton;
+
+class ruledit_gui;
+
+class tab_good : public QWidget
+{
+  Q_OBJECT
+
+  public:
+    explicit tab_good(ruledit_gui *ui_in);
+    void refresh();
+
+  private:
+    ruledit_gui *ui;
+    void update_good_info(struct goods_type *pgood);
+    bool initialize_new_good(struct goods_type *pgood);
+
+    QLineEdit *name;
+    QLineEdit *rname;
+    QListWidget *good_list;
+    QRadioButton *same_name;
+
+    struct goods_type *selected;
+
+  private slots:
+    void name_given();
+    void select_good();
+    void add_now();
+    void delete_now();
+    void same_name_toggle(bool checked);
+};
+
+
+#endif // FC__TAB_GOOD_H

Modified: trunk/tools/ruledit/validity.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/validity.c?rev=33015&r1=33014&r2=33015&view=diff
==============================================================================
--- trunk/tools/ruledit/validity.c      (original)
+++ trunk/tools/ruledit/validity.c      Fri Jun 24 19:07:20 2016
@@ -1,4 +1,4 @@
-/**********************************************************************
+/***********************************************************************
  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
@@ -215,3 +215,17 @@
 
   return needed;
 }
+
+/**************************************************************************
+  Check if anything in ruleset needs unit type
+**************************************************************************/
+bool is_good_needed(struct goods_type *pgood, requirers_cb cb,
+                    void *data)
+{
+  struct universal uni = { .value.good = pgood, .kind = VUT_GOOD };
+  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=33015&r1=33014&r2=33015&view=diff
==============================================================================
--- trunk/tools/ruledit/validity.h      (original)
+++ trunk/tools/ruledit/validity.h      Fri Jun 24 19:07:20 2016
@@ -1,4 +1,4 @@
-/********************************************************************** 
+/***********************************************************************
  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
@@ -22,6 +22,7 @@
 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);
 
 #ifdef __cplusplus
 }


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

Reply via email to