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 // Qt-6.7
126 connect(show_all, &QCheckBox::checkStateChanged,
128#else // FC_QT6X_MODE
129 connect(show_all, &QCheckBox::stateChanged,
131#endif // FC_QT6X_MODE
132 connect(goto_tab->selectionModel(),
134 const QItemSelection &)),
136 const QItemSelection &)));
137
138 connect(goto_tab,
139 &QTableWidget::itemDoubleClicked,
141
143 original_tile = nullptr;
144 setFocus();
145}
146
147/***********************************************************************/
157
158/***********************************************************************/
164
165/***********************************************************************/
168void goto_dialog::checkbox_changed(Qt::CheckState state)
169{
170 update_dlg();
171}
172
173/***********************************************************************/
178{
179 update_dlg();
180}
181
182/***********************************************************************/
186 const QItemSelection &ds)
187{
188 int i;
189 int city_id;
190 QModelIndex index;
191 QModelIndexList indexes = sl.indexes();
193 struct city *dest;
194 bool can_airlift;
195
196 if (indexes.isEmpty()) {
197 return;
198 }
199 index = indexes.at(0);
200 i = index.row();
201 item = goto_tab->item(i, 0);
202 city_id = item->data(Qt::UserRole).toInt();
203 dest = game_city_by_number(city_id);
204 if (dest == nullptr) {
205 return;
206 }
208 can_airlift = false;
210 if (unit_can_airlift_to(&(wld.map), punit, dest)) {
211 can_airlift = true;
212 break;
213 }
215
216 if (can_airlift) {
217 airlift_city->setEnabled(true);
218 } else {
219 airlift_city->setDisabled(true);
220 }
221
222}
223
224/***********************************************************************/
228{
229 goto_tab->sortByColumn(0, Qt::AscendingOrder);
230}
231
232/***********************************************************************/
236{
237 QPoint p, final_p;
238 p = QCursor::pos();
239 p = parentWidget()->mapFromGlobal(p);
240 final_p.setX(p.x());
241 final_p.setY(p.y());
242 if (p.x() + width() > parentWidget()->width()) {
243 final_p.setX(parentWidget()->width() - width());
244 }
245 if (p.y() - height() < 0) {
246 final_p.setY(height());
247 }
248 move(final_p.x(), final_p.y() - height());
249 if (original_tile == nullptr) {
250 init();
251 }
252 show();
253}
254
255/***********************************************************************/
259{
260 goto_tab->clearContents();
261 goto_tab->setRowCount(0);
262 goto_tab->setSortingEnabled(false);
263 if (show_all->isChecked()) {
264 players_iterate(pplayer) {
265 fill_tab(pplayer);
267 } else {
269 }
270 goto_tab->setSortingEnabled(true);
271 goto_tab->horizontalHeader()->setStretchLastSection(false);
272 goto_tab->resizeRowsToContents();
273 goto_tab->horizontalHeader()->setStretchLastSection(true);
274}
275
276/***********************************************************************/
280{
281 int i;
282
283 QString str;
284 const char *at;
285 QFont f = QApplication::font();
287 int h;
288 struct sprite *sprite;
289 QPixmap *pix;
292 unit *punit = nullptr;
293 int rnum = 0;
294 int rvalue = 0;
296 int rdir_value = 0;
297
298 h = fm.height() + 6;
299 i = goto_tab->rowCount();
300 city_list_iterate(pplayer->cities, pcity) {
301 goto_tab->insertRow(i);
305
307 str.clear();
308
309 switch (col) {
310 case GOTODLG_CITY:
312 break;
313
314 case GOTODLG_NATION:
316 if (sprite != nullptr) {
317 pix = sprite->pm;
318 pix_scaled = pix->scaledToHeight(h);
319 item->setData(Qt::DecorationRole, pix_scaled);
320 }
322 break;
323
324 case GOTODLG_BUILDING:
326 break;
327
329 str.setNum(tile_continent(pcity->tile));
330 while (str.length() < 3) {
331 str.prepend('0');
332 }
333 break;
334
335 case GOTODLG_AIRLIFT:
337 if (at == nullptr) {
338 str = "-";
339 } else {
340 str = at;
341 }
342 item->setTextAlignment(Qt::AlignHCenter);
343 break;
344
345 case GOTODLG_SIZE:
346 str.setNum(pcity->size);
347 while (str.length() < 2) {
348 str.prepend('0');
349 }
350 break;
351
352 case GOTODLG_DISTANCE:
354 if (punit == nullptr) {
355 str = "-";
356 item->setTextAlignment(Qt::AlignHCenter);
357 } else {
359 while (str.length() < 6) {
360 str.prepend('0');
361 }
362 }
363 break;
364
365 case GOTODLG_TRADE:
367 rnum = 0;
368 rvalue = 0;
369 rdir = RDIR_NONE;
370 rdir_value = 0;
371 if (punit != nullptr) {
373 rnum++;
374 rvalue += proute->value;
375 if (punit->homecity == proute->partner) {
376 rdir = proute->dir;
377 rdir_value = proute->value;
378 }
380 }
381
382 if (rnum == 0) {
383 str = "-";
384 item->setTextAlignment(Qt::AlignHCenter);
385 } else {
386 str.setNum(rnum);
387 str.append(" (");
388 while (str.length() < 3) {
389 str.prepend('0');
390 }
391 str.append(" (");
392 if (rvalue < 100) {
393 str.append('0');
394 }
395 if (rvalue < 10) {
396 str.append('0');
397 }
398 str.append(QString::number(rvalue));
399 str.append(") ");
400
401 switch (rdir) {
402
404 str.append("<< ");
405 str.append(QString::number(rdir_value));
406 str.append(" >>");
407 break;
408
409 case RDIR_FROM:
410 str.append(QString::number(rdir_value));
411 str.append(" >>");
412 break;
413
414 case RDIR_TO:
415 str.append("<< ");
416 str.append(QString::number(rdir_value));
417 break;
418
419 case RDIR_NONE:
420 break;
421 }
422 }
423 break;
424
427 break;
428 }
429
430 item->setText(str);
431 item->setData(Qt::UserRole, pcity->id);
432 goto_tab->setItem(i, col, item);
433 }
434 i++;
436}
437
438/***********************************************************************/
442{
443 struct city *pdest;
444
445 if (goto_tab->currentRow() == -1) {
446 return;
447 }
448 pdest = game_city_by_number(goto_tab->item(goto_tab->currentRow(),
449 0)->data(Qt::UserRole).toInt());
450 if (pdest) {
454 }
456 }
457}
458
459/***********************************************************************/
463{
464 struct city *pdest;
465
466 if (goto_tab->currentRow() == -1) {
467 return;
468 }
469
470 pdest = game_city_by_number(goto_tab->item(goto_tab->currentRow(),
471 0)->data(Qt::UserRole).toInt());
472 if (pdest) {
476 }
477}
478
479/************************************************************************/
486{
487 struct city *pdest;
488
489 if (item == nullptr) {
490 return;
491 }
492
493 pdest = game_city_by_number(item->data(Qt::UserRole).toInt());
494 if (pdest) {
497 && !QGuiApplication::keyboardModifiers().testFlag(
498 Qt::ControlModifier)) {
500 } else if (!QGuiApplication::keyboardModifiers().testFlag(
501 Qt::AltModifier)) {
503 }
505 }
506}
507
508/***********************************************************************/
512{
513 if (original_tile != nullptr) {
516 original_tile = nullptr;
517 }
518
519 hide();
520}
521
522/***********************************************************************/
526{
527 painter->setBrush(QColor(0, 0, 30, 85));
528 painter->drawRect(0, 0, width(), height());
529 painter->setBrush(QColor(0, 0, 0, 85));
530 painter->drawRect(5, 5, width() - 10, height() - 10);
531}
532
533/***********************************************************************/
537{
539
540 painter.begin(this);
542 painter.end();
543}
544
545/***********************************************************************/
549{
550 goto_dialog *gtd;
551
552 if (C_S_RUNNING != client_state()) {
553 return;
554 }
555 if (get_num_units_in_focus() == 0) {
556 return;
557 }
558 if (!client_has_player()) {
559 return;
560 }
561
562 gtd = gui()->gtd;
563
564 if (gtd != nullptr) {
565 gtd->init();
566 gtd->update_dlg();
567 gtd->sort_def();
568 gtd->show_me();
569 }
570}
#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:227
QPushButton * close_but
Definition gotodlg.h:48
void go_to_city()
Definition gotodlg.cpp:462
QPushButton * goto_city
Definition gotodlg.h:46
void checkbox_changed(Qt::CheckState state)
Definition gotodlg.cpp:168
void update_dlg()
Definition gotodlg.cpp:258
void init()
Definition gotodlg.cpp:150
void close_dlg()
Definition gotodlg.cpp:511
struct tile * original_tile
Definition gotodlg.h:77
QCheckBox * show_all
Definition gotodlg.h:49
void dbl_click_p(QTableWidgetItem *item)
Definition gotodlg.cpp:485
goto_dialog(QWidget *parent=0)
Definition gotodlg.cpp:70
void checkbox_changed_depr(int state)
Definition gotodlg.cpp:177
QLabel * show_all_label
Definition gotodlg.h:51
QTableWidget * goto_tab
Definition gotodlg.h:45
void paint(QPainter *painter, QPaintEvent *event)
Definition gotodlg.cpp:525
QPushButton * airlift_city
Definition gotodlg.h:47
void airlift_to()
Definition gotodlg.cpp:441
void show_me()
Definition gotodlg.cpp:235
void paintEvent(QPaintEvent *event)
Definition gotodlg.cpp:536
void item_selected(const QItemSelection &sl, const QItemSelection &ds)
Definition gotodlg.cpp:185
QGridLayout * layout
Definition gotodlg.h:50
void fill_tab(struct player *pplayer)
Definition gotodlg.cpp:279
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:548
#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:598
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:7003
#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:204
#define unit_tile(_pu)
Definition unit.h:407
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_end
Definition unitlist.h:33