Salome HOME
A new dialog class for edition of Ports of an InLine nodes is introduced. This funct...
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_View.cxx
1 //  SUPERV SUPERVGUI : GUI for Supervisor component
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : SUPERVGUI_View.cxx
8 //  Author : Vitaly SMETANNIKOV
9 //  Module : SUPERV
10
11 using namespace std;
12 #include "SUPERVGUI_View.h"
13 #include "SUPERVGUI_Main.h"
14 #include "QAD_Config.h"
15 #include <qcolordialog.h>
16
17
18 #if QT_VERSION >= 0x030005
19 QCursor panCursor(Qt::SizeAllCursor);
20 #else
21 QCursor panCursor(SizeAllCursor);
22 #endif
23
24 SUPERVGUI_View::SUPERVGUI_View(SUPERVGUI_Main* theMain):
25   QScrollView(theMain, 0, Qt::WRepaintNoErase),
26   main(theMain)
27 {
28   myIsPanActivated = false;
29   myAddStudyItem = 0;
30   myDx = 0;
31   myDx = 0;
32   myLastX = 0;
33   myLastY = 0;
34   myIsDragging = false;
35   myCursor = cursor();
36   myDSItemID = -1;
37
38 //  myLastMinVisX = horizontalScrollBar()->value(); //viewport()->x();
39 //  myLastMinVisY = verticalScrollBar()->value();   //viewport()->y();
40
41   myPopup = new QPopupMenu(viewport());
42
43   if (main->isEditable()) {
44     myPopup->insertItem(tr("MSG_ADD_NODE"), main, SLOT(addNode()));
45     myPopup->insertItem(tr("MSG_INS_FILE"), main, SLOT(insertFile()));
46     myPopup->insertSeparator();
47   }
48
49   myViewPopup = new QPopupMenu(viewport());
50   //  myViewPopup->insertItem(tr("POP_FULLVIEW"), main, SLOT(showFullGraph()));
51   myViewPopup->insertItem(tr("POP_FULLVIEW"), main, SLOT(showCanvas()));
52   myViewPopup->insertItem(tr("POP_CONTROLVIEW"), main, SLOT(showContolFlow()));
53   myViewPopup->insertItem(tr("POP_TABLEVIEW"), main, SLOT(showTable()));
54   //  myViewPopup->insertItem("Previous Full View", main, SLOT(showFullGraph()));
55   //  myViewPopup->insertItem(tr("POP_CANVASVIEW"), main, SLOT(showCanvas()));
56
57   myPopup->insertItem(tr("POP_VIEW"), myViewPopup);
58   myPopup->insertSeparator();
59
60   myAddStudyItem = myPopup->insertItem(tr("MSG_ADD_STUDY"), this, SLOT(addToStudy()));
61   myPopup->insertItem(tr("MSG_CHANGE_INFO"), main, SLOT(changeInformation()));
62   myPopup->insertSeparator();
63
64   myPopup->insertItem(tr("MSG_COPY_DATAFLOW"), main, SLOT(copy()));
65   myPopup->insertItem(tr("MSG_FILTER_NOTIFY"), main, SLOT(filterNotification()));
66
67   myPopup->insertSeparator();
68   myPopup->insertItem(tr("MSG_CHANGE_BACKGROUND"), this, SLOT(changeBackground()));
69
70   QString aRed   = QAD_CONFIG->getSetting("SUPERVGraph:BackgroundColorRed");
71   QString aGreen = QAD_CONFIG->getSetting("SUPERVGraph:BackgroundColorGreen");
72   QString aBlue  = QAD_CONFIG->getSetting("SUPERVGraph:BackgroundColorBlue");
73   
74   if( (!aRed.isEmpty()) && (!aGreen.isEmpty()) && (!aBlue.isEmpty()) ) {
75     QColor aColor(aRed.toInt(), aGreen.toInt(), aBlue.toInt());
76     setPaletteBackgroundColor(QColor(aRed.toInt(), aGreen.toInt(), aBlue.toInt()));
77   } else
78     setPaletteBackgroundColor(MAIN_COLOR);
79 }
80  
81
82 SUPERVGUI_View::~SUPERVGUI_View()
83 {
84 }
85
86 bool SUPERVGUI_View::isHavingStreamPort()
87 {
88   SUPERV::ListOfNodes* aNodesList = main->getDataflow()->Nodes();
89
90   //Computing Nodes
91   for ( int i = 0 ; i < (int) aNodesList->CNodes.length() ; i++ ) {
92     SUPERV::ListOfStreamPorts aStrPortsList = *( (aNodesList->CNodes)[i]->StreamPorts() );
93     if ((int) aStrPortsList.length() > 0) {
94       return true;
95     }
96   }
97    
98   //FactoryNodes
99   for ( int i = 0 ; i < (int) aNodesList->FNodes.length() ; i++ ) {
100     SUPERV::ListOfStreamPorts aStrPortsList = *( (aNodesList->FNodes)[i]->StreamPorts() );
101     if ((int) aStrPortsList.length() > 0) {
102       return true;
103     }
104   }
105         
106   //InLineNodes
107   for ( int i = 0 ; i < (int) aNodesList->INodes.length() ; i++ ) {
108     SUPERV::ListOfStreamPorts aStrPortsList = *( (aNodesList->INodes)[i]->StreamPorts() );
109     if ((int) aStrPortsList.length() > 0) {
110       return true;
111     }
112   }
113         
114   //GOTONodes
115   for ( int i = 0 ; i < (int) aNodesList->GNodes.length() ; i++ ) {
116     SUPERV::ListOfStreamPorts aStrPortsList = *( (aNodesList->GNodes)[i]->StreamPorts() );
117     if ((int) aStrPortsList.length() > 0) {
118       return true;
119     }
120   }
121         
122   //LoopNodes
123   for ( int i = 0 ; i < (int) aNodesList->LNodes.length() ; i++ ) {
124     SUPERV::ListOfStreamPorts aStrPortsList = *( (aNodesList->LNodes)[i]->StreamPorts() );
125     if ((int) aStrPortsList.length() > 0) {
126      return true;
127     }
128   }
129         
130   //SwitchNodes
131   for ( int i = 0 ; i < (int) aNodesList->SNodes.length() ; i++ ) {
132     SUPERV::ListOfStreamPorts aStrPortsList = *( (aNodesList->SNodes)[i]->StreamPorts() );
133     if ((int) aStrPortsList.length() > 0) {
134       return true;
135     }
136   }
137         
138   return false;
139 }
140
141 void SUPERVGUI_View::addDSPopupItem()
142 {
143   if (myDSItemID == -1) {
144     //Popupmenu items for user specification of parameters, if there are any DataStream ports in the graph
145     if (isHavingStreamPort()) {
146       myPopup->insertSeparator();
147       myDSItemID = myPopup->insertItem(tr("MSG_SET_GRAPHPARAMS"), main, SLOT(changeDSGraphParameters()));
148     }
149   }
150 }
151   
152 void SUPERVGUI_View::viewportMousePressEvent(QMouseEvent* theEvent) 
153 {
154   myIsDragging = true;
155   myLastX = theEvent->globalX();
156   myLastY = theEvent->globalY();
157
158   if (((theEvent->button() == Qt::MidButton)&&(theEvent->state() == Qt::ControlButton)) || myIsPanActivated) {
159     myCursor = cursor();
160     setCursor(panCursor);
161     return;
162   } 
163   QScrollView::viewportMousePressEvent(theEvent);
164 }
165
166 void SUPERVGUI_View::viewportMouseMoveEvent(QMouseEvent* theEvent) 
167 {
168   if (myIsDragging && ((theEvent->state() == (Qt::ControlButton|Qt::MidButton)) || myIsPanActivated)) {
169     myDx = theEvent->globalX() - myLastX;
170     myDy = theEvent->globalY() - myLastY;
171     
172     myLastX = theEvent->globalX();
173     myLastY = theEvent->globalY();
174
175     scrollBy(-myDx, -myDy);
176     return;
177   }
178   QScrollView::viewportMouseMoveEvent(theEvent);
179 }
180
181
182
183 void SUPERVGUI_View::viewportMouseReleaseEvent(QMouseEvent* theEvent) 
184 {
185   myDx = 0;
186   myDx = 0;
187   myIsDragging = false;
188   myIsPanActivated = false;
189   setCursor(myCursor);
190   QScrollView::viewportMouseReleaseEvent(theEvent);
191 }
192
193
194 void SUPERVGUI_View::ActivatePanning()
195 {
196   myIsPanActivated = true;
197 }
198
199
200 void SUPERVGUI_View::ResetView()
201 {
202   setContentsPos(0,0);
203 }
204
205
206 void SUPERVGUI_View::setAsFromStudy(bool theToStudy) {
207   if (myAddStudyItem != 0) {
208     myPopup->setItemEnabled(myAddStudyItem, !theToStudy);
209   }
210 }
211
212 void SUPERVGUI_View::addToStudy() {
213   if (main->addStudy()) main->setAsFromStudy(true);
214 }
215
216 void SUPERVGUI_View::setPaletteBackgroundColor(const QColor& color) { 
217   viewport()->setPaletteBackgroundColor(color);
218   QScrollView::setPaletteBackgroundColor(color.light()); 
219   repaintContents();
220 }
221
222 void SUPERVGUI_View::ResizeGraph ( QWidget * theChild, int theX, int theY ) {
223
224   int aGraphWidth = contentsWidth();
225   int aGraphHeight = contentsHeight();
226   if (theX > (contentsWidth() - theChild->width())) {
227     myLastX = theX;
228     myDx = theChild->width();
229     aGraphWidth = theX + theChild->width();
230   }
231   if (theY > (contentsHeight() - theChild->height())) {
232     myLastY = theY;
233     myDy = theChild->height();
234     aGraphHeight = theY + theChild->height();
235   }
236
237   setMaximumWidth(aGraphWidth);
238   setMaximumHeight(aGraphHeight);  
239
240   resizeContents(aGraphWidth, aGraphHeight);
241 }
242
243 int SUPERVGUI_View::getLastX() {
244   return myLastX;
245 }
246
247 int SUPERVGUI_View::getLastY() {
248   return myLastY;
249 }
250 void SUPERVGUI_View::changeBackground()
251 {
252   QColor selColor = QColorDialog::getColor(viewport()->paletteBackgroundColor(), this );
253   if ( selColor.isValid() ) {
254     setPaletteBackgroundColor( selColor );
255   }
256 }