]> rtime.felk.cvut.cz Git - orte.git/blob - orte/contrib/shape/FPublisher.cpp
ortedemo checks for errors in publisher/subscriber creation
[orte.git] / orte / contrib / shape / FPublisher.cpp
1 #include "FPublisher.h"
2
3 FPublisher::FPublisher(QWidget *parent)
4     : QDialog(parent)
5 {
6     stepx=rand()%2+1;stepy=rand()%2+1;
7     incx=incy=0;
8     while(incx==0) incx=(rand()%3-1)*stepx;
9     while(incy==0) incy=(rand()%3-1)*stepy;
10     rect.setRect(0,0,25,45);
11     publisher=NULL;
12     /* setup UI */
13     setupUi(this);
14 }
15
16 bool FPublisher::initPublisher(int icolor,int istrength)
17 {
18     NtpTime     persistence;
19     const char  *topic;
20
21     ORTEInit();
22     domain=ORTEDomainAppCreate(ORTE_DEFAULT_DOMAIN,NULL,NULL,ORTE_FALSE);
23     if (!domain)
24         return false;
25     
26     BoxType_type_register(domain);
27
28     color=icolor;
29     strength=istrength;
30     boxType.color=color;
31     boxType.shape=strength;
32
33     view->activateObject(0,color,strength);//color,shape
34     NtpTimeAssembFromMs(persistence, 5, 0);
35     switch(color) {
36         case CL_BLUE:topic="Blue";break;
37         case CL_GREEN:topic="Green";break;
38         case CL_RED:topic="Red";break;
39         case CL_BLACK:topic="Black";break;
40         case CL_YELLOW:topic="Yellow";break;
41     }
42     publisher=ORTEPublicationCreate(
43         domain,
44         topic,
45         "BoxType",
46         &boxType,
47         &persistence,
48         strength,
49         NULL,
50         NULL,
51         NULL);
52
53     timer = new QTimer();
54     connect( timer, SIGNAL(timeout()), this, SLOT(Timer()));
55     timer->start( 50 );
56     return true;
57 }
58
59
60 void FPublisher::closeEvent( QCloseEvent *e )
61 {
62     if (domain!=NULL)
63        destroy();
64     e->accept();
65 }
66
67 void FPublisher::destroy()
68 {
69     if (domain) {
70       delete timer;
71       ORTEDomainAppDestroy(domain);
72       domain=NULL;
73       close();
74     }
75 }
76
77 void FPublisher::Timer()
78 {
79     if(rect.left()<=0) incx=stepx;
80     if(rect.top()<=0) incy=stepy;
81     if((rect.right())>=view->width()) incx=-stepx;
82     if((rect.bottom())>=view->height()) incy=-stepy;
83     if(view->mousePressed==1) {
84         int tmpW=rect.width();
85         int tmpH=rect.height();
86         rect.setRect(view->mouseX-tmpW/2,view->mouseY-tmpH/2,tmpW,tmpH);
87     } else {
88         rect.moveTo(rect.left()+incx,rect.top()+incy);
89     }
90     view->setPosition(0,rect);
91     //prepare published data
92     boxType.rectangle.top_left_x=rect.left();
93     boxType.rectangle.top_left_y=rect.top();
94     boxType.rectangle.bottom_right_x=rect.right();
95     boxType.rectangle.bottom_right_y=rect.bottom();
96     ORTEPublicationSend(publisher);    
97 }
98
99
100 void FPublisher::strengthChanged()
101 {
102     ORTEPublProp  pp;
103     
104     if (!publisher) return;
105     ORTEPublicationPropertiesGet(publisher,&pp);
106     pp.strength=slider->value();
107     ORTEPublicationPropertiesSet(publisher,&pp);
108 }
109