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)
55 if (ch == '"' || ch == '\\') {
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 acmp = new ACMPClient("127.0.0.1", 5250);
88 acmp->add_init_command("MIXER 1 STRAIGHT_ALPHA_OUTPUT 1");
89 acmp->add_init_command("CG 1 ADD 0 \"score\" 1 \"<templateData></templateData>\"");
90 acmp->set_connection_callback([this](bool connected) {
91 string msg = connected ? "Connected" : "Not connected";
92 post_to_main_thread([this, msg]() {
93 ui->casparcg_connected_label->setText(QString::fromStdString(msg));
99 connect(ui->casparcg_reconnect_btn, &QPushButton::clicked, this, &MainWindow::casparcg_reconnect_clicked);
100 connect(ui->set_initials_btn, &QPushButton::clicked, this, &MainWindow::set_initials_clicked);
101 connect(ui->set_color_btn, &QPushButton::clicked, this, &MainWindow::set_color_clicked);
102 connect(ui->set_score_btn, &QPushButton::clicked, this, &MainWindow::set_score_clicked);
103 connect(ui->set_all_scorebug_btn, &QPushButton::clicked, this, &MainWindow::set_all_scorebug_clicked);
104 connect(ui->goal_1_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_1_box, 1); });
105 connect(ui->ungoal_1_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_1_box, -1); });
106 connect(ui->goal_2_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_2_box, 1); });
107 connect(ui->ungoal_2_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_2_box, -1); });
109 connect(ui->set_clock_btn, &QPushButton::clicked, this, &MainWindow::set_clock_clicked);
110 connect(ui->start_and_show_clock_btn, &QPushButton::clicked, this, &MainWindow::start_and_show_clock_clicked);
111 connect(ui->stop_clock_btn, &QPushButton::clicked, this, &MainWindow::stop_clock_clicked);
112 connect(ui->show_clock_btn, &QPushButton::clicked, this, &MainWindow::show_clock_clicked);
113 connect(ui->hide_clock_btn, &QPushButton::clicked, this, &MainWindow::hide_clock_clicked);
115 connect(ui->set_comment_btn, &QPushButton::clicked, this, &MainWindow::set_comment_clicked);
116 connect(ui->set_and_show_comment_btn, &QPushButton::clicked, this, &MainWindow::set_and_show_comment_clicked);
117 connect(ui->hide_comment_btn, &QPushButton::clicked, this, &MainWindow::hide_comment_clicked);
118 connect(ui->set_and_show_autocomment_btn, &QPushButton::clicked, this, &MainWindow::set_and_show_autocomment_clicked);
120 connect(ui->show_lower_third_btn, &QPushButton::clicked, this, &MainWindow::show_lower_third_clicked);
121 connect(ui->hide_lower_third_btn, &QPushButton::clicked, this, &MainWindow::hide_lower_third_clicked);
123 connect(ui->quick_lower_third_edit, &QLineEdit::returnPressed, this, &MainWindow::quick_lower_third_activate);
124 connect(ui->show_quick_lower_third_btn, &QPushButton::clicked, this, &MainWindow::quick_lower_third_activate);
126 connect(ui->show_scorebug_btn, &QPushButton::clicked, this, &MainWindow::show_scorebug_clicked);
127 connect(ui->show_group_a_btn, &QPushButton::clicked, this, [this]() { show_group_clicked("Group A"); });
128 connect(ui->show_group_b_btn, &QPushButton::clicked, this, [this]() { show_group_clicked("Group B"); });
129 connect(ui->show_schedule_btn, &QPushButton::clicked, this, &MainWindow::show_schedule_clicked);
130 connect(ui->show_carousel_btn, &QPushButton::clicked, this, &MainWindow::show_carousel_clicked);
131 connect(ui->show_nothing_btn, &QPushButton::clicked, this, &MainWindow::show_nothing_clicked);
132 connect(ui->show_roster_1_btn, &QPushButton::clicked, this, [this]() { show_roster_clicked(ui->initials_1_edit->text().toStdString()); });
133 connect(ui->show_roster_2_btn, &QPushButton::clicked, this, [this]() { show_roster_clicked(ui->initials_2_edit->text().toStdString()); });
134 connect(ui->show_roster_carousel_btn, &QPushButton::clicked, this, &MainWindow::show_roster_carousel_clicked);
136 autocomment_update();
138 const set<pair<unsigned, unsigned>> usb{{ 0x0e8f, 0x0041 }};
139 event_device = new EventDevice(usb, ui->quick_lower_third_edit);
140 event_device->start_thread();
143 MainWindow::~MainWindow()
149 void MainWindow::casparcg_reconnect_clicked()
151 acmp->change_server(ui->casparcg_host_box->text().toStdString(),
152 stoi(ui->casparcg_port_box->text().toStdString()));
155 void MainWindow::set_initials_clicked()
157 map<string, string> param;
158 param["team1"] = escape_html(ui->initials_1_edit->text().toStdString());
159 param["team2"] = escape_html(ui->initials_2_edit->text().toStdString());
160 acmp->send_command("cg 1 update 1 \"" + escape_quotes(serialize_as_json(param)) + "\"");
161 acmp->send_command("cg 1 invoke 1 setteams");
164 void MainWindow::set_color_clicked()
166 map<string, string> param;
167 param["team1color"] = ui->color_1_edit->text().toStdString(); // Should maybe be escaped, but meh.
168 param["team2color"] = ui->color_2_edit->text().toStdString();
169 acmp->send_command("cg 1 update 1 \"" + escape_quotes(serialize_as_json(param)) + "\"");
170 acmp->send_command("cg 1 invoke 1 setcolors");
173 void MainWindow::set_score_clicked()
175 map<string, string> param;
176 param["score1"] = to_string(ui->score_1_box->value());
177 param["score2"] = to_string(ui->score_2_box->value());
178 acmp->send_command("cg 1 update 1 \"" + escape_quotes(serialize_as_json(param)) + "\"");
179 acmp->send_command("cg 1 invoke 1 setscore");
180 autocomment_update();
183 void MainWindow::set_all_scorebug_clicked()
185 set_initials_clicked();
190 void MainWindow::add_goal(QSpinBox *box, int delta)
192 box->setValue(box->value() + delta);
196 void MainWindow::set_clock_clicked()
198 map<string, string> param;
199 param["clock_min"] = to_string(ui->clock_min_box->value());
200 param["clock_sec"] = to_string(ui->clock_sec_box->value());
201 acmp->send_command("cg 1 update 1 \"" + escape_quotes(serialize_as_json(param)) + "\"");
202 acmp->send_command("cg 1 invoke 1 setclockfromstate");
205 void MainWindow::start_and_show_clock_clicked()
207 acmp->send_command("cg 1 invoke 1 startclock"); // Also shows.
210 void MainWindow::stop_clock_clicked()
212 acmp->send_command("cg 1 invoke 1 stopclock");
215 void MainWindow::show_clock_clicked()
217 acmp->send_command("cg 1 invoke 1 showclock");
220 void MainWindow::hide_clock_clicked()
222 acmp->send_command("cg 1 invoke 1 hideclock");
225 void MainWindow::set_comment_clicked()
227 map<string, string> param;
228 param["comment"] = ui->comment_edit->text().toStdString();
229 acmp->send_command("cg 1 update 1 \"" + escape_quotes(serialize_as_json(param)) + "\"");
230 acmp->send_command("cg 1 invoke 1 setcomment");
233 void MainWindow::set_and_show_comment_clicked()
235 set_comment_clicked();
236 acmp->send_command("cg 1 invoke 1 showcomment");
239 void MainWindow::set_and_show_autocomment_clicked()
241 ui->comment_edit->setText(ui->autocomment_edit->text());
242 set_and_show_comment_clicked();
245 void MainWindow::hide_comment_clicked()
247 acmp->send_command("cg 1 invoke 1 hidecomment");
250 void MainWindow::show_lower_third_clicked()
252 map<string, string> param;
253 param["text1"] = ui->lowerthird_heading_edit->text().toStdString();
254 param["text2"] = ui->lowerthird_subheading_edit->text().toStdString();
255 acmp->send_command("cg 1 update 1 \"" + escape_quotes(serialize_as_json(param)) + "\"");
256 acmp->send_command("cg 1 invoke 1 setandshowlowerthird");
259 void MainWindow::hide_lower_third_clicked()
261 acmp->send_command("cg 1 invoke 1 hidelowerthird");
264 void MainWindow::quick_lower_third_activate()
266 string code = ui->quick_lower_third_edit->text().toUpper().toStdString();
268 add_goal(ui->score_1_box, 1);
269 } else if (code == "B") {
270 add_goal(ui->score_2_box, 1);
271 } else if (code == "C") {
272 acmp->send_command("cg 1 invoke 1 hidelowerthird");
274 map<string, string> param;
275 param["code"] = code;
276 acmp->send_command("cg 1 update 1 \"" + escape_quotes(serialize_as_json(param)) + "\"");
277 acmp->send_command("cg 1 invoke 1 quicklowerthird");
279 ui->quick_lower_third_edit->clear();
282 void MainWindow::autocomment_update()
284 int score1 = ui->score_1_box->value();
285 int score2 = ui->score_2_box->value();
287 if (abs(score1 - score2) >= 3) {
288 msg = "Game ends after this point";
289 } else if (score1 >= 12 || score2 >= 12) {
290 msg = "Point cap: First to 13";
293 snprintf(buf, sizeof(buf), "Pagacap: First to %d", max(score1, score2) + 1);
296 ui->autocomment_edit->setText(QString::fromStdString(msg));
299 void MainWindow::show_scorebug_clicked()
301 acmp->send_command("cg 1 invoke 1 stopcarousel");
302 acmp->send_command("cg 1 invoke 1 hidetable");
303 acmp->send_command("cg 1 invoke 1 showscorebug");
306 void MainWindow::show_group_clicked(const std::string &group_name)
308 map<string, string> param;
309 param["group_name"] = group_name;
310 acmp->send_command("cg 1 invoke 1 stopcarousel");
311 acmp->send_command("cg 1 update 1 \"" + escape_quotes(serialize_as_json(param)) + "\"");
312 acmp->send_command("cg 1 invoke 1 showgroup_from_state");
315 void MainWindow::show_roster_clicked(const std::string &team_code)
317 map<string, string> param;
318 param["team_code"] = team_code;
319 acmp->send_command("cg 1 invoke 1 stopcarousel");
320 acmp->send_command("cg 1 update 1 \"" + escape_quotes(serialize_as_json(param)) + "\"");
321 acmp->send_command("cg 1 invoke 1 showroster_from_state");
324 void MainWindow::show_schedule_clicked()
326 acmp->send_command("cg 1 invoke 1 stopcarousel");
327 acmp->send_command("cg 1 invoke 1 showschedule");
330 void MainWindow::show_carousel_clicked()
332 acmp->send_command("cg 1 invoke 1 stopcarousel");
333 acmp->send_command("cg 1 invoke 1 showcarousel");
336 void MainWindow::show_roster_carousel_clicked()
338 map<string, string> param;
339 param["team1"] = escape_html(ui->initials_1_edit->text().toStdString());
340 param["team2"] = escape_html(ui->initials_2_edit->text().toStdString());
341 acmp->send_command("cg 1 invoke 1 stopcarousel");
342 acmp->send_command("cg 1 update 1 \"" + escape_quotes(serialize_as_json(param)) + "\"");
343 acmp->send_command("cg 1 invoke 1 showrostercarousel_from_state");
346 void MainWindow::show_nothing_clicked()
348 acmp->send_command("cg 1 invoke 1 hidescorebug");
349 acmp->send_command("cg 1 invoke 1 stopcarousel");
350 acmp->send_command("cg 1 invoke 1 hidetable");