]> git.sesse.net Git - ultimatescore/blob - client/mainwindow.cpp
6b8f7136e06c41087bb1fc393145897a036fc44a
[ultimatescore] / client / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "post_to_main_thread.h"
3 #include "ui_mainwindow.h"
4
5 #include <stdlib.h>
6
7 using namespace std;
8
9 string escape_html(const string &str)
10 {
11         string s = "";
12         for (char ch : str) {
13                 if (ch == '<') {
14                         s += "&lt;";
15                 } else if (ch == '>') {
16                         s += "&gt;";
17                 } else if (ch == '&') {
18                         s += "&amp;";
19                 } else {
20                         s += ch;
21                 }
22         }
23         return s;
24 }
25
26 string escape_unicode(const string &str)
27 {
28         string s = "";
29         for (size_t pos = 0; pos < str.size(); ) {
30                 wchar_t wc;
31                 int len = mbtowc(&wc, str.data() + pos, str.size() - pos);
32                 if (len == -1) {
33                         wc = '?';
34                         len = 1;
35                 }
36                 pos += len;
37
38                 if (wc == '\\') {
39                         s += "\\\\";
40                 } else if (isprint(wc)) {
41                         s += wc;
42                 } else {
43                         char buf[16];
44                         snprintf(buf, sizeof(buf), "\\u%04x", wc);
45                         s += buf;
46                 }
47         }
48         return s;
49 }
50
51 string escape_quotes(const string &str)
52 {
53         string s = "";
54         for (char ch : str) {
55                 if (ch == '"') {
56                         s += '\\';
57                 }
58                 s += ch;
59         }
60         return s;
61 }
62
63 string serialize_as_json(const map<string, string> &param)
64 {
65         string s = "{";
66
67         bool first = true;
68         for (const auto &key_value : param) {
69                 if (!first) s += ", ";
70                 first = false;
71
72                 s += '"';
73                 s += escape_quotes(escape_unicode(key_value.first));
74                 s += "\": \"";
75                 s += escape_quotes(escape_unicode(key_value.second));
76                 s += '"';
77         }
78         s += "}";
79         return s;       
80 }
81
82 MainWindow::MainWindow(QWidget *parent) :
83     QMainWindow(parent),
84     ui(new Ui::MainWindow)
85 {
86         ui->setupUi(this);
87         ws = new WSServer("127.0.0.1", 5250);
88         ws->set_connection_callback([this](bool connected) {
89                 string msg = connected ? "Connected" : "Not connected";
90                 post_to_main_thread([this, msg]() {
91                         ui->ws_connected_label->setText(QString::fromStdString(msg));
92                 });
93         });
94
95         connect(ui->ws_disconnect_btn, &QPushButton::clicked, this, &MainWindow::ws_disconnect_clicked);
96         connect(ui->set_initials_btn, &QPushButton::clicked, this, &MainWindow::set_initials_clicked);
97         connect(ui->set_color_btn, &QPushButton::clicked, this, &MainWindow::set_color_clicked);
98         connect(ui->set_score_btn, &QPushButton::clicked, this, &MainWindow::set_score_clicked);
99         connect(ui->set_all_scorebug_btn, &QPushButton::clicked, this, &MainWindow::set_all_scorebug_clicked);
100         connect(ui->goal_1_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_1_box, 1); });
101         connect(ui->ungoal_1_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_1_box, -1); });
102         connect(ui->goal_2_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_2_box, 1); });
103         connect(ui->ungoal_2_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_2_box, -1); });
104
105         connect(ui->set_clock_btn, &QPushButton::clicked, this, &MainWindow::set_clock_clicked);
106         connect(ui->set_clock_limit_btn, &QPushButton::clicked, this, &MainWindow::set_clock_limit_clicked);
107         connect(ui->start_and_show_clock_btn, &QPushButton::clicked, this, &MainWindow::start_and_show_clock_clicked);
108         connect(ui->stop_clock_btn, &QPushButton::clicked, this, &MainWindow::stop_clock_clicked);
109         connect(ui->show_clock_btn, &QPushButton::clicked, this, &MainWindow::show_clock_clicked);
110         connect(ui->hide_clock_btn, &QPushButton::clicked, this, &MainWindow::hide_clock_clicked);
111
112         connect(ui->set_comment_btn, &QPushButton::clicked, this, &MainWindow::set_comment_clicked);
113         connect(ui->set_and_show_comment_btn, &QPushButton::clicked, this, &MainWindow::set_and_show_comment_clicked);
114         connect(ui->hide_comment_btn, &QPushButton::clicked, this, &MainWindow::hide_comment_clicked);
115         connect(ui->set_and_show_autocomment_btn, &QPushButton::clicked, this, &MainWindow::set_and_show_autocomment_clicked);
116
117         connect(ui->show_lower_third_btn, &QPushButton::clicked, this, &MainWindow::show_lower_third_clicked);
118         connect(ui->hide_lower_third_btn, &QPushButton::clicked, this, &MainWindow::hide_lower_third_clicked);
119
120         connect(ui->quick_lower_third_edit, &QLineEdit::returnPressed, this, &MainWindow::quick_lower_third_activate);
121         connect(ui->show_quick_lower_third_btn, &QPushButton::clicked, this, &MainWindow::quick_lower_third_activate);
122
123         connect(ui->show_scorebug_btn, &QPushButton::clicked, this, &MainWindow::show_scorebug_clicked);
124         connect(ui->show_group_a_btn, &QPushButton::clicked, this, [this]() { show_group_clicked("Group A"); });
125         connect(ui->show_group_b_btn, &QPushButton::clicked, this, [this]() { show_group_clicked("Group B"); });
126         connect(ui->show_schedule_btn, &QPushButton::clicked, this, &MainWindow::show_schedule_clicked);
127         connect(ui->show_carousel_btn, &QPushButton::clicked, this, &MainWindow::show_carousel_clicked);
128         connect(ui->show_nothing_btn, &QPushButton::clicked, this, &MainWindow::show_nothing_clicked);
129         connect(ui->show_roster_1_btn, &QPushButton::clicked, this, [this]() { show_roster_clicked(ui->initials_1_edit->text().toStdString()); });
130         connect(ui->show_roster_2_btn, &QPushButton::clicked, this, [this]() { show_roster_clicked(ui->initials_2_edit->text().toStdString()); });
131         connect(ui->show_roster_carousel_btn, &QPushButton::clicked, this, &MainWindow::show_roster_carousel_clicked);
132
133         autocomment_update();
134
135         const set<pair<unsigned, unsigned>> usb{{ 0x0e8f, 0x0041 }};
136         event_device = new EventDevice(usb, ui->quick_lower_third_edit);
137         event_device->start_thread();
138 }
139
140 MainWindow::~MainWindow()
141 {
142         delete ui;
143 }
144
145 void MainWindow::ws_disconnect_clicked()
146 {
147         ws->change_port(stoi(ui->ws_port_box->text().toStdString()));
148 }
149
150 void MainWindow::set_initials_clicked()
151 {
152         map<string, string> param;
153         param["team1"] = escape_html(ui->initials_1_edit->text().toStdString());
154         param["team2"] = escape_html(ui->initials_2_edit->text().toStdString());
155         ws->send_command("update " + serialize_as_json(param));
156         ws->send_command("eval setteams()");
157 }
158
159 void MainWindow::set_color_clicked()
160 {
161         map<string, string> param;
162         param["team1color"] = ui->color_1_edit->text().toStdString();  // Should maybe be escaped, but meh.
163         param["team2color"] = ui->color_2_edit->text().toStdString();
164         ws->send_command("update " + serialize_as_json(param));
165         ws->send_command("eval setcolors()");
166 }
167
168 void MainWindow::set_score_clicked()
169 {
170         map<string, string> param;
171         param["score1"] = to_string(ui->score_1_box->value());
172         param["score2"] = to_string(ui->score_2_box->value());
173         ws->send_command("update " + serialize_as_json(param));
174         ws->send_command("eval setscore()");
175         autocomment_update();
176 }
177
178 void MainWindow::set_all_scorebug_clicked()
179 {
180         set_initials_clicked();
181         set_color_clicked();
182         set_score_clicked();
183 }
184
185 void MainWindow::add_goal(QSpinBox *box, int delta)
186 {
187         box->setValue(box->value() + delta);
188         set_score_clicked();
189 }
190
191 void MainWindow::set_clock_clicked()
192 {
193         map<string, string> param;
194         param["clock_min"] = to_string(ui->clock_min_box->value());
195         param["clock_sec"] = to_string(ui->clock_sec_box->value());
196         ws->send_command("update " + serialize_as_json(param));
197         ws->send_command("eval setclockfromstate()");
198 }
199
200 void MainWindow::set_clock_limit_clicked()
201 {
202         map<string, string> param;
203         param["clock_limit_min"] = to_string(ui->clock_limit_min_box->value());
204         param["clock_limit_sec"] = to_string(ui->clock_limit_sec_box->value());
205         ws->send_command("update " + serialize_as_json(param));
206         ws->send_command("eval setclocklimitfromstate()");
207 }
208
209 void MainWindow::start_and_show_clock_clicked()
210 {
211         ws->send_command("eval startclock()");  // Also shows.
212 }
213
214 void MainWindow::stop_clock_clicked()
215 {
216         ws->send_command("eval stopclock()");
217 }
218
219 void MainWindow::show_clock_clicked()
220 {
221         ws->send_command("eval showclock()");
222 }
223
224 void MainWindow::hide_clock_clicked()
225 {
226         ws->send_command("eval hideclock()");
227 }
228
229 void MainWindow::set_comment_clicked()
230 {
231         map<string, string> param;
232         param["comment"] = ui->comment_edit->text().toStdString();
233         ws->send_command("update " + serialize_as_json(param));
234         ws->send_command("eval setcomment()");
235 }
236
237 void MainWindow::set_and_show_comment_clicked()
238 {
239         set_comment_clicked();
240         ws->send_command("eval showcomment()");
241 }
242
243 void MainWindow::set_and_show_autocomment_clicked()
244 {
245         ui->comment_edit->setText(ui->autocomment_edit->text());
246         set_and_show_comment_clicked();
247 }
248
249 void MainWindow::hide_comment_clicked()
250 {
251         ws->send_command("eval hidecomment()");
252 }
253
254 void MainWindow::show_lower_third_clicked()
255 {
256         map<string, string> param;
257         param["text1"] = ui->lowerthird_heading_edit->text().toStdString();
258         param["text2"] = ui->lowerthird_subheading_edit->text().toStdString();
259         ws->send_command("update " + serialize_as_json(param));
260         ws->send_command("eval setandshowlowerthird()");
261 }
262
263 void MainWindow::hide_lower_third_clicked()
264 {
265         ws->send_command("eval hidelowerthird()");
266 }
267
268 void MainWindow::quick_lower_third_activate()
269 {
270         string code = ui->quick_lower_third_edit->text().toUpper().toStdString();
271         if (code == "A") {
272                 add_goal(ui->score_1_box, 1);
273         } else if (code == "B") {
274                 add_goal(ui->score_2_box, 1);
275         } else if (code == "C") {
276                 ws->send_command("eval hidelowerthird()");
277         } else {
278                 map<string, string> param;
279                 param["code"] = code;
280                 ws->send_command("update " + serialize_as_json(param));
281                 ws->send_command("eval quicklowerthird()");
282         }
283         ui->quick_lower_third_edit->clear();
284 }
285
286 void MainWindow::autocomment_update()
287 {
288         int score1 = ui->score_1_box->value();
289         int score2 = ui->score_2_box->value();
290         string msg;
291         if (abs(score1 - score2) >= 3) {
292                 msg = "Game ends after this point";
293         } else {
294                 int cap = max(score1, score2) + 1;
295                 if (score1 == score2) ++cap;
296
297                 if (cap >= 13) {
298                         msg = "Point cap: First to 13";
299                 } else {
300                         char buf[32];
301                         snprintf(buf, sizeof(buf), "Pagacap: First to %d", cap);
302                         msg = buf;
303                 }
304         }
305         ui->autocomment_edit->setText(QString::fromStdString(msg));
306 }
307
308 void MainWindow::show_scorebug_clicked()
309 {
310         ws->send_command("eval stopcarousel()");
311         ws->send_command("eval hidetable()");
312         ws->send_command("eval showscorebug()");
313 }
314
315 void MainWindow::show_group_clicked(const std::string &group_name)
316 {
317         map<string, string> param;
318         param["group_name"] = group_name;
319         ws->send_command("eval stopcarousel()");
320         ws->send_command("update " + serialize_as_json(param));
321         ws->send_command("eval showgroup_from_state()");
322 }
323
324 void MainWindow::show_roster_clicked(const std::string &team_code)
325 {
326         map<string, string> param;
327         param["team_code"] = team_code;
328         ws->send_command("eval stopcarousel()");
329         ws->send_command("update " + serialize_as_json(param));
330         ws->send_command("eval showroster_from_state()");
331 }
332
333 void MainWindow::show_schedule_clicked()
334 {
335         ws->send_command("eval stopcarousel()");
336         ws->send_command("eval showschedule()");
337 }
338
339 void MainWindow::show_carousel_clicked()
340 {
341         ws->send_command("eval stopcarousel()");
342         ws->send_command("eval showcarousel()");
343 }
344
345 void MainWindow::show_roster_carousel_clicked()
346 {
347         map<string, string> param;
348         param["team1"] = escape_html(ui->initials_1_edit->text().toStdString());
349         param["team2"] = escape_html(ui->initials_2_edit->text().toStdString());
350         ws->send_command("eval stopcarousel()");
351         ws->send_command("update " + serialize_as_json(param));
352         ws->send_command("eval showrostercarousel_from_state()");
353 }
354
355 void MainWindow::show_nothing_clicked()
356 {
357         ws->send_command("eval hidescorebug()");
358         ws->send_command("eval stopcarousel()");
359         ws->send_command("eval hidetable()");
360 }