summaryrefslogtreecommitdiff
path: root/testIvyQt.cxx
blob: 9eaca76b5cb184b7dc49cfe6119d84c62c90ef20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 *	testIvyQt
 *
 *	Copyright (C) 1997-2010
 *	Centre d'Études de la Navigation Aérienne
 *
 * 	Main and only file
 *
 *	Authors: Alexandre Bustico <alexandre.bustico@cena.fr>
 *
 *	$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 <iostream>
#include <fstream>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/time.h>
#include <unistd.h>
#include <QApplication>

#include <QFont>
#include <QLCDNumber>
#include <QPushButton>
#include <QSlider>
#include <QVBoxLayout>
#include <QWidget>

#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<MyWidget *> (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"