Переглянути джерело

Created demo application for org.commontk.eventbus

Sascha Zelzer 13 роки тому
батько
коміт
aefae43d62

+ 54 - 0
Applications/ctkEventBusDemo/CMakeLists.txt

@@ -0,0 +1,54 @@
+PROJECT(ctkEventBusDemo)
+
+#
+# See CTK/CMake/ctkMacroBuildApp.cmake for details
+#
+
+SET(KIT_SRCS
+  ctkEventBusDemoMain.cpp
+  MainWindow.cpp
+  MainWindow.h
+)
+
+# Headers that should run through moc
+SET(KIT_MOC_SRCS
+  MainWindow.h
+)
+
+# UI files
+SET(KIT_UI_FORMS
+  MainWindow.ui
+)
+
+# Resources
+SET(KIT_resources
+  
+)
+
+# Target libraries - See CMake/ctkMacroGetTargetLibraries.cmake
+# The following macro will read the target libraries from the file 'target_libraries.cmake'
+ctkFunctionGetTargetLibraries(KIT_target_libraries)
+
+# Additional directories to include - Note that CTK_INCLUDE_LIBRARIES is already included
+SET(KIT_include_directories
+)
+
+# Fix this after having discussed include dependencies with Jc
+FOREACH(_dep ${${PROJECT_NAME}_DEPENDENCIES})
+  INCLUDE_DIRECTORIES(${${_dep}_SOURCE_DIR} ${${_dep}_BINARY_DIR})
+ENDFOREACH()
+
+ctkMacroBuildApp(
+  NAME ${PROJECT_NAME}
+  INCLUDE_DIRECTORIES ${KIT_include_directories}
+  SRCS ${KIT_SRCS}
+  MOC_SRCS ${KIT_MOC_SRCS}
+  UI_FORMS ${KIT_UI_FORMS}
+  TARGET_LIBRARIES ${KIT_target_libraries}
+  RESOURCES ${KIT_resources}
+  )
+
+# Testing
+IF(BUILD_TESTING)
+#   ADD_SUBDIRECTORY(Testing)
+ENDIF(BUILD_TESTING)

+ 104 - 0
Applications/ctkEventBusDemo/MainWindow.cpp

@@ -0,0 +1,104 @@
+#include "MainWindow.h"
+#include "ui_MainWindow.h"
+
+#include "ctkEventAdminBus.h"
+
+MainWindow::MainWindow(QWidget *parent)
+  :  QMainWindow(parent), ui(new Ui::MainWindow)
+{
+    ui->setupUi(this);
+}
+
+MainWindow::MainWindow(ctkEventAdminBus *bus, QWidget *parent)
+  : QMainWindow(parent),
+    ui(new Ui::MainWindow),  m_EventBus(bus)
+{
+    ui->setupUi(this);
+    connectEvents();
+}
+
+
+MainWindow::~MainWindow()
+{
+    delete handler;
+    delete ui;
+}
+
+void MainWindow::connectEvents() {
+    handler = new ctkEventDemo();
+    connect(ui->btnSend, SIGNAL(released()), this, SLOT(sendEvent()));
+    connect(handler, SIGNAL(updateMessageSignal(QString)), this, SLOT(updateMessage(QString)));
+    connect(ui->connectButton, SIGNAL(released()), this, SLOT(connectClient()));
+
+    qDebug() << "connectEvents";
+    m_EventBus->publishSignal(handler, "receiveEventSignal(QVariantList)", "ctk/remote/eventBus/comunication/receive/xmlrpc");
+    ctkDictionary dic;
+    dic.insert("event.topics","ctk/remote/eventBus/comunication/receive/xmlrpc");
+    m_EventBus->subscribeSlot(handler, "receiveEvent(QVariantList)", dic);
+}
+
+void MainWindow::sendEvent() {
+
+    QString textToDisplay("Me: ");
+    textToDisplay.append(ui->txtParameter->property("plainText").toString());
+    ui->textBrowser->append(textToDisplay);
+
+    // event bus starts here
+
+    QVariantList localEventList;
+    localEventList.append("ctk/remote/eventBus/comunication/receive/xmlrpc");
+
+    QVariantList dataList;
+    dataList.append("myUser");
+    dataList.append(ui->txtParameter->property("plainText").toString());
+
+    ctkDictionary dic;
+
+    dic.insert("localEvent",localEventList);
+    dic.insert("localData",dataList);
+
+    QString value = "ctk/remote/eventBus/comunication/send/xmlrpc";
+    ctkEvent event(value,dic);
+
+    m_EventBus->sendEvent(event);
+}
+
+void MainWindow::changeEvent(QEvent *e)
+{
+    QMainWindow::changeEvent(e);
+    switch (e->type()) {
+    case QEvent::LanguageChange:
+        ui->retranslateUi(this);
+        break;
+    default:
+        break;
+    }
+}
+
+void MainWindow::updateMessage(QString message) {
+    ui->textBrowser->append(message);
+}
+
+void MainWindow::connectClient() {
+    bool result, resultClient, resultServer;
+    resultClient = m_EventBus->createServer("XMLRPC", ui->portLineEdit->text().toInt());
+    m_EventBus->startListen();
+    resultServer = m_EventBus->createClient("XMLRPC", ui->hostLineEdit->text(), ui->portLineEdit->text().toInt());
+    result = resultClient && resultServer;
+    if(result) {
+        ui->hostLineEdit->setEnabled(false);
+        ui->portLineEdit->setEnabled(false);
+        ui->connectButton->setEnabled(false);
+
+        ui->txtParameter->setEnabled(true);
+        ui->btnSend->setEnabled(true);
+    }
+}
+
+void ctkEventDemo::receiveEvent(QVariantList l) {
+    QString value;
+    value.append(l.at(0).toString());
+    value.append(": ");
+    value.append(l.at(1).toString());
+    emit updateMessageSignal(value);
+}

+ 47 - 0
Applications/ctkEventBusDemo/MainWindow.h

@@ -0,0 +1,47 @@
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include <QMainWindow>
+#include <QVariant>
+
+class ctkEventAdminBus;
+
+namespace Ui {
+    class MainWindow;
+}
+
+class ctkEventDemo : public QObject {
+    Q_OBJECT
+
+signals:
+    void receiveEventSignal(QVariantList l);
+    void updateMessageSignal(QString message);
+
+public slots:
+    void receiveEvent(QVariantList l);
+};
+
+class MainWindow : public QMainWindow {
+    Q_OBJECT
+public:
+    MainWindow(QWidget *parent = 0);
+    MainWindow(ctkEventAdminBus *bus, QWidget *parent = 0);
+    ~MainWindow();
+
+public slots:
+    void sendEvent();
+    void updateMessage(QString message);
+    void connectClient();
+
+protected:
+    void changeEvent(QEvent *e);
+    void connectEvents();
+
+private:
+    Ui::MainWindow *ui;
+    ctkEventAdminBus *m_EventBus;
+
+    ctkEventDemo *handler;
+};
+
+#endif // MAINWINDOW_H

+ 161 - 0
Applications/ctkEventBusDemo/MainWindow.ui

@@ -0,0 +1,161 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>MainWindow</class>
+ <widget class="QMainWindow" name="MainWindow">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>395</width>
+    <height>352</height>
+   </rect>
+  </property>
+  <property name="sizePolicy">
+   <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
+    <horstretch>0</horstretch>
+    <verstretch>0</verstretch>
+   </sizepolicy>
+  </property>
+  <property name="minimumSize">
+   <size>
+    <width>392</width>
+    <height>223</height>
+   </size>
+  </property>
+  <property name="maximumSize">
+   <size>
+    <width>16777215</width>
+    <height>16777215</height>
+   </size>
+  </property>
+  <property name="windowTitle">
+   <string>EventBus Demo App</string>
+  </property>
+  <widget class="QWidget" name="centralWidget">
+   <widget class="QWidget" name="layoutWidget">
+    <property name="geometry">
+     <rect>
+      <x>20</x>
+      <y>0</y>
+      <width>361</width>
+      <height>311</height>
+     </rect>
+    </property>
+    <layout class="QVBoxLayout" name="verticalLayout">
+     <item>
+      <layout class="QHBoxLayout" name="horizontalLayout">
+       <item>
+        <widget class="QLineEdit" name="hostLineEdit">
+         <property name="text">
+          <string>127.0.0.1</string>
+         </property>
+        </widget>
+       </item>
+       <item>
+        <widget class="QLineEdit" name="portLineEdit">
+         <property name="text">
+          <string>8000</string>
+         </property>
+        </widget>
+       </item>
+       <item>
+        <widget class="QPushButton" name="connectButton">
+         <property name="text">
+          <string>connect</string>
+         </property>
+        </widget>
+       </item>
+      </layout>
+     </item>
+     <item>
+      <widget class="QLabel" name="lblAnswer">
+       <property name="text">
+        <string>Chat:</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QTextBrowser" name="textBrowser">
+       <property name="horizontalScrollBarPolicy">
+        <enum>Qt::ScrollBarAlwaysOff</enum>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QPlainTextEdit" name="txtParameter">
+       <property name="enabled">
+        <bool>false</bool>
+       </property>
+       <property name="sizePolicy">
+        <sizepolicy hsizetype="Expanding" vsizetype="Preferred">
+         <horstretch>0</horstretch>
+         <verstretch>40</verstretch>
+        </sizepolicy>
+       </property>
+       <property name="minimumSize">
+        <size>
+         <width>0</width>
+         <height>30</height>
+        </size>
+       </property>
+       <property name="maximumSize">
+        <size>
+         <width>16777215</width>
+         <height>30</height>
+        </size>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QPushButton" name="btnSend">
+       <property name="enabled">
+        <bool>false</bool>
+       </property>
+       <property name="text">
+        <string>Send</string>
+       </property>
+      </widget>
+     </item>
+    </layout>
+   </widget>
+  </widget>
+  <widget class="QMenuBar" name="menuBar">
+   <property name="geometry">
+    <rect>
+     <x>0</x>
+     <y>0</y>
+     <width>395</width>
+     <height>22</height>
+    </rect>
+   </property>
+  </widget>
+  <widget class="QToolBar" name="mainToolBar">
+   <property name="maximumSize">
+    <size>
+     <width>0</width>
+     <height>0</height>
+    </size>
+   </property>
+   <attribute name="toolBarArea">
+    <enum>TopToolBarArea</enum>
+   </attribute>
+   <attribute name="toolBarBreak">
+    <bool>false</bool>
+   </attribute>
+  </widget>
+  <widget class="QStatusBar" name="statusBar">
+   <property name="minimumSize">
+    <size>
+     <width>0</width>
+     <height>20</height>
+    </size>
+   </property>
+  </widget>
+ </widget>
+ <layoutdefault spacing="6" margin="11"/>
+ <resources/>
+ <connections/>
+ <slots>
+  <slot>sendEvent()</slot>
+ </slots>
+</ui>

+ 111 - 0
Applications/ctkEventBusDemo/ctkEventBusDemoMain.cpp

@@ -0,0 +1,111 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) 2010 BioComputing Competence Centre - Super Computing Solutions
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=============================================================================*/
+
+#include <ctkConfig.h>
+#include <ctkPluginFrameworkFactory.h>
+#include <ctkPluginFramework.h>
+#include <ctkPluginException.h>
+#include <ctkPluginContext.h>
+#include <ctkServiceReference.h>
+
+#include "ctkEventAdminBus.h"
+
+#include <QApplication>
+#include <QString>
+#include <QStringList>
+#include <QDirIterator>
+#include <QWidget>
+#include <QFileInfo>
+
+#include "MainWindow.h"
+
+int main(int argv, char** argc) {
+  QApplication app(argv, argc);
+  qDebug() << "################################################################";
+
+  qApp->setOrganizationName("CTK");
+  qApp->setOrganizationDomain("commontk.org");
+  qApp->setApplicationName("ctkEventBusDemoApp");
+
+  qDebug() << "################################################################";
+
+  // setup the plugin framework
+  ctkPluginFrameworkFactory fwFactory;
+  QSharedPointer<ctkPluginFramework> framework = fwFactory.getFramework();
+
+  try {
+    framework->init();
+  } catch (const ctkPluginException& exc) {
+    qCritical() << "Failed to initialize the plug-in framework:" << exc;
+    exit(2);
+  }
+
+#ifdef CMAKE_INTDIR
+  QString pluginPath = CTK_PLUGIN_DIR CMAKE_INTDIR "/";
+#else
+  QString pluginPath = CTK_PLUGIN_DIR;
+#endif
+
+  qApp->addLibraryPath(pluginPath);
+
+  QStringList libFilter;
+  libFilter << "*.dll" << "*.so" << "*.dylib";
+  QDirIterator dirIter(pluginPath, libFilter, QDir::Files);
+
+  QStringList pluginsToInstall;
+  pluginsToInstall << "org_commontk_eventbus";
+    qDebug() << pluginPath;
+  QList<QSharedPointer<ctkPlugin> > installedPlugins;
+  while(dirIter.hasNext())
+  {
+    try
+    {
+      QString fileLocation = dirIter.next();
+      foreach(QString pluginToInstall, pluginsToInstall)
+      {          
+        if (fileLocation.contains(pluginToInstall))
+        {
+          QSharedPointer<ctkPlugin> plugin = framework->getPluginContext()->installPlugin(QUrl::fromLocalFile(fileLocation));
+          installedPlugins << plugin;
+          break;
+        }
+      }
+    }
+    catch (const ctkPluginException& e)
+    {
+      qCritical() << e.what();
+    }
+  }
+
+  framework->start();
+
+  foreach(QSharedPointer<ctkPlugin> plugin, installedPlugins)
+  {
+      plugin->start(ctkPlugin::START_TRANSIENT);
+  }
+
+  ctkServiceReference ebr = framework->getPluginContext()->getServiceReference("ctkEventAdminBus");
+  ctkEventAdminBus *eb = framework->getPluginContext()->getService<ctkEventAdminBus>(ebr);
+
+  MainWindow win(eb);
+  win.show();
+
+  return app.exec();
+}

+ 10 - 0
Applications/ctkEventBusDemo/target_libraries.cmake

@@ -0,0 +1,10 @@
+#
+# See CMake/ctkMacroGetTargetLibraries.cmake
+# 
+# This file should list the libraries required to build the current CTK application.
+# 
+
+SET(target_libraries
+  CTKPluginFramework
+  org_commontk_eventbus
+  )

+ 1 - 0
CMakeLists.txt

@@ -398,6 +398,7 @@ SET(CTK_APPLICATIONS
   ctkDICOMQueryRetrieve:OFF
   ctkExampleHost:OFF
   ctkExampleHostedApp:OFF
+  ctkEventBusDemo:OFF
   ctkPluginBrowser:OFF
   ctkPluginGenerator:OFF
   ctkDICOMObjectViewer:OFF