]> rtime.felk.cvut.cz Git - orte.git/blob - orte/contrib/shape/MyQFrame.cpp
SHAPE: Fix undefined behavior resulting in strange lock-ups
[orte.git] / orte / contrib / shape / MyQFrame.cpp
1 /****************************************************************
2 **
3 ** Implementation MyQFrame class
4 **
5 ****************************************************************/
6
7 #include "MyQFrame.h"
8 #include <QPolygon>
9 #include <QMouseEvent>
10 #include <QPaintEvent>
11
12 MyQFrame::MyQFrame( QWidget *parent)
13         : QFrame( parent)
14 {
15     mousePressed=0;
16     objects[0]=objects[1]=objects[2]=objects[3]=objects[4]=0;
17
18     connect(this,SIGNAL(changed()),this,SLOT(update()));
19 }
20
21
22 void MyQFrame::activateObject(int object,int color, int shape) {
23     if (object>4) return;
24     objects[object]=1;
25     switch(color) {
26         case 0:colors[object]=QColor(0,0,255);break;
27         case 1:colors[object]=QColor(0,255,0);break;
28         case 2:colors[object]=QColor(255,0,0);break;
29         case 3:colors[object]=QColor(0,0,0);break;
30         case 4:colors[object]=QColor(255,255,0);break;
31     }
32     shapes[object]=shape;
33 }
34
35 void MyQFrame::deactivateObject(int object) {
36     if (object>4) return;
37     objects[object]=0;
38     emit changed();
39 }
40
41 void MyQFrame::setPosition(int object,QRect position) {
42     positions[object]=position;
43     emit changed();
44 }
45
46 void MyQFrame::paintEvent(QPaintEvent*)
47 {
48     QPainter p(this);
49         
50     for(int i=0;i<5;i++) {
51         if (!objects[i]) continue;
52         p.setBrush(colors[i]);
53         p.setPen(Qt::NoPen);
54         switch(shapes[i]){
55             case 0://rectangle
56                 p.drawRect(positions[i]);
57                 break;
58             case 1: //ellipse
59                 p.drawEllipse(positions[i]);
60                 break;
61             case 2: //triangle
62                 QPolygon pt;
63                 pt.putPoints(0,3, positions[i].center().x(),positions[i].top(), 
64                                   positions[i].right(),positions[i].bottom(), 
65                                   positions[i].left(),positions[i].bottom());
66                 p.drawPolygon(pt);
67                 break;
68         }
69         QString strPos;
70         strPos="X:" + QString::number(positions[i].center().x())+" " +
71                "Y:"+QString::number(positions[i].center().y());
72         p.drawText(positions[i].left()-(70-positions[i].width())/2,positions[i].top()-1,strPos);
73     }
74
75 }
76
77 void MyQFrame::mouseMoveEvent(QMouseEvent *e)
78 {
79     if(mousePressed==0) return; 
80     mouseX=e->pos().x();
81     mouseY=e->pos().y();
82 }
83
84 void MyQFrame::mousePressEvent (QMouseEvent *e)
85 {
86     mousePressed=1;
87     mouseX=e->pos().x();
88     mouseY=e->pos().y();
89 }
90
91 void MyQFrame::mouseReleaseEvent (QMouseEvent *)
92 {
93     mousePressed=0;
94 }