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