Freeciv-3.2
Loading...
Searching...
No Matches
gotodlg.cpp
Go to the documentation of this file.
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 <QApplication>
20#include <QCheckBox>
21#include <QHBoxLayout>
22#include <QHBoxLayout>
23#include <QHeaderView>
24#include <QLabel>
25#include <QPainter>
26#include <QPushButton>
27#include <QTableWidget>
28
29// common
30#include "game.h"
31
32// client
33#include "client_main.h"
34#include "control.h"
35#include "goto.h"
36#include "text.h"
37
38// gui-qt
39#include "fc_client.h"
40#include "gotodlg.h"
41#include "qtg_cxxside.h"
42#include "sprite.h"
43
44#define SPECENUM_NAME gotodlg_columns
45
46#define SPECENUM_VALUE0 GOTODLG_CITY
47#define SPECENUM_VALUE0NAME N_("City")
48#define SPECENUM_VALUE1 GOTODLG_NATION
49#define SPECENUM_VALUE1NAME N_("Nation")
50#define SPECENUM_VALUE2 GOTODLG_CONTINENT
51#define SPECENUM_VALUE2NAME N_("Continent")
52#define SPECENUM_VALUE3 GOTODLG_BUILDING
53#define SPECENUM_VALUE3NAME N_("Building")
54#define SPECENUM_VALUE4 GOTODLG_AIRLIFT
55#define SPECENUM_VALUE4NAME N_("Airlift")
56#define SPECENUM_VALUE5 GOTODLG_SIZE
57#define SPECENUM_VALUE5NAME N_("Size")
58#define SPECENUM_VALUE6 GOTODLG_DISTANCE
59#define SPECENUM_VALUE6NAME N_("Distance")
60#define SPECENUM_VALUE7 GOTODLG_TRADE
61#define SPECENUM_VALUE7NAME N_("Trade")
62
63#define SPECENUM_COUNT NUM_GOTODLG_COLUMNS // number of columns in the goto dialog
64#include "specenum_gen.h"
65
66
67/***********************************************************************/
71{
74
78
80 }
81
82 setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
83 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
85 goto_tab = new QTableWidget;
86 goto_city = new QPushButton(_("&Goto"));
87 airlift_city = new QPushButton(_("&Airlift"));
88 close_but = new QPushButton(_("&Close"));
89 layout = new QGridLayout;
90
91 show_all = new QCheckBox;
92 show_all->setChecked(false);
93 show_all_label = new QLabel(_("Show All Cities"));
94 show_all_label->setAlignment(Qt::AlignLeft);
95 hb = new QHBoxLayout;
96 hb->addWidget(show_all);
97 hb->addWidget(show_all_label, Qt::AlignLeft);
98
99 goto_tab->setProperty("showGrid", "false");
100 goto_tab->setSelectionBehavior(QAbstractItemView::SelectRows);
101 goto_tab->setEditTriggers(QAbstractItemView::NoEditTriggers);
102 goto_tab->verticalHeader()->setVisible(false);
103 goto_tab->horizontalHeader()->setVisible(true);
104 goto_tab->setSelectionMode(QAbstractItemView::SingleSelection);
105 goto_tab->setColumnCount(NUM_GOTODLG_COLUMNS);
106 goto_tab->setHorizontalHeaderLabels(headers_lst);
107 goto_tab->setSortingEnabled(true);
108 goto_tab->horizontalHeader()->setSectionResizeMode(
109 QHeaderView::ResizeToContents);
110
111 layout->addWidget(goto_tab, 0, 0, 1, 4);
112 layout->setRowStretch(0, 100);
113 layout->addItem(hb, 1, 0, 1, 2);
114 layout->addWidget(goto_city, 2, 0, 1, 1);
115 layout->addWidget(airlift_city, 2, 1, 1, 1);
116 layout->addWidget(close_but, 2, 3, 1, 1);
117
118 if (width() < goto_tab->horizontalHeader()->width()) {
119 resize(goto_tab->horizontalHeader()->width(), height());
120 }
121 connect(close_but, &QAbstractButton::clicked, this, &goto_dialog::close_dlg);
122 connect(goto_city, &QAbstractButton::clicked, this, &goto_dialog::go_to_city);
123 connect(airlift_city, &QAbstractButton::clicked, this, &goto_dialog::airlift_to);
124 connect(show_all, &QCheckBox::stateChanged,
126 connect(goto_tab->selectionModel(),
128 const QItemSelection &)),
130 const QItemSelection &)));
131
133 original_tile = nullptr;
134 setFocus();
135}
136
137/***********************************************************************/
147
148/***********************************************************************/
154
155/***********************************************************************/
159{
160 update_dlg();
161}
162
163/***********************************************************************/
167 const QItemSelection &ds)
168{
169 int i;
170 int city_id;
171 QModelIndex index;
172 QModelIndexList indexes = sl.indexes();
174 struct city *dest;
175 bool can_airlift;
176
177 if (indexes.isEmpty()) {
178 return;
179 }
180 index = indexes.at(0);
181 i = index.row();
182 item = goto_tab->item(i, 0);
183 city_id = item->data(Qt::UserRole).toInt();
184 dest = game_city_by_number(city_id);
186 can_airlift = false;
188 if (unit_can_airlift_to(&(wld.map), punit, dest)) {
189 can_airlift = true;
190 break;
191 }
193
194 if (can_airlift) {
195 airlift_city->setEnabled(true);
196 } else {
197 airlift_city->setDisabled(true);
198 }
199
200}
201
202/***********************************************************************/
206{
207 goto_tab->sortByColumn(0, Qt::AscendingOrder);
208}
209
210/***********************************************************************/
214{
215 QPoint p, final_p;
216 p = QCursor::pos();
217 p = parentWidget()->mapFromGlobal(p);
218 final_p.setX(p.x());
219 final_p.setY(p.y());
220 if (p.x() + width() > parentWidget()->width()) {
221 final_p.setX(parentWidget()->width() - width());
222 }
223 if (p.y() - height() < 0) {
224 final_p.setY(height());
225 }
226 move(final_p.x(), final_p.y() - height());
227 if (original_tile == NULL) {
228 init();
229 }
230 show();
231}
232
233/***********************************************************************/
237{
238 goto_tab->clearContents();
239 goto_tab->setRowCount(0);
240 goto_tab->setSortingEnabled(false);
241 if (show_all->isChecked()) {
242 players_iterate(pplayer) {
243 fill_tab(pplayer);
245 } else {
247 }
248 goto_tab->setSortingEnabled(true);
249 goto_tab->horizontalHeader()->setStretchLastSection(false);
250 goto_tab->resizeRowsToContents();
251 goto_tab->horizontalHeader()->setStretchLastSection(true);
252}
253
254/***********************************************************************/
258{
259 int i;
260
261 QString str;
262 const char *at;
263 QFont f = QApplication::font();
265 int h;
266 struct sprite *sprite;
267 QPixmap *pix;
270 unit *punit = NULL;
271 int rnum = 0;
272 int rvalue = 0;
274 int rdir_value = 0;
275
276 h = fm.height() + 6;
277 i = goto_tab->rowCount();
278 city_list_iterate(pplayer->cities, pcity) {
279 goto_tab->insertRow(i);
283
285 str.clear();
286
287 switch (col) {
288 case GOTODLG_CITY:
289 str = city_name_get(pcity);
290 break;
291
292 case GOTODLG_NATION:
294 if (sprite != NULL) {
295 pix = sprite->pm;
296 pix_scaled = pix->scaledToHeight(h);
297 item->setData(Qt::DecorationRole, pix_scaled);
298 }
300 break;
301
302 case GOTODLG_BUILDING:
304 break;
305
307 str.setNum(tile_continent(pcity->tile));
308 while (str.length() < 3) {
309 str.prepend('0');
310 }
311 break;
312
313 case GOTODLG_AIRLIFT:
315 if (at == NULL) {
316 str = "-";
317 } else {
318 str = at;
319 }
320 item->setTextAlignment(Qt::AlignHCenter);
321 break;
322
323 case GOTODLG_SIZE:
324 str.setNum(pcity->size);
325 while (str.length() < 2) {
326 str.prepend('0');
327 }
328 break;
329
330 case GOTODLG_DISTANCE:
332 if (punit == NULL) {
333 str = "-";
334 item->setTextAlignment(Qt::AlignHCenter);
335 } else {
336 str.setNum(sq_map_distance(pcity->tile, unit_tile(punit)));
337 while (str.length() < 6) {
338 str.prepend('0');
339 }
340 }
341 break;
342
343 case GOTODLG_TRADE:
345 rnum = 0;
346 rvalue = 0;
347 rdir = RDIR_NONE;
348 rdir_value = 0;
349 if (punit != NULL) {
351 rnum++;
352 rvalue += proute->value;
353 if (punit->homecity == proute->partner) {
354 rdir = proute->dir;
355 rdir_value = proute->value;
356 }
358 }
359
360 if (rnum == 0) {
361 str = "-";
362 item->setTextAlignment(Qt::AlignHCenter);
363 } else {
364 str.setNum(rnum);
365 str.append(" (");
366 while (str.length() < 3) {
367 str.prepend('0');
368 }
369 str.append(" (");
370 if (rvalue < 100) {
371 str.append('0');
372 }
373 if (rvalue < 10) {
374 str.append('0');
375 }
376 str.append(QString::number(rvalue));
377 str.append(") ");
378
379 switch (rdir) {
380
382 str.append("<< ");
383 str.append(QString::number(rdir_value));
384 str.append(" >>");
385 break;
386
387 case RDIR_FROM:
388 str.append(QString::number(rdir_value));
389 str.append(" >>");
390 break;
391
392 case RDIR_TO:
393 str.append("<< ");
394 str.append(QString::number(rdir_value));
395 break;
396
397 case RDIR_NONE:
398 break;
399 }
400 }
401 break;
402
405 break;
406 }
407
408 item->setText(str);
409 item->setData(Qt::UserRole, pcity->id);
410 goto_tab->setItem(i, col, item);
411 }
412 i++;
414}
415
416/***********************************************************************/
420{
421 struct city *pdest;
422
423 if (goto_tab->currentRow() == -1) {
424 return;
425 }
426 pdest = game_city_by_number(goto_tab->item(goto_tab->currentRow(),
427 0)->data(Qt::UserRole).toInt());
428 if (pdest) {
432 }
434 }
435}
436
437/***********************************************************************/
441{
442 struct city *pdest;
443
444 if (goto_tab->currentRow() == -1) {
445 return;
446 }
447
448 pdest = game_city_by_number(goto_tab->item(goto_tab->currentRow(),
449 0)->data(Qt::UserRole).toInt());
450 if (pdest) {
454 }
455}
456
457/***********************************************************************/
461{
462 if (original_tile != nullptr) {
465 original_tile = nullptr;
466 }
467
468 hide();
469}
470
471/***********************************************************************/
475{
476 painter->setBrush(QColor(0, 0, 30, 85));
477 painter->drawRect(0, 0, width(), height());
478 painter->setBrush(QColor(0, 0, 0, 85));
479 painter->drawRect(5, 5, width() - 10, height() - 10);
480}
481
482/***********************************************************************/
486{
488
489 painter.begin(this);
491 painter.end();
492}
493
494/***********************************************************************/
498{
499 goto_dialog *gtd;
500
501 if (C_S_RUNNING != client_state()) {
502 return;
503 }
504 if (get_num_units_in_focus() == 0) {
505 return;
506 }
507 if (!client_has_player()) {
508 return;
509 }
510
511 gtd = gui()->gtd;
512
513 if (gtd != nullptr) {
514 gtd->init();
515 gtd->update_dlg();
516 gtd->sort_def();
517 gtd->show_me();
518 }
519}
#define str
Definition astring.c:76
struct canvas int int struct sprite int int int int height
Definition canvas_g.h:44
struct canvas int int struct sprite int int int width
Definition canvas_g.h:44
const char * city_name_get(const struct city *pcity)
Definition city.c:1137
const char * city_production_name_translation(const struct city *pcity)
Definition city.c:700
#define city_list_iterate(citylist, pcity)
Definition city.h:508
#define city_tile(_pcity_)
Definition city.h:564
#define city_list_iterate_end
Definition city.h:510
void sort_def()
Definition gotodlg.cpp:205
QPushButton * close_but
Definition gotodlg.h:48
void go_to_city()
Definition gotodlg.cpp:440
QPushButton * goto_city
Definition gotodlg.h:46
void update_dlg()
Definition gotodlg.cpp:236
void init()
Definition gotodlg.cpp:140
void close_dlg()
Definition gotodlg.cpp:460
struct tile * original_tile
Definition gotodlg.h:75
QCheckBox * show_all
Definition gotodlg.h:49
goto_dialog(QWidget *parent=0)
Definition gotodlg.cpp:70
void checkbox_changed(int state)
Definition gotodlg.cpp:158
QLabel * show_all_label
Definition gotodlg.h:51
QTableWidget * goto_tab
Definition gotodlg.h:45
void paint(QPainter *painter, QPaintEvent *event)
Definition gotodlg.cpp:474
QPushButton * airlift_city
Definition gotodlg.h:47
void airlift_to()
Definition gotodlg.cpp:419
void show_me()
Definition gotodlg.cpp:213
void paintEvent(QPaintEvent *event)
Definition gotodlg.cpp:485
void item_selected(const QItemSelection &sl, const QItemSelection &ds)
Definition gotodlg.cpp:166
QGridLayout * layout
Definition gotodlg.h:50
void fill_tab(struct player *pplayer)
Definition gotodlg.cpp:257
enum client_states client_state(void)
bool client_has_player(void)
#define client_player()
@ C_S_RUNNING
Definition client_main.h:47
char * incite_cost
Definition comments.c:75
void request_unit_airlift(struct unit *punit, struct city *pcity)
Definition control.c:1543
struct unit_list * get_units_in_focus(void)
Definition control.c:177
struct unit * head_of_units_in_focus(void)
Definition control.c:410
int get_num_units_in_focus(void)
Definition control.c:185
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:74
enum event_type event
Definition events.c:81
#define _(String)
Definition fcintl.h:67
struct world wld
Definition game.c:63
struct city * game_city_by_number(int id)
Definition game.c:107
bool send_goto_tile(struct unit *punit, struct tile *ptile)
Definition goto.c:1551
void popup_goto_dialog(void)
Definition gotodlg.cpp:497
#define show(id)
Definition widget.h:235
#define hide(id)
Definition widget.h:238
#define fc_assert(condition)
Definition log.h:176
int sq_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:641
struct tile * get_center_tile_mapcanvas(void)
void center_tile_mapcanvas(const struct tile *ptile)
static mpgui * gui
Definition mpgui_qt.cpp:52
const char * nation_adjective_translation(const struct nation_type *pnation)
Definition nation.c:149
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:444
#define players_iterate_end
Definition player.h:537
#define players_iterate(_pplayer)
Definition player.h:532
Definition city.h:320
Definition climisc.h:82
struct universal item
Definition climisc.h:83
struct city_list * cities
Definition player.h:279
QPixmap * pm
Definition sprite.h:25
Definition unit.h:138
struct tile * tile
Definition unit.h:140
int homecity
Definition unit.h:146
struct civ_map map
const char * get_airlift_text(const struct unit_list *punits, const struct city *pdest)
Definition text.c:545
void tile_virtual_destroy(struct tile *vtile)
Definition tile.c:1033
struct tile * tile_virtual_new(const struct tile *ptile)
Definition tile.c:981
#define tile_continent(_tile)
Definition tile.h:92
struct sprite * get_nation_flag_sprite(const struct tileset *t, const struct nation_type *pnation)
Definition tilespec.c:6756
#define trade_routes_iterate_end
#define trade_routes_iterate(c, proute)
bool unit_can_airlift_to(const struct civ_map *nmap, const struct unit *punit, const struct city *pdest_city)
Definition unit.c:186
#define unit_tile(_pu)
Definition unit.h:397
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_end
Definition unitlist.h:33