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)
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 ***********************************************************************/
15 #include <fc_config.h>
19 #include <QApplication>
20 #include <QHeaderView>
21 #include <QTableWidget>
27 #include "client_main.h"
34 #include "qtg_cxxside.h"
37 /***************************************************************************
38 Constructor for goto_dialog
39 ***************************************************************************/
40 goto_dialog::goto_dialog(QWidget
*parent
)
42 QStringList headers_lst
;
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
);
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
&)));
97 /***************************************************************************
98 Sets variables which must be destroyed later
99 ***************************************************************************/
100 void goto_dialog::init()
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()
114 tile_virtual_destroy(original_tile
);
118 /***************************************************************************
119 Slot for checkbox 'all nations'
120 ***************************************************************************/
121 void goto_dialog::checkbox_changed(int state
)
126 /***************************************************************************
127 User has chosen some city on table
128 ***************************************************************************/
129 void goto_dialog::item_selected(const QItemSelection
&sl
,
130 const QItemSelection
&ds
)
135 QModelIndexList indexes
= sl
.indexes();
136 QTableWidgetItem
*item
;
140 if (indexes
.isEmpty()) {
143 index
= indexes
.at(0);
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
));
150 unit_list_iterate(get_units_in_focus(), punit
) {
151 if (unit_can_airlift_to(punit
, dest
)) {
155 } unit_list_iterate_end
;
158 airlift_city
->setEnabled(true);
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()
180 p
= parentWidget()->mapFromGlobal(p
);
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());
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
) {
204 } players_iterate_end
;
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
)
223 QFont f
= QApplication::font();
226 struct sprite
*sprite
;
229 QTableWidgetItem
*item
;
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
;
240 str
= city_name_get(pcity
);
243 sprite
= get_nation_flag_sprite(tileset
, nation_of_player(pplayer
));
244 if (sprite
!= NULL
) {
246 pix_scaled
= pix
->scaledToHeight(h
);
247 item
->setData(Qt::DecorationRole
, pix_scaled
);
249 str
= nation_adjective_translation(nation_of_player(pplayer
));
252 at
= get_airlift_text(get_units_in_focus(), pcity
);
258 item
->setTextAlignment(Qt::AlignHCenter
);
262 item
->setData(Qt::UserRole
, pcity
->id
);
263 goto_tab
->setItem(i
, j
, item
);
266 } city_list_iterate_end
;
269 /***************************************************************************
270 Slot for airlifting unit
271 ***************************************************************************/
272 void goto_dialog::airlift_to()
276 if (goto_tab
->currentRow() == -1) {
279 pdest
= game_city_by_number(goto_tab
->item(goto_tab
->currentRow(),
280 0)->data(Qt::UserRole
).toInt());
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()
297 if (goto_tab
->currentRow() == -1) {
301 pdest
= game_city_by_number(goto_tab
->item(goto_tab
->currentRow(),
302 0)->data(Qt::UserRole
).toInt());
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
);
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
)
338 paint(&painter
, event
);
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()) {
350 if (get_num_units_in_focus() == 0) {
353 if (!client_has_player()) {
357 if (gui()->gtd
!= NULL
) {
359 gui()->gtd
->update_dlg();
360 gui()->gtd
->sort_def();
361 gui()->gtd
->show_me();