1 #include "mainwindow.h"
2 #include "post_to_main_thread.h"
3 #include "ui_mainwindow.h"
9 string escape_html(const string &str)
15 } else if (ch == '>') {
17 } else if (ch == '&') {
26 string escape_unicode(const string &str)
29 for (size_t pos = 0; pos < str.size(); ) {
31 int len = mbtowc(&wc, str.data() + pos, str.size() - pos);
40 } else if (isprint(wc)) {
44 snprintf(buf, sizeof(buf), "\\u%04x", wc);
51 string escape_quotes(const string &str)
63 string serialize_as_json(const map<string, string> ¶m)
68 for (const auto &key_value : param) {
69 if (!first) s += ", ";
73 s += escape_quotes(escape_unicode(key_value.first));
75 s += escape_quotes(escape_unicode(key_value.second));
82 MainWindow::MainWindow(QWidget *parent) :
84 ui(new Ui::MainWindow)
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));
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); });
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);
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);
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);
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);
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);
133 autocomment_update();
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();
140 MainWindow::~MainWindow()
145 void MainWindow::ws_disconnect_clicked()
147 ws->change_port(stoi(ui->ws_port_box->text().toStdString()));
150 void MainWindow::set_initials_clicked()
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()");
159 void MainWindow::set_color_clicked()
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()");
168 void MainWindow::set_score_clicked()
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();
178 void MainWindow::set_all_scorebug_clicked()
180 set_initials_clicked();
185 void MainWindow::add_goal(QSpinBox *box, int delta)
187 box->setValue(box->value() + delta);
191 void MainWindow::set_clock_clicked()
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()");
200 void MainWindow::set_clock_limit_clicked()
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()");
209 void MainWindow::start_and_show_clock_clicked()
211 ws->send_command("eval startclock()"); // Also shows.
214 void MainWindow::stop_clock_clicked()
216 ws->send_command("eval stopclock()");
219 void MainWindow::show_clock_clicked()
221 ws->send_command("eval showclock()");
224 void MainWindow::hide_clock_clicked()
226 ws->send_command("eval hideclock()");
229 void MainWindow::set_comment_clicked()
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()");
237 void MainWindow::set_and_show_comment_clicked()
239 set_comment_clicked();
240 ws->send_command("eval showcomment()");
243 void MainWindow::set_and_show_autocomment_clicked()
245 ui->comment_edit->setText(ui->autocomment_edit->text());
246 set_and_show_comment_clicked();
249 void MainWindow::hide_comment_clicked()
251 ws->send_command("eval hidecomment()");
254 void MainWindow::show_lower_third_clicked()
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()");
263 void MainWindow::hide_lower_third_clicked()
265 ws->send_command("eval hidelowerthird()");
268 void MainWindow::quick_lower_third_activate()
270 string code = ui->quick_lower_third_edit->text().toUpper().toStdString();
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()");
278 map<string, string> param;
279 param["code"] = code;
280 ws->send_command("update " + serialize_as_json(param));
281 ws->send_command("eval quicklowerthird()");
283 ui->quick_lower_third_edit->clear();
286 void MainWindow::autocomment_update()
288 int score1 = ui->score_1_box->value();
289 int score2 = ui->score_2_box->value();
291 if (abs(score1 - score2) >= 3) {
292 msg = "Game ends after this point";
294 int cap = max(score1, score2) + 1;
295 if (score1 == score2) ++cap;
298 msg = "Point cap: First to 13";
301 snprintf(buf, sizeof(buf), "Pagacap: First to %d", cap);
305 ui->autocomment_edit->setText(QString::fromStdString(msg));
308 void MainWindow::show_scorebug_clicked()
310 ws->send_command("eval stopcarousel()");
311 ws->send_command("eval hidetable()");
312 ws->send_command("eval showscorebug()");
315 void MainWindow::show_group_clicked(const std::string &group_name)
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()");
324 void MainWindow::show_roster_clicked(const std::string &team_code)
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()");
333 void MainWindow::show_schedule_clicked()
335 ws->send_command("eval stopcarousel()");
336 ws->send_command("eval showschedule()");
339 void MainWindow::show_carousel_clicked()
341 ws->send_command("eval stopcarousel()");
342 ws->send_command("eval showcarousel()");
345 void MainWindow::show_roster_carousel_clicked()
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()");
355 void MainWindow::show_nothing_clicked()
357 ws->send_command("eval hidescorebug()");
358 ws->send_command("eval stopcarousel()");
359 ws->send_command("eval hidetable()");