Author: cazfi
Date: Wed May  3 21:19:33 2017
New Revision: 35378

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

See hrm Feature #656692

Added:
    trunk/tools/ruledit/tab_extras.cpp
    trunk/tools/ruledit/tab_extras.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=35378&r1=35377&r2=35378&view=diff
==============================================================================
--- trunk/tools/ruledit/Makefile.am     (original)
+++ trunk/tools/ruledit/Makefile.am     Wed May  3 21:19:33 2017
@@ -29,6 +29,7 @@
             meta_requirers_dlg.cpp \
             meta_ruledit_qt.cpp \
             meta_tab_enablers.cpp \
+           meta_tab_extras.cpp \
             meta_tab_misc.cpp  \
             meta_tab_nation.cpp        \
             meta_tab_tech.cpp  \
@@ -44,6 +45,8 @@
                tab_building.h  \
                tab_enablers.cpp \
                tab_enablers.h  \
+               tab_extras.cpp  \
+               tab_extras.h    \
                tab_good.cpp    \
                tab_good.h      \
                tab_gov.cpp     \

Modified: trunk/tools/ruledit/ruledit_qt.cpp
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/ruledit_qt.cpp?rev=35378&r1=35377&r2=35378&view=diff
==============================================================================
--- trunk/tools/ruledit/ruledit_qt.cpp  (original)
+++ trunk/tools/ruledit/ruledit_qt.cpp  Wed May  3 21:19:33 2017
@@ -43,6 +43,7 @@
 #include "ruledit.h"
 #include "tab_building.h"
 #include "tab_enablers.h"
+#include "tab_extras.h"
 #include "tab_good.h"
 #include "tab_gov.h"
 #include "tab_misc.h"
@@ -168,6 +169,8 @@
   stack->addTab(gov, QString::fromUtf8(R__("Governments")));
   enablers = new tab_enabler(this);
   stack->addTab(enablers, QString::fromUtf8(R__("Enablers")));
+  extras = new tab_extras(this);
+  stack->addTab(extras, QString::fromUtf8(R__("Extras")));
   nation = new tab_nation(this);
   stack->addTab(nation, QString::fromUtf8(R__("Nations")));
 
@@ -213,6 +216,7 @@
     good->refresh();
     gov->refresh();
     enablers->refresh();
+    extras->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=35378&r1=35377&r2=35378&view=diff
==============================================================================
--- trunk/tools/ruledit/ruledit_qt.h    (original)
+++ trunk/tools/ruledit/ruledit_qt.h    Wed May  3 21:19:33 2017
@@ -36,6 +36,7 @@
 class tab_unit;
 class tab_nation;
 class tab_enabler;
+class tab_extras;
 
 class ruledit_main : public QMainWindow
 {
@@ -80,6 +81,7 @@
     tab_good *good;
     tab_gov *gov;
     tab_enabler *enablers;
+    tab_extras *extras;
     tab_nation *nation;
 
   private slots:

Added: trunk/tools/ruledit/tab_extras.cpp
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/tab_extras.cpp?rev=35378&view=auto
==============================================================================
--- trunk/tools/ruledit/tab_extras.cpp  (added)
+++ trunk/tools/ruledit/tab_extras.cpp  Wed May  3 21:19:33 2017
@@ -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 "extras.h"
+
+// ruledit
+#include "req_edit.h"
+#include "ruledit.h"
+#include "ruledit_qt.h"
+#include "validity.h"
+
+#include "tab_extras.h"
+
+/**************************************************************************
+  Setup tab_extras object
+**************************************************************************/
+tab_extras::tab_extras(ruledit_gui *ui_in) : QWidget()
+{
+  QVBoxLayout *main_layout = new QVBoxLayout(this);
+  QGridLayout *extra_layout = new QGridLayout();
+  QLabel *label;
+  QPushButton *add_button;
+  QPushButton *delete_button;
+  QPushButton *reqs_button;
+
+  ui = ui_in;
+  selected = 0;
+
+  extra_list = new QListWidget(this);
+
+  connect(extra_list, SIGNAL(itemSelectionChanged()), this, 
SLOT(select_extra()));
+  main_layout->addWidget(extra_list);
+
+  extra_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()));
+  extra_layout->addWidget(label, 0, 0);
+  extra_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()));
+  extra_layout->addWidget(label, 1, 0);
+  extra_layout->addWidget(same_name, 1, 1);
+  extra_layout->addWidget(name, 1, 2);
+
+  reqs_button = new QPushButton(QString::fromUtf8(R__("Requirements")), this);
+  connect(reqs_button, SIGNAL(pressed()), this, SLOT(edit_reqs()));
+  extra_layout->addWidget(reqs_button, 2, 2);
+  show_experimental(reqs_button);
+
+  add_button = new QPushButton(QString::fromUtf8(R__("Add Extra")), this);
+  connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
+  extra_layout->addWidget(add_button, 5, 0);
+  show_experimental(add_button);
+
+  delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Extra")), 
this);
+  connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
+  extra_layout->addWidget(delete_button, 5, 2);
+  show_experimental(delete_button);
+
+  refresh();
+
+  main_layout->addLayout(extra_layout);
+
+  setLayout(main_layout);
+}
+
+/**************************************************************************
+  Refresh the information.
+**************************************************************************/
+void tab_extras::refresh()
+{
+  extra_list->clear();
+
+  extra_type_iterate(pextra) {
+    if (!pextra->disabled) {
+      QListWidgetItem *item =
+        new QListWidgetItem(QString::fromUtf8(extra_rule_name(pextra)));
+
+      extra_list->insertItem(extra_index(pextra), item);
+    }
+  } extra_type_iterate_end;
+}
+
+/**************************************************************************
+  Update info of the extra
+**************************************************************************/
+void tab_extras::update_extra_info(struct extra_type *pextra)
+{
+  selected = pextra;
+
+  if (selected != nullptr) {
+    QString dispn = QString::fromUtf8(untranslated_name(&(pextra->name)));
+    QString rulen = QString::fromUtf8(extra_rule_name(pextra));
+
+    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 extra from the list.
+**************************************************************************/
+void tab_extras::select_extra()
+{
+  QList<QListWidgetItem *> select_list = extra_list->selectedItems();
+
+  if (!select_list.isEmpty()) {
+    
update_extra_info(extra_type_by_rule_name(select_list.at(0)->text().toUtf8().data()));
+  }
+}
+
+/**************************************************************************
+  User entered name for the extra
+**************************************************************************/
+void tab_extras::name_given()
+{
+  if (selected != nullptr) {
+    extra_type_iterate(pextra) {
+      if (pextra != selected && !pextra->disabled) {
+        if (!strcmp(extra_rule_name(pextra), rname->text().toUtf8().data())) {
+          ui->display_msg(R__("Extra with that rule name already exist!"));
+          return;
+        }
+      }
+    } extra_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 extra deletion 
+**************************************************************************/
+void tab_extras::delete_now()
+{
+  if (selected != nullptr) {
+    requirers_dlg *requirers;
+
+    requirers = ui->create_requirers(extra_rule_name(selected));
+    if (is_extra_needed(selected, &ruledit_qt_display_requirers, requirers)) {
+      return;
+    }
+
+    selected->disabled = true;
+
+    refresh();
+    update_extra_info(nullptr);
+  }
+}
+
+/**************************************************************************
+  Initialize new extra for use.
+**************************************************************************/
+bool tab_extras::initialize_new_extra(struct extra_type *pextra)
+{
+  if (extra_type_by_rule_name("New Extra") != nullptr) {
+    return false;
+  }
+
+  name_set(&(pextra->name), 0, "New Extra");
+
+  return true;
+}
+
+/**************************************************************************
+  User requested new extra
+**************************************************************************/
+void tab_extras::add_now()
+{
+  struct extra_type *new_extra;
+
+  // Try to reuse freed extra slot
+  extra_type_iterate(pextra) {
+    if (pextra->disabled) {
+      if (initialize_new_extra(pextra)) {
+        pextra->disabled = false;
+        update_extra_info(pextra);
+        refresh();
+      }
+      return;
+    }
+  } extra_type_iterate_end;
+
+  // Try to add completely new extra
+  if (game.control.num_extra_types >= MAX_EXTRA_TYPES) {
+    return;
+  }
+
+  // num_extra_types must be big enough to hold new extra or
+  // extra_by_number() fails.
+  game.control.num_extra_types++;
+  new_extra = extra_by_number(game.control.num_extra_types - 1);
+  if (initialize_new_extra(new_extra)) {
+    update_extra_info(new_extra);
+
+    refresh();
+  } else {
+    game.control.num_extra_types--; // Restore
+  }
+}
+
+/**************************************************************************
+  Toggled whether rule_name and name should be kept identical
+**************************************************************************/
+void tab_extras::same_name_toggle(bool checked)
+{
+  name->setEnabled(!checked);
+  if (checked) {
+    name->setText(rname->text());
+  }
+}
+
+/**************************************************************************
+  User wants to edit reqs
+**************************************************************************/
+void tab_extras::edit_reqs()
+{
+  if (selected != nullptr) {
+    req_edit *redit = new req_edit(ui, 
QString::fromUtf8(extra_rule_name(selected)),
+                                   &selected->reqs);
+
+    redit->show();
+  }
+}

Added: trunk/tools/ruledit/tab_extras.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/tab_extras.h?rev=35378&view=auto
==============================================================================
--- trunk/tools/ruledit/tab_extras.h    (added)
+++ trunk/tools/ruledit/tab_extras.h    Wed May  3 21:19:33 2017
@@ -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_EXTRAS_H
+#define FC__TAB_EXTRAS_H
+
+#ifdef HAVE_CONFIG_H
+#include <fc_config.h>
+#endif
+
+// Qt
+#include <QWidget>
+
+class QLineEdit;
+class QListWidget;
+class QRadioButton;
+
+class ruledit_gui;
+
+class tab_extras : public QWidget
+{
+  Q_OBJECT
+
+  public:
+    explicit tab_extras(ruledit_gui *ui_in);
+    void refresh();
+
+  private:
+    ruledit_gui *ui;
+    void update_extra_info(struct extra_type *pextra);
+    bool initialize_new_extra(struct extra_type *pextra);
+
+    QLineEdit *name;
+    QLineEdit *rname;
+    QListWidget *extra_list;
+    QRadioButton *same_name;
+
+    struct extra_type *selected;
+
+  private slots:
+    void name_given();
+    void select_extra();
+    void add_now();
+    void delete_now();
+    void same_name_toggle(bool checked);
+    void edit_reqs();
+};
+
+
+#endif // FC__TAB_EXTRAS_H

Modified: trunk/tools/ruledit/validity.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruledit/validity.c?rev=35378&r1=35377&r2=35378&view=diff
==============================================================================
--- trunk/tools/ruledit/validity.c      (original)
+++ trunk/tools/ruledit/validity.c      Wed May  3 21:19:33 2017
@@ -238,6 +238,20 @@
 }
 
 /**************************************************************************
+  Check if anything in ruleset needs extra type
+**************************************************************************/
+bool is_extra_needed(struct extra_type *pextra, requirers_cb cb,
+                     void *data)
+{
+  struct universal uni = { .value.extra = pextra, .kind = VUT_EXTRA };
+  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=35378&r1=35377&r2=35378&view=diff
==============================================================================
--- trunk/tools/ruledit/validity.h      (original)
+++ trunk/tools/ruledit/validity.h      Wed May  3 21:19:33 2017
@@ -24,6 +24,7 @@
 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_government_needed(struct government *pgov, requirers_cb cb, void 
*data);
+bool is_extra_needed(struct extra_type *pextra, 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