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
132 connect(goto_tab,
133 &QTableWidget::itemDoubleClicked,
135
137 original_tile = nullptr;
138 setFocus();
139}
140
141/***********************************************************************/
151
152/***********************************************************************/
158
159/***********************************************************************/
163{
164 update_dlg();
165}
166
167/***********************************************************************/
171 const QItemSelection &ds)
172{
173 int i;
174 int city_id;
175 QModelIndex index;
176 QModelIndexList indexes = sl.indexes();
178 struct city *dest;
179 bool can_airlift;
180
181 if (indexes.isEmpty()) {
182 return;
183 }
184 index = indexes.at(0);
185 i = index.row();
186 item = goto_tab->item(i, 0);
187 city_id = item->data(Qt::UserRole).toInt();
188 dest = game_city_by_number(city_id);
189 if (dest == nullptr) {
190 return;
191 }
193 can_airlift = false;
195 if (unit_can_airlift_to(&(wld.map), punit, dest)) {
196 can_airlift = true;
197 break;
198 }
200
201 if (can_airlift) {
202 airlift_city->setEnabled(true);
203 } else {
204 airlift_city->setDisabled(true);
205 }
206
207}
208
209/***********************************************************************/
213{
214 goto_tab->sortByColumn(0, Qt::AscendingOrder);
215}
216
217/***********************************************************************/
221{
222 QPoint p, final_p;
223 p = QCursor::pos();
224 p = parentWidget()->mapFromGlobal(p);
225 final_p.setX(p.x());
226 final_p.setY(p.y());
227 if (p.x() + width() > parentWidget()->width()) {
228 final_p.setX(parentWidget()->width() - width());
229 }
230 if (p.y() - height() < 0) {
231 final_p.setY(height());
232 }
233 move(final_p.x(), final_p.y() - height());
234 if (original_tile == NULL) {
235 init();
236 }
237 show();
238}
239
240/***********************************************************************/
244{
245 goto_tab->clearContents();
246 goto_tab->setRowCount(0);
247 goto_tab->setSortingEnabled(false);
248 if (show_all->isChecked()) {
249 players_iterate(pplayer) {
250 fill_tab(pplayer);
252 } else {
254 }
255 goto_tab->setSortingEnabled(true);
256 goto_tab->horizontalHeader()->setStretchLastSection(false);
257 goto_tab->resizeRowsToContents();
258 goto_tab->horizontalHeader()->setStretchLastSection(true);
259}
260
261/***********************************************************************/
265{
266 int i;
267
268 QString str;
269 const char *at;
270 QFont f = QApplication::font();
272 int h;
273 struct sprite *sprite;
274 QPixmap *pix;
277 unit *punit = NULL;
278 int rnum = 0;
279 int rvalue = 0;
281 int rdir_value = 0;
282
283 h = fm.height() + 6;
284 i = goto_tab->rowCount();
285 city_list_iterate(pplayer->cities, pcity) {
286 goto_tab->insertRow(i);
290
292 str.clear();
293
294 switch (col) {
295 case GOTODLG_CITY:
296 str = city_name_get(pcity);
297 break;
298
299 case GOTODLG_NATION:
301 if (sprite != NULL) {
302 pix = sprite->pm;
303 pix_scaled = pix->scaledToHeight(h);
304 item->setData(Qt::DecorationRole, pix_scaled);
305 }
307 break;
308
309 case GOTODLG_BUILDING:
311 break;
312
314 str.setNum(tile_continent(pcity->tile));
315 while (str.length() < 3) {
316 str.prepend('0');
317 }
318 break;
319
320 case GOTODLG_AIRLIFT:
322 if (at == NULL) {
323 str = "-";
324 } else {
325 str = at;
326 }
327 item->setTextAlignment(Qt::AlignHCenter);
328 break;
329
330 case GOTODLG_SIZE:
331 str.setNum(pcity->size);
332 while (str.length() < 2) {
333 str.prepend('0');
334 }
335 break;
336
337 case GOTODLG_DISTANCE:
339 if (punit == NULL) {
340 str = "-";
341 item->setTextAlignment(Qt::AlignHCenter);
342 } else {
343 str.setNum(sq_map_distance(pcity->tile, unit_tile(punit)));
344 while (str.length() < 6) {
345 str.prepend('0');
346 }
347 }
348 break;
349
350 case GOTODLG_TRADE:
352 rnum = 0;
353 rvalue = 0;
354 rdir = RDIR_NONE;
355 rdir_value = 0;
356 if (punit != NULL) {
358 rnum++;
359 rvalue += proute->value;
360 if (punit->homecity == proute->partner) {
361 rdir = proute->dir;
362 rdir_value = proute->value;
363 }
365 }
366
367 if (rnum == 0) {
368 str = "-";
369 item->setTextAlignment(Qt::AlignHCenter);
370 } else {
371 str.setNum(rnum);
372 str.append(" (");
373 while (str.length() < 3) {
374 str.prepend('0');
375 }
376 str.append(" (");
377 if (rvalue < 100) {
378 str.append('0');
379 }
380 if (rvalue < 10) {
381 str.append('0');
382 }
383 str.append(QString::number(rvalue));
384 str.append(") ");
385
386 switch (rdir) {
387
389 str.append("<< ");
390 str.append(QString::number(rdir_value));
391 str.append(" >>");
392 break;
393
394 case RDIR_FROM:
395 str.append(QString::number(rdir_value));
396 str.append(" >>");
397 break;
398
399 case RDIR_TO:
400 str.append("<< ");
401 str.append(QString::number(rdir_value));
402 break;
403
404 case RDIR_NONE:
405 break;
406 }
407 }
408 break;
409
412 break;
413 }
414
415 item->setText(str);
416 item->setData(Qt::UserRole, pcity->id);
417 goto_tab->setItem(i, col, item);
418 }
419 i++;
421}
422
423/***********************************************************************/
427{
428 struct city *pdest;
429
430 if (goto_tab->currentRow() == -1) {
431 return;
432 }
433 pdest = game_city_by_number(goto_tab->item(goto_tab->currentRow(),
434 0)->data(Qt::UserRole).toInt());
435 if (pdest) {
439 }
441 }
442}
443
444/***********************************************************************/
448{
449 struct city *pdest;
450
451 if (goto_tab->currentRow() == -1) {
452 return;
453 }
454
455 pdest = game_city_by_number(goto_tab->item(goto_tab->currentRow(),
456 0)->data(Qt::UserRole).toInt());
457 if (pdest) {
461 }
462}
463
464/************************************************************************/
471{
472 struct city *pdest;
473
474 if (item == nullptr) {
475 return;
476 }
477
478 pdest = game_city_by_number(item->data(Qt::UserRole).toInt());
479 if (pdest) {
482 && !QGuiApplication::keyboardModifiers().testFlag(
483 Qt::ControlModifier)) {
485 } else if (!QGuiApplication::keyboardModifiers().testFlag(
486 Qt::AltModifier)) {
488 }
490 }
491}
492
493/***********************************************************************/
497{
498 if (original_tile != nullptr) {
501 original_tile = nullptr;
502 }
503
504 hide();
505}
506
507/***********************************************************************/
511{
512 painter->setBrush(QColor(0, 0, 30, 85));
513 painter->drawRect(0, 0, width(), height());
514 painter->setBrush(QColor(0, 0, 0, 85));
515 painter->drawRect(5, 5, width() - 10, height() - 10);
516}
517
518/***********************************************************************/
522{
524
525 painter.begin(this);
527 painter.end();
528}
529
530/***********************************************************************/
534{
535 goto_dialog *gtd;
536
537 if (C_S_RUNNING != client_state()) {
538 return;
539 }
540 if (get_num_units_in_focus() == 0) {
541 return;
542 }
543 if (!client_has_player()) {
544 return;
545 }
546
547 gtd = gui()->gtd;
548
549 if (gtd != nullptr) {
550 gtd->init();
551 gtd->update_dlg();
552 gtd->sort_def();
553 gtd->show_me();
554 }
555}
#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:212
QPushButton * close_but
Definition gotodlg.h:48
void go_to_city()
Definition gotodlg.cpp:447
QPushButton * goto_city
Definition gotodlg.h:46
void update_dlg()
Definition gotodlg.cpp:243
void init()
Definition gotodlg.cpp:144
void close_dlg()
Definition gotodlg.cpp:496
struct tile * original_tile
Definition gotodlg.h:76
QCheckBox * show_all
Definition gotodlg.h:49
void dbl_click_p(QTableWidgetItem *item)
Definition gotodlg.cpp:470
goto_dialog(QWidget *parent=0)
Definition gotodlg.cpp:70
void checkbox_changed(int state)
Definition gotodlg.cpp:162
QLabel * show_all_label
Definition gotodlg.h:51
QTableWidget * goto_tab
Definition gotodlg.h:45
void paint(QPainter *painter, QPaintEvent *event)
Definition gotodlg.cpp:510
QPushButton * airlift_city
Definition gotodlg.h:47
void airlift_to()
Definition gotodlg.cpp:426
void show_me()
Definition gotodlg.cpp:220
void paintEvent(QPaintEvent *event)
Definition gotodlg.cpp:521
void item_selected(const QItemSelection &sl, const QItemSelection &ds)
Definition gotodlg.cpp:170
QGridLayout * layout
Definition gotodlg.h:50
void fill_tab(struct player *pplayer)
Definition gotodlg.cpp:264
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:1555
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:533
#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:551
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:6767
#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:194
#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