MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
relaywindow.cpp
Go to the documentation of this file.
1#include "relaywindow.hpp"
2#include <QTextCursor>
3#include <QTextDocument>
4#include <QTextEdit>
5#include <cstdlib>
6#include <iostream>
7
8RelayWindow::RelayWindow(QWidget *parent) : QMainWindow(parent) {
9 containerWidget = new QWidget(this);
10 layout = new QVBoxLayout(containerWidget);
11 messages = new QTextEdit(this);
12 inputField = new QLineEdit(this);
13 sendButton = new QPushButton("Send", this);
14 layout->addWidget(messages);
15 layout->addWidget(inputField);
16 layout->addWidget(sendButton);
17 setCentralWidget(containerWidget);
18 connect(inputField, &QLineEdit::returnPressed, this, &RelayWindow::sendMessage);
19 connect(sendButton, &QPushButton::clicked, this, &RelayWindow::sendMessage);
20 setGeometry(100, 100, 640, 480);
21 connect(&socket, &QObject::destroyed, [](QObject *o) {
22 qDebug() << "Destroyed object:" << qobject_cast<QTcpSocket *>(o);
23 });
24 connect(&socket, &QTcpSocket::readyRead, this, &RelayWindow::readData);
25 messages->setReadOnly(true);
26 messages->moveCursor(QTextCursor::End);
27 setStyleSheet("QTextEdit, QLineEdit { background-color: #000; color: #FFFFFF; } QPushButton, QMainWindow { background-color: #CCCCCC; color: #000; }");
28}
29
32
33bool RelayWindow::makeConnection(const QString &cuser_name, const QString &cip, const QString &cport) {
34 socket.connectToHost(cip, static_cast<quint16>(cport.toUInt()));
35 if (!socket.waitForConnected(3000)) {
36 qWarning() << "Connection failed:" << socket.errorString();
37 return false;
38 }
39 ip = cip;
40 port = cport;
41 user_name = cuser_name;
42 messages->append("<span style='color: #0000FF;'>connected.</span>");
43 messages->moveCursor(QTextCursor::End);
44 return true;
45}
46
47static constexpr size_t BUFFER_SIZE = 1024 * 8;
48
49void RelayWindow::readData() {
50 if (socket.state() != QTcpSocket::ConnectedState) {
51 return;
52 }
53 QByteArray data_text = socket.readAll();
54 if (data_text.isEmpty()) {
55 return;
56 }
57 QString receivedMessage = QString::fromUtf8(data_text);
58 messages->append("<span style='color: #FF0000; font-size: 18px;'>" + receivedMessage + "</span>");
59 messages->moveCursor(QTextCursor::End);
60 emit messageReceived(receivedMessage);
61}
62
63void RelayWindow::sendMessage() {
64 QString message = inputField->text();
65 if (!message.isEmpty()) {
66 QString final_message = user_name + ": " + message;
67 messages->append("<span style='color: #00CC00; font-size: 18px;'>" + final_message + "</span>");
68 messages->moveCursor(QTextCursor::End);
69 if (socket.state() == QTcpSocket::ConnectedState) {
70 QByteArray data_buf = final_message.toUtf8();
71 qint64 bytesWritten = socket.write(data_buf);
72 if (bytesWritten > 0) {
73 emit messageSent(final_message);
74 }
75 } else {
76 std::cout << "Connection is not connected\n";
77 messages->append("<span style='color: red;'>Connection lost</span>");
78 messages->moveCursor(QTextCursor::End);
79 }
80 inputField->clear();
81 }
82}
83
84void RelayWindow::onMessageReceived() {
85}
86
87void RelayWindow::onConnectionClosed() {
88 std::cout << "Connection closed.";
89 exit(EXIT_SUCCESS);
90}
void messageReceived(const QString &message)
void messageSent(const QString &message)
RelayWindow(QWidget *parent=nullptr)
bool makeConnection(const QString &user, const QString &ip, const QString &port)