Salome HOME
Stream Graph parameters dialog box
[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_CONTROLVIEW"), main, SLOT(showContolFlow()));
52   myViewPopup->insertItem(tr("POP_TABLEVIEW"), main, SLOT(showTable()));
53
54   myPopup->insertItem(tr("POP_VIEW"), myViewPopup);
55   myPopup->insertSeparator();
56
57   myAddStudyItem = myPopup->insertItem(tr("MSG_ADD_STUDY"), this, SLOT(addToStudy()));
58   myPopup->insertItem(tr("MSG_CHANGE_INFO"), main, SLOT(changeInformation()));
59   myPopup->insertSeparator();
60
61   myPopup->insertItem(tr("MSG_COPY_DATAFLOW"), main, SLOT(copy()));
62   myPopup->insertItem(tr("MSG_FILTER_NOTIFY"), main, SLOT(filterNotification()));
63
64   myPopup->insertSeparator();
65   myPopup->insertItem(tr("MSG_CHANGE_BACKGROUND"), this, SLOT(changeBackground()));
66
67   QString aRed   = QAD_CONFIG->getSetting("SUPERVGraph:BackgroundColorRed");
68   QString aGreen = QAD_CONFIG->getSetting("SUPERVGraph:BackgroundColorGreen");
69   QString aBlue  = QAD_CONFIG->getSetting("SUPERVGraph:BackgroundColorBlue");
70   
71   if( (!aRed.isEmpty()) && (!aGreen.isEmpty()) && (!aBlue.isEmpty()) ) {
72     QColor aColor(aRed.toInt(), aGreen.toInt(), aBlue.toInt());
73     setPaletteBackgroundColor(QColor(aRed.toInt(), aGreen.toInt(), aBlue.toInt()));
74   } else
75     setPaletteBackgroundColor(MAIN_COLOR);
76 }
77  
78
79 SUPERVGUI_View::~SUPERVGUI_View()
80 {
81 }
82
83 bool SUPERVGUI_View::isHavingStreamPort()
84 {
85   SUPERV::ListOfNodes* aNodesList = main->getDataflow()->Nodes();
86
87   //Computing Nodes
88   for ( int i = 0 ; i < (int) aNodesList->CNodes.length() ; i++ ) {
89     SUPERV::ListOfStreamPorts aStrPortsList = *( (aNodesList->CNodes)[i]->StreamPorts() );
90     if ((int) aStrPortsList.length() > 0) {
91       return true;
92     }
93   }
94    
95   //FactoryNodes
96   for ( int i = 0 ; i < (int) aNodesList->FNodes.length() ; i++ ) {
97     SUPERV::ListOfStreamPorts aStrPortsList = *( (aNodesList->FNodes)[i]->StreamPorts() );
98     if ((int) aStrPortsList.length() > 0) {
99       return true;
100     }
101   }
102         
103   //InLineNodes
104   for ( int i = 0 ; i < (int) aNodesList->INodes.length() ; i++ ) {
105     SUPERV::ListOfStreamPorts aStrPortsList = *( (aNodesList->INodes)[i]->StreamPorts() );
106     if ((int) aStrPortsList.length() > 0) {
107       return true;
108     }
109   }
110         
111   //GOTONodes
112   for ( int i = 0 ; i < (int) aNodesList->GNodes.length() ; i++ ) {
113     SUPERV::ListOfStreamPorts aStrPortsList = *( (aNodesList->GNodes)[i]->StreamPorts() );
114     if ((int) aStrPortsList.length() > 0) {
115       return true;
116     }
117   }
118         
119   //LoopNodes
120   for ( int i = 0 ; i < (int) aNodesList->LNodes.length() ; i++ ) {
121     SUPERV::ListOfStreamPorts aStrPortsList = *( (aNodesList->LNodes)[i]->StreamPorts() );
122     if ((int) aStrPortsList.length() > 0) {
123      return true;
124     }
125   }
126         
127   //SwitchNodes
128   for ( int i = 0 ; i < (int) aNodesList->SNodes.length() ; i++ ) {
129     SUPERV::ListOfStreamPorts aStrPortsList = *( (aNodesList->SNodes)[i]->StreamPorts() );
130     if ((int) aStrPortsList.length() > 0) {
131       return true;
132     }
133   }
134         
135   return false;
136 }
137
138 void SUPERVGUI_View::addDSPopupItem()
139 {
140   if (myDSItemID == -1) {
141     //Popupmenu items for user specification of parameters, if there are any DataStream ports in the graph
142     if (isHavingStreamPort()) {
143       myPopup->insertSeparator();
144       myDSItemID = myPopup->insertItem(tr("MSG_SET_GRAPHPARAMS"), main, SLOT(changeDSGraphParameters()));
145     }
146   }
147 }
148   
149 void SUPERVGUI_View::viewportMousePressEvent(QMouseEvent* theEvent) 
150 {
151   myIsDragging = true;
152   myLastX = theEvent->globalX();
153   myLastY = theEvent->globalY();
154
155   if (((theEvent->button() == Qt::MidButton)&&(theEvent->state() == Qt::ControlButton)) || myIsPanActivated) {
156     myCursor = cursor();
157     setCursor(panCursor);
158     return;
159   } 
160   QScrollView::viewportMousePressEvent(theEvent);
161 }
162
163 void SUPERVGUI_View::viewportMouseMoveEvent(QMouseEvent* theEvent) 
164 {
165   if (myIsDragging && ((theEvent->state() == (Qt::ControlButton|Qt::MidButton)) || myIsPanActivated)) {
166     myDx = theEvent->globalX() - myLastX;
167     myDy = theEvent->globalY() - myLastY;
168     
169     myLastX = theEvent->globalX();
170     myLastY = theEvent->globalY();
171
172     scrollBy(-myDx, -myDy);
173     return;
174   }
175   QScrollView::viewportMouseMoveEvent(theEvent);
176 }
177
178
179
180 void SUPERVGUI_View::viewportMouseReleaseEvent(QMouseEvent* theEvent) 
181 {
182   myDx = 0;
183   myDx = 0;
184   myIsDragging = false;
185   myIsPanActivated = false;
186   setCursor(myCursor);
187   QScrollView::viewportMouseReleaseEvent(theEvent);
188 }
189
190
191 void SUPERVGUI_View::ActivatePanning()
192 {
193   myIsPanActivated = true;
194 }
195
196
197 void SUPERVGUI_View::ResetView()
198 {
199   setContentsPos(0,0);
200 }
201
202
203 void SUPERVGUI_View::setAsFromStudy(bool theToStudy) {
204   if (myAddStudyItem != 0) {
205     myPopup->setItemEnabled(myAddStudyItem, !theToStudy);
206   }
207 }
208
209 void SUPERVGUI_View::addToStudy() {
210   if (main->addStudy()) main->setAsFromStudy(true);
211 }
212
213 void SUPERVGUI_View::setPaletteBackgroundColor(const QColor& color) { 
214   viewport()->setPaletteBackgroundColor(color);
215   QScrollView::setPaletteBackgroundColor(color.light()); 
216   repaintContents();
217 }
218
219 void SUPERVGUI_View::ResizeGraph ( QWidget * theChild, int theX, int theY ) {
220
221   int aGraphWidth = contentsWidth();
222   int aGraphHeight = contentsHeight();
223   if (theX > (contentsWidth() - theChild->width())) {
224     myLastX = theX;
225     myDx = theChild->width();
226     aGraphWidth = theX + theChild->width();
227   }
228   if (theY > (contentsHeight() - theChild->height())) {
229     myLastY = theY;
230     myDy = theChild->height();
231     aGraphHeight = theY + theChild->height();
232   }
233
234   setMaximumWidth(aGraphWidth);
235   setMaximumHeight(aGraphHeight);  
236
237   resizeContents(aGraphWidth, aGraphHeight);
238 }
239
240 int SUPERVGUI_View::getLastX() {
241   return myLastX;
242 }
243
244 int SUPERVGUI_View::getLastY() {
245   return myLastY;
246 }
247 void SUPERVGUI_View::changeBackground()
248 {
249   QColor selColor = QColorDialog::getColor(viewport()->paletteBackgroundColor(), this );
250   if ( selColor.isValid() ) {
251     setPaletteBackgroundColor( selColor );
252   }
253 }