Freeciv-3.3
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#ifdef FC_QT6X_MODE
125 connect(show_all, &QCheckBox::checkStateChanged,
127#else // FC_QT6X_MODE
128 connect(show_all, &QCheckBox::stateChanged,
130#endif // FC_QT6X_MODE
131 connect(goto_tab->selectionModel(),
133 const QItemSelection &)),
135 const QItemSelection &)));
136
138 original_tile = nullptr;
139 setFocus();
140}
141
142/***********************************************************************/
152
153/***********************************************************************/
159
160/***********************************************************************/
163void goto_dialog::checkbox_changed(Qt::CheckState state)
164{
165 update_dlg();
166}
167
168/***********************************************************************/
173{
174 update_dlg();
175}
176
177/***********************************************************************/
181 const QItemSelection &ds)
182{
183 int i;
184 int city_id;
185 QModelIndex index;
186 QModelIndexList indexes = sl.indexes();
188 struct city *dest;
189 bool can_airlift;
190
191 if (indexes.isEmpty()) {
192 return;
193 }
194 index = indexes.at(0);
195 i = index.row();
196 item = goto_tab->item(i, 0);
197 city_id = item->data(Qt::UserRole).toInt();
198 dest = game_city_by_number(city_id);
200 can_airlift = false;
202 if (unit_can_airlift_to(&(wld.map), punit, dest)) {
203 can_airlift = true;
204 break;
205 }
207
208 if (can_airlift) {
209 airlift_city->setEnabled(true);
210 } else {
211 airlift_city->setDisabled(true);
212 }
213
214}
215
216/***********************************************************************/
220{
221 goto_tab->sortByColumn(0, Qt::AscendingOrder);
222}
223
224/***********************************************************************/
228{
229 QPoint p, final_p;
230 p = QCursor::pos();
231 p = parentWidget()->mapFromGlobal(p);
232 final_p.setX(p.x());
233 final_p.setY(p.y());
234 if (p.x() + width() > parentWidget()->width()) {
235 final_p.setX(parentWidget()->width() - width());
236 }
237 if (p.y() - height() < 0) {
238 final_p.setY(height());
239 }
240 move(final_p.x(), final_p.y() - height());
241 if (original_tile == nullptr) {
242 init();
243 }
244 show();
245}
246
247/***********************************************************************/
251{
252 goto_tab->clearContents();
253 goto_tab->setRowCount(0);
254 goto_tab->setSortingEnabled(false);
255 if (show_all->isChecked()) {
256 players_iterate(pplayer) {
257 fill_tab(pplayer);
259 } else {
261 }
262 goto_tab->setSortingEnabled(true);
263 goto_tab->horizontalHeader()->setStretchLastSection(false);
264 goto_tab->resizeRowsToContents();
265 goto_tab->horizontalHeader()->setStretchLastSection(true);
266}
267
268/***********************************************************************/
272{
273 int i;
274
275 QString str;
276 const char *at;
277 QFont f = QApplication::font();
279 int h;
280 struct sprite *sprite;
281 QPixmap *pix;
284 unit *punit = nullptr;
285 int rnum = 0;
286 int rvalue = 0;
288 int rdir_value = 0;
289
290 h = fm.height() + 6;
291 i = goto_tab->rowCount();
292 city_list_iterate(pplayer->cities, pcity) {
293 goto_tab->insertRow(i);
297
299 str.clear();
300
301 switch (col) {
302 case GOTODLG_CITY:
304 break;
305
306 case GOTODLG_NATION:
308 if (sprite != nullptr) {
309 pix = sprite->pm;
310 pix_scaled = pix->scaledToHeight(h);
311 item->setData(Qt::DecorationRole, pix_scaled);
312 }
314 break;
315
316 case GOTODLG_BUILDING:
318 break;
319
321 str.setNum(tile_continent(pcity->tile));
322 while (str.length() < 3) {
323 str.prepend('0');
324 }
325 break;
326
327 case GOTODLG_AIRLIFT:
329 if (at == nullptr) {
330 str = "-";
331 } else {
332 str = at;
333 }
334 item->setTextAlignment(Qt::AlignHCenter);
335 break;
336
337 case GOTODLG_SIZE:
338 str.setNum(pcity->size);
339 while (str.length() < 2) {
340 str.prepend('0');
341 }
342 break;
343
344 case GOTODLG_DISTANCE:
346 if (punit == nullptr) {
347 str = "-";
348 item->setTextAlignment(Qt::AlignHCenter);
349 } else {
351 while (str.length() < 6) {
352 str.prepend('0');
353 }
354 }
355 break;
356
357 case GOTODLG_TRADE:
359 rnum = 0;
360 rvalue = 0;
361 rdir = RDIR_NONE;
362 rdir_value = 0;
363 if (punit != nullptr) {
365 rnum++;
366 rvalue += proute->value;
367 if (punit->homecity == proute->partner) {
368 rdir = proute->dir;
369 rdir_value = proute->value;
370 }
372 }
373
374 if (rnum == 0) {
375 str = "-";
376 item->setTextAlignment(Qt::AlignHCenter);
377 } else {
378 str.setNum(rnum);
379 str.append(" (");
380 while (str.length() < 3) {
381 str.prepend('0');
382 }
383 str.append(" (");
384 if (rvalue < 100) {
385 str.append('0');
386 }
387 if (rvalue < 10) {
388 str.append('0');
389 }
390 str.append(QString::number(rvalue));
391 str.append(") ");
392
393 switch (rdir) {
394
396 str.append("<< ");
397 str.append(QString::number(rdir_value));
398 str.append(" >>");
399 break;
400
401 case RDIR_FROM:
402 str.append(QString::number(rdir_value));
403 str.append(" >>");
404 break;
405
406 case RDIR_TO:
407 str.append("<< ");
408 str.append(QString::number(rdir_value));
409 break;
410
411 case RDIR_NONE:
412 break;
413 }
414 }
415 break;
416
419 break;
420 }
421
422 item->setText(str);
423 item->setData(Qt::UserRole, pcity->id);
424 goto_tab->setItem(i, col, item);
425 }
426 i++;
428}
429
430/***********************************************************************/
434{
435 struct city *pdest;
436
437 if (goto_tab->currentRow() == -1) {
438 return;
439 }
440 pdest = game_city_by_number(goto_tab->item(goto_tab->currentRow(),
441 0)->data(Qt::UserRole).toInt());
442 if (pdest) {
446 }
448 }
449}
450
451/***********************************************************************/
455{
456 struct city *pdest;
457
458 if (goto_tab->currentRow() == -1) {
459 return;
460 }
461
462 pdest = game_city_by_number(goto_tab->item(goto_tab->currentRow(),
463 0)->data(Qt::UserRole).toInt());
464 if (pdest) {
468 }
469}
470
471/***********************************************************************/
475{
476 if (original_tile != nullptr) {
479 original_tile = nullptr;
480 }
481
482 hide();
483}
484
485/***********************************************************************/
489{
490 painter->setBrush(QColor(0, 0, 30, 85));
491 painter->drawRect(0, 0, width(), height());
492 painter->setBrush(QColor(0, 0, 0, 85));
493 painter->drawRect(5, 5, width() - 10, height() - 10);
494}
495
496/***********************************************************************/
500{
502
503 painter.begin(this);
505 painter.end();
506}
507
508/***********************************************************************/
512{
513 goto_dialog *gtd;
514
515 if (C_S_RUNNING != client_state()) {
516 return;
517 }
518 if (get_num_units_in_focus() == 0) {
519 return;
520 }
521 if (!client_has_player()) {
522 return;
523 }
524
525 gtd = gui()->gtd;
526
527 if (gtd != nullptr) {
528 gtd->init();
529 gtd->update_dlg();
530 gtd->sort_def();
531 gtd->show_me();
532 }
533}
#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:505
#define city_tile(_pcity_)
Definition city.h:561
#define city_list_iterate_end
Definition city.h:507
void sort_def()
Definition gotodlg.cpp:219
QPushButton * close_but
Definition gotodlg.h:48
void go_to_city()
Definition gotodlg.cpp:454
QPushButton * goto_city
Definition gotodlg.h:46
void checkbox_changed(Qt::CheckState state)
Definition gotodlg.cpp:163
void update_dlg()
Definition gotodlg.cpp:250
void init()
Definition gotodlg.cpp:145
void close_dlg()
Definition gotodlg.cpp:474
struct tile * original_tile
Definition gotodlg.h:76
QCheckBox * show_all
Definition gotodlg.h:49
goto_dialog(QWidget *parent=0)
Definition gotodlg.cpp:70
void checkbox_changed_depr(int state)
Definition gotodlg.cpp:172
QLabel * show_all_label
Definition gotodlg.h:51
QTableWidget * goto_tab
Definition gotodlg.h:45
void paint(QPainter *painter, QPaintEvent *event)
Definition gotodlg.cpp:488
QPushButton * airlift_city
Definition gotodlg.h:47
void airlift_to()
Definition gotodlg.cpp:433
void show_me()
Definition gotodlg.cpp:227
void paintEvent(QPaintEvent *event)
Definition gotodlg.cpp:499
void item_selected(const QItemSelection &sl, const QItemSelection &ds)
Definition gotodlg.cpp:180
QGridLayout * layout
Definition gotodlg.h:50
void fill_tab(struct player *pplayer)
Definition gotodlg.cpp:271
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:76
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
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 int const struct action *paction struct unit struct city * pcity
Definition dialogs_g.h:78
enum event_type event
Definition events.c:81
#define _(String)
Definition fcintl.h:67
struct world wld
Definition game.c:62
struct city * game_city_by_number(int id)
Definition game.c:106
bool send_goto_tile(struct unit *punit, struct tile *ptile)
Definition goto.c:1569
void popup_goto_dialog(void)
Definition gotodlg.cpp:511
#define show(id)
Definition widget.h:235
#define hide(id)
Definition widget.h:238
#define fc_assert(condition)
Definition log.h:177
int sq_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:686
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:542
#define players_iterate(_pplayer)
Definition player.h:537
Definition city.h:317
Definition climisc.h:82
struct universal item
Definition climisc.h:83
struct city_list * cities
Definition player.h:281
QPixmap * pm
Definition sprite.h:25
Definition unit.h:140
int id
Definition unit.h:147
struct tile * tile
Definition unit.h:142
int homecity
Definition unit.h:148
struct civ_map map
const char * get_airlift_text(const struct unit_list *punits, const struct city *pdest)
Definition text.c:592
void tile_virtual_destroy(struct tile *vtile)
Definition tile.c:1035
struct tile * tile_virtual_new(const struct tile *ptile)
Definition tile.c:981
#define tile_continent(_tile)
Definition tile.h:93
struct sprite * get_nation_flag_sprite(const struct tileset *t, const struct nation_type *pnation)
Definition tilespec.c:6980
#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:404
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_end
Definition unitlist.h:33