/* * testIvyQt * * Copyright (C) 1997-2010 * Centre d'Études de la Navigation Aérienne * * Main and only file * * Authors: Alexandre Bustico * * $Id: testIvyQt.cxx 1253 2006-05-24 13:02:58Z fourdan $ * * Please refer to file version.h for the * copyright notice regarding this software */ /* this example show how to bind a regexp with a one message / one class methaphor using callback on OnMessage method it shows also how to bind several regexp in one class using BindMsg method. it shows a common usage of user_data callback parameter : using it so pass a pointer on object usage : launch test_IvyQt in another terminal launch ivyprobe '(.*)' manipulate slider, see effect on ivyprobe send message in ivyprobe : set col red (or any color) and see effects on test_IvyQt */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "Ivycpp.h" #include "IvyApplication.h" // int main(int argc, char *argv[]) // { // IvyTest test; // test.bus->setXtAppContext(cntx); // test.Start(); // XtAppMainLoop (cntx); // return 0; // } class MyWidget : public QWidget, public IvyApplicationCallback, public IvyMessageCallback { Q_OBJECT public: MyWidget(QWidget *parent = 0); Ivy *bus; void IvyStart(); void OnApplicationConnected(IvyApplication *app); void OnApplicationDisconnected(IvyApplication *app); void OnApplicationCongestion(IvyApplication *app); void OnApplicationDecongestion(IvyApplication *app); void OnApplicationFifoFull(IvyApplication *app); void OnMessage(IvyApplication *app, int argc, const char **argv); static void myOwnCB1 ( IvyApplication *app, void *user_data, int argc, const char **argv ); static void myOwnCB2 ( IvyApplication *app, void *user_data, int argc, const char **argv ); private: QSlider *slider; QLCDNumber *lcd; QPushButton *quit; static void ivyAppConnCb( IvyApplication *app ) {}; static void ivyAppDiscConnCb( IvyApplication *app ) {}; private slots: void sliderChange (int value); }; MyWidget::MyWidget(QWidget *parent) : QWidget(parent) { quit = new QPushButton(tr("Quit")); quit->setFont(QFont("Times", 18, QFont::Bold)); lcd = new QLCDNumber(2); lcd->setSegmentStyle(QLCDNumber::Filled); slider = new QSlider(Qt::Horizontal); slider->setRange(0, 99); slider->setValue(0); connect(quit, SIGNAL(clicked()), qApp, SLOT(quit())); connect(slider, SIGNAL(valueChanged(int)), lcd, SLOT(display(int))); connect(slider, SIGNAL(valueChanged(int)), this, SLOT(sliderChange(int))); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(quit); layout->addWidget(lcd); layout->addWidget(slider); setLayout(layout); } void MyWidget::myOwnCB1 ( IvyApplication *app, void *user_data, int argc, const char **argv ) { printf ("coucou CB1\n"); } void MyWidget::myOwnCB2 ( IvyApplication *app, void *user_data, int argc, const char **argv ) { MyWidget *w = static_cast (user_data); printf ("coucou CB2\n"); if (argc >= 1) { QPushButton *b = w->quit; QPalette pal = b->palette(); QColor color (argv[0]); pal.setColor( QPalette::Button, color ); b->setPalette( pal ); b->setAutoFillBackground( true ); } else { printf ("no color given\n"); } } void MyWidget::IvyStart() { bus = new Ivy( "TestIvyQt", "TestIvyQt READY", BUS_APPLICATION_CALLBACK( ivyAppConnCb, ivyAppDiscConnCb ),false); bus->BindMsg( "^coucou", new IvyMessageCallbackFunction(myOwnCB1, NULL)); bus->BindMsg( "^set col (.*)", new IvyMessageCallbackFunction(myOwnCB2, this)); bus->start(NULL); } void MyWidget::OnMessage(IvyApplication *app, int argc, const char **argv) { int i; printf ("%s sent ",app->GetName()); for (i = 0; i < argc; i++) { printf(" '%s'",argv[i]); int j = atoi (argv[i]); slider->setValue(j); } printf("\n"); } void MyWidget::OnApplicationConnected (IvyApplication *app) { const char *appname; const char *host; appname = app->GetName(); host = app->GetHost(); printf("%s connected from %s\n", appname, host); } void MyWidget::OnApplicationDisconnected (IvyApplication *app) { const char *appname; const char *host; appname = app->GetName (); host = app->GetHost(); printf("%s disconnected from %s\n", appname, host); } void MyWidget::sliderChange (int val) { bus->SendMsg ("TestIvyQt slider=%d", val); } void MyWidget::OnApplicationCongestion(IvyApplication *app) { std::cerr << "Ivy Congestion notififation\n"; } void MyWidget::OnApplicationDecongestion(IvyApplication *app) { std::cerr << "Ivy Decongestion notififation\n"; } void MyWidget::OnApplicationFifoFull(IvyApplication *app) { std::cerr << "Ivy FIFO Full notififation : MESSAGE WILL BE LOST\n"; } int main(int argc, char *argv[]) { QApplication app(argc, argv); MyWidget widget; widget.IvyStart(); widget.show(); return app.exec(); } #include "testIvyQt.moc"