#include "ui/mainwindow.h" #include "logic/client_reactor.h" #include "messages.pb.h" #include "ui_mainwindow.h" #include #include #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); auto channel = grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials()); auto stub = chat::Chat::NewStub(channel); m_reactor = std::make_unique(stub.get()); connect(ui->sendButton, &QPushButton::clicked, this, [this]() { auto msg = ui->inputText->toPlainText(); if (!msg.isEmpty()) { sendMsg(msg); ui->inputText->clear(); } }); connect(m_reactor->getHandler().get(), &MessageHandler::messageReceived, this, &MainWindow::receiveMsg); } MainWindow::~MainWindow() { delete ui; } void MainWindow::sendMsg(const QString &msg) { if (m_reactor && m_reactor->IsConnected()) { chat::chatMsg chatMsg; chatMsg.set_message(msg.toStdString()); m_reactor->SendMessage(chatMsg); } } void MainWindow::receiveMsg(const chat::chatMsg &chatMsg) { auto userId = QString::fromStdString(chatMsg.userid()); auto content = QString::fromStdString(chatMsg.message()); Message message(userId, QDateTime::currentDateTime(), content); m_chatroom.addMessage(message); ui->outputText->setText(m_chatroom.getMessagesString()); }