Salome HOME
Bug fix: don't set "Loading" state for MacroNodes in InitialState() function (called...
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_ArrayView.cxx
1 //  SUPERV SUPERVGUI : GUI for Supervisor component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE
4 //
5 //  File   : SUPERVGUI_ArrayView.cxx
6 //  Author : 
7 //  Module : SUPERV
8
9
10 using namespace std;
11 #include "SUPERVGUI_ArrayView.h"
12 #include "SUPERVGUI_Main.h"
13 #include "SUPERVGUI_CanvasCellNodePrs.h"
14
15 #include <qcolordialog.h>
16
17 #if QT_VERSION >= 0x030005
18 QCursor PanCursor(Qt::SizeAllCursor);
19 #else
20 QCursor PanCursor(SizeAllCursor);
21 #endif
22
23 SUPERVGUI_ArrayView::SUPERVGUI_ArrayView(SUPERVGUI_CanvasArray* theArray, SUPERVGUI_Main* theMain):
24   QCanvasView(theArray, theMain),
25   myMain(theMain)
26 {
27   setName("TableView");
28
29   myIsPanActivated = false;
30
31   myAddStudyItem = 0;
32   myCursor = cursor();
33
34   viewport()->setMouseTracking(true); //widget receives mouse move events 
35                                       //even if no buttons are pressed down
36
37   myPopup = new QPopupMenu(viewport());
38
39   if (myMain->isEditable()) {
40     myPopup->insertItem(tr("MSG_ADD_NODE"), myMain, SLOT(addNode()));
41     myPopup->insertItem(tr("MSG_INS_FILE"), myMain, SLOT(insertFile()));
42     myPopup->insertSeparator();
43   }
44
45   QPopupMenu* aViewPopup = new QPopupMenu(viewport());
46   aViewPopup->insertItem(tr("POP_FULLVIEW"), myMain, SLOT(showCanvas()));
47   aViewPopup->insertItem(tr("POP_CONTROLVIEW"), myMain, SLOT(showContolFlow()));
48   aViewPopup->insertItem(tr("POP_TABLEVIEW"), myMain, SLOT(showTable()));
49   aViewPopup->insertItem(tr("POP_TABLEVIEWCANVAS"), myMain, SLOT(showCanvasTable()));
50
51   myPopup->insertItem(tr("POP_VIEW"), aViewPopup);
52   myPopup->insertSeparator();
53
54   /*QPopupMenu* aZoomPopup = new QPopupMenu(viewport());
55   aZoomPopup->insertItem("200%", this, SLOT(zoomIn()));
56   aZoomPopup->insertItem("100%", this, SLOT(zoomReset()));
57   aZoomPopup->insertItem("50%", this, SLOT(zoomOut()));
58   aZoomPopup->insertSeparator();
59   aZoomPopup->insertItem("Fit All", this, SLOT(fitAll()));
60
61   myPopup->insertItem("Zoom", aZoomPopup);
62   myPopup->insertSeparator();*/
63
64   myAddStudyItem = myPopup->insertItem(tr("MSG_ADD_STUDY"), this, SLOT(addToStudy()));
65   myPopup->insertItem(tr("MSG_CHANGE_INFO"), myMain, SLOT(changeInformation()));
66   myPopup->insertSeparator();
67
68   myPopup->insertItem(tr("MSG_COPY_DATAFLOW"), myMain, SLOT(copy()));
69   myPopup->insertItem(tr("MSG_FILTER_NOTIFY"), myMain, SLOT(filterNotification()));
70
71   myPopup->insertSeparator();
72   myPopup->insertItem(tr("MSG_CHANGE_BACKGROUND"), this, SLOT(changeBackground()));
73  
74   hide();
75 }
76  
77
78 SUPERVGUI_ArrayView::~SUPERVGUI_ArrayView()
79 {
80 }
81
82 void SUPERVGUI_ArrayView::ActivatePanning()
83 {
84   cout<<" ===> SUPERVGUI_ArrayView::ActivatePanning()"<<endl;
85   myIsPanActivated = true;
86   viewport()->setMouseTracking(false);
87 }
88
89 void SUPERVGUI_ArrayView::ResetView()
90 {
91   setContentsPos(0,0);
92   QWMatrix m;
93   setWorldMatrix(m);
94 }
95
96 void SUPERVGUI_ArrayView::addToStudy() 
97 {
98   if (myMain->addStudy()) myMain->setAsFromStudy(true);
99 }
100
101 void SUPERVGUI_ArrayView::changeBackground()
102 {
103   QColor aColor = QColorDialog::getColor(canvas()->backgroundColor(), this );
104   if ( aColor.isValid() ) {
105     canvas()->setBackgroundColor(aColor);
106     setPaletteBackgroundColor(aColor.light(120));
107   }
108 }
109
110 void SUPERVGUI_ArrayView::contentsMousePressEvent(QMouseEvent* theEvent) {
111   myGlobalPoint = theEvent->globalPos();
112
113   cout<<" ===> myIsPanActivated = "<<myIsPanActivated<<endl;
114   if (((theEvent->button() == Qt::MidButton)
115        &&
116        (theEvent->state() == Qt::ControlButton)) || myIsPanActivated) {
117     myIsPanActivated = true;
118     viewport()->setMouseTracking(false); //widget only receives mouse move events when at least one 
119                                          //mouse button is pressed down while the mouse is being moved
120     myCursor = cursor();
121     setCursor(PanCursor);
122     return;
123   } 
124
125   QPoint thePoint = inverseWorldMatrix().map(theEvent->pos());
126   // compute collision rectangle
127   QRect aSel(thePoint.x()-MARGIN, thePoint.y()-MARGIN, 1+2*MARGIN, 1+2*MARGIN);
128
129   if (theEvent->button() == RightButton) {
130     QCanvasItemList l = canvas()->collisions(aSel);
131     for (QCanvasItemList::Iterator it = l.begin(); it != l.end(); ++it) {
132       if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_Node) {
133         SUPERVGUI_CanvasCellNodePrs* aNodePrs = (SUPERVGUI_CanvasCellNodePrs*) (*it);
134         QObject* anObj = aNodePrs->getObject(thePoint);
135         if (anObj->inherits("SUPERVGUI_CanvasCellNode")) {
136           myMain->showPopup(((SUPERVGUI_CanvasCellNode*)anObj)->getPopupMenu(viewport()), 
137                             theEvent);
138           return;
139         }
140       }
141     }
142
143     myPopup->setItemEnabled(myAddStudyItem, !myMain->isFromStudy());
144     myMain->showPopup(myPopup, theEvent);
145     return;
146   }
147 }
148
149 void SUPERVGUI_ArrayView::contentsMouseMoveEvent(QMouseEvent* theEvent) {
150   QPoint g = theEvent->globalPos();
151   if (myIsPanActivated) {
152     scrollBy(myGlobalPoint.x() - g.x(),
153              myGlobalPoint.y() - g.y());
154     myGlobalPoint = g;
155     return;
156   }
157
158   // QToolTip for title and label for SUPERVGUI_CanvasCellNode
159   SUPERVGUI_ToolTip* aTT = new SUPERVGUI_ToolTip(this);
160   QPoint aPoint1 = inverseWorldMatrix().map(theEvent->pos());
161   aTT->maybeTip(aPoint1);
162 }
163
164 void SUPERVGUI_ArrayView::contentsMouseReleaseEvent(QMouseEvent* theEvent) {
165   if (myIsPanActivated) {
166     myIsPanActivated = false;
167     viewport()->setMouseTracking(true);
168     setCursor(myCursor);
169   }
170 }
171
172 void SUPERVGUI_ToolTip::maybeTip(const QPoint& theP) {
173   // compute collision rectangle
174   QRect aSel(theP.x()-MARGIN, theP.y()-MARGIN, 1+2*MARGIN, 1+2*MARGIN);
175
176   QCanvasItemList l = ((SUPERVGUI_ArrayView*)parentWidget())->canvas()->collisions(aSel);
177   for (QCanvasItemList::Iterator it = l.begin(); it != l.end(); ++it) {
178     if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_Node) {
179       SUPERVGUI_CanvasCellNodePrs* aNodePrs = (SUPERVGUI_CanvasCellNodePrs*) (*it);
180       QObject* anObj = aNodePrs->getObject(theP);
181       if (anObj->inherits("SUPERVGUI_CanvasCellNode")) {
182         
183         QRect aTitleRect = aNodePrs->getTitleRect();
184         if (aTitleRect.contains(theP, true)) {
185           tip(aTitleRect, ((SUPERVGUI_CanvasCellNode*)anObj)->getEngine()->Name());
186           return;
187         }
188
189         QRect aLabelRect = aNodePrs->getLabelRect();
190         if (aLabelRect.contains(theP, true)) {
191           tip(aLabelRect, ((SUPERVGUI_CanvasCellNode*)anObj)->getLabelText());
192           return;
193         }
194       }
195     }
196   }
197 }