-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot_console.cpp
More file actions
106 lines (89 loc) · 3.75 KB
/
bot_console.cpp
File metadata and controls
106 lines (89 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* This file is part of the ct-Bot remote viewer tool.
* Copyright (c) 2020-2022 Timo Sandmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file bot_console.cpp
* @brief Bot console component
* @author Timo Sandmann
* @date 06.06.2022
*/
#include <QQmlApplicationEngine>
#include <QDebug>
#include "bot_console.h"
#include "connection_manager.h"
BotConsole::BotConsole(QQmlApplicationEngine* p_engine, ConnectionManagerV2& command_eval)
: p_engine_ { p_engine }, conn_manager_ { command_eval }, p_console_ {}, p_cmd_button_ {}, p_active_switch_ {} {
conn_manager_.register_cmd("", [this](const std::string_view& str) {
if constexpr (DEBUG_) {
qDebug() << "CONSOLE received: " << QString::fromUtf8(str.data(), str.size());
}
if (conn_manager_.get_version() != conn_manager_.version_active()) {
return false;
}
auto root { p_engine_->rootObjects() };
if (!p_console_) {
p_console_ = root.first()->findChild<QObject*>("bot_console");
}
if (!p_console_) {
return false;
}
QString data { QString::fromUtf8(str.data(), str.size()) };
data.remove(regex_clear_);
data.replace(regex_tab_, HTML_TAB_.cbegin());
data.replace(regex_space_, HTML_SPACE_.cbegin());
data.remove(regex_ignore_0_);
data.remove(regex_ignore_1_);
data.remove(regex_ignore_2_);
data.remove(regex_ignore_3_);
data.remove(regex_discard_color_);
data.replace(regex_red_start_, HTML_COLOR_RED_START_.cbegin());
data.replace(regex_green_start_, HTML_COLOR_GREEN_START_.cbegin());
data.replace(regex_yellow_start_, HTML_COLOR_YELLOW_START_.cbegin());
data.replace(regex_color_end_, HTML_COLOR_END_.cbegin());
data.replace(regex_eol_, HTML_EOL_.cbegin());
if (data.length() < 1) {
return false;
}
if constexpr (DEBUG_) {
qDebug() << "CONSOLE data= " << data;
}
QMetaObject::invokeMethod(p_console_, "add", Qt::DirectConnection, Q_ARG(QVariant, data));
return true;
});
}
void BotConsole::register_buttons() {
p_cmd_button_ = new ConnectButton { [this](QString cmd, QString) {
if (p_console_) {
QMetaObject::invokeMethod(p_console_, "add", Qt::DirectConnection, Q_ARG(QVariant, "% "));
}
cmd += "\r\n";
if (conn_manager_.get_socket()->isOpen()) {
conn_manager_.get_socket()->write(cmd.toUtf8(), cmd.length());
}
if constexpr (DEBUG_) {
qDebug() << "CMD sent: " << cmd;
}
} };
QObject::connect(p_engine_->rootObjects().at(0)->findChild<QObject*>("Cmd"), SIGNAL(sendClicked(QString)), p_cmd_button_, SLOT(cppSlot(QString)));
p_active_switch_ = new ConnectButton { [this](QString state, QString) {
if (conn_manager_.get_socket()->isOpen()) {
const QString cmd { "c viewer " + state + "\r\n" };
conn_manager_.get_socket()->write(cmd.toUtf8(), cmd.length());
}
} };
QObject::connect(
p_engine_->rootObjects().at(0)->findChild<QObject*>("viewer_active"), SIGNAL(activeChanged(QString)), p_active_switch_, SLOT(cppSlot(QString)));
}