]> rtime.felk.cvut.cz Git - orte.git/blob - orte/contrib/shape/Publisher.cpp
Added prerelease of ORTE-0.2 (Real Time Publisher Subscriber communication protocol...
[orte.git] / orte / contrib / shape / Publisher.cpp
1 #include "Publisher.h"
2 #include <orte.h>
3 #include <qtimer.h>
4 #include "ViewFrm.h"
5
6 Publisher::Publisher( QWidget *parent, const char *name )
7         : QWidget( parent, name )
8 {
9         m_stepx=rand()%2+1;
10         m_stepy=rand()%2+1;
11         m_incx=0;
12         m_incy=0;
13
14         while(m_incx==0) m_incx=(rand()%3-1)*m_stepx;
15         while(m_incy==0) m_incy=(rand()%3-1)*m_stepy;
16
17         m_shapeRect.setRect(0,0,25,45);
18 }
19
20 void Publisher::Create(QString name, char shape, char color, long strength)
21 {
22   NtpTime       timePersistence;
23         //init main frame
24         m_mainFrm = new MainForm;
25         Text = new QLabel( m_mainFrm, "Text" );
26     Text->setGeometry( QRect(10 , 0, 60, 15 ) );
27     Text->setText( trUtf8( "Strength : " ) );
28         str = new QSlider(m_mainFrm, "str" );
29     str->setGeometry( QRect( 70, 0, 130, 15 ) );
30         str->setMaxValue( 10 );
31     str->setOrientation( QSlider::Horizontal );
32         connect( str, SIGNAL( valueChanged(int) ), this, SLOT(changeStrenght() ) );
33         m_mainFrm->show();
34         m_mainFrm->SetProperties(shape,color);
35         m_shapeRect.moveBy(rand()%m_mainFrm->MaxX(),rand()%m_mainFrm->MaxY());
36
37
38         //init caption
39
40  QString topic;
41         QString type;
42         top=shape;
43         typ=color;
44         strTitle="Publisher : "+name+" (Topic=";
45
46         switch(shape)
47         {
48         case RECTANGLE:
49                 strTitle+="RECTANGLE, Type=";
50                 topic="Rectangle";
51                 break;
52         case ELLIPSE:
53                 strTitle+="ELLIPSE, Type=";
54                 topic="Ellipse";
55                 break;
56         case TRIANGLE:
57                 strTitle+="TRIANGLE, Type=";
58                 topic="Triangle";
59                 break;
60         }
61
62         switch(color)
63         {
64         case BLUE:
65                 strTitle+="BLUE";
66                 type="Blue";
67                 break;
68         case GREEN:
69                 strTitle+="GREEN";
70                 type="Green";
71                 break;
72         case RED:
73                 strTitle+="RED";
74                 type="Red";
75                 break;
76         case BLACK:
77                 strTitle+="BLACK";
78                 type="Black";
79                 break;
80         }
81
82         ORTEAppCreate(&app1);
83   NtpTimeAssembFromMs(timePersistence, 5, 0);
84   h_pub=ORTEAppPublAdd(app1,topic,type,
85                        &timePersistence,strength);
86
87         strTitle+=", Strength="+QString::number(strength)+" )";
88         m_mainFrm->setCaption(strTitle);
89
90         //create the timer
91         QTimer *timer = new QTimer(this);
92         connect( timer, SIGNAL(timeout()), SLOT(Timer()));
93         timer->start( 50, FALSE );
94 }
95
96 void Publisher::Timer()
97 {
98         char            msg[128];
99         memset(msg,0,sizeof(msg));
100         MoveShape();
101         sprintf(msg,"%i %i %i %i",m_shapeRect.left(),m_shapeRect.top(),m_shapeRect.right(),m_shapeRect.bottom());
102         //Don't forget add to length last zero char !!!
103     ORTEAppPublSend(app1,h_pub,msg,strlen(msg)+1);
104 }
105
106 void Publisher::MoveShape()
107 {
108         if(m_shapeRect.left()<=0) m_incx=m_stepx;
109         if(m_shapeRect.top()<=0) m_incy=m_stepy;
110         if((m_shapeRect.right())>=m_mainFrm->MaxX()) m_incx=-m_stepx;
111         if((m_shapeRect.bottom())>=m_mainFrm->MaxY()) m_incy=-m_stepy;
112
113         if(m_mainFrm->View->m_mousePressed==1)
114         {
115                 int tmpW=m_shapeRect.width();
116                 int tmpH=m_shapeRect.height();
117
118                 m_shapeRect.setRect(m_mainFrm->View->m_mouseX-tmpW/2,m_mainFrm->View->m_mouseY-tmpH/2,tmpW,tmpH);
119         }
120         else
121         {
122                 m_shapeRect.moveBy(m_incx,m_incy);
123         }
124
125         m_mainFrm->SetShapeRect(m_shapeRect);
126 }
127
128 void Publisher::Destroy()
129 {       
130         m_mainFrm->WantClose();
131         m_mainFrm->close();
132         ORTEAppDestroy(app1);
133 }
134 void Publisher::changeStrenght()
135 {
136         ORTEPublProp  publ_prop;
137         ORTEAppPublPropGet(this->app1,this->h_pub,&publ_prop);
138         publ_prop.strength=str->value();
139         ORTEAppPublPropSet(this->app1,this->h_pub,&publ_prop);
140         QString name=QString::number(str->value());
141         if((str->value())<10) name+=" ";
142         (this->strTitle).replace((this->strTitle).length()-3,2,name);
143         (this->m_mainFrm)->setCaption(this->strTitle);
144 }