Added translator comment about government overthrown message.
[freeciv.git] / client / gui-qt / gotodlg.cpp
blob476296389c715d9e9960fd6a6174fc2c46b27a1e
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.
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 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 //qt
19 #include <QApplication>
20 #include <QHeaderView>
21 #include <QTableWidget>
23 /* common */
24 #include "game.h"
26 /* client */
27 #include "client_main.h"
28 #include "control.h"
29 #include "goto.h"
30 #include "text.h"
32 // gui-qt
33 #include "sprite.h"
34 #include "qtg_cxxside.h"
35 #include "gotodlg.h"
37 /***************************************************************************
38 Constructor for goto_dialog
39 ***************************************************************************/
40 goto_dialog::goto_dialog(QWidget *parent)
42 QStringList headers_lst;
43 QHBoxLayout *hb;
45 setParent(parent);
46 headers_lst << QString(_("City")) << QString(_("Nation"))
47 << QString(_("Airlift"));
48 goto_tab = new QTableWidget;
49 goto_city = new QPushButton(_("&Goto"));
50 airlift_city = new QPushButton(_("&Airlift"));
51 close_but = new QPushButton(_("&Close"));
52 layout = new QGridLayout;
54 show_all = new QCheckBox;
55 show_all->setChecked(false);
56 show_all_label = new QLabel(_("Show All Cities"));
57 show_all_label->setAlignment(Qt::AlignLeft);
58 hb = new QHBoxLayout;
59 hb->addWidget(show_all);
60 hb->addWidget(show_all_label, Qt::AlignLeft);
62 goto_tab->setProperty("showGrid", "false");
63 goto_tab->setSelectionBehavior(QAbstractItemView::SelectRows);
64 goto_tab->setEditTriggers(QAbstractItemView::NoEditTriggers);
65 goto_tab->verticalHeader()->setVisible(false);
66 goto_tab->horizontalHeader()->setVisible(true);
67 goto_tab->setSelectionMode(QAbstractItemView::SingleSelection);
68 goto_tab->setColumnCount(3);
69 goto_tab->setHorizontalHeaderLabels(headers_lst);
70 goto_tab->setSortingEnabled(true);
71 goto_tab->horizontalHeader()->setSectionResizeMode(
72 QHeaderView::ResizeToContents);
74 layout->addWidget(goto_tab, 0, 0, 4, 4);
75 layout->addItem(hb, 4, 0, 1, 2);
76 layout->addWidget(goto_city, 5, 0, 1, 1);
77 layout->addWidget(airlift_city, 5, 1, 1, 1);
78 layout->addWidget(close_but, 5, 3, 1, 1);
80 setFixedWidth(goto_tab->horizontalHeader()->width());
81 connect(close_but, SIGNAL(clicked()), SLOT(close_dlg()));
82 connect(goto_city, SIGNAL(clicked()), SLOT(go_to_city()));
83 connect(airlift_city, SIGNAL(clicked()), SLOT(airlift_to()));
84 connect(show_all, SIGNAL(stateChanged(int)),
85 SLOT(checkbox_changed(int)));
86 connect(goto_tab->selectionModel(),
87 SIGNAL(selectionChanged(const QItemSelection &,
88 const QItemSelection &)),
89 SLOT(item_selected(const QItemSelection &,
90 const QItemSelection &)));
92 setLayout(layout);
93 original_tile = NULL;
94 setFocus();
97 /***************************************************************************
98 Sets variables which must be destroyed later
99 ***************************************************************************/
100 void goto_dialog::init()
102 if (original_tile) {
103 tile_virtual_destroy(original_tile);
105 original_tile = tile_virtual_new(get_center_tile_mapcanvas());
108 /***************************************************************************
109 Destructor for goto dialog
110 ***************************************************************************/
111 goto_dialog::~goto_dialog()
113 if (original_tile) {
114 tile_virtual_destroy(original_tile);
118 /***************************************************************************
119 Slot for checkbox 'all nations'
120 ***************************************************************************/
121 void goto_dialog::checkbox_changed(int state)
123 update_dlg();
126 /***************************************************************************
127 User has chosen some city on table
128 ***************************************************************************/
129 void goto_dialog::item_selected(const QItemSelection &sl,
130 const QItemSelection &ds)
132 int i;
133 int city_id;
134 QModelIndex index;
135 QModelIndexList indexes = sl.indexes();
136 QTableWidgetItem *item;
137 struct city *dest;
138 bool can_airlift;
140 if (indexes.isEmpty()) {
141 return;
143 index = indexes.at(0);
144 i = index.row();
145 item = goto_tab->item(i, 0);
146 city_id = item->data(Qt::UserRole).toInt();
147 dest = game_city_by_number(city_id);
148 center_tile_mapcanvas(city_tile(dest));
149 can_airlift = false;
150 unit_list_iterate(get_units_in_focus(), punit) {
151 if (unit_can_airlift_to(punit, dest)) {
152 can_airlift = true;
153 break;
155 } unit_list_iterate_end;
157 if (can_airlift) {
158 airlift_city->setEnabled(true);
159 } else {
160 airlift_city->setDisabled(true);
165 /***************************************************************************
166 Sorts dialog by default column (0)
167 ***************************************************************************/
168 void goto_dialog::sort_def()
170 goto_tab->sortByColumn(0, Qt::AscendingOrder);
173 /***************************************************************************
174 Shows and moves widget
175 ***************************************************************************/
176 void goto_dialog::show_me()
178 QPoint p, final_p;
179 p = QCursor::pos();
180 p = parentWidget()->mapFromGlobal(p);
181 final_p.setX(p.x());
182 final_p.setY(p.y());
183 if (p.x() + width() > parentWidget()->width()) {
184 final_p.setX(parentWidget()->width() - width());
186 if (p.y() - height() < 0) {
187 final_p.setY(height());
189 move(final_p.x(), final_p.y() - height());
190 show();
193 /***************************************************************************
194 Updates table in widget
195 ***************************************************************************/
196 void goto_dialog::update_dlg()
198 goto_tab->clearContents();
199 goto_tab->setRowCount(0);
200 goto_tab->setSortingEnabled(false);
201 if (show_all->isChecked()) {
202 players_iterate(pplayer) {
203 fill_tab(pplayer);
204 } players_iterate_end;
205 } else {
206 fill_tab(client_player());
208 goto_tab->setSortingEnabled(true);
209 goto_tab->horizontalHeader()->setStretchLastSection(false);
210 goto_tab->resizeRowsToContents();
211 goto_tab->horizontalHeader()->setStretchLastSection(true);
214 /***************************************************************************
215 Helper for function for filling table
216 ***************************************************************************/
217 void goto_dialog::fill_tab(player *pplayer)
219 int i;
221 QString str;
222 const char *at;
223 QFont f = QApplication::font();
224 QFontMetrics fm(f);
225 int h;
226 struct sprite *sprite;
227 QPixmap *pix;
228 QPixmap pix_scaled;
229 QTableWidgetItem *item;
232 h = fm.height() + 6;
233 i = goto_tab->rowCount();
234 city_list_iterate(pplayer->cities, pcity) {
235 goto_tab->insertRow(i);
236 for (int j = 0; j < 3; j++) {
237 item = new QTableWidgetItem;
238 switch (j) {
239 case 0:
240 str = city_name_get(pcity);
241 break;
242 case 1:
243 sprite = get_nation_flag_sprite(tileset, nation_of_player(pplayer));
244 if (sprite != NULL) {
245 pix = sprite->pm;
246 pix_scaled = pix->scaledToHeight(h);
247 item->setData(Qt::DecorationRole, pix_scaled);
249 str = nation_adjective_translation(nation_of_player(pplayer));
250 break;
251 case 2:
252 at = get_airlift_text(get_units_in_focus(), pcity);
253 if (at == NULL) {
254 str = "-";
255 } else {
256 str = at;
258 item->setTextAlignment(Qt::AlignHCenter);
259 break;
261 item->setText(str);
262 item->setData(Qt::UserRole, pcity->id);
263 goto_tab->setItem(i, j, item);
265 i++;
266 } city_list_iterate_end;
269 /***************************************************************************
270 Slot for airlifting unit
271 ***************************************************************************/
272 void goto_dialog::airlift_to()
274 struct city *pdest;
276 if (goto_tab->currentRow() == -1) {
277 return;
279 pdest = game_city_by_number(goto_tab->item(goto_tab->currentRow(),
280 0)->data(Qt::UserRole).toInt());
281 if (pdest) {
282 unit_list_iterate(get_units_in_focus(), punit) {
283 if (unit_can_airlift_to(punit, pdest)) {
284 request_unit_airlift(punit, pdest);
286 } unit_list_iterate_end;
290 /***************************************************************************
291 Slot for goto for city
292 ***************************************************************************/
293 void goto_dialog::go_to_city()
295 struct city *pdest;
297 if (goto_tab->currentRow() == -1) {
298 return;
301 pdest = game_city_by_number(goto_tab->item(goto_tab->currentRow(),
302 0)->data(Qt::UserRole).toInt());
303 if (pdest) {
304 unit_list_iterate(get_units_in_focus(), punit) {
305 send_goto_tile(punit, pdest->tile);
306 } unit_list_iterate_end;
310 /***************************************************************************
311 Slot for hiding dialog
312 ***************************************************************************/
313 void goto_dialog::close_dlg()
315 center_tile_mapcanvas(original_tile);
316 hide();
319 /***************************************************************************
320 Paints rectangles for goto_dialog
321 ***************************************************************************/
322 void goto_dialog::paint(QPainter *painter, QPaintEvent *event)
324 painter->setBrush(QColor(0, 0, 30, 85));
325 painter->drawRect(0, 0, width(), height());
326 painter->setBrush(QColor(0, 0, 0, 85));
327 painter->drawRect(5, 5, width() - 10, height() - 10);
330 /***************************************************************************
331 Paint event for goto_dialog
332 ***************************************************************************/
333 void goto_dialog::paintEvent(QPaintEvent *event)
335 QPainter painter;
337 painter.begin(this);
338 paint(&painter, event);
339 painter.end();
342 /**************************************************************************
343 Popup a dialog to have the focus unit goto to a city.
344 **************************************************************************/
345 void popup_goto_dialog(void)
347 if (C_S_RUNNING != client_state()) {
348 return;
350 if (get_num_units_in_focus() == 0) {
351 return;
353 if (!client_has_player()) {
354 return;
357 if (gui()->gtd != NULL) {
358 gui()->gtd->init();
359 gui()->gtd->update_dlg();
360 gui()->gtd->sort_def();
361 gui()->gtd->show_me();