Freeciv-3.2
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/**********************************************************************/
46{
48 QVBoxLayout *main_layout;
49 QPushButton *cancel_button;
50 QPushButton *ok_button;
51 QPushButton *apply_button;
52 QLabel *l1, *l2;
54 int max;
55
56 setWindowTitle(_("Tax rates"));
57 main_layout = new QVBoxLayout;
58
59 if (client.conn.playing != nullptr) {
61 } else {
62 max = 100;
63 }
64
65 // TRANS: Government - max rate (of taxes) x%
66 str = QString(_("%1 - max rate: %2%")).
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"));
82 connect(cancel_button, &QAbstractButton::pressed,
84 connect(apply_button, &QAbstractButton::pressed,
86 connect(ok_button, &QAbstractButton::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/**********************************************************************/
102{
103 delete this;
104}
105
106/**********************************************************************/
110{
112 10 * (10 - fcde->current_max),
113 10 * (fcde->current_max - fcde->current_min));
114 delete this;
115}
116
117/**********************************************************************/
126
127/**********************************************************************/
132 : QDialog(parent)
133{
136 QLabel *label;
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) {
148 int val = player_multiplier_target_value(pplayer, pmul);
149
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,
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
168 cancel_button->setText(_("Cancel"));
169 ok_button->setText(_("Ok"));
170 connect(cancel_button, &QAbstractButton::pressed,
172 connect(ok_button, &QAbstractButton::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/**********************************************************************/
185{
186 QSlider *qo;
187 qo = (QSlider *) QObject::sender();
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/**********************************************************************/
204
205/**********************************************************************/
209{
210 int j = 0;
211 int value;
213
214 multipliers_iterate(pmul) {
216
217 value = slider_list.at(j)->value();
218 mul.multipliers[i] = scale_to_mult(pmul, value);
219 j++;
221 mul.count = multiplier_count();
223 close();
224 deleteLater();
225}
226
227/**********************************************************************/
230int mult_to_scale(const struct multiplier *pmul, int val)
231{
232 return (val - pmul->start) / pmul->step;
233}
234
235/**********************************************************************/
238int scale_to_mult(const struct multiplier *pmul, int scale)
239{
240 return scale * pmul->step + pmul->start;
241}
242
243/**********************************************************************/
247{
248 QRect rect = QApplication::primaryScreen()->availableGeometry();
249 QPoint p;
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/**********************************************************************/
272{
273 // PORTME
274}
275
276/**********************************************************************/
280{
282
284 return;
285 }
286 mrd = new multipler_rates_dialog(gui()->central_wdg);
287 mrd->show();
288}
289
290/**********************************************************************/
294 : QWidget(parent)
295{
296
299 mouse_x = 0.;
300 moved = 0;
301 on_min = false;
302 on_max = false;
303
304 if (client.conn.playing != nullptr) {
306 } else {
307 max_rates = 10;
308 }
310 setMouseTracking(true);
311}
312
313/**********************************************************************/
319
320/**********************************************************************/
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/**********************************************************************/
333{
334 QPainter p;
335 int i, j, pos;
336 QPixmap *pix;
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
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;
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;
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/**********************************************************************/
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 }
401 update();
402}
403
404/**********************************************************************/
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) {
440 }
441 if (current_max < current_min) {
443 }
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));
460 }
461 if (current_max < 10 - max_rates) {
462 current_max = 10 - max_rates;
463 }
464 if (current_min > current_max) {
466 }
467 moved = 2;
468 }
469 update();
470 } else {
471 moved = 0;
472 }
473
475}
#define str
Definition astring.c:76
struct canvas int int struct sprite int int int int height
Definition canvas_g.h:44
struct canvas int int struct sprite int int int width
Definition canvas_g.h:44
void mouseMoveEvent(QMouseEvent *event)
Definition ratesdlg.cpp:407
double cursor_size
Definition ratesdlg.h:56
fc_double_edge(QWidget *parent=NULL)
Definition ratesdlg.cpp:293
void paintEvent(QPaintEvent *event)
Definition ratesdlg.cpp:332
QSize sizeHint() const
Definition ratesdlg.cpp:323
double mouse_x
Definition ratesdlg.h:57
void mousePressEvent(QMouseEvent *event)
Definition ratesdlg.cpp:387
QPixmap cursor_pix
Definition ratesdlg.h:62
QPixmap * get_pixmap(const QString &id)
static fc_icons * instance()
void slot_cancel_button_pressed()
Definition ratesdlg.cpp:199
QPushButton * ok_button
Definition ratesdlg.h:106
void slot_set_value(int i)
Definition ratesdlg.cpp:184
QList< QSlider * > slider_list
Definition ratesdlg.h:104
QPushButton * cancel_button
Definition ratesdlg.h:105
multipler_rates_dialog(QWidget *parent=0)
Definition ratesdlg.cpp:131
fc_double_edge * fcde
Definition ratesdlg.h:87
void slot_cancel_button_pressed()
Definition ratesdlg.cpp:101
void slot_apply_button_pressed()
Definition ratesdlg.cpp:120
void slot_ok_button_pressed()
Definition ratesdlg.cpp:109
tax_rates_dialog(QWidget *parent=0)
Definition ratesdlg.cpp:44
struct civclient client
bool can_client_issue_orders(void)
#define client_player()
char * incite_cost
Definition comments.c:75
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:828
enum event_type event
Definition events.c:81
@ O_SCIENCE
Definition fc_types.h:101
@ O_LUXURY
Definition fc_types.h:101
@ O_GOLD
Definition fc_types.h:101
int Multiplier_type_id
Definition fc_types.h:386
#define _(String)
Definition fcintl.h:67
const char * government_name_for_player(const struct player *pplayer)
Definition government.c:154
static struct tile * pos
Definition finddlg.c:53
static int scale_to_mult(const struct multiplier *pmul, int scale)
Definition gamedlgs.c:227
static int mult_to_scale(const struct multiplier *pmul, int val)
Definition gamedlgs.c:219
static mpgui * gui
Definition mpgui_qt.cpp:52
Multiplier_type_id multiplier_count(void)
Definition multipliers.c:88
const char * multiplier_name_translation(const struct multiplier *pmul)
Definition multipliers.c:97
bool multiplier_can_be_changed(struct multiplier *pmul, struct player *pplayer)
Multiplier_type_id multiplier_index(const struct multiplier *pmul)
Definition multipliers.c:80
#define multipliers_iterate(_mul_)
Definition multipliers.h:61
#define multipliers_iterate_end
Definition multipliers.h:67
int dsend_packet_player_rates(struct connection *pc, int tax, int luxury, int science)
int send_packet_player_multiplier(struct connection *pc, const struct packet_player_multiplier *packet)
int player_multiplier_target_value(const struct player *pplayer, const struct multiplier *pmul)
Definition player.c:1981
static int scale_to_mult(const struct multiplier *pmul, int scale)
Definition ratesdlg.cpp:238
void popup_multiplier_dialog(void)
Definition ratesdlg.cpp:279
void popup_rates_dialog(void)
Definition ratesdlg.cpp:246
void real_multipliers_dialog_update(void *unused)
Definition ratesdlg.cpp:271
static int mult_to_scale(const struct multiplier *pmul, int val)
Definition ratesdlg.cpp:230
struct sprite int int int int struct sprite int int float scale
Definition sprite_g.h:33
struct connection conn
Definition client_main.h:96
struct player * playing
Definition connection.h:151
struct player_economic economic
Definition player.h:282
QPixmap * pm
Definition sprite.h:25
struct sprite * get_tax_sprite(const struct tileset *t, Output_type_id otype)
Definition tilespec.c:6879