Project

Profile

Help

HostedRedmine.com has moved to the Planio platform. All logins and passwords remained the same. All users will be able to login and use Redmine just as before. Read more...

Feature #657637 ยป RuleditTerrainsTab.patch

Marko Lindqvist, 2017-05-04 02:28 PM

View differences:

freeciv/tools/ruledit/Makefile.am 2017-05-04 17:06:41.318978843 +0300
33 33
            meta_tab_misc.cpp	\
34 34
            meta_tab_nation.cpp	\
35 35
            meta_tab_tech.cpp	\
36
	    meta_tab_terrains.cpp \
36 37
            meta_tab_building.cpp \
37 38
	    meta_tab_good.cpp	\
38 39
	    meta_tab_gov.cpp	\
......
57 58
		tab_nation.h	\
58 59
                tab_tech.cpp	\
59 60
		tab_tech.h	\
61
		tab_terrains.cpp \
62
		tab_terrains.h	\
60 63
                tab_unit.cpp	\
61 64
		tab_unit.h	\
62 65
		req_edit.cpp	\
freeciv/tools/ruledit/ruledit_qt.cpp 2017-05-04 17:09:17.849708935 +0300
49 49
#include "tab_misc.h"
50 50
#include "tab_nation.h"
51 51
#include "tab_tech.h"
52
#include "tab_terrains.h"
52 53
#include "tab_unit.h"
53 54

  
54 55
#include "ruledit_qt.h"
......
171 172
  stack->addTab(enablers, QString::fromUtf8(R__("Enablers")));
172 173
  extras = new tab_extras(this);
173 174
  stack->addTab(extras, QString::fromUtf8(R__("Extras")));
175
  terrains = new tab_terrains(this);
176
  stack->addTab(terrains, QString::fromUtf8(R__("Terrains")));
174 177
  nation = new tab_nation(this);
175 178
  stack->addTab(nation, QString::fromUtf8(R__("Nations")));
176 179

  
......
217 220
    gov->refresh();
218 221
    enablers->refresh();
219 222
    extras->refresh();
223
    terrains->refresh();
220 224
    main_layout->setCurrentIndex(1);
221 225
  } else {
222 226
    display_msg(R__("Ruleset loading failed!"));
freeciv/tools/ruledit/ruledit_qt.h 2017-05-04 17:09:37.657548305 +0300
37 37
class tab_nation;
38 38
class tab_enabler;
39 39
class tab_extras;
40
class tab_terrains;
40 41

  
41 42
class ruledit_main : public QMainWindow
42 43
{
......
82 83
    tab_gov *gov;
83 84
    tab_enabler *enablers;
84 85
    tab_extras *extras;
86
    tab_terrains *terrains;
85 87
    tab_nation *nation;
86 88

  
87 89
  private slots:
freeciv/tools/ruledit/tab_terrains.cpp 2017-05-04 17:21:55.815568567 +0300
1
/***********************************************************************
2
 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; either version 2, or (at your option)
6
   any later version.
7

  
8
   This program is distributed in the hope that it will be useful,
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
   GNU General Public License for more details.
12
***********************************************************************/
13

  
14
#ifdef HAVE_CONFIG_H
15
#include <fc_config.h>
16
#endif
17

  
18
// Qt
19
#include <QGridLayout>
20
#include <QLineEdit>
21
#include <QListWidget>
22
#include <QMenu>
23
#include <QPushButton>
24
#include <QRadioButton>
25
#include <QToolButton>
26

  
27
// utility
28
#include "fcintl.h"
29
#include "log.h"
30

  
31
// common
32
#include "game.h"
33
#include "terrain.h"
34

  
35
// ruledit
36
#include "req_edit.h"
37
#include "ruledit.h"
38
#include "ruledit_qt.h"
39
#include "validity.h"
40

  
41
#include "tab_terrains.h"
42

  
43
/**************************************************************************
44
  Setup tab_terrains object
45
**************************************************************************/
46
tab_terrains::tab_terrains(ruledit_gui *ui_in) : QWidget()
47
{
48
  QVBoxLayout *main_layout = new QVBoxLayout(this);
49
  QGridLayout *terrains_layout = new QGridLayout();
50
  QLabel *label;
51
  QPushButton *add_button;
52
  QPushButton *delete_button;
53

  
54
  ui = ui_in;
55
  selected = 0;
56

  
57
  terrain_list = new QListWidget(this);
58

  
59
  connect(terrain_list, SIGNAL(itemSelectionChanged()), this, SLOT(select_terrain()));
60
  main_layout->addWidget(terrain_list);
61

  
62
  terrains_layout->setSizeConstraint(QLayout::SetMaximumSize);
63

  
64
  label = new QLabel(QString::fromUtf8(R__("Rule Name")));
65
  label->setParent(this);
66
  rname = new QLineEdit(this);
67
  rname->setText("None");
68
  connect(rname, SIGNAL(returnPressed()), this, SLOT(name_given()));
69
  terrains_layout->addWidget(label, 0, 0);
70
  terrains_layout->addWidget(rname, 0, 2);
71

  
72
  label = new QLabel(QString::fromUtf8(R__("Name")));
73
  label->setParent(this);
74
  same_name = new QRadioButton();
75
  connect(same_name, SIGNAL(toggled(bool)), this, SLOT(same_name_toggle(bool)));
76
  name = new QLineEdit(this);
77
  name->setText("None");
78
  connect(name, SIGNAL(returnPressed()), this, SLOT(name_given()));
79
  terrains_layout->addWidget(label, 1, 0);
80
  terrains_layout->addWidget(same_name, 1, 1);
81
  terrains_layout->addWidget(name, 1, 2);
82

  
83
  add_button = new QPushButton(QString::fromUtf8(R__("Add Terrain")), this);
84
  connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
85
  terrains_layout->addWidget(add_button, 2, 0);
86
  show_experimental(add_button);
87

  
88
  delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Terrain")), this);
89
  connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
90
  terrains_layout->addWidget(delete_button, 2, 2);
91
  show_experimental(delete_button);
92

  
93
  refresh();
94

  
95
  main_layout->addLayout(terrains_layout);
96

  
97
  setLayout(main_layout);
98
}
99

  
100
/**************************************************************************
101
  Refresh the information.
102
**************************************************************************/
103
void tab_terrains::refresh()
104
{
105
  terrain_list->clear();
106

  
107
  terrain_type_iterate(pterr) {
108
    if (!pterr->disabled) {
109
      QListWidgetItem *item =
110
        new QListWidgetItem(QString::fromUtf8(terrain_rule_name(pterr)));
111

  
112
      terrain_list->insertItem(terrain_index(pterr), item);
113
    }
114
  } terrain_type_iterate_end;
115
}
116

  
117
/**************************************************************************
118
  Update info of the terrain
119
**************************************************************************/
120
void tab_terrains::update_terrain_info(struct terrain *pterr)
121
{
122
  selected = pterr;
123

  
124
  if (selected != nullptr) {
125
    QString dispn = QString::fromUtf8(untranslated_name(&(pterr->name)));
126
    QString rulen = QString::fromUtf8(terrain_rule_name(pterr));
127

  
128
    name->setText(dispn);
129
    rname->setText(rulen);
130
    if (dispn == rulen) {
131
      name->setEnabled(false);
132
      same_name->setChecked(true);
133
    } else {
134
      same_name->setChecked(false);
135
      name->setEnabled(true);
136
    }
137
  } else {
138
    name->setText("None");
139
    rname->setText("None");
140
    same_name->setChecked(true);
141
    name->setEnabled(false);
142
  }
143
}
144

  
145
/**************************************************************************
146
  User selected terrain from the list.
147
**************************************************************************/
148
void tab_terrains::select_terrain()
149
{
150
  QList<QListWidgetItem *> select_list = terrain_list->selectedItems();
151

  
152
  if (!select_list.isEmpty()) {
153
    update_terrain_info(terrain_by_rule_name(select_list.at(0)->text().toUtf8().data()));
154
  }
155
}
156

  
157
/**************************************************************************
158
  User entered name for the terrain
159
**************************************************************************/
160
void tab_terrains::name_given()
161
{
162
  if (selected != nullptr) {
163
    terrain_type_iterate(pterr) {
164
      if (pterr != selected && !pterr->disabled) {
165
        if (!strcmp(terrain_rule_name(pterr), rname->text().toUtf8().data())) {
166
          ui->display_msg(R__("Terrain with that rule name already exist!"));
167
          return;
168
        }
169
      }
170
    } terrain_type_iterate_end;
171

  
172
    if (same_name->isChecked()) {
173
      name->setText(rname->text());
174
    }
175

  
176
    names_set(&(selected->name), 0,
177
              name->text().toUtf8().data(),
178
              rname->text().toUtf8().data());
179
    refresh();
180
  }
181
}
182

  
183
/**************************************************************************
184
  User requested terrain deletion 
185
**************************************************************************/
186
void tab_terrains::delete_now()
187
{
188
  if (selected != nullptr) {
189
    requirers_dlg *requirers;
190

  
191
    requirers = ui->create_requirers(terrain_rule_name(selected));
192
    if (is_terrain_needed(selected, &ruledit_qt_display_requirers, requirers)) {
193
      return;
194
    }
195

  
196
    selected->disabled = true;
197

  
198
    refresh();
199
    update_terrain_info(nullptr);
200
  }
201
}
202

  
203
/**************************************************************************
204
  Initialize new terrain for use.
205
**************************************************************************/
206
bool tab_terrains::initialize_new_terrain(struct terrain *pterr)
207
{
208
  if (terrain_by_rule_name("New Terrain") != nullptr) {
209
    return false;
210
  }
211

  
212
  name_set(&(pterr->name), 0, "New Terrain");
213

  
214
  return true;
215
}
216

  
217
/**************************************************************************
218
  User requested new terrain
219
**************************************************************************/
220
void tab_terrains::add_now()
221
{
222
  struct terrain *new_terr;
223

  
224
  // Try to reuse freed terrain slot
225
  terrain_type_iterate(pterr) {
226
    if (pterr->disabled) {
227
      if (initialize_new_terrain(pterr)) {
228
        pterr->disabled = false;
229
        update_terrain_info(pterr);
230
        refresh();
231
      }
232
      return;
233
    }
234
  } terrain_type_iterate_end;
235

  
236
  // Try to add completely new terrain
237
  if (game.control.terrain_count >= MAX_NUM_TERRAINS) {
238
    return;
239
  }
240

  
241
  // terrain_count must be big enough to hold new extra or
242
  // terrain_by_number() fails.
243
  game.control.terrain_count++;
244
  new_terr = terrain_by_number(game.control.terrain_count - 1);
245
  if (initialize_new_terrain(new_terr)) {
246
    update_terrain_info(new_terr);
247

  
248
    refresh();
249
  } else {
250
    game.control.terrain_count--; // Restore
251
  }
252
}
253

  
254
/**************************************************************************
255
  Toggled whether rule_name and name should be kept identical
256
**************************************************************************/
257
void tab_terrains::same_name_toggle(bool checked)
258
{
259
  name->setEnabled(!checked);
260
  if (checked) {
261
    name->setText(rname->text());
262
  }
263
}
freeciv/tools/ruledit/tab_terrains.h 2017-05-04 17:21:02.967996424 +0300
1
/***********************************************************************
2
 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; either version 2, or (at your option)
6
   any later version.
7

  
8
   This program is distributed in the hope that it will be useful,
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
   GNU General Public License for more details.
12
***********************************************************************/
13

  
14
#ifndef FC__TAB_TERRAINS_H
15
#define FC__TAB_TERRAINS_H
16

  
17
#ifdef HAVE_CONFIG_H
18
#include <fc_config.h>
19
#endif
20

  
21
// Qt
22
#include <QWidget>
23

  
24
class QLineEdit;
25
class QListWidget;
26
class QRadioButton;
27

  
28
class ruledit_gui;
29

  
30
class tab_terrains : public QWidget
31
{
32
  Q_OBJECT
33

  
34
  public:
35
    explicit tab_terrains(ruledit_gui *ui_in);
36
    void refresh();
37

  
38
  private:
39
    ruledit_gui *ui;
40
    void update_terrain_info(struct terrain *pterr);
41
    bool initialize_new_terrain(struct terrain *pterr);
42

  
43
    QLineEdit *name;
44
    QLineEdit *rname;
45
    QListWidget *terrain_list;
46
    QRadioButton *same_name;
47

  
48
    struct terrain *selected;
49

  
50
  private slots:
51
    void name_given();
52
    void select_terrain();
53
    void add_now();
54
    void delete_now();
55
    void same_name_toggle(bool checked);
56
};
57

  
58

  
59
#endif // FC__TAB_TERRAINS_H
freeciv/tools/ruledit/validity.c 2017-05-04 17:26:30.629344028 +0300
252 252
}
253 253

  
254 254
/**************************************************************************
255
  Check if anything in ruleset needs terrain type
256
**************************************************************************/
257
bool is_terrain_needed(struct terrain *pterr, requirers_cb cb, void *data)
258
{
259
  struct universal uni = { .value.terrain = pterr, .kind = VUT_TERRAIN };
260
  bool needed = FALSE;
261

  
262
  needed |= is_universal_needed(&uni, cb, data);
263

  
264
  return needed;
265
}
266

  
267
/**************************************************************************
255 268
  Check if anything in ruleset needs government
256 269
**************************************************************************/
257 270
bool is_government_needed(struct government *pgov, requirers_cb cb, void *data)
freeciv/tools/ruledit/validity.h 2017-05-04 17:22:38.595222241 +0300
25 25
bool is_good_needed(struct goods_type *pgood, requirers_cb cb, void *data);
26 26
bool is_government_needed(struct government *pgov, requirers_cb cb, void *data);
27 27
bool is_extra_needed(struct extra_type *pextra, requirers_cb cb, void *data);
28
bool is_terrain_needed(struct terrain *pterr, requirers_cb cb, void *data);
28 29

  
29 30
#ifdef __cplusplus
30 31
}
    (1-1/1)