ctkEventBusDemoMainWindow.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "ctkEventBusDemoMainWindow.h"
  2. #include "ui_ctkEventBusDemoMainWindow.h"
  3. #include "ctkEventAdminBus.h"
  4. ctkEventBusDemoMainWindow::ctkEventBusDemoMainWindow(QWidget *parent)
  5. : QMainWindow(parent), ui(new Ui::ctkEventBusDemoMainWindow)
  6. {
  7. ui->setupUi(this);
  8. }
  9. ctkEventBusDemoMainWindow::ctkEventBusDemoMainWindow(ctkEventAdminBus *bus, QWidget *parent)
  10. : QMainWindow(parent),
  11. ui(new Ui::ctkEventBusDemoMainWindow), m_EventBus(bus)
  12. {
  13. ui->setupUi(this);
  14. connectEvents();
  15. }
  16. ctkEventBusDemoMainWindow::~ctkEventBusDemoMainWindow()
  17. {
  18. delete handler;
  19. delete ui;
  20. }
  21. void ctkEventBusDemoMainWindow::connectEvents() {
  22. handler = new ctkEventDemo();
  23. connect(ui->btnSend, SIGNAL(released()), this, SLOT(sendEvent()));
  24. connect(handler, SIGNAL(updateMessageSignal(QString)), this, SLOT(updateMessage(QString)));
  25. connect(ui->connectButton, SIGNAL(released()), this, SLOT(connectClient()));
  26. qDebug() << "connectEvents";
  27. m_EventBus->publishSignal(handler, "receiveEventSignal(QVariantList)", "ctk/remote/eventBus/comunication/receive/xmlrpc");
  28. ctkDictionary dic;
  29. dic.insert("event.topics","ctk/remote/eventBus/comunication/receive/xmlrpc");
  30. m_EventBus->subscribeSlot(handler, "receiveEvent(QVariantList)", dic);
  31. }
  32. void ctkEventBusDemoMainWindow::sendEvent() {
  33. QString textToDisplay("Me: ");
  34. textToDisplay.append(ui->txtParameter->property("plainText").toString());
  35. ui->textBrowser->append(textToDisplay);
  36. // event bus starts here
  37. QVariantList localEventList;
  38. localEventList.append("ctk/remote/eventBus/comunication/receive/xmlrpc");
  39. QVariantList dataList;
  40. dataList.append("myUser");
  41. dataList.append(ui->txtParameter->property("plainText").toString());
  42. ctkDictionary dic;
  43. dic.insert("localEvent",localEventList);
  44. dic.insert("localData",dataList);
  45. QString value = "ctk/remote/eventBus/comunication/send/xmlrpc";
  46. ctkEvent event(value,dic);
  47. m_EventBus->sendEvent(event);
  48. }
  49. void ctkEventBusDemoMainWindow::changeEvent(QEvent *e)
  50. {
  51. QMainWindow::changeEvent(e);
  52. switch (e->type()) {
  53. case QEvent::LanguageChange:
  54. ui->retranslateUi(this);
  55. break;
  56. default:
  57. break;
  58. }
  59. }
  60. void ctkEventBusDemoMainWindow::updateMessage(QString message) {
  61. ui->textBrowser->append(message);
  62. }
  63. void ctkEventBusDemoMainWindow::connectClient() {
  64. bool result, resultClient, resultServer;
  65. resultClient = m_EventBus->createServer("XMLRPC", ui->portLineEdit->text().toInt());
  66. m_EventBus->startListen();
  67. resultServer = m_EventBus->createClient("XMLRPC", ui->hostLineEdit->text(), ui->portLineEdit->text().toInt());
  68. result = resultClient && resultServer;
  69. if(result) {
  70. ui->hostLineEdit->setEnabled(false);
  71. ui->portLineEdit->setEnabled(false);
  72. ui->connectButton->setEnabled(false);
  73. ui->txtParameter->setEnabled(true);
  74. ui->btnSend->setEnabled(true);
  75. }
  76. }
  77. void ctkEventDemo::receiveEvent(QVariantList l) {
  78. QString value;
  79. value.append(l.at(0).toString());
  80. value.append(": ");
  81. value.append(l.at(1).toString());
  82. emit updateMessageSignal(value);
  83. }