Salome HOME
NRI : Comment unused ComponentType enum.
[modules/kernel.git] / src / SUPERVGraph / SUPERVGraph_Graph.cxx
1 using namespace std;
2 //  File      : SUPERVGraph_Graph.cxx
3 //  Created   : 17 / 10 / 2001
4 //  Author    : Francis KLOSS
5 //  Project   : SALOME
6 //  Module    : SUPERVGraph
7 //  Copyright : Open CASCADE
8
9 #include "SUPERVGraph_Graph.h"
10
11 #include "QAD_Settings.h"
12 #include "QAD_Config.h"
13
14 #include <qcolordialog.h>
15 #include <qpopupmenu.h>
16
17
18 SUPERVGraph_Graph::SUPERVGraph_Graph(QWidget* parent) : 
19   QScrollView(parent),
20   where(QPoint(0, 0))
21 {
22   installEventFilter(viewport());
23   //  viewport()->setBackgroundColor(QColor(165, 255, 176));
24
25   connect(this, SIGNAL(contentsMoving(int, int)), this, SLOT(Draw(int, int)));
26   
27   popup = new QPopupMenu(viewport());
28   //      if (main->isModify()) {
29   //          pan = popup->insertItem("Add Node",            main, SLOT(addNode()));
30   //          pap = popup->insertItem("Add Point",           this, SLOT(addPoint()));
31   //          pdl = popup->insertItem("Delete Link",         this, SLOT(deleteLink()));
32   //          pif = popup->insertItem("Insert File",         main, SLOT(insertFile()));
33   //          pci = popup->insertItem("Change Informations", this, SLOT(changeInformation()));
34   //                popup->insertSeparator();
35   //      };
36   //    popup->insertItem("Add in Study",        main, SLOT(addStudy()));
37   //    popup->insertItem("Show Informations",   this, SLOT(showInformation()));
38   //    popup->insertSeparator();
39   //    popup->insertItem("Copy (Read Only)",    main, SLOT(copyReading()));
40   //    popup->insertItem("Copy (Modifiying)",   main, SLOT(copyModifying()));
41 }
42
43 void SUPERVGraph_Graph::sync() {
44   //   bool editing = main->getDataflow()->IsEditing();
45   //      if (main->isModify()) {
46   //          popup->setItemEnabled(pci, editing);
47   //          popup->setItemEnabled(pan, editing);
48   //          popup->setItemEnabled(pap, editing);
49   //          popup->setItemEnabled(pdl, editing);
50   //          popup->setItemEnabled(pif, editing);
51   //      };
52   
53   //      SUPERVGraph_Node* ihmNode;
54   //      QObjectList* ihmList = queryList("SUPERVGraph_Node");
55   //      SUPERV_Nodes nodes = main->getDataflow()->Nodes();
56   //      int n = nodes->length();
57
58   //      for (int i=0; i<n; i++) {
59   //          ihmNode = (SUPERVGraph_Node *)child(nodes[i]->Name(), "SUPERVGraph_Node");
60   //          if (ihmNode == NULL) {
61   //              ihmNode = new SUPERVGraph_Node(viewport(), main, nodes[i]);
62   //              addChild(ihmNode, nodes[i]->X(), nodes[i]->Y());
63   //          } else {
64   //              moveChild(ihmNode, nodes[i]->X(), nodes[i]->Y());
65   //        ihmList->removeRef(ihmNode);
66   //          };
67   //          ihmNode->sync();
68   //      };
69
70   //      QObjectListIt i(*ihmList);
71   //      while ((ihmNode=(SUPERVGraph_Node*)i.current()) != 0) {
72   //          ++i;
73   //          ihmNode->close();
74   //      };
75   //      delete ihmList;
76
77   //      SUPERVGraph_PortIn* pi;
78   //      ihmList = queryList("SUPERVGraph_PortIn");
79   //      i = *ihmList;
80   //      while ((pi=(SUPERVGraph_PortIn*)i.current()) != 0) {
81   //          ++i;
82   //          pi->sync(true);
83   //      };
84   //      delete ihmList;
85
86   //      SUPERVGraph_PortOut* po;
87   //      ihmList = queryList("SUPERVGraph_PortOut");
88   //      i = *ihmList;
89   //      while ((po=(SUPERVGraph_PortOut*)i.current()) != 0) {
90   //          ++i;
91   //          po->sync();
92   //      };
93   //      delete ihmList;
94
95   //      Draw(0, 0);
96 }
97
98 SUPERVGraph_Graph::~SUPERVGraph_Graph() {
99 }
100
101 bool SUPERVGraph_Graph::eventFilter(QObject* object, QEvent* event) {
102   if ((event->type() == QEvent::Move ) ||
103       (event->type() == QEvent::Paint) ||
104       (event->type() == QEvent::Resize) ) {
105     Draw(0, 0);
106   };
107   return(QScrollView::eventFilter(object, event));
108 }
109
110 void SUPERVGraph_Graph::Draw(int x, int y) {
111   QPainter p(viewport());
112   p.eraseRect(viewport()->rect());
113   p.setPen(QPen(Qt::black, 2));
114   
115   //   SUPERVGraph_Point* c;
116   //    SUPERVGraph_Point* n;
117   //    QObjectList* points = queryList("SUPERVGraph_Point");
118   //    QObjectListIt i(*points);
119   //    while ((c=(SUPERVGraph_Point*)i.current()) != 0) {
120   //      ++i;
121   //          n=c->getNext();
122   //          if (n!=c) {
123   //              p.drawLine(contentsToViewport(QPoint(c->getX()+POINT_SIZE_HALF, c->getY()+POINT_SIZE_HALF)),
124   //                         contentsToViewport(QPoint(n->getX()+POINT_SIZE_HALF, n->getY()+POINT_SIZE_HALF)) );
125   //          };
126   //      };
127   //      delete points;
128 }
129
130 void SUPERVGraph_Graph::contentsMousePressEvent(QMouseEvent* e) {
131   where = viewportToContents(viewport()->mapFromGlobal(e->globalPos()));
132   //   main->showPopup(popup, e);
133 }
134
135 void SUPERVGraph_Graph::contentsMouseReleaseEvent(QMouseEvent* e) {
136   //    main->sketchPoint(NULL);
137
138  if ( e->button() == Qt::RightButton ) {
139    QPopupMenu* popup = createPopup();
140    if ( popup ) {
141      popup->exec( QCursor::pos() );
142      destroyPopup();
143    }
144    return;
145  }  
146
147 }
148
149 void SUPERVGraph_Graph::contentsMouseMoveEvent(QMouseEvent* e) {
150   //   main->sketchMove();
151 }
152
153 float SUPERVGraph_Graph::distance(int xp, int yp, int xl, int yl, int xn, int yn) {
154   float r, s;
155   float a, b, c, d;
156
157   float px = (float)(xp);
158   float py = (float)(yp);
159   float lx = (float)(xl);
160   float ly = (float)(yl);
161   float nx = (float)(xn);
162   float ny = (float)(yn);
163
164   r = px - lx;
165   r = r * r;
166   s = py - ly;
167   s = s * s;
168   a = r + s;
169
170   r = px - nx;
171   r = r * r;
172   s = py - ny;
173   s = s * s;
174   b = r + s;
175
176   r = nx - lx;
177   r = r * r;
178   s = ny - ly;
179   s = s * s;
180   c = r + s;
181
182   if (c<0.001) {
183     return(a);
184   } else {
185     r = a + c - b;
186     r = r * r;
187     d = r / ( 4 * c);
188     return(a - d);
189   };
190 }
191
192 QPoint SUPERVGraph_Graph::getPoint() {
193   return(where);
194 }
195
196 //  SUPERVGraph_Point* SUPERVGraph_Graph::findPoint(int x, int y) {
197 //      float            m;
198 //      float            d = SCROLL_MAX;
199 //      SUPERVGraph_Point* t = NULL;
200 //      SUPERVGraph_Point* c;
201 //      SUPERVGraph_Point* n;
202 //      QObjectList* points = queryList("SUPERVGraph_Point");
203 //      QObjectListIt i(*points);
204
205 //      while ((c=(SUPERVGraph_Point*)i.current()) != 0) {
206 //          ++i;
207 //          n=c->getNext();
208 //          if (n != c) {
209 //              m = distance(x, y, c->getX(), c->getY(), n->getX(), n->getY());
210 //              if (m < d) {
211 //                  d = m;
212 //                  t = c;
213 //              };
214 //          };
215 //      };
216 //      delete points;
217 //      return(t);
218 //  }
219
220 void SUPERVGraph_Graph::addPoint() {
221   //    int              x = where.x();
222   //      int              y = where.y();
223   //      SUPERVGraph_Point* p = findPoint(x, y);
224   //      if (p == NULL) {
225   //          QMessageBox::warning(0, "Supervision Error", "No Link to Add a Point");
226   //      } else {
227   //          p->addPoint(x, y, true);
228   //      };
229 }
230
231 void SUPERVGraph_Graph::deleteLink() {
232   //   SUPERVGraph_Point* p = findPoint(where.x(), where.y());
233   //      if (p == NULL) {
234   //          QMessageBox::warning(0, "Supervision Error", "No Link to Delete");
235   //      } else {
236   //          SUPERV_Link l = p->getLink();
237   //          if (l == NULL) {
238   //              QMessageBox::warning(0, "Supervision Error", "No Link to Delete");
239   //          } else {
240   //              l->Destroy();
241   //              main->sync();
242   //          };
243   //      };
244 }
245
246 void SUPERVGraph_Graph::showInformation() {
247   //   if (Supervision.information(Engines::Node::_narrow(main->getDataflow()), false)) {
248   //          main->sync();
249   //      };
250 }
251
252 void SUPERVGraph_Graph::changeInformation() {
253   //    if (Supervision.information(Engines::Node::_narrow(main->getDataflow()), true)) {
254   //          main->sync();
255   //      };
256 }
257
258 /*!
259     Creates the popup 
260 */
261 void SUPERVGraph_Graph::onCreatePopup() 
262 {
263   if ( myPopup ) {                      
264 //    if (myPopup->count()<1) {
265     if ( myPopup->count() > 0 )
266       myIDs.append ( myPopup->insertSeparator() );      
267     int id;
268     myIDs.append ( id = myPopup->insertItem (tr ("MEN_VP3D_CHANGEBGR")) );      
269     QAD_ASSERT ( myPopup->connectItem ( id, this, SLOT(onChangeBackgroundColor())) );
270 //    }
271   }
272 }
273
274 void SUPERVGraph_Graph::onChangeBackgroundColor()
275 {
276   QColor previousColor = viewport()->backgroundColor();
277   QColor selColor = QColorDialog::getColor ( previousColor, NULL );     
278   if ( selColor.isValid() ) 
279     viewport()->setBackgroundColor( selColor );
280   
281   QAD_CONFIG->addSetting( "SUPERVGraph:BackgroundColorRed",   selColor.red() );
282   QAD_CONFIG->addSetting( "SUPERVGraph:BackgroundColorGreen", selColor.green() );
283   QAD_CONFIG->addSetting( "SUPERVGraph:BackgroundColorBlue",  selColor.blue() );
284 }