Salome HOME
08477e5477f5cf04eed7ef11b2bf72a2c168c4d1
[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.h"
12 #include "SUPERVGUI_ArrayView.h"
13 #include "SUPERVGUI_Main.h"
14 #include "SUPERVGUI_CanvasCellNodePrs.h"
15
16 #include <qcolordialog.h>
17
18 #if QT_VERSION >= 0x030005
19 QCursor PanCursor(Qt::SizeAllCursor);
20 #else
21 QCursor PanCursor(SizeAllCursor);
22 #endif
23
24 SUPERVGUI_ArrayView::SUPERVGUI_ArrayView(SUPERVGUI_CanvasArray* theArray, SUPERVGUI_Main* theMain):
25   QCanvasView(theArray, theMain),
26   myMain(theMain)
27 {
28   setName("TableView");
29
30   myIsPanActivated = false;
31
32   myAddStudyItem = 0;
33   myCursor = cursor();
34
35   viewport()->setMouseTracking(true); //widget receives mouse move events 
36                                       //even if no buttons are pressed down
37
38   myPopup = new QPopupMenu(viewport());
39
40   if (myMain->isEditable()) {
41     myPopup->insertItem(tr("MSG_ADD_NODE"), myMain, SLOT(addNode()));
42     myPopup->insertItem(tr("MSG_INS_FILE"), myMain, SLOT(insertFile()));
43     myPopup->insertSeparator();
44   }
45
46   QPopupMenu* aViewPopup = new QPopupMenu(viewport());
47   aViewPopup->insertItem(tr("POP_FULLVIEW"), myMain, SLOT(showCanvas()));
48   aViewPopup->insertItem(tr("POP_CONTROLVIEW"), myMain, SLOT(showContolFlow()));
49   aViewPopup->insertItem(tr("POP_TABLEVIEW"), 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"), myMain, SLOT(addDataflowToStudy()));
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   SUPERVGraph_ViewFrame* anActiveVF = (SUPERVGraph_ViewFrame*)myMain->parent();
75   if ( anActiveVF ) {
76     myPopup->insertSeparator();
77     myShowToolBarItem = myPopup->insertItem( tr( "MEN_SHOW_TOOLBAR" ), myMain, SLOT( onShowToolbar() ) );
78   }
79
80   hide();
81 }
82  
83
84 SUPERVGUI_ArrayView::~SUPERVGUI_ArrayView()
85 {
86 }
87
88 void SUPERVGUI_ArrayView::ActivatePanning()
89 {
90   myIsPanActivated = true;
91   viewport()->setMouseTracking(false);
92 }
93
94 void SUPERVGUI_ArrayView::ResetView()
95 {
96   setContentsPos(0,0);
97   QWMatrix m;
98   setWorldMatrix(m);
99 }
100
101 void SUPERVGUI_ArrayView::changeBackground()
102 {
103   QColor aColor = QColorDialog::getColor(canvas()->backgroundColor(), this );
104   if ( aColor.isValid() ) {
105     // change background color for array view
106     canvas()->setBackgroundColor(aColor);
107     setPaletteBackgroundColor(aColor.light(120));
108     // change background color for canvas view
109     getMain()->getCanvasView()->canvas()->setBackgroundColor(aColor);
110     getMain()->getCanvasView()->setPaletteBackgroundColor(aColor.light(120));
111
112     // mkr : IPAL10825 -->
113     SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
114     if ( !aSupMod ) {
115       MESSAGE("NULL Supervision module!");
116       return;
117     }
118     aSupMod->setIVFBackgroundColor(aColor);
119     // <--
120   }
121 }
122
123 void SUPERVGUI_ArrayView::resizeEvent( QResizeEvent* e) {
124   QCanvasView::resizeEvent( e );
125   canvas()->resize( e->size().width() > canvas()->width() ? e->size().width() : canvas()->width(), 
126                     e->size().height() > canvas()->height() ? e->size().height() : canvas()->height() );
127 }
128
129 void SUPERVGUI_ArrayView::contentsMousePressEvent(QMouseEvent* theEvent) {
130   myGlobalPoint = theEvent->globalPos();
131
132   if (((theEvent->button() == Qt::MidButton)
133        &&
134        (theEvent->state() == Qt::ControlButton)) || myIsPanActivated) {
135     myIsPanActivated = true;
136     viewport()->setMouseTracking(false); //widget only receives mouse move events when at least one 
137                                          //mouse button is pressed down while the mouse is being moved
138     myCursor = cursor();
139     setCursor(PanCursor);
140     return;
141   } 
142
143   QPoint thePoint = inverseWorldMatrix().map(theEvent->pos());
144   // compute collision rectangle
145   QRect aSel(thePoint.x()-MARGIN, thePoint.y()-MARGIN, 1+2*MARGIN, 1+2*MARGIN);
146
147   if (theEvent->button() == RightButton) {
148     QCanvasItemList l = canvas()->collisions(aSel);
149     for (QCanvasItemList::Iterator it = l.begin(); it != l.end(); ++it) {
150       if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_Node) {
151         SUPERVGUI_CanvasCellNodePrs* aNodePrs = (SUPERVGUI_CanvasCellNodePrs*) (*it);
152         QObject* anObj = aNodePrs->getObject(thePoint);
153         if (anObj->inherits("SUPERVGUI_CanvasCellNode")) {
154           myMain->showPopup(((SUPERVGUI_CanvasCellNode*)anObj)->getPopupMenu(viewport()), 
155                             theEvent);
156           return;
157         }
158       }
159     }
160
161     myPopup->setItemEnabled(myAddStudyItem, !myMain->isDataflowInStudy());
162     myPopup->setItemEnabled(myShowToolBarItem, !((SUPERVGraph_ViewFrame*)myMain->parent())->getToolBar()->isVisible());
163     myMain->showPopup(myPopup, theEvent);
164     return;
165   }
166 }
167
168 void SUPERVGUI_ArrayView::contentsMouseMoveEvent(QMouseEvent* theEvent) {
169   QPoint g = theEvent->globalPos();
170   if (myIsPanActivated) {
171     scrollBy(myGlobalPoint.x() - g.x(),
172              myGlobalPoint.y() - g.y());
173     myGlobalPoint = g;
174     return;
175   }
176
177   // QToolTip for title and label for SUPERVGUI_CanvasCellNode
178   SUPERVGUI_ToolTip* aTT = new SUPERVGUI_ToolTip(this);
179   QPoint aPoint1 = inverseWorldMatrix().map(theEvent->pos());
180   aTT->maybeTip(aPoint1);
181 }
182
183 void SUPERVGUI_ArrayView::contentsMouseReleaseEvent(QMouseEvent* theEvent) {
184   if (myIsPanActivated) {
185     myIsPanActivated = false;
186     viewport()->setMouseTracking(true);
187     setCursor(myCursor);
188   }
189 }
190
191 void SUPERVGUI_ToolTip::maybeTip(const QPoint& theP) {
192   // compute collision rectangle
193   QRect aSel(theP.x()-MARGIN, theP.y()-MARGIN, 1+2*MARGIN, 1+2*MARGIN);
194
195   QCanvasItemList l = ((SUPERVGUI_ArrayView*)parentWidget())->canvas()->collisions(aSel);
196   for (QCanvasItemList::Iterator it = l.begin(); it != l.end(); ++it) {
197     if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_Node) {
198       SUPERVGUI_CanvasCellNodePrs* aNodePrs = (SUPERVGUI_CanvasCellNodePrs*) (*it);
199       QObject* anObj = aNodePrs->getObject(theP);
200       if (anObj->inherits("SUPERVGUI_CanvasCellNode")) {
201         
202         QRect aTitleRect = aNodePrs->getTitleRect();
203         if (aTitleRect.contains(theP, true)) {
204           tip(aTitleRect, ((SUPERVGUI_CanvasCellNode*)anObj)->getEngine()->Name());
205           return;
206         }
207
208         QRect aLabelRect = aNodePrs->getLabelRect();
209         if (aLabelRect.contains(theP, true)) {
210           tip(aLabelRect, ((SUPERVGUI_CanvasCellNode*)anObj)->getLabelText());
211           return;
212         }
213       }
214     }
215   }
216 }