mainwindow.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QCommandLinkButton>
  4. #include <QDebug>
  5. #include <QLabel>
  6. #include <QTextStream>
  7. #include <QString>
  8. #include <QLayout>
  9. #include <client.hpp>
  10. #include <vector>
  11. /**
  12. * @brief MainWindow::MainWindow
  13. * @param argc
  14. * @param argv
  15. * @param parent
  16. */
  17. MainWindow::MainWindow(int argc, char** argv, QWidget *parent) :
  18. QMainWindow(parent),
  19. ui(new Ui::MainWindow),
  20. cli_argc(argc),
  21. cli_argv(argv) {
  22. ui->setupUi(this);
  23. QCommandLinkButton *button = this->findChild<QCommandLinkButton*>("connect");
  24. connect(button, &QCommandLinkButton::clicked, this, &MainWindow::connectClient);
  25. }
  26. /**
  27. * @brief MainWindow::~MainWindow
  28. */
  29. MainWindow::~MainWindow()
  30. {
  31. delete ui;
  32. }
  33. /**
  34. * @brief MainWindow::buttonClicked
  35. */
  36. void MainWindow::connectClient() {
  37. qDebug() << "Connecting to KServer";
  38. Client* q_client = new Client(this, cli_argc, cli_argv);
  39. QObject::connect(q_client, &Client::messageReceived, this, &MainWindow::updateMessages);
  40. q_client->start();
  41. QTextEdit* send_message_box = this->findChild<QTextEdit*>("inputText");
  42. QPushButton* send_message_button = this->findChild<QPushButton*>("sendMessage");
  43. // Handle mouse
  44. QObject::connect(send_message_button, &QPushButton::clicked, this, [q_client, send_message_box]() {
  45. q_client->sendMessage(send_message_box->toPlainText());
  46. send_message_box->clear();
  47. });
  48. // TODO: Handle enter key
  49. // QObject::connect(send_message_box, &QTextEdit::keyReleaseEvent, this, [q_client, send_message_box]() {
  50. // q_client->sendMessage(send_message_box->toPlainText());
  51. // send_message_box->clear();
  52. // });
  53. QPushButton* disconnect_button = this->findChild<QPushButton*>("disconnect");
  54. QObject::connect(disconnect_button, &QPushButton::clicked, q_client, &Client::closeConnection);
  55. }
  56. /**
  57. * @brief MainWindow::updateMessages
  58. * @param s
  59. */
  60. void MainWindow::updateMessages(const QString& s) {
  61. QLabel* message_display = this->findChild<QLabel*>("messages");
  62. message_display->setText(message_display->text() + "\n" + s);
  63. }