Author: cazfi
Date: Sat May  6 06:02:30 2017
New Revision: 35414

URL: http://svn.gna.org/viewcvs/freeciv?rev=35414&view=rev
Log:
Add Terrains tab to ruledit

See hrm Feature #657637

Added:
    trunk/tools/ruledit/tab_terrains.cpp
    trunk/tools/ruledit/tab_terrains.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=35414&r1=35413&r2=35414&view=diff
==============================================================================
--- trunk/tools/ruledit/Makefile.am     (original)
+++ trunk/tools/ruledit/Makefile.am     Sat May  6 06:02:30 2017
@@ -33,6 +33,7 @@
             meta_tab_misc.cpp  \
             meta_tab_nation.cpp        \
             meta_tab_tech.cpp  \
+           meta_tab_terrains.cpp \
             meta_tab_building.cpp \
            meta_tab_good.cpp   \
            meta_tab_gov.cpp    \
@@ -57,6 +58,8 @@
                tab_nation.h    \
                 tab_tech.cpp   \
                tab_tech.h      \
+               tab_terrains.cpp \
+               tab_terrains.h  \
                 tab_unit.cpp   \
                tab_unit.h      \
                req_edit.cpp    \

Modified: trunk/tools/ruledit/ruledit_qt.cpp
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/ruledit_qt.cpp?rev=35414&r1=35413&r2=35414&view=diff
==============================================================================
--- trunk/tools/ruledit/ruledit_qt.cpp  (original)
+++ trunk/tools/ruledit/ruledit_qt.cpp  Sat May  6 06:02:30 2017
@@ -49,6 +49,7 @@
 #include "tab_misc.h"
 #include "tab_nation.h"
 #include "tab_tech.h"
+#include "tab_terrains.h"
 #include "tab_unit.h"
 
 #include "ruledit_qt.h"
@@ -171,6 +172,8 @@
   stack->addTab(enablers, QString::fromUtf8(R__("Enablers")));
   extras = new tab_extras(this);
   stack->addTab(extras, QString::fromUtf8(R__("Extras")));
+  terrains = new tab_terrains(this);
+  stack->addTab(terrains, QString::fromUtf8(R__("Terrains")));
   nation = new tab_nation(this);
   stack->addTab(nation, QString::fromUtf8(R__("Nations")));
 
@@ -217,6 +220,7 @@
     gov->refresh();
     enablers->refresh();
     extras->refresh();
+    terrains->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=35414&r1=35413&r2=35414&view=diff
==============================================================================
--- trunk/tools/ruledit/ruledit_qt.h    (original)
+++ trunk/tools/ruledit/ruledit_qt.h    Sat May  6 06:02:30 2017
@@ -37,6 +37,7 @@
 class tab_nation;
 class tab_enabler;
 class tab_extras;
+class tab_terrains;
 
 class ruledit_main : public QMainWindow
 {
@@ -82,6 +83,7 @@
     tab_gov *gov;
     tab_enabler *enablers;
     tab_extras *extras;
+    tab_terrains *terrains;
     tab_nation *nation;
 
   private slots:

Added: trunk/tools/ruledit/tab_terrains.cpp
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/tab_terrains.cpp?rev=35414&view=auto
==============================================================================
--- trunk/tools/ruledit/tab_terrains.cpp        (added)
+++ trunk/tools/ruledit/tab_terrains.cpp        Sat May  6 06:02:30 2017
@@ -0,0 +1,263 @@
+/***********************************************************************
+ 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 "terrain.h"
+
+// ruledit
+#include "req_edit.h"
+#include "ruledit.h"
+#include "ruledit_qt.h"
+#include "validity.h"
+
+#include "tab_terrains.h"
+
+/**************************************************************************
+  Setup tab_terrains object
+**************************************************************************/
+tab_terrains::tab_terrains(ruledit_gui *ui_in) : QWidget()
+{
+  QVBoxLayout *main_layout = new QVBoxLayout(this);
+  QGridLayout *terrains_layout = new QGridLayout();
+  QLabel *label;
+  QPushButton *add_button;
+  QPushButton *delete_button;
+
+  ui = ui_in;
+  selected = 0;
+
+  terrain_list = new QListWidget(this);
+
+  connect(terrain_list, SIGNAL(itemSelectionChanged()), this, 
SLOT(select_terrain()));
+  main_layout->addWidget(terrain_list);
+
+  terrains_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()));
+  terrains_layout->addWidget(label, 0, 0);
+  terrains_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()));
+  terrains_layout->addWidget(label, 1, 0);
+  terrains_layout->addWidget(same_name, 1, 1);
+  terrains_layout->addWidget(name, 1, 2);
+
+  add_button = new QPushButton(QString::fromUtf8(R__("Add Terrain")), this);
+  connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
+  terrains_layout->addWidget(add_button, 2, 0);
+  show_experimental(add_button);
+
+  delete_button = new QPushButton(QString::fromUtf8(R__("Remove this 
Terrain")), this);
+  connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
+  terrains_layout->addWidget(delete_button, 2, 2);
+  show_experimental(delete_button);
+
+  refresh();
+
+  main_layout->addLayout(terrains_layout);
+
+  setLayout(main_layout);
+}
+
+/**************************************************************************
+  Refresh the information.
+**************************************************************************/
+void tab_terrains::refresh()
+{
+  terrain_list->clear();
+
+  terrain_type_iterate(pterr) {
+    if (!pterr->disabled) {
+      QListWidgetItem *item =
+        new QListWidgetItem(QString::fromUtf8(terrain_rule_name(pterr)));
+
+      terrain_list->insertItem(terrain_index(pterr), item);
+    }
+  } terrain_type_iterate_end;
+}
+
+/**************************************************************************
+  Update info of the terrain
+**************************************************************************/
+void tab_terrains::update_terrain_info(struct terrain *pterr)
+{
+  selected = pterr;
+
+  if (selected != nullptr) {
+    QString dispn = QString::fromUtf8(untranslated_name(&(pterr->name)));
+    QString rulen = QString::fromUtf8(terrain_rule_name(pterr));
+
+    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 terrain from the list.
+**************************************************************************/
+void tab_terrains::select_terrain()
+{
+  QList<QListWidgetItem *> select_list = terrain_list->selectedItems();
+
+  if (!select_list.isEmpty()) {
+    
update_terrain_info(terrain_by_rule_name(select_list.at(0)->text().toUtf8().data()));
+  }
+}
+
+/**************************************************************************
+  User entered name for the terrain
+**************************************************************************/
+void tab_terrains::name_given()
+{
+  if (selected != nullptr) {
+    terrain_type_iterate(pterr) {
+      if (pterr != selected && !pterr->disabled) {
+        if (!strcmp(terrain_rule_name(pterr), rname->text().toUtf8().data())) {
+          ui->display_msg(R__("Terrain with that rule name already exist!"));
+          return;
+        }
+      }
+    } terrain_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 terrain deletion 
+**************************************************************************/
+void tab_terrains::delete_now()
+{
+  if (selected != nullptr) {
+    requirers_dlg *requirers;
+
+    requirers = ui->create_requirers(terrain_rule_name(selected));
+    if (is_terrain_needed(selected, &ruledit_qt_display_requirers, requirers)) 
{
+      return;
+    }
+
+    selected->disabled = true;
+
+    refresh();
+    update_terrain_info(nullptr);
+  }
+}
+
+/**************************************************************************
+  Initialize new terrain for use.
+**************************************************************************/
+bool tab_terrains::initialize_new_terrain(struct terrain *pterr)
+{
+  if (terrain_by_rule_name("New Terrain") != nullptr) {
+    return false;
+  }
+
+  name_set(&(pterr->name), 0, "New Terrain");
+
+  return true;
+}
+
+/**************************************************************************
+  User requested new terrain
+**************************************************************************/
+void tab_terrains::add_now()
+{
+  struct terrain *new_terr;
+
+  // Try to reuse freed terrain slot
+  terrain_type_iterate(pterr) {
+    if (pterr->disabled) {
+      if (initialize_new_terrain(pterr)) {
+        pterr->disabled = false;
+        update_terrain_info(pterr);
+        refresh();
+      }
+      return;
+    }
+  } terrain_type_iterate_end;
+
+  // Try to add completely new terrain
+  if (game.control.terrain_count >= MAX_NUM_TERRAINS) {
+    return;
+  }
+
+  // terrain_count must be big enough to hold new extra or
+  // terrain_by_number() fails.
+  game.control.terrain_count++;
+  new_terr = terrain_by_number(game.control.terrain_count - 1);
+  if (initialize_new_terrain(new_terr)) {
+    update_terrain_info(new_terr);
+
+    refresh();
+  } else {
+    game.control.terrain_count--; // Restore
+  }
+}
+
+/**************************************************************************
+  Toggled whether rule_name and name should be kept identical
+**************************************************************************/
+void tab_terrains::same_name_toggle(bool checked)
+{
+  name->setEnabled(!checked);
+  if (checked) {
+    name->setText(rname->text());
+  }
+}

Added: trunk/tools/ruledit/tab_terrains.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/tab_terrains.h?rev=35414&view=auto
==============================================================================
--- trunk/tools/ruledit/tab_terrains.h  (added)
+++ trunk/tools/ruledit/tab_terrains.h  Sat May  6 06:02:30 2017
@@ -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_TERRAINS_H
+#define FC__TAB_TERRAINS_H
+
+#ifdef HAVE_CONFIG_H
+#include <fc_config.h>
+#endif
+
+// Qt
+#include <QWidget>
+
+class QLineEdit;
+class QListWidget;
+class QRadioButton;
+
+class ruledit_gui;
+
+class tab_terrains : public QWidget
+{
+  Q_OBJECT
+
+  public:
+    explicit tab_terrains(ruledit_gui *ui_in);
+    void refresh();
+
+  private:
+    ruledit_gui *ui;
+    void update_terrain_info(struct terrain *pterr);
+    bool initialize_new_terrain(struct terrain *pterr);
+
+    QLineEdit *name;
+    QLineEdit *rname;
+    QListWidget *terrain_list;
+    QRadioButton *same_name;
+
+    struct terrain *selected;
+
+  private slots:
+    void name_given();
+    void select_terrain();
+    void add_now();
+    void delete_now();
+    void same_name_toggle(bool checked);
+};
+
+
+#endif // FC__TAB_TERRAINS_H

Modified: trunk/tools/ruledit/validity.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/validity.c?rev=35414&r1=35413&r2=35414&view=diff
==============================================================================
--- trunk/tools/ruledit/validity.c      (original)
+++ trunk/tools/ruledit/validity.c      Sat May  6 06:02:30 2017
@@ -252,6 +252,19 @@
 }
 
 /**************************************************************************
+  Check if anything in ruleset needs terrain type
+**************************************************************************/
+bool is_terrain_needed(struct terrain *pterr, requirers_cb cb, void *data)
+{
+  struct universal uni = { .value.terrain = pterr, .kind = VUT_TERRAIN };
+  bool needed = FALSE;
+
+  needed |= is_universal_needed(&uni, cb, data);
+
+  return needed;
+}
+
+/**************************************************************************
   Check if anything in ruleset needs government
 **************************************************************************/
 bool is_government_needed(struct government *pgov, requirers_cb cb, void *data)

Modified: trunk/tools/ruledit/validity.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/validity.h?rev=35414&r1=35413&r2=35414&view=diff
==============================================================================
--- trunk/tools/ruledit/validity.h      (original)
+++ trunk/tools/ruledit/validity.h      Sat May  6 06:02:30 2017
@@ -25,6 +25,7 @@
 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);
 bool is_extra_needed(struct extra_type *pextra, requirers_cb cb, void *data);
+bool is_terrain_needed(struct terrain *pterr, 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