Freeciv-3.3: /fast/freeciv/nightly/main/client/gui-qt/gotodlg.cpp Source File

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/***********************************************************************/
70goto_dialog::goto_dialog(QWidget *parent): qfc_dialog(parent)
71{
72 QStringList headers_lst;
73 QHBoxLayout *hb;
74
75 for (enum gotodlg_columns col = gotodlg_columns_begin();
76 col != gotodlg_columns_end();
77 col = gotodlg_columns_next(col)) {
78
79 headers_lst << gotodlg_columns_name(col);
80 }
81
82 setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
83 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
84 setParent(parent);
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,
125 this, &goto_dialog::checkbox_changed);
126 connect(goto_tab->selectionModel(),
127 SIGNAL(selectionChanged(const QItemSelection &,
128 const QItemSelection &)),
129 SLOT(item_selected(const QItemSelection &,
130 const QItemSelection &)));
131
132 setLayout(layout);
133 original_tile = nullptr;
134 setFocus();
135}
136
137/***********************************************************************/
140 void goto_dialog::init()
141{
142 if (original_tile != nullptr) {
143 tile_virtual_destroy(original_tile);
144 }
145 original_tile = tile_virtual_new(get_center_tile_mapcanvas());
146}
147
148/***********************************************************************/
151goto_dialog::~goto_dialog()
152{
153}
154
155/***********************************************************************/
158 void goto_dialog::checkbox_changed(int state)
159{
160 update_dlg();
161}
162
163/***********************************************************************/
166 void goto_dialog::item_selected(const QItemSelection &sl,
167 const QItemSelection &ds)
168{
169 int i;
170 int city_id;
171 QModelIndex index;
172 QModelIndexList indexes = sl.indexes();
173 QTableWidgetItem *item;
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);
185 center_tile_mapcanvas(city_tile(dest));
186 can_airlift = false;
187 unit_list_iterate(get_units_in_focus(), punit) {
188 if (unit_can_airlift_to(punit, dest)) {
189 can_airlift = true;
190 break;
191 }
192 } unit_list_iterate_end;
193
194 if (can_airlift) {
195 airlift_city->setEnabled(true);
196 } else {
197 airlift_city->setDisabled(true);
198 }
199
200}
201
202/***********************************************************************/
205 void goto_dialog::sort_def()
206{
207 goto_tab->sortByColumn(0, Qt::AscendingOrder);
208}
209
210/***********************************************************************/
213 void goto_dialog::show_me()
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 == nullptr) {
228 init();
229 }
230 show();
231}
232
233/***********************************************************************/
236 void goto_dialog::update_dlg()
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);
244 } players_iterate_end;
245 } else {
246 fill_tab(client_player());
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/***********************************************************************/
257 void goto_dialog::fill_tab(player *pplayer)
258{
259 int i;
260
261 QString str;
262 const char *at;
263 QFont f = QApplication::font();
264 QFontMetrics fm(f);
265 int h;
266 struct sprite *sprite;
267 QPixmap *pix;
268 QPixmap pix_scaled;
269 QTableWidgetItem *item;
270 unit *punit = nullptr;
271 int rnum = 0;
272 int rvalue = 0;
273 enum route_direction rdir = RDIR_NONE;
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);
280 for (enum gotodlg_columns col = gotodlg_columns_begin();
281 col != gotodlg_columns_end();
282 col = gotodlg_columns_next(col)) {
283
284 item = new QTableWidgetItem;
285 str.clear();
286
287 switch (col) {
288 case GOTODLG_CITY:
289 str = city_name_get(pcity);
290 break;
291
292 case GOTODLG_NATION:
293 sprite = get_nation_flag_sprite(tileset, nation_of_player(pplayer));
294 if (sprite != nullptr) {
295 pix = sprite->pm;
296 pix_scaled = pix->scaledToHeight(h);
297 item->setData(Qt::DecorationRole, pix_scaled);
298 }
299 str = nation_adjective_translation(nation_of_player(pplayer));
300 break;
301
302 case GOTODLG_BUILDING:
303 str = city_production_name_translation(pcity);
304 break;
305
306 case GOTODLG_CONTINENT:
307 str.setNum(tile_continent(pcity->tile));
308 while (str.length() < 3) {
309 str.prepend('0');
310 }
311 break;
312
313 case GOTODLG_AIRLIFT:
314 at = get_airlift_text(get_units_in_focus(), pcity);
315 if (at == nullptr) {
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:
331 punit = head_of_units_in_focus();
332 if (punit == nullptr) {
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:
344 punit = head_of_units_in_focus();
345 rnum = 0;
346 rvalue = 0;
347 rdir = RDIR_NONE;
348 rdir_value = 0;
349 if (punit != nullptr) {
350 trade_routes_iterate(pcity, proute) {
351 rnum++;
352 rvalue += proute->value;
353 if (punit->homecity == proute->partner) {
354 rdir = proute->dir;
355 rdir_value = proute->value;
356 }
357 } trade_routes_iterate_end;
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
381 case RDIR_BIDIRECTIONAL:
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
403 case NUM_GOTODLG_COLUMNS:
404 fc_assert(col != NUM_GOTODLG_COLUMNS);
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++;
413 } city_list_iterate_end;
414}
415
416/***********************************************************************/
419 void goto_dialog::airlift_to()
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) {
429 unit_list_iterate(get_units_in_focus(), punit) {
430 if (unit_can_airlift_to(punit, pdest)) {
431 request_unit_airlift(punit, pdest);
432 }
433 } unit_list_iterate_end;
434 }
435}
436
437/***********************************************************************/
440 void goto_dialog::go_to_city()
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) {
451 unit_list_iterate(get_units_in_focus(), punit) {
452 send_goto_tile(punit, pdest->tile);
453 } unit_list_iterate_end;
454 }
455}
456
457/***********************************************************************/
460 void goto_dialog::close_dlg()
461{
462 if (original_tile != nullptr) {
463 center_tile_mapcanvas(original_tile);
464 tile_virtual_destroy(original_tile);
465 original_tile = nullptr;
466 }
467
468 hide();
469}
470
471/***********************************************************************/
474 void goto_dialog::paint(QPainter *painter, QPaintEvent *event)
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/***********************************************************************/
485 void goto_dialog::paintEvent(QPaintEvent *event)
486{
487 QPainter painter;
488
489 painter.begin(this);
490 paint(&painter, event);
491 painter.end();
492}
493
494/***********************************************************************/
497 void popup_goto_dialog(void)
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}
str
#define str
Definition astring.c:76
height
struct canvas int int struct sprite int int int int height
Definition canvas_g.h:44
width
struct canvas int int struct sprite int int int width
Definition canvas_g.h:44
city_name_get
const char * city_name_get(const struct city *pcity)
Definition city.c:1128
city_production_name_translation
const char * city_production_name_translation(const struct city *pcity)
Definition city.c:699
city_list_iterate
#define city_list_iterate(citylist, pcity)
Definition city.h:508
city_tile
#define city_tile(_pcity_)
Definition city.h:564
city_list_iterate_end
#define city_list_iterate_end
Definition city.h:510
goto_dialog
Definition gotodlg.h:43
goto_dialog::sort_def
void sort_def()
Definition gotodlg.cpp:205
goto_dialog::close_but
QPushButton * close_but
Definition gotodlg.h:48
goto_dialog::go_to_city
void go_to_city()
Definition gotodlg.cpp:440
goto_dialog::goto_city
QPushButton * goto_city
Definition gotodlg.h:46
goto_dialog::update_dlg
void update_dlg()
Definition gotodlg.cpp:236
goto_dialog::init
void init()
Definition gotodlg.cpp:140
goto_dialog::close_dlg
void close_dlg()
Definition gotodlg.cpp:460
goto_dialog::original_tile
struct tile * original_tile
Definition gotodlg.h:75
goto_dialog::show_all
QCheckBox * show_all
Definition gotodlg.h:49
goto_dialog::goto_dialog
goto_dialog(QWidget *parent=0)
Definition gotodlg.cpp:70
goto_dialog::checkbox_changed
void checkbox_changed(int state)
Definition gotodlg.cpp:158
goto_dialog::show_all_label
QLabel * show_all_label
Definition gotodlg.h:51
goto_dialog::goto_tab
QTableWidget * goto_tab
Definition gotodlg.h:45
goto_dialog::paint
void paint(QPainter *painter, QPaintEvent *event)
Definition gotodlg.cpp:474
goto_dialog::airlift_city
QPushButton * airlift_city
Definition gotodlg.h:47
goto_dialog::airlift_to
void airlift_to()
Definition gotodlg.cpp:419
goto_dialog::~goto_dialog
~goto_dialog()
Definition gotodlg.cpp:151
goto_dialog::show_me
void show_me()
Definition gotodlg.cpp:213
goto_dialog::paintEvent
void paintEvent(QPaintEvent *event)
Definition gotodlg.cpp:485
goto_dialog::item_selected
void item_selected(const QItemSelection &sl, const QItemSelection &ds)
Definition gotodlg.cpp:166
goto_dialog::layout
QGridLayout * layout
Definition gotodlg.h:50
goto_dialog::fill_tab
void fill_tab(struct player *pplayer)
Definition gotodlg.cpp:257
qfc_dialog
Definition dialogs.h:74
client_state
enum client_states client_state(void)
Definition client_main.c:1026
client_has_player
bool client_has_player(void)
Definition client_main.c:1364
client_main.h
client_player
#define client_player()
Definition client_main.h:106
C_S_RUNNING
@ C_S_RUNNING
Definition client_main.h:47
incite_cost
char * incite_cost
Definition comments.c:74
request_unit_airlift
void request_unit_airlift(struct unit *punit, struct city *pcity)
Definition control.c:1517
get_units_in_focus
struct unit_list * get_units_in_focus(void)
Definition control.c:177
head_of_units_in_focus
struct unit * head_of_units_in_focus(void)
Definition control.c:405
get_num_units_in_focus
int get_num_units_in_focus(void)
Definition control.c:185
control.h
punit
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
event
enum event_type event
Definition events.c:81
fc_client.h
_
#define _(String)
Definition fcintl.h:67
game_city_by_number
struct city * game_city_by_number(int id)
Definition game.c:107
game.h
send_goto_tile
bool send_goto_tile(struct unit *punit, struct tile *ptile)
Definition goto.c:1559
goto.h
popup_goto_dialog
void popup_goto_dialog(void)
Definition gotodlg.cpp:497
show
#define show(id)
Definition widget.h:235
hide
#define hide(id)
Definition widget.h:238
gotodlg.h
sprite.h
fc_assert
#define fc_assert(condition)
Definition log.h:176
sq_map_distance
int sq_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:647
get_center_tile_mapcanvas
struct tile * get_center_tile_mapcanvas(void)
Definition mapview_common.c:1227
center_tile_mapcanvas
void center_tile_mapcanvas(const struct tile *ptile)
Definition mapview_common.c:1237
gui
static mpgui * gui
Definition mpgui_qt.cpp:52
nation_adjective_translation
const char * nation_adjective_translation(const struct nation_type *pnation)
Definition nation.c:149
nation_of_player
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:444
players_iterate_end
#define players_iterate_end
Definition player.h:539
players_iterate
#define players_iterate(_pplayer)
Definition player.h:534
qtg_cxxside.h
specenum_gen.h
city
Definition city.h:320
item
Definition climisc.h:80
item::item
struct universal item
Definition climisc.h:81
move
Definition widget_window.c:39
player
Definition player.h:249
player::cities
struct city_list * cities
Definition player.h:281
sprite
Definition sprite.h:22
sprite::pm
QPixmap * pm
Definition sprite.h:25
tileset
Definition tilespec.c:481
unit
Definition unit.h:140
unit::tile
struct tile * tile
Definition unit.h:142
unit::homecity
int homecity
Definition unit.h:148
get_airlift_text
const char * get_airlift_text(const struct unit_list *punits, const struct city *pdest)
Definition text.c:558
text.h
tile_virtual_destroy
void tile_virtual_destroy(struct tile *vtile)
Definition tile.c:1035
tile_virtual_new
struct tile * tile_virtual_new(const struct tile *ptile)
Definition tile.c:981
tile_continent
#define tile_continent(_tile)
Definition tile.h:93
get_nation_flag_sprite
struct sprite * get_nation_flag_sprite(const struct tileset *t, const struct nation_type *pnation)
Definition tilespec.c:6774
trade_routes_iterate_end
#define trade_routes_iterate_end
Definition traderoutes.h:135
trade_routes_iterate
#define trade_routes_iterate(c, proute)
Definition traderoutes.h:131
unit_can_airlift_to
bool unit_can_airlift_to(const struct unit *punit, const struct city *pdest_city)
Definition unit.c:186
unit_tile
#define unit_tile(_pu)
Definition unit.h:397
unit_list_iterate
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
unit_list_iterate_end
#define unit_list_iterate_end
Definition unitlist.h:33