Freeciv21
Develop your civilization from humble roots to a global empire
voteinfo_bar.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 #include "voteinfo_bar.h"
12 // Qt
13 #include <QGridLayout>
14 #include <QLabel>
15 #include <QPainter>
16 #include <QPushButton>
17 // client
18 #include "fc_client.h"
19 #include "page_game.h"
20 #include "page_pregame.h"
21 #include "voteinfo.h"
22 
26 pregamevote::pregamevote(QWidget *parent)
27 {
28  setParent(parent);
29  layout = new QGridLayout;
30  label_text = new QLabel;
31  label_vote_text = new QLabel;
32  vote_yes = new QPushButton(_("Yes"));
33  vote_no = new QPushButton(_("No"));
34  vote_abstain = new QPushButton(_("Abstain"));
35  lab_yes = new QLabel;
36  lab_no = new QLabel;
37  lab_abstain = new QLabel;
38  voters = new QLabel;
39  label_text->setAlignment(Qt::AlignHCenter);
40  label_vote_text->setAlignment(Qt::AlignHCenter);
41  label_text->setTextFormat(Qt::RichText);
42  label_vote_text->setTextFormat(Qt::RichText);
43  layout->addWidget(label_text, 0, 0, 1, 7);
44  layout->addWidget(label_vote_text, 1, 0, 1, 7);
45  layout->addWidget(vote_yes, 2, 0, 1, 1);
46  layout->addWidget(vote_no, 2, 2, 1, 1);
47  layout->addWidget(vote_abstain, 2, 4, 1, 1);
48  layout->addWidget(lab_yes, 2, 1, 1, 1);
49  layout->addWidget(lab_no, 2, 3, 1, 1);
50  layout->addWidget(lab_abstain, 2, 5, 1, 1);
51  layout->addWidget(voters, 2, 6, 1, 1);
52  setLayout(layout);
53  connect(vote_yes, &QAbstractButton::clicked, this, &pregamevote::v_yes);
54  connect(vote_no, &QAbstractButton::clicked, this, &pregamevote::v_no);
55  connect(vote_abstain, &QAbstractButton::clicked, this,
57 }
58 
63 {
64  struct voteinfo *vi;
65 
66  vi = voteinfo_queue_get_current(nullptr);
67  if (vi == nullptr) {
68  return;
69  }
71 }
72 
77 {
78  struct voteinfo *vi;
79 
80  vi = voteinfo_queue_get_current(nullptr);
81  if (vi == nullptr) {
82  return;
83  }
85 }
86 
91 {
92  struct voteinfo *vi;
93 
94  vi = voteinfo_queue_get_current(nullptr);
95  if (vi == nullptr) {
96  return;
97  }
99 }
100 
105 {
106  int vote_count, index;
107  struct voteinfo *vi = nullptr;
108  char buf[1024], status[1024], color[32];
109  bool running;
110 
111  show();
112  vote_count = voteinfo_queue_size();
113  vi = voteinfo_queue_get_current(&index);
114  if (vi != nullptr && vi->resolved && vi->passed) {
115  // TRANS: Describing a vote that passed.
116  fc_snprintf(status, sizeof(status), _("[passed]"));
117  sz_strlcpy(color, "green");
118  } else if (vi != nullptr && vi->resolved && !vi->passed) {
119  // TRANS: Describing a vote that failed.
120  fc_snprintf(status, sizeof(status), _("[failed]"));
121  sz_strlcpy(color, "red");
122  } else if (vi != nullptr && vi->remove_time > 0) {
123  // TRANS: Describing a vote that was removed.
124  fc_snprintf(status, sizeof(status), _("[removed]"));
125  sz_strlcpy(color, "grey");
126  } else {
127  status[0] = '\0';
128  }
129  if (status[0] != '\0') {
130  fc_snprintf(buf, sizeof(buf),
131  "<b><p style=\"background-color: %s\"> %s</p></b> ", color,
132  status);
133  sz_strlcpy(status, buf);
134  } else {
135  buf[0] = '\0';
136  }
137  if (vi != nullptr) {
138  lab_yes->setText(QString::number(vi->yes));
139  lab_no->setText(QString::number(vi->no));
140  lab_abstain->setText(QString::number(vi->abstain));
141  if (buf[0] != '\0') {
142  label_text->setText(buf);
143  } else {
144  label_text->setText(QString(_("<b>%1 called a vote for:</b>"))
145  .arg(QString(vi->user).toHtmlEscaped()));
146  }
147  label_vote_text->setText(QStringLiteral("</b><p style=\"color:"
148  " red\"> %1</p></b>")
149  .arg(QString(vi->desc).toHtmlEscaped()));
150  voters->setText(QStringLiteral(" /%1").arg(vi->num_voters));
151  } else {
152  label_text->setText(QLatin1String(""));
153  lab_yes->setText(QStringLiteral("-"));
154  lab_no->setText(QStringLiteral("-"));
155  lab_abstain->setText(QStringLiteral("-"));
156  }
157  running = vi != nullptr && !vi->resolved && vi->remove_time == 0;
158  vote_yes->setEnabled(running);
159  vote_no->setEnabled(running);
160  vote_abstain->setEnabled(running);
161 
162  if (vote_count < 1) {
163  hide();
164  }
165  update();
166 }
167 
171 pregamevote::~pregamevote() = default;
172 
176 xvote::xvote(QWidget *parent) : pregamevote(parent)
177 {
178  QPalette palette;
179 
180  setParent(parent);
181  palette.setColor(QPalette::WindowText, QColor(0, 255, 255));
182  label_text->setPalette(palette);
183  label_vote_text->setPalette(palette);
184  palette.setColor(QPalette::WindowText, QColor(255, 255, 0));
185  lab_yes->setPalette(palette);
186  lab_no->setPalette(palette);
187  lab_abstain->setPalette(palette);
188  voters->setPalette(palette);
189 }
190 
194 void xvote::paint(QPainter *painter, QPaintEvent *event)
195 {
196  Q_UNUSED(event)
197  painter->setBrush(QColor(90, 90, 192, 185));
198  painter->drawRect(0, 0, width(), height());
199  painter->setBrush(QColor(90, 90, 192, 185));
200  painter->drawRect(5, 5, width() - 10, height() - 10);
201 }
202 
206 void xvote::paintEvent(QPaintEvent *event)
207 {
208  QPainter painter;
209 
210  painter.begin(this);
211  paint(&painter, event);
212  painter.end();
213 }
214 
220 {
221  if (king()->current_page() == PAGE_START) {
222  qobject_cast<page_pregame *>(king()->pages[PAGE_START])->update_vote();
223  }
224  if (king()->current_page() == PAGE_GAME) {
225  if (queen()->x_vote != nullptr) {
226  queen()->x_vote->show();
227  queen()->x_vote->update_vote();
228  }
229  }
230 }
xvote * x_vote
Definition: page_game.h:85
QPushButton * vote_yes
Definition: voteinfo_bar.h:33
QGridLayout * layout
Definition: voteinfo_bar.h:40
QLabel * label_vote_text
Definition: voteinfo_bar.h:32
void v_yes()
Slot vote yes.
QLabel * voters
Definition: voteinfo_bar.h:39
QPushButton * vote_abstain
Definition: voteinfo_bar.h:35
void v_no()
Slot vote no.
pregamevote(QWidget *parent=nullptr)
Constructor for pregamevote.
QLabel * lab_abstain
Definition: voteinfo_bar.h:38
void v_abstain()
Slot vote abstain.
QLabel * lab_yes
Definition: voteinfo_bar.h:36
void update_vote()
Updates text on vote.
QLabel * label_text
Definition: voteinfo_bar.h:31
QPushButton * vote_no
Definition: voteinfo_bar.h:34
~pregamevote() override
Destructor for pregamevote.
QLabel * lab_no
Definition: voteinfo_bar.h:37
void paint(QPainter *painter, QPaintEvent *event)
Paints frames for xvote.
xvote(QWidget *parent)
pregamevote class used for displaying vote bar in PAGE START
void paintEvent(QPaintEvent *event) override
Paint event for xvote.
enum event_type event
Definition: events.cpp:68
class fc_client * king()
Return fc_client instance.
Definition: gui_main.cpp:58
#define _(String)
Definition: fcintl.h:50
pageGame * queen()
Return game instandce.
Definition: page_game.cpp:557
int yes
Definition: voteinfo.h:26
int num_voters
Definition: voteinfo.h:29
bool passed
Definition: voteinfo.h:31
char desc[512]
Definition: voteinfo.h:23
int abstain
Definition: voteinfo.h:28
bool resolved
Definition: voteinfo.h:30
int vote_no
Definition: voteinfo.h:21
int no
Definition: voteinfo.h:27
time_t remove_time
Definition: voteinfo.h:35
char user[MAX_LEN_NAME]
Definition: voteinfo.h:22
int fc_snprintf(char *str, size_t n, const char *format,...)
See also fc_utf8_snprintf_trunc(), fc_utf8_snprintf_rep().
Definition: support.cpp:537
#define sz_strlcpy(dest, src)
Definition: support.h:140
void voteinfo_do_vote(int vote_no, enum client_vote_type vote)
Convenience function for submitting a vote to the server.
Definition: voteinfo.cpp:231
struct voteinfo * voteinfo_queue_get_current(int *pindex)
Get the voteinfo record at the start of the vote queue.
Definition: voteinfo.cpp:198
int voteinfo_queue_size()
Returns the number of pending votes.
Definition: voteinfo.cpp:288
@ CVT_YES
Definition: voteinfo.h:17
@ CVT_NO
Definition: voteinfo.h:17
@ CVT_ABSTAIN
Definition: voteinfo.h:17
void voteinfo_gui_update(void)
Refresh all vote related GUI widgets.