mainwindow.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QCommandLinkButton>
  4. #include <QDebug>
  5. #include <QTextEdit>
  6. #include <QTextStream>
  7. #include <QString>
  8. #include <QLayout>
  9. #include <vector>
  10. /**
  11. * @brief MainWindow::MainWindow
  12. * @param argc
  13. * @param argv
  14. * @param parent
  15. */
  16. MainWindow::MainWindow(int argc, char** argv, QWidget *parent) :
  17. QMainWindow(parent),
  18. ui(new Ui::MainWindow),
  19. arg_ui(new ArgDialog),
  20. cli_argc(argc),
  21. cli_argv(argv) {
  22. ui->setupUi(this);
  23. this->setWindowTitle("KYGUI");
  24. QCommandLinkButton *button = this->findChild<QCommandLinkButton*>("connect");
  25. connect(button, &QCommandLinkButton::clicked, this, &MainWindow::connectClient);
  26. }
  27. /**
  28. * @brief MainWindow::~MainWindow
  29. */
  30. MainWindow::~MainWindow()
  31. {
  32. delete ui;
  33. }
  34. /**
  35. * @brief MainWindow::buttonClicked
  36. */
  37. void MainWindow::connectClient() {
  38. qDebug() << "Connecting to KServer";
  39. Client* q_client = new Client(this, cli_argc, cli_argv);
  40. QObject::connect(q_client, &Client::messageReceived, this, &MainWindow::updateMessages);
  41. QProgressBar* progressBar = ui->progressBar;
  42. q_client->start();
  43. for (int i = 1; i < 101; i++) {
  44. progressBar->setValue(i);
  45. }
  46. KTextEdit* send_message_box = reinterpret_cast<KTextEdit*>(ui->inputText);
  47. send_message_box->show();
  48. QPushButton* send_message_button = this->findChild<QPushButton*>("sendMessage");
  49. // Handle mouse
  50. QObject::connect(send_message_button, &QPushButton::clicked, this, [q_client, send_message_box]() {
  51. q_client->sendMessage(send_message_box->toPlainText());
  52. send_message_box->clear();
  53. });
  54. // TODO: Handle enter key
  55. // QObject::connect(send_message_box, &QTextEdit::keyReleaseEvent, this, [q_client, send_message_box]() {
  56. // q_client->sendMessage(send_message_box->toPlainText());
  57. // send_message_box->clear();
  58. // });
  59. QListWidget* q_list_widget = ui->appList;
  60. QObject::connect(q_list_widget, &QListWidget::itemSelectionChanged, this, [q_client, q_list_widget]() {
  61. QList<QListWidgetItem*> items = q_list_widget->selectedItems();
  62. if (!items.empty()) {
  63. std::vector<QString> app_names{};
  64. for (const auto& item : items) {
  65. app_names.push_back(item->text());
  66. }
  67. q_client->setSelectedApp(app_names);
  68. }
  69. });
  70. QPushButton* disconnect_button = this->findChild<QPushButton*>("disconnect");
  71. QObject::connect(disconnect_button, &QPushButton::clicked, this, [this, q_client, progressBar]() {
  72. q_client->closeConnection();
  73. progressBar->setValue(0);
  74. ui->appList->clear();
  75. ui->messages->clear();
  76. });
  77. QObject::connect(ui->execute, &QPushButton::clicked, this, [this, q_client]() {
  78. q_client->execute();
  79. });
  80. QObject::connect(ui->addArgs, &QPushButton::clicked, this, [this]() {
  81. auto items = ui->appList->selectedItems();
  82. if (items.size() == 1) {
  83. // open dialog to add arguments
  84. arg_ui->show();
  85. } else if (items.size() == 0) {
  86. qDebug() << "You must select an App to add arguments to";
  87. } else {
  88. qDebug() << "Can only add arguments to one app. Please select just one app before adding arguments";
  89. }
  90. });
  91. QObject::connect(arg_ui, &ArgDialog::uploadFile, this, [q_client](QByteArray bytes) {
  92. q_client->sendFile(bytes);
  93. });
  94. }
  95. /**
  96. * @brief MainWindow::updateMessages
  97. * @param s
  98. */
  99. void MainWindow::updateMessages(int t, const QString& s, StringVec v) {
  100. if (t == MESSAGE_UPDATE_TYPE) {
  101. qDebug() << "Updating message area";
  102. ui->messages->append(s);
  103. } else if (t == COMMANDS_UPDATE_TYPE) {
  104. qDebug() << "Updating commands";
  105. QListWidget* appList = ui->appList;
  106. appList->clear();
  107. for(const auto& s : v) {
  108. new QListWidgetItem(tr(s.toUtf8()), appList);
  109. }
  110. //TODO: We do this because a CommandLinkButton turns transparent by default, except when hovered or checked
  111. ui->connect->setChecked(true);
  112. } else {
  113. qDebug() << "Unknown update type. Cannot update UI";
  114. }
  115. }