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

Freeciv-3.3

Loading...
Searching...
No Matches
ratesdlg.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 <QGroupBox>
21#include <QMouseEvent>
22#include <QPainter>
23#include <QScreen>
24#include <QVBoxLayout>
25
26// common
27#include "multipliers.h"
28
29// gui-qt
30#include "fc_client.h"
31#include "dialogs.h"
32#include "qtg_cxxside.h"
33#include "sprite.h"
34
35#include "ratesdlg.h"
36
37static int scale_to_mult(const struct multiplier *pmul, int scale);
38static int mult_to_scale(const struct multiplier *pmul, int val);
39
40/**********************************************************************/
44tax_rates_dialog::tax_rates_dialog(QWidget *parent)
45 : qfc_dialog(parent)
46{
47 QHBoxLayout *some_layout;
48 QVBoxLayout *main_layout;
49 QPushButton *cancel_button;
50 QPushButton *ok_button;
51 QPushButton *apply_button;
52 QLabel *l1, *l2;
53 QString str;
54 int max;
55
56 setWindowTitle(_("Tax rates"));
57 main_layout = new QVBoxLayout;
58
59 if (client.conn.playing != nullptr) {
60 max = get_player_bonus(client.conn.playing, EFT_MAX_RATES);
61 } else {
62 max = 100;
63 }
64
65 // TRANS: Government - max rate (of taxes) x%
66 str = QString(_("%1 - max rate: %2%")).
67 arg(government_name_for_player(client.conn.playing),
68 QString::number(max));
69
70 l2 = new QLabel(_("Select tax, luxury and science rates"));
71 l1 = new QLabel(str);
72 l1->setAlignment(Qt::AlignHCenter);
73 l2->setAlignment(Qt::AlignHCenter);
74 main_layout->addWidget(l2);
75 main_layout->addWidget(l1);
76 main_layout->addSpacing(20);
77
78 cancel_button = new QPushButton(_("Cancel"));
79 ok_button = new QPushButton(_("Ok"));
80 apply_button = new QPushButton(_("Apply"));
81 some_layout = new QHBoxLayout;
82 connect(cancel_button, &QAbstractButton::pressed,
83 this, &tax_rates_dialog::slot_cancel_button_pressed);
84 connect(apply_button, &QAbstractButton::pressed,
85 this, &tax_rates_dialog::slot_apply_button_pressed);
86 connect(ok_button, &QAbstractButton::pressed,
87 this, &tax_rates_dialog::slot_ok_button_pressed);
88 some_layout->addWidget(cancel_button);
89 some_layout->addWidget(apply_button);
90 some_layout->addWidget(ok_button);
91 fcde = new fc_double_edge(this);
92 main_layout->addWidget(fcde);
93 main_layout->addSpacing(20);
94 main_layout->addLayout(some_layout);
95 setLayout(main_layout);
96}
97
98/**********************************************************************/
101 void tax_rates_dialog::slot_cancel_button_pressed()
102{
103 delete this;
104}
105
106/**********************************************************************/
109 void tax_rates_dialog::slot_ok_button_pressed()
110{
111 dsend_packet_player_rates(&client.conn, 10 * fcde->current_min,
112 10 * (10 - fcde->current_max),
113 10 * (fcde->current_max - fcde->current_min));
114 delete this;
115}
116
117/**********************************************************************/
120 void tax_rates_dialog::slot_apply_button_pressed()
121{
122 dsend_packet_player_rates(&client.conn, 10 * fcde->current_min,
123 10 * (10 - fcde->current_max),
124 10 * (fcde->current_max - fcde->current_min));
125}
126
127/**********************************************************************/
131multipler_rates_dialog::multipler_rates_dialog(QWidget *parent)
132 : QDialog(parent)
133{
134 QGroupBox *group_box;
135 QHBoxLayout *some_layout;
136 QLabel *label;
137 QSlider *slider;
138 QVBoxLayout *main_layout;
139 struct player *pplayer = client_player();
140
141 cancel_button = new QPushButton;
142 ok_button = new QPushButton;
143 setWindowTitle(_("Change policies"));
144 main_layout = new QVBoxLayout;
145
146 multipliers_iterate(pmul) {
147 QHBoxLayout *hb = new QHBoxLayout;
148 int val = player_multiplier_target_value(pplayer, pmul);
149
150 group_box = new QGroupBox(multiplier_name_translation(pmul));
151 slider = new QSlider(Qt::Horizontal, this);
152 slider->setMinimum(mult_to_scale(pmul, pmul->start));
153 slider->setMaximum(mult_to_scale(pmul, pmul->stop));
154 slider->setValue(mult_to_scale(pmul, val));
155 connect(slider, &QAbstractSlider::valueChanged,
156 this, &multipler_rates_dialog::slot_set_value);
157 slider_list.append(slider);
158 label = new QLabel(QString::number(mult_to_scale(pmul, val)));
159 hb->addWidget(slider);
160 slider->setEnabled(multiplier_can_be_changed(pmul, client_player()));
161 hb->addWidget(label);
162 group_box->setLayout(hb);
163 slider->setProperty("lab", QVariant::fromValue((void *) label));
164 main_layout->addWidget(group_box);
165
166 } multipliers_iterate_end;
167 some_layout = new QHBoxLayout;
168 cancel_button->setText(_("Cancel"));
169 ok_button->setText(_("Ok"));
170 connect(cancel_button, &QAbstractButton::pressed,
171 this, &multipler_rates_dialog::slot_cancel_button_pressed);
172 connect(ok_button, &QAbstractButton::pressed,
173 this, &multipler_rates_dialog::slot_ok_button_pressed);
174 some_layout->addWidget(cancel_button);
175 some_layout->addWidget(ok_button);
176 main_layout->addSpacing(20);
177 main_layout->addLayout(some_layout);
178 setLayout(main_layout);
179}
180
181/**********************************************************************/
184 void multipler_rates_dialog::slot_set_value(int i)
185{
186 QSlider *qo;
187 qo = (QSlider *) QObject::sender();
188 QVariant qvar;
189 QLabel *lab;
190
191 qvar = qo->property("lab");
192 lab = reinterpret_cast<QLabel *>(qvar.value<void *>());
193 lab->setText(QString::number(qo->value()));
194}
195
196/**********************************************************************/
199 void multipler_rates_dialog::slot_cancel_button_pressed()
200{
201 close();
202 deleteLater();
203}
204
205/**********************************************************************/
208 void multipler_rates_dialog::slot_ok_button_pressed()
209{
210 int j = 0;
211 int value;
212 struct packet_player_multiplier mul;
213
214 multipliers_iterate(pmul) {
215 Multiplier_type_id i = multiplier_index(pmul);
216
217 value = slider_list.at(j)->value();
218 mul.multipliers[i] = scale_to_mult(pmul, value);
219 j++;
220 } multipliers_iterate_end;
221 mul.count = multiplier_count();
222 send_packet_player_multiplier(&client.conn, &mul);
223 close();
224 deleteLater();
225}
226
227/**********************************************************************/
230 int mult_to_scale(const struct multiplier *pmul, int val)
231{
232 return (val - pmul->start) / pmul->step;
233}
234
235/**********************************************************************/
238 int scale_to_mult(const struct multiplier *pmul, int scale)
239{
240 return scale * pmul->step + pmul->start;
241}
242
243/**********************************************************************/
246 void popup_rates_dialog(void)
247{
248 QRect rect = QApplication::primaryScreen()->availableGeometry();
249 QPoint p;
250 tax_rates_dialog *trd;
251
252 p = QCursor::pos();
253 trd = new tax_rates_dialog(gui()->central_wdg);
254 p.setY(p.y() - trd->height() / 2);
255 if (p.y() < 50) {
256 p.setY(50);
257 }
258 if (p.y() + trd->height() > rect.bottom()) {
259 p.setY(rect.bottom() - trd->height());
260 }
261 if (p.x() + trd->width() > rect.right()) {
262 p.setX(rect.right() - trd->width());
263 }
264 trd->move(p);
265 trd->show();
266}
267
268/**********************************************************************/
271 void real_multipliers_dialog_update(void *unused)
272{
273 // PORTME
274}
275
276/**********************************************************************/
279 void popup_multiplier_dialog(void)
280{
281 multipler_rates_dialog *mrd;
282
283 if (!can_client_issue_orders()) {
284 return;
285 }
286 mrd = new multipler_rates_dialog(gui()->central_wdg);
287 mrd->show();
288}
289
290/**********************************************************************/
293fc_double_edge::fc_double_edge(QWidget *parent)
294 : QWidget(parent)
295{
296
297 current_min = client.conn.playing->economic.tax / 10;
298 current_max = 10 - (client.conn.playing->economic.luxury / 10);
299 mouse_x = 0.;
300 moved = 0;
301 on_min = false;
302 on_max = false;
303
304 if (client.conn.playing != nullptr) {
305 max_rates = get_player_bonus(client.conn.playing, EFT_MAX_RATES) / 10;
306 } else {
307 max_rates = 10;
308 }
309 cursor_pix = *fc_icons::instance()->get_pixmap("control");
310 setMouseTracking(true);
311}
312
313/**********************************************************************/
316fc_double_edge::~fc_double_edge()
317{
318}
319
320/**********************************************************************/
323 QSize fc_double_edge::sizeHint() const
324{
325 return QSize(30 * get_tax_sprite(tileset, O_LUXURY)->pm->width(),
326 3 * get_tax_sprite(tileset, O_LUXURY)->pm->height());
327}
328
329/**********************************************************************/
332 void fc_double_edge::paintEvent(QPaintEvent *event)
333{
334 QPainter p;
335 int i, j, pos;
336 QPixmap *pix;
337 QPixmap pix_scaled;
338 QSize s;
339 double x_min, x_max;
340
341 cursor_pix = cursor_pix.scaled(width() / 20, height());
342 cursor_size = cursor_pix.width();
343 p.begin(this);
344 p.setRenderHint(QPainter::TextAntialiasing);
345 p.setBrush(Qt::SolidPattern);
346 p.setPen(Qt::SolidLine);
347
348 x_min = static_cast<double>(current_min) / 10 *
349 ((width() - 1) - 2 * cursor_size) + cursor_size;
350 x_max = static_cast<double>(current_max) / 10 *
351 ((width() - 1) - 2 * cursor_size) + cursor_size;
352
353 pos = cursor_size;
354 pix = get_tax_sprite(tileset, O_GOLD)->pm;
355 s.setWidth((width() - 2 * cursor_size) / 10);
356 s.setHeight(height());
357 pix_scaled = pix->scaled(s, Qt::IgnoreAspectRatio,
358 Qt::SmoothTransformation);
359 for (i = 0; i < current_min; i++) {
360 p.drawPixmap(pos, 0, pix_scaled);
361 pos = pos + pix_scaled.width();
362 }
363 j = i;
364 pix = get_tax_sprite(tileset, O_SCIENCE)->pm;
365 pix_scaled = pix->scaled(s, Qt::IgnoreAspectRatio,
366 Qt::SmoothTransformation);
367 for (i = j; i < current_max; i++) {
368 p.drawPixmap(pos, 0, pix_scaled);
369 pos = pos + pix_scaled.width();
370 }
371 j = i;
372 pix = get_tax_sprite(tileset, O_LUXURY)->pm;
373 pix_scaled = pix->scaled(s, Qt::IgnoreAspectRatio,
374 Qt::SmoothTransformation);
375 for (i = j; i < 10; i++) {
376 p.drawPixmap(pos, 0, pix_scaled);
377 pos = pos + pix_scaled.width();
378 }
379 p.drawPixmap(x_max - cursor_size / 2, 0, cursor_pix);
380 p.drawPixmap(x_min - cursor_size / 2, 0, cursor_pix);
381 p.end();
382}
383
384/**********************************************************************/
387 void fc_double_edge::mousePressEvent(QMouseEvent *event)
388{
389 if (event->buttons() & Qt::LeftButton) {
390 mouse_x = static_cast<double>(event->pos().x());
391
392 if (mouse_x <= current_max * width() / 10 - 2 * cursor_size) {
393 moved = 1;
394 } else {
395 moved = 2;
396 }
397 } else {
398 moved = 0;
399 }
400 mouseMoveEvent(event);
401 update();
402}
403
404/**********************************************************************/
407 void fc_double_edge::mouseMoveEvent(QMouseEvent *event)
408{
409 float x_min, x_max, x_mouse;
410
411 if (on_max || on_min) {
412 setCursor(Qt::SizeHorCursor);
413 } else {
414 setCursor(Qt::ArrowCursor);
415 }
416
417 x_mouse = static_cast<float>(event->pos().x());
418 x_min = static_cast<float>(current_min) / 10 *
419 ((width() - 1) - 2 * cursor_size) + cursor_size;
420 x_max = static_cast<float>(current_max) / 10 *
421 ((width() - 1) - 2 * cursor_size) + cursor_size;
422
423 on_min = (((x_mouse > (x_min - cursor_size * 1.1))
424 && (x_mouse < (x_min + cursor_size * 1.1)))
425 && (!on_max))
426 || (moved == 1);
427 on_max = (((x_mouse > (x_max - cursor_size * 1.1))
428 && (x_mouse < (x_max + cursor_size * 1.1)))
429 && !on_min)
430 || (moved == 2);
431 if (event->buttons() & Qt::LeftButton) {
432 if ((moved != 2) && on_min) {
433 x_min = x_mouse * width() /
434 ((width() - 1) - 2 * cursor_size) - cursor_size;
435 if (x_min < 0) x_min = 0;
436 if (x_min > width()) x_min = width();
437 current_min = (x_min * 10 / (width() - 1));
438 if (current_min > max_rates) {
439 current_min = max_rates;
440 }
441 if (current_max < current_min) {
442 current_max = current_min;
443 }
444 if (current_max - current_min > max_rates) {
445 current_min = current_max - max_rates;
446 }
447 moved = 1;
448 } else if ((moved != 1) && on_max) {
449 x_max = x_mouse * width() /
450 ((width() - 1) - 2 * cursor_size) - cursor_size;
451 if (x_max < 0) {
452 x_max = 0;
453 }
454 if (x_max > width()) {
455 x_max = width();
456 }
457 current_max = (x_max * 10 / (width() - 1));
458 if (current_max > max_rates + current_min) {
459 current_max = max_rates + current_min;
460 }
461 if (current_max < 10 - max_rates) {
462 current_max = 10 - max_rates;
463 }
464 if (current_min > current_max) {
465 current_min = current_max;
466 }
467 moved = 2;
468 }
469 update();
470 } else {
471 moved = 0;
472 }
473
474 mouse_x = x_mouse;
475}
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
fc_double_edge
Definition ratesdlg.h:52
fc_double_edge::current_min
int current_min
Definition ratesdlg.h:66
fc_double_edge::max_rates
int max_rates
Definition ratesdlg.h:61
fc_double_edge::mouseMoveEvent
void mouseMoveEvent(QMouseEvent *event)
Definition ratesdlg.cpp:407
fc_double_edge::cursor_size
double cursor_size
Definition ratesdlg.h:56
fc_double_edge::paintEvent
void paintEvent(QPaintEvent *event)
Definition ratesdlg.cpp:332
fc_double_edge::moved
int moved
Definition ratesdlg.h:58
fc_double_edge::current_max
int current_max
Definition ratesdlg.h:67
fc_double_edge::sizeHint
QSize sizeHint() const
Definition ratesdlg.cpp:323
fc_double_edge::on_max
bool on_max
Definition ratesdlg.h:60
fc_double_edge::~fc_double_edge
~fc_double_edge()
Definition ratesdlg.cpp:316
fc_double_edge::mouse_x
double mouse_x
Definition ratesdlg.h:57
fc_double_edge::mousePressEvent
void mousePressEvent(QMouseEvent *event)
Definition ratesdlg.cpp:387
fc_double_edge::cursor_pix
QPixmap cursor_pix
Definition ratesdlg.h:62
fc_double_edge::fc_double_edge
fc_double_edge(QWidget *parent=nullptr)
Definition ratesdlg.cpp:293
fc_double_edge::on_min
bool on_min
Definition ratesdlg.h:59
fc_icons::get_pixmap
QPixmap * get_pixmap(const QString &id)
Definition fc_client.cpp:1048
fc_icons::instance
static fc_icons * instance()
Definition fc_client.cpp:997
multipler_rates_dialog
Definition ratesdlg.h:98
multipler_rates_dialog::slot_cancel_button_pressed
void slot_cancel_button_pressed()
Definition ratesdlg.cpp:199
multipler_rates_dialog::ok_button
QPushButton * ok_button
Definition ratesdlg.h:106
multipler_rates_dialog::slot_set_value
void slot_set_value(int i)
Definition ratesdlg.cpp:184
multipler_rates_dialog::slot_ok_button_pressed
void slot_ok_button_pressed()
Definition ratesdlg.cpp:208
multipler_rates_dialog::multipler_rates_dialog
multipler_rates_dialog(QWidget *parent=nullptr)
Definition ratesdlg.cpp:131
multipler_rates_dialog::slider_list
QList< QSlider * > slider_list
Definition ratesdlg.h:104
multipler_rates_dialog::cancel_button
QPushButton * cancel_button
Definition ratesdlg.h:105
qfc_dialog
Definition dialogs.h:74
tax_rates_dialog
Definition ratesdlg.h:80
tax_rates_dialog::fcde
fc_double_edge * fcde
Definition ratesdlg.h:87
tax_rates_dialog::slot_cancel_button_pressed
void slot_cancel_button_pressed()
Definition ratesdlg.cpp:101
tax_rates_dialog::tax_rates_dialog
tax_rates_dialog(QWidget *parent=nullptr)
Definition ratesdlg.cpp:44
tax_rates_dialog::slot_apply_button_pressed
void slot_apply_button_pressed()
Definition ratesdlg.cpp:120
tax_rates_dialog::slot_ok_button_pressed
void slot_ok_button_pressed()
Definition ratesdlg.cpp:109
client
struct civclient client
Definition client_main.c:150
can_client_issue_orders
bool can_client_issue_orders(void)
Definition client_main.c:1264
client_player
#define client_player()
Definition client_main.h:106
incite_cost
char * incite_cost
Definition comments.c:74
get_player_bonus
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:828
event
enum event_type event
Definition events.c:81
fc_client.h
O_SCIENCE
@ O_SCIENCE
Definition fc_types.h:101
O_LUXURY
@ O_LUXURY
Definition fc_types.h:101
O_GOLD
@ O_GOLD
Definition fc_types.h:101
Multiplier_type_id
int Multiplier_type_id
Definition fc_types.h:390
_
#define _(String)
Definition fcintl.h:67
government_name_for_player
const char * government_name_for_player(const struct player *pplayer)
Definition government.c:154
pos
static struct tile * pos
Definition finddlg.c:53
scale_to_mult
static int scale_to_mult(const struct multiplier *pmul, int scale)
Definition gamedlgs.c:227
mult_to_scale
static int mult_to_scale(const struct multiplier *pmul, int val)
Definition gamedlgs.c:219
dialogs.h
ratesdlg.h
sprite.h
gui
static mpgui * gui
Definition mpgui_qt.cpp:52
multiplier_count
Multiplier_type_id multiplier_count(void)
Definition multipliers.c:88
multiplier_name_translation
const char * multiplier_name_translation(const struct multiplier *pmul)
Definition multipliers.c:97
multiplier_can_be_changed
bool multiplier_can_be_changed(struct multiplier *pmul, struct player *pplayer)
Definition multipliers.c:137
multiplier_index
Multiplier_type_id multiplier_index(const struct multiplier *pmul)
Definition multipliers.c:80
multipliers.h
multipliers_iterate
#define multipliers_iterate(_mul_)
Definition multipliers.h:61
multipliers_iterate_end
#define multipliers_iterate_end
Definition multipliers.h:67
dsend_packet_player_rates
int dsend_packet_player_rates(struct connection *pc, int tax, int luxury, int science)
Definition packets_gen.c:25447
send_packet_player_multiplier
int send_packet_player_multiplier(struct connection *pc, const struct packet_player_multiplier *packet)
Definition packets_gen.c:26973
player_multiplier_target_value
int player_multiplier_target_value(const struct player *pplayer, const struct multiplier *pmul)
Definition player.c:1985
qtg_cxxside.h
scale_to_mult
static int scale_to_mult(const struct multiplier *pmul, int scale)
Definition ratesdlg.cpp:238
popup_multiplier_dialog
void popup_multiplier_dialog(void)
Definition ratesdlg.cpp:279
popup_rates_dialog
void popup_rates_dialog(void)
Definition ratesdlg.cpp:246
real_multipliers_dialog_update
void real_multipliers_dialog_update(void *unused)
Definition ratesdlg.cpp:271
mult_to_scale
static int mult_to_scale(const struct multiplier *pmul, int val)
Definition ratesdlg.cpp:230
scale
struct sprite int int int int struct sprite int int float scale
Definition sprite_g.h:33
civclient::conn
struct connection conn
Definition client_main.h:96
connection::playing
struct player * playing
Definition connection.h:151
multiplier
Definition multipliers.h:31
multiplier::start
int start
Definition multipliers.h:35
multiplier::step
int step
Definition multipliers.h:37
packet_player_multiplier
Definition packets_gen.h:606
player_economic::luxury
int luxury
Definition player.h:66
player_economic::tax
int tax
Definition player.h:64
player
Definition player.h:249
player::economic
struct player_economic economic
Definition player.h:284
sprite::pm
QPixmap * pm
Definition sprite.h:25
tileset
Definition tilespec.c:481
get_tax_sprite
struct sprite * get_tax_sprite(const struct tileset *t, Output_type_id otype)
Definition tilespec.c:6897