Freeciv21
Develop your civilization from humble roots to a global empire
spaceshipdlg.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 1996-2020 Freeciv21 and Freeciv contributors. This file is
3  part of Freeciv21. Freeciv21 is free software: you can redistribute it
4  and/or modify it under the terms of the GNU General Public License as
5  published by the Free Software Foundation, either version 3 of the
6  License, or (at your option) any later version. You should have received
7  a copy of the GNU General Public License along with Freeciv21. If not,
8  see https://www.gnu.org/licenses/.
9  */
10 
11 // Qt
12 #include <QGridLayout>
13 #include <QLabel>
14 #include <QPainter>
15 #include <QPushButton>
16 
17 // common
18 #include "game.h"
19 #include "victory.h"
20 // client
21 #include "client_main.h"
22 #include "colors_common.h"
23 #include "fc_client.h"
24 #include "page_game.h"
25 #include "qtg_cxxside.h"
26 #include "spaceshipdlg.h"
27 #include "spaceshipdlg_g.h"
28 
29 class QGridLayout;
30 
31 namespace /* anonymous */ {
35 QSize get_spaceship_dimensions()
36 {
38 }
39 
43 void put_spaceship(QPixmap *pcanvas, const struct player *pplayer)
44 {
45  int i, x, y;
46  const struct player_spaceship *ship = &pplayer->spaceship;
47  const QPixmap *spr;
48  struct tileset *t = tileset;
49 
51  const int w = spr->width(), h = spr->height();
52 
53  QPainter p(pcanvas);
54  p.fillRect(0, 0, w * 7, h * 7,
55  get_color(tileset, COLOR_SPACESHIP_BACKGROUND));
56 
57  for (i = 0; i < NUM_SS_MODULES; i++) {
58  const int j = i / 3;
59  const int k = i % 3;
60 
61  if ((k == 0 && j >= ship->habitation)
62  || (k == 1 && j >= ship->life_support)
63  || (k == 2 && j >= ship->solar_panels)) {
64  continue;
65  }
66  x = modules_info[i].x * w / 4 - w / 2;
67  y = modules_info[i].y * h / 4 - h / 2;
68 
69  spr = (k == 0 ? get_spaceship_sprite(t, SPACESHIP_HABITATION)
72  p.drawPixmap(x, y, *spr);
73  }
74 
75  for (i = 0; i < NUM_SS_COMPONENTS; i++) {
76  const int j = i / 2;
77  const int k = i % 2;
78 
79  if ((k == 0 && j >= ship->fuel) || (k == 1 && j >= ship->propulsion)) {
80  continue;
81  }
82  x = components_info[i].x * w / 4 - w / 2;
83  y = components_info[i].y * h / 4 - h / 2;
84 
85  spr = ((k == 0) ? get_spaceship_sprite(t, SPACESHIP_FUEL)
87 
88  p.drawPixmap(x, y, *spr);
89 
90  if (k && ship->state == SSHIP_LAUNCHED) {
92  p.drawPixmap(x + w, y, *spr);
93  }
94  }
95 
96  for (i = 0; i < NUM_SS_STRUCTURALS; i++) {
97  if (!BV_ISSET(ship->structure, i)) {
98  continue;
99  }
100  x = structurals_info[i].x * w / 4 - w / 2;
101  y = structurals_info[i].y * h / 4 - h / 2;
102 
104  p.drawPixmap(x, y, *spr);
105  }
106 
107  p.end();
108 }
109 } // anonymous namespace
110 
114 ss_report::ss_report(struct player *pplayer)
115 {
116  setAttribute(Qt::WA_DeleteOnClose);
117  player = pplayer;
118  can = new QPixmap(get_spaceship_dimensions());
119 
120  QGridLayout *layout = new QGridLayout;
121  ss_pix_label = new QLabel;
122  ss_pix_label->setPixmap(*can);
123  layout->addWidget(ss_pix_label, 0, 0, 3, 3);
124  ss_label = new QLabel;
125  layout->addWidget(ss_label, 0, 3, 3, 1);
126  launch_button = new QPushButton(_("Launch"));
127  connect(launch_button, &QAbstractButton::clicked, this,
129  layout->addWidget(launch_button, 4, 3, 1, 1);
130  setLayout(layout);
131  update_report();
132 }
133 
138 {
139  queen()->removeRepoDlg(QStringLiteral("SPS"));
140  delete can;
141 }
142 
147 {
148  int index;
149  queen()->gimmePlace(this, QStringLiteral("SPS"));
150  index = queen()->addGameTab(this);
151  queen()->game_tab_widget->setCurrentIndex(index);
152  update_report();
153 }
154 
159 {
160  QString ch;
161  struct player_spaceship *pship;
162 
163  pship = &(player->spaceship);
164 
166  && pship->state == SSHIP_STARTED && pship->success_rate > 0.0) {
167  launch_button->setEnabled(true);
168  } else {
169  launch_button->setEnabled(false);
170  }
172  ss_label->setText(ch);
173  put_spaceship(can, player);
174  ss_pix_label->setPixmap(*can);
175  update();
176 }
177 
181 void ss_report::launch() { send_packet_spaceship_launch(&client.conn); }
182 
186 void popup_spaceship_dialog(struct player *pplayer)
187 {
188  ss_report *ss_rep;
189  int i;
190  QWidget *w;
191 
193  return;
194  }
195  if (!queen()->isRepoDlgOpen(QStringLiteral("SPS"))) {
196  ss_rep = new ss_report(pplayer);
197  ss_rep->init();
198  } else {
199  i = queen()->gimmeIndexOf(QStringLiteral("SPS"));
200  fc_assert(i != -1);
201  if (queen()->game_tab_widget->currentIndex() == i) {
202  return;
203  }
204  w = queen()->game_tab_widget->widget(i);
205  ss_rep = reinterpret_cast<ss_report *>(w);
206  queen()->game_tab_widget->setCurrentWidget(ss_rep);
207  }
208 }
209 
213 void popdown_spaceship_dialog(struct player *pplayer)
214 { // PORTME
215 }
216 
220 void refresh_spaceship_dialog(struct player *pplayer)
221 {
222  int i;
223  ss_report *ss_rep;
224  QWidget *w;
225 
226  if (!queen()->isRepoDlgOpen(QStringLiteral("SPS"))) {
227  return;
228  } else {
229  i = queen()->gimmeIndexOf(QStringLiteral("SPS"));
230  fc_assert(i != -1);
231  w = queen()->game_tab_widget->widget(i);
232  ss_rep = reinterpret_cast<ss_report *>(w);
233  queen()->game_tab_widget->setCurrentWidget(ss_rep);
234  ss_rep->update_report();
235  }
236 }
237 
242 {
243  int i;
244  ss_report *ss_rep;
245  QWidget *w;
246 
247  if (!queen()->isRepoDlgOpen(QStringLiteral("SPS"))) {
248  return;
249  } else {
250  i = queen()->gimmeIndexOf(QStringLiteral("SPS"));
251  fc_assert(i != -1);
252  w = queen()->game_tab_widget->widget(i);
253  ss_rep = reinterpret_cast<ss_report *>(w);
254  ss_rep->deleteLater();
255  }
256 }
bool BV_ISSET(const BV &bv, int bit)
Definition: bitvector.h:37
int gimmeIndexOf(const QString &str)
Returns index on game tab page of given report dialog.
Definition: page_game.cpp:706
int addGameTab(QWidget *widget)
Inserts tab widget to game view page.
Definition: page_game.cpp:670
void removeRepoDlg(const QString &str)
Removes report dialog string from the list marking it as closed.
Definition: page_game.cpp:723
void gimmePlace(QWidget *widget, const QString &str)
Finds not used index on game_view_tab and returns it.
Definition: page_game.cpp:690
fc_game_tab_widget * game_tab_widget
Definition: page_game.h:72
QPixmap * can
Definition: spaceshipdlg.h:29
QLabel * ss_pix_label
Definition: spaceshipdlg.h:27
QLabel * ss_label
Definition: spaceshipdlg.h:28
~ss_report() override
Destructor for spaceship report.
void launch()
Launch spaceship.
QPushButton * launch_button
Definition: spaceshipdlg.h:26
void update_report()
Updates spaceship report.
ss_report(struct player *pplayer)
Constructor for spaceship report.
void init()
Initializes widget on game_tab_widget.
bool client_is_global_observer()
Returns whether client is global observer.
struct civclient client
QColor get_color(const struct tileset *t, enum color_std stdcolor)
Return a pointer to the given "standard" color.
@ VC_SPACERACE
Definition: fc_types.h:1083
#define _(String)
Definition: fcintl.h:50
#define fc_assert(condition)
Definition: log.h:89
pageGame * queen()
Return game instandce.
Definition: page_game.cpp:557
const struct sship_part_info structurals_info[NUM_SS_STRUCTURALS]
Definition: spaceship.cpp:18
const struct sship_part_info modules_info[NUM_SS_MODULES]
Definition: spaceship.cpp:36
const struct sship_part_info components_info[NUM_SS_COMPONENTS]
Definition: spaceship.cpp:28
#define NUM_SS_MODULES
Definition: spaceship.h:86
#define NUM_SS_COMPONENTS
Definition: spaceship.h:85
#define NUM_SS_STRUCTURALS
Definition: spaceship.h:84
@ SSHIP_STARTED
Definition: spaceship.h:79
@ SSHIP_LAUNCHED
Definition: spaceship.h:80
void popdown_spaceship_dialog(struct player *pplayer)
Close the spaceship dialog for the given player.
void popdown_all_spaceships_dialogs()
Close all spaceships dialogs.
void popup_spaceship_dialog(struct player *pplayer)
Popup (or raise) the spaceship dialog for the given player.
void refresh_spaceship_dialog(struct player *pplayer)
Refresh (update) the spaceship dialog for the given player.
struct connection conn
Definition: client_main.h:89
struct player * playing
Definition: connection.h:142
bv_spaceship_structure structure
Definition: spaceship.h:97
double success_rate
Definition: spaceship.h:112
enum spaceship_state state
Definition: spaceship.h:105
Definition: player.h:231
struct player_spaceship spaceship
Definition: player.h:268
const QString get_spaceship_descr(struct player_spaceship *pship)
Returns a description of the given spaceship.
Definition: text.cpp:1197
struct tileset * tileset
Definition: tilespec.cpp:276
const QPixmap * get_spaceship_sprite(const struct tileset *t, enum spaceship_part part)
Return the sprite for drawing the given spaceship part.
Definition: tilespec.cpp:3324
@ SPACESHIP_STRUCTURAL
Definition: tilespec.h:182
@ SPACESHIP_HABITATION
Definition: tilespec.h:181
@ SPACESHIP_EXHAUST
Definition: tilespec.h:185
@ SPACESHIP_FUEL
Definition: tilespec.h:183
@ SPACESHIP_PROPULSION
Definition: tilespec.h:184
@ SPACESHIP_SOLAR_PANEL
Definition: tilespec.h:179
@ SPACESHIP_LIFE_SUPPORT
Definition: tilespec.h:180
bool victory_enabled(enum victory_condition_type victory)
Whether victory condition is enabled.
Definition: victory.cpp:20