Salome HOME
NPAL19658: Deletion problem. Invalid actions were called by shortcuts.
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_ArrayView.cxx
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
4 //  This library is free software; you can redistribute it and/or 
5 //  modify it under the terms of the GNU Lesser General Public 
6 //  License as published by the Free Software Foundation; either 
7 //  version 2.1 of the License. 
8 // 
9 //  This library is distributed in the hope that it will be useful, 
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 //  Lesser General Public License for more details. 
13 // 
14 //  You should have received a copy of the GNU Lesser General Public 
15 //  License along with this library; if not, write to the Free Software 
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 // 
18 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //  SUPERV SUPERVGUI : GUI for Supervisor component
21 //
22 //  File   : SUPERVGUI_ArrayView.cxx
23 //  Author : 
24 //  Module : SUPERV
25
26 #include "SUPERVGUI.h"
27 #include "SUPERVGUI_ArrayView.h"
28 #include "SUPERVGUI_Main.h"
29 #include "SUPERVGUI_CanvasCellNodePrs.h"
30 #include "SUPERVGUI_CanvasLink.h"
31 #include "SUPERVGUI_CanvasPort.h"
32
33 #include <qcolordialog.h>
34
35 SUPERVGUI_ArrayView::SUPERVGUI_ArrayView(SUPERVGUI_CanvasArray* theArray, SUPERVGUI_Main* theMain):
36   QCanvasView(theArray, theMain),
37   myMain(theMain)
38 {
39   setName("TableView");
40
41   myIsPanActivated = false;
42
43   myAddStudyItem = 0;
44   myCursor = cursor();
45
46   viewport()->setMouseTracking(true); //widget receives mouse move events 
47                                       //even if no buttons are pressed down
48
49   myPopup = new QPopupMenu(viewport());
50
51   if (myMain->isEditable()) {
52     myPopup->insertItem(tr("MSG_ADD_NODE"), myMain, SLOT(addNode()));
53     myPopup->insertItem(tr("MSG_INS_FILE"), myMain, SLOT(insertFile()));
54     myPopup->insertSeparator();
55   }
56
57   QPopupMenu* aViewPopup = new QPopupMenu(viewport());
58   aViewPopup->insertItem(tr("POP_FULLVIEW"), myMain, SLOT(showCanvas()));
59   aViewPopup->insertItem(tr("POP_CONTROLVIEW"), myMain, SLOT(showContolFlow()));
60   aViewPopup->insertItem(tr("POP_TABLEVIEW"), myMain, SLOT(showCanvasTable()));
61
62   myPopup->insertItem(tr("POP_VIEW"), aViewPopup);
63   myPopup->insertSeparator();
64
65   /*QPopupMenu* aZoomPopup = new QPopupMenu(viewport());
66   aZoomPopup->insertItem("200%", this, SLOT(zoomIn()));
67   aZoomPopup->insertItem("100%", this, SLOT(zoomReset()));
68   aZoomPopup->insertItem("50%", this, SLOT(zoomOut()));
69   aZoomPopup->insertSeparator();
70   aZoomPopup->insertItem("Fit All", this, SLOT(fitAll()));
71
72   myPopup->insertItem("Zoom", aZoomPopup);
73   myPopup->insertSeparator();*/
74
75   myAddStudyItem = myPopup->insertItem(tr("MSG_ADD_STUDY"), myMain, SLOT(addDataflowToStudy()));
76   myPopup->insertItem(tr("MSG_CHANGE_INFO"), myMain, SLOT(changeInformation()));
77   myPopup->insertSeparator();
78
79   myPopup->insertItem(tr("MSG_COPY_DATAFLOW"), myMain, SLOT(copy()));
80   myPopup->insertItem(tr("MSG_FILTER_NOTIFY"), myMain, SLOT(filterNotification()));
81
82   myPopup->insertSeparator();
83   myPopup->insertItem(tr("MSG_CHANGE_BACKGROUND"), this, SLOT(changeBackground()));
84
85   SUPERVGraph_ViewFrame* anActiveVF = (SUPERVGraph_ViewFrame*)myMain->parent();
86   if ( anActiveVF ) {
87     myPopup->insertSeparator();
88     myShowToolBarItem = myPopup->insertItem( tr( "MEN_SHOW_TOOLBAR" ), myMain, SLOT( onShowToolbar() ) );
89   }
90
91   hide();
92 }
93  
94
95 SUPERVGUI_ArrayView::~SUPERVGUI_ArrayView()
96 {
97 }
98
99 void SUPERVGUI_ArrayView::ActivatePanning()
100 {
101   myIsPanActivated = true;
102   viewport()->setMouseTracking(false);
103 }
104
105 void SUPERVGUI_ArrayView::ResetView()
106 {
107   setContentsPos(0,0);
108   QWMatrix m;
109   setWorldMatrix(m);
110 }
111
112 void SUPERVGUI_ArrayView::changeBackground()
113 {
114   QColor aColor = QColorDialog::getColor(canvas()->backgroundColor(), this );
115   if ( aColor.isValid() ) {
116     // change background color for array view
117     canvas()->setBackgroundColor(aColor);
118     setPaletteBackgroundColor(aColor.light(120));
119     // change background color for canvas view
120     getMain()->getCanvasView()->canvas()->setBackgroundColor(aColor);
121     getMain()->getCanvasView()->setPaletteBackgroundColor(aColor.light(120));
122
123     // mkr : IPAL10825 -->
124     SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
125     if ( !aSupMod ) {
126       MESSAGE("NULL Supervision module!");
127       return;
128     }
129     aSupMod->setIVFBackgroundColor(aColor);
130     // <--
131   }
132 }
133
134 void SUPERVGUI_ArrayView::resizeEvent( QResizeEvent* e) {
135   QCanvasView::resizeEvent( e );
136   canvas()->resize( e->size().width() > canvas()->width() ? e->size().width() : canvas()->width(), 
137                     e->size().height() > canvas()->height() ? e->size().height() : canvas()->height() );
138 }
139
140 void SUPERVGUI_ArrayView::contentsMousePressEvent(QMouseEvent* theEvent) {
141   myGlobalPoint = theEvent->globalPos();
142
143   if (((theEvent->button() == Qt::MidButton)
144        &&
145        (theEvent->state() == Qt::ControlButton)) || myIsPanActivated) {
146     myIsPanActivated = true;
147     viewport()->setMouseTracking(false); //widget only receives mouse move events when at least one 
148                                          //mouse button is pressed down while the mouse is being moved
149     myCursor = cursor();
150
151 #if QT_VERSION >= 0x030005
152     QCursor PanCursor(Qt::SizeAllCursor);
153 #else
154     QCursor PanCursor(SizeAllCursor);
155 #endif
156     setCursor(PanCursor);
157     return;
158   } 
159
160   QPoint thePoint = inverseWorldMatrix().map(theEvent->pos());
161   // compute collision rectangle
162   QRect aSel(thePoint.x()-MARGIN, thePoint.y()-MARGIN, 1+2*MARGIN, 1+2*MARGIN);
163
164   if (theEvent->button() == RightButton) {
165     QCanvasItemList l = canvas()->collisions(aSel);
166     for (QCanvasItemList::Iterator it = l.begin(); it != l.end(); ++it) {
167       if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_Node) {
168         SUPERVGUI_CanvasCellNodePrs* aNodePrs = (SUPERVGUI_CanvasCellNodePrs*) (*it);
169         QObject* anObj = aNodePrs->getObject(thePoint);
170         if (anObj->inherits("SUPERVGUI_CanvasCellNode")) {
171           myMain->showPopup(((SUPERVGUI_CanvasCellNode*)anObj)->getPopupMenu(viewport()), 
172                             theEvent);
173           return;
174         }
175       }
176     }
177
178     myPopup->setItemEnabled(myAddStudyItem, !myMain->isDataflowInStudy());
179     myPopup->setItemEnabled(myShowToolBarItem, !((SUPERVGraph_ViewFrame*)myMain->parent())->getToolBar()->isVisible());
180     myMain->showPopup(myPopup, theEvent);
181     return;
182   }
183 }
184
185 void SUPERVGUI_ArrayView::contentsMouseMoveEvent(QMouseEvent* theEvent) {
186   QPoint g = theEvent->globalPos();
187   if (myIsPanActivated) {
188     scrollBy(myGlobalPoint.x() - g.x(),
189              myGlobalPoint.y() - g.y());
190     myGlobalPoint = g;
191     return;
192   }
193
194   // QToolTip for title and label for SUPERVGUI_CanvasCellNode
195   SUPERVGUI_ToolTip* aTT = new SUPERVGUI_ToolTip(this);
196   aTT->maybeTip(theEvent->pos());
197 }
198
199 void SUPERVGUI_ArrayView::contentsMouseReleaseEvent(QMouseEvent* theEvent) {
200   if (myIsPanActivated) {
201     myIsPanActivated = false;
202     viewport()->setMouseTracking(true);
203     setCursor(myCursor);
204   }
205 }
206
207 void SUPERVGUI_ToolTip::maybeTip(const QPoint& thePos) {
208   // mkr : 23.11.2006 - PAL13957 - modifications for correct tooltips displaying after scroll or zoom
209   QPoint theP = ((QCanvasView*)parentWidget())->inverseWorldMatrix().map(thePos);
210
211   int avX, avY;
212   ((QCanvasView*)parentWidget())->contentsToViewport(theP.x(), theP.y(), avX, avY);
213   QPoint avP(avX, avY);
214
215   QWMatrix aWM = ((QCanvasView*)parentWidget())->worldMatrix();
216   
217   // compute collision rectangle for nodes, ports
218   QRect aNodeSel(theP.x()-MARGIN, theP.y()-MARGIN, 1+2*MARGIN, 1+2*MARGIN);
219
220   QCanvasItemList lN = ((QCanvasView*)parentWidget())->canvas()->collisions(aNodeSel);
221   for (QCanvasItemList::Iterator it = lN.begin(); it != lN.end(); ++it)
222   {
223     // tooltip for node
224     if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_Node)
225     {
226       SUPERVGUI_CanvasNodePrs* aNodePrs = (SUPERVGUI_CanvasNodePrs*) (*it);
227       QObject* anObj = aNodePrs->getObject(theP);
228       if (anObj->inherits("SUPERVGUI_CanvasNode"))
229       {
230         QRect aTitleRect = aNodePrs->getTitleRect();
231         QRect aLabelRect = aNodePrs->getLabelRect();
232         QRect aStatusRect = aNodePrs->getStatusRect();
233
234         if (aTitleRect.bottom()+1 == aLabelRect.top() &&
235             aLabelRect.bottom()+1 == aStatusRect.top())
236         {
237           ((QCanvasView*)parentWidget())->contentsToViewport((int)(aTitleRect.left()*aWM.m11()), 
238                                                              (int)(aTitleRect.top()*aWM.m22()), 
239                                                              avX, avY);
240           QRect avTipRect(avX, avY,
241                           (int)(aTitleRect.width()*aWM.m11()),
242                           (int)((aTitleRect.height() + aLabelRect.height() + aStatusRect.height())*aWM.m22()));
243           tip(avTipRect, ((SUPERVGUI_CanvasNode*)anObj)->getToolTipText());
244           return;
245         }
246
247         if (aTitleRect.contains(theP, true))
248         {
249           ((QCanvasView*)parentWidget())->contentsToViewport((int)(aTitleRect.left()*aWM.m11()), 
250                                                              (int)(aTitleRect.top()*aWM.m22()), 
251                                                              avX, avY);
252           QRect avTipRect(avX, avY, 
253                           (int)(aTitleRect.width()*aWM.m11()), (int)(aTitleRect.height()*aWM.m22()));
254           tip(avTipRect, ((SUPERVGUI_CanvasNode*)anObj)->getToolTipText());
255           return;
256         }
257
258         if (aLabelRect.contains(theP, true))
259         {
260           ((QCanvasView*)parentWidget())->contentsToViewport((int)(aLabelRect.left()*aWM.m11()), 
261                                                              (int)(aLabelRect.top()*aWM.m22()), 
262                                                              avX, avY);
263           QRect avTipRect(avX, avY, 
264                           (int)(aLabelRect.width()*aWM.m11()), (int)(aLabelRect.height()*aWM.m22()));
265           tip(avTipRect, ((SUPERVGUI_CanvasNode*)anObj)->getToolTipText());
266           return;
267         }
268
269         if (aStatusRect.contains(theP, true))
270         {
271           ((QCanvasView*)parentWidget())->contentsToViewport((int)(aStatusRect.left()*aWM.m11()), 
272                                                              (int)(aStatusRect.top()*aWM.m22()), 
273                                                              avX, avY);
274           QRect avTipRect(avX, avY, 
275                           (int)(aStatusRect.width()*aWM.m11()), (int)(aStatusRect.height()*aWM.m22()));
276           tip(avTipRect, ((SUPERVGUI_CanvasNode*)anObj)->getToolTipText());
277           return;
278         }
279         
280       }
281       // tooltip for nodes' port
282       if (anObj->inherits("SUPERVGUI_CanvasPort"))
283       {
284         SUPERVGUI_CanvasPort* aPort = (SUPERVGUI_CanvasPort*)anObj;
285         QRect aPortRect = aPort->getPrs()->getPortRect();
286         ((QCanvasView*)parentWidget())->contentsToViewport((int)(aPortRect.left()*aWM.m11()), 
287                                                            (int)(aPortRect.top()*aWM.m22()), 
288                                                            avX, avY);
289         QRect avTipRect(avX, avY, 
290                         (int)(aPortRect.width()*aWM.m11()), (int)(aPortRect.height()*aWM.m22()));
291         tip(avTipRect, aPort->getEngine()->Type() + QString(" ") + aPort->getPrs()->getText());
292         return;
293       }
294     }
295   }
296
297   // compute collision rectangle for links
298   QRect aLinkSel((int)( theP.x() + (theP.x()-avP.x())/aWM.m11() ) - MARGIN, 
299                  (int)( theP.y() + (theP.y()-avP.y())/aWM.m22() ) - MARGIN, 
300                  1+2*MARGIN, 1+2*MARGIN);
301
302   QCanvasItemList lL = ((QCanvasView*)parentWidget())->canvas()->collisions(aLinkSel);
303   for (QCanvasItemList::Iterator it = lL.begin(); it != lL.end(); ++it)
304   {
305     // tootip for links' point
306     if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_LinkPoint)
307     {
308       SUPERVGUI_CanvasPointPrs* aPrs = (SUPERVGUI_CanvasPointPrs*) (*it);
309       if (aPrs->getLink()->getMain()->getViewType() == CANVAS)
310       {
311         ((QCanvasView*)parentWidget())->contentsToViewport((int)(aLinkSel.left()*aWM.m11()), 
312                                                            (int)(aLinkSel.top()*aWM.m22()), 
313                                                            avX, avY);
314         QRect avTipRect(avX, avY, 
315                         (int)(aLinkSel.width()*aWM.m11()), (int)(aLinkSel.height()*aWM.m22()));
316         tip(avTipRect, aPrs->getLink()->getToolTipText());
317       }
318       return;
319     }
320     // tooltip for links' edge
321     if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_LinkEdge)
322     {
323       SUPERVGUI_CanvasEdgePrs* aPrs = (SUPERVGUI_CanvasEdgePrs*) (*it);
324       if (aPrs->getLink()->getMain()->getViewType() == CANVAS)
325       {
326         ((QCanvasView*)parentWidget())->contentsToViewport((int)(aLinkSel.left()*aWM.m11()), 
327                                                            (int)(aLinkSel.top()*aWM.m22()), 
328                                                            avX, avY);
329         QRect avTipRect(avX, avY, 
330                         (int)(aLinkSel.width()*aWM.m11()), (int)(aLinkSel.height()*aWM.m22()));
331         tip(avTipRect, aPrs->getLink()->getToolTipText());
332       }
333       return;
334     }
335   }  
336 }