]> SALOME platform Git repositories - modules/visu.git/blob - src/VISUGUI/VisuGUI_StreamLinesDlg.cxx
Salome HOME
503347116b09dee25e0ecfc45fbd7d0c9021f9ed
[modules/visu.git] / src / VISUGUI / VisuGUI_StreamLinesDlg.cxx
1 //  VISU VISUGUI : GUI of VISU component
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : VisuGUI_StreamLinesDlg.cxx
8 //  Author : Vitaly SMETANNIKOV
9 //  Module : VISU
10
11 #include "VisuGUI_StreamLinesDlg.h"
12 #include "VISU_StreamLines_i.hh"
13 #include "VISU_Mesh_i.hh"
14 #include "VISU_Gen_i.hh"
15 #include "VISU_StreamLinesPL.hxx"
16 #include "VISU_Actor.h"
17
18 #include "QAD_Application.h"
19 #include "QAD_Desktop.h"
20 #include "QAD_Config.h"
21 #include "QAD_MessageBox.h"
22 #include "VisuGUI.h"
23 #include "SALOME_Selection.h"
24 #include "VTKViewer_ViewFrame.h"
25
26 #include <limits.h>
27 #include <qlayout.h>
28 #include <qgroupbox.h>
29 #include <qcolordialog.h>
30
31 using namespace std;
32
33 extern VisuGUI *visuGUI;
34
35 VisuGUI_StreamLinesDlg::VisuGUI_StreamLinesDlg()
36     : QDialog( QAD_Application::getDesktop(), "VisuGUI_StreamLinesDlg", false, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
37 {
38   setCaption("Stream lines Preferences");
39   setSizeGripEnabled( TRUE );
40
41   QGridLayout* TopLayout = new QGridLayout( this ); 
42   TopLayout->setSpacing( 6 );
43   TopLayout->setMargin( 11 );
44
45   // Source box
46   QGroupBox* aSourceBox = new QGroupBox(tr("Source"), this);
47   aSourceBox->setColumnLayout(0, Qt::Vertical);
48   QGridLayout* aSrcLayout = new QGridLayout( aSourceBox->layout() ); 
49   aSrcLayout->setSpacing( 6 );
50   aSrcLayout->setMargin( 0 );
51   
52   QLabel* aTypeLbl = new QLabel( tr( "LBL_SOURCE_TYPE" ), aSourceBox);
53   aSrcLayout->addWidget(aTypeLbl, 0, 0);
54
55   myUseSrcCombo = new QComboBox(aSourceBox);
56   const char* aChoise[] = { "None", "Entity", "Family", "Group", "Presentation", 0 };
57   myUseSrcCombo->insertStrList(aChoise);
58   aSrcLayout->addWidget(myUseSrcCombo, 0, 1);
59
60   mySrcCombo = new QComboBox(aSourceBox);
61   mySrcCombo->setEnabled((myUseSrcCombo->currentItem() == 0));
62   
63   connect(myUseSrcCombo, SIGNAL(activated(int)), this, SLOT(onSourceTypeChange(int)));
64
65   // Find All prs obj
66   QAD_Study* aActiveStudy =  VisuGUI::GetActiveStudy();
67   SALOME_Selection* aSel = SALOME_Selection::Selection( aActiveStudy->getSelection() );
68   if( aSel->IObjectCount() > 0 ) {
69     Handle(SALOME_InteractiveObject) aIO = aSel->firstIObject();
70     if (aIO->hasEntry()){
71       SALOMEDS::SObject_var aSObject = VisuGUI::GetStudyDocument()->FindObjectID(aIO->getEntry());
72       aSObject = aSObject->GetFather();
73       aSObject = aSObject->GetFather();
74       aSObject = aSObject->GetFather();
75       aSObject = aSObject->GetFather();
76       mySelectionObj = aSObject;
77       CORBA::Object_var anObject = VISU::SObjectToObject(mySelectionObj);
78       VISU::Result_var aResult;
79       if (CORBA::is_nil(anObject)) {
80         mySelectionObj = mySelectionObj->GetFather();
81       }
82       SALOMEDS::ChildIterator_var aIter = VisuGUI::GetStudyDocument()->NewChildIterator( mySelectionObj );
83
84       for (aIter->InitEx(true); aIter->More(); aIter->Next() ) {
85         SALOMEDS::SObject_var aChildSObj = aIter->Value();
86         CORBA::Object_var aChildObject = VISU::SObjectToObject( aChildSObj );
87
88         if(!CORBA::is_nil( aChildObject ) ) {
89           VISU::Prs3d_var aPrsObj = VISU::Prs3d::_narrow( aChildObject );
90           if( !CORBA::is_nil( aPrsObj ) ) { // if this is a Prs object
91             if ((aPrsObj->GetType() != VISU::TSTREAMLINES) && 
92                 (aPrsObj->GetType() != VISU::TMESH)) {
93               SALOMEDS::GenericAttribute_var anAttr;
94               if ( aChildSObj->FindAttribute(anAttr, "AttributeName") ) {           
95                 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
96                 myPrsLst += QString(aName->Value());
97                 myPrsList.append(aPrsObj);
98                 continue;
99               }
100             }
101           }
102         }
103         // else { // This is not a Prs object
104         SALOMEDS::GenericAttribute_var anAttr;
105         if (aChildSObj->FindAttribute(anAttr, "AttributeComment")) {
106           SALOMEDS::AttributeComment_var aComment = SALOMEDS::AttributeComment::_narrow(anAttr);
107           string aComm = aComment->Value();
108           QString strIn(aComm.c_str());
109           VISU::Storable::TRestoringMap aMap;
110           VISU::Storable::StrToMap(strIn,aMap);
111           bool isExist;
112           VISU::VISUType aType = (VISU::VISUType)VISU::Storable::FindValue(aMap,"myType",&isExist).toInt();
113           if (isExist) {
114             if ((aType == VISU::TFAMILY) || (aType == VISU::TGROUP) || (aType == VISU::TENTITY)) { 
115               SALOMEDS::GenericAttribute_var aNameAttr;
116               if ( aChildSObj->FindAttribute(aNameAttr, "AttributeName") ) {        
117                 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(aNameAttr);
118                 VISU::Prs3d_var aPrsObj = VISU::Prs3d::_narrow( aChildObject );
119                 switch(aType) {
120                 case VISU::TFAMILY:
121                   myFamilisLst += QString(aName->Value());
122                   myFamilyList.append(aPrsObj);
123                   break;
124                 case VISU::TGROUP:
125                   myGroupsLst += QString(aName->Value());
126                   myGroupList.append(aPrsObj);
127                   break;
128                 case VISU::TENTITY:
129                   myEntitiesLst += QString(aName->Value());
130                   myEntityList.append(aPrsObj);
131                   break;
132                 }
133               }
134             }
135           }
136         }
137       }
138     }
139   }  
140   //  connect(myUseSrcChk, SIGNAL(toggled(bool)), mySrcCombo, SLOT(setEnabled(bool)));
141   aSrcLayout->addMultiCellWidget(mySrcCombo, 1, 1, 0, 1);
142
143   QLabel* aPercentLbl = new QLabel( tr( "Used points (0..1)" ), aSourceBox);
144   aSrcLayout->addWidget(aPercentLbl, 2, 0);
145
146   myPntPercent = new QAD_SpinBoxDbl( aSourceBox, 0, 1, 0.1);
147   aSrcLayout->addWidget(myPntPercent, 2, 1);
148
149   TopLayout->addMultiCellWidget(aSourceBox, 0, 0, 0, 1);
150
151
152   QLabel* aStepLenLbl = new QLabel( tr( "Step Length" ), this);
153   TopLayout->addWidget(aStepLenLbl, 1, 0);
154   myStepLen = new QAD_SpinBoxDbl( this, DBL_MIN, DBL_MAX, 0.1);
155   myStepLen->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
156   TopLayout->addWidget(myStepLen, 1, 1);
157   connect(myStepLen, SIGNAL(valueChanged(double)), this, SLOT(StepLengthChanged(double)));
158
159   QLabel* aIntegStepLenLbl = new QLabel( tr( "Integration Step" ), this);
160   TopLayout->addWidget(aIntegStepLenLbl, 2, 0);
161   myIntegStepLen = new QAD_SpinBoxDbl( this, DBL_MIN, DBL_MAX, 0.1);
162   myIntegStepLen->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
163   TopLayout->addWidget(myIntegStepLen, 2, 1);
164   connect(myIntegStepLen, SIGNAL(valueChanged(double)), this, SLOT(IntegrationStepChanged(double)));
165   
166   QLabel* aPropagationLbl = new QLabel( tr( "Propagation Time" ), this);
167   TopLayout->addWidget(aPropagationLbl, 3, 0);
168   myPropTime = new QAD_SpinBoxDbl( this, DBL_MIN, DBL_MAX, 1);
169   myPropTime->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
170   TopLayout->addWidget(myPropTime, 3, 1);
171   connect(myPropTime, SIGNAL(valueChanged(double)), this, SLOT(PropagationTimeChanged(double)));
172   
173   QLabel* aDirLbl = new QLabel( tr( "Direction" ), this);
174   TopLayout->addWidget(aDirLbl, 4, 0);
175   myDirCombo = new QComboBox(this);
176   myDirCombo->insertItem("Forward");
177   myDirCombo->insertItem("Backward");
178   myDirCombo->insertItem("Both");
179   TopLayout->addWidget(myDirCombo, 4, 1);
180
181   myUseScalar = new QCheckBox( tr( "Magnitude coloring" ), this, "UseMagn" );
182   connect( myUseScalar, SIGNAL( clicked() ), this, SLOT( enableSetColor() ) );
183   TopLayout->addWidget(myUseScalar, 5, 0);
184
185   SelColor = new QPushButton( tr( "Use Color" ), this, "SelColor" );
186   connect( SelColor,     SIGNAL( clicked() ), this, SLOT( setVColor() ) );
187   TopLayout->addWidget( SelColor, 5, 1);
188  
189   QGroupBox* aGroupButtons = new QGroupBox( this, "GroupButtons" );
190   aGroupButtons->setColumnLayout(0, Qt::Vertical );
191   aGroupButtons->layout()->setSpacing( 0 );
192   aGroupButtons->layout()->setMargin( 0 );
193   
194   QGridLayout* aGroupButtonsLayout = new QGridLayout( aGroupButtons->layout() );
195   aGroupButtonsLayout->setAlignment( Qt::AlignTop );
196   aGroupButtonsLayout->setSpacing( 6 );
197   aGroupButtonsLayout->setMargin( 11 );
198
199   QPushButton* aOkBtn = new QPushButton( tr( "&OK" ), aGroupButtons, "buttonOk" );
200   aOkBtn->setAutoDefault( TRUE );
201   aOkBtn->setDefault( TRUE );
202   aGroupButtonsLayout->addWidget( aOkBtn, 0, 0 );
203   aGroupButtonsLayout->addItem( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
204
205   QPushButton* aCancelBtn = new QPushButton( tr( "&Cancel" ) , aGroupButtons, "buttonCancel" );
206   aCancelBtn->setAutoDefault( TRUE );
207   aGroupButtonsLayout->addWidget( aCancelBtn, 0, 2 );
208   connect( aOkBtn,     SIGNAL( clicked() ), this, SLOT( accept() ) );
209   connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
210   TopLayout->addMultiCellWidget( aGroupButtons, 6, 6, 0, 1 );
211   enableSetColor();
212 }
213
214
215
216 void VisuGUI_StreamLinesDlg::initFromPrsObject(VISU::StreamLines_i* thePrs) {
217   myPropTime->setValue(thePrs->GetPropagationTime());
218
219   myStepLen->setRange(VISU_StreamLinesPL::GetMinStepLength(thePrs->GetIntegrationStep()),
220                       VISU_StreamLinesPL::GetMaxStepLength(thePrs->GetPropagationTime()));
221   myStepLen->setValue(thePrs->GetStepLength());
222   
223   myIntegStepLen->setRange(thePrs->GetMinIntegrationStep(),thePrs->GetMaxIntegrationStep());
224   myIntegStepLen->setValue(thePrs->GetIntegrationStep());
225   
226   switch (thePrs->GetDirection()) {
227   case VISU::StreamLines::FORWARD:
228     myDirCombo->setCurrentItem(0);
229     break;
230   case VISU::StreamLines::BACKWARD:
231     myDirCombo->setCurrentItem(1);
232     break;
233   case VISU::StreamLines::BOTH:
234     myDirCombo->setCurrentItem(2);
235   }
236   myUseScalar->setChecked(thePrs->IsColored());
237   myPntPercent->setValue(thePrs->GetUsedPoints());
238   
239   mySrcCombo->setEnabled(false);
240   QString aSrcEntry = thePrs->GetSourceEntry();
241   
242   SALOMEDS::Color anOldColor = thePrs->GetColor();
243   QColor aColor = QColor(int(255*anOldColor.R),int(255*anOldColor.G),int(255*anOldColor.B));
244   setColor(aColor);
245   enableSetColor();
246   
247 #define INITPRS(PRSLIST, PRSNUM) \
248     for (int i = 0; i < PRSLIST.count(); i++) { \
249       VISU::Prs3d_i* aPrs = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(PRSLIST[i])); \
250       if (aPrs == NULL) continue; \
251       if (aSrcEntry == aPrs->GetEntry()) { \
252         onSourceTypeChange(PRSNUM); \
253         myUseSrcCombo->setCurrentItem(PRSNUM); \
254         mySrcCombo->setEnabled(true); \
255         mySrcCombo->setCurrentItem(i); \
256         return; \
257       } \
258     }
259   
260   if (!aSrcEntry.isEmpty()) {
261     INITPRS(myEntityList, 1);
262     INITPRS(myFamilyList, 2);
263     INITPRS(myGroupList, 3);    
264     INITPRS(myPrsList, 4);
265   }
266   
267 #undef INITPRS
268
269 }
270
271
272
273 void VisuGUI_StreamLinesDlg::storeToPrsObject(VISU::StreamLines_i* thePrs) {
274   //Order of setting of the values are important 
275   thePrs->SetIntegrationStep(myIntegStepLen->value());
276   thePrs->SetPropagationTime(myPropTime->value());
277   thePrs->SetStepLength(myStepLen->value());
278   switch (myDirCombo->currentItem()) {
279   case 0:
280     thePrs->SetDirection(VISU::StreamLines::FORWARD);
281     break;
282   case 1:
283     thePrs->SetDirection(VISU::StreamLines::BACKWARD);
284     break;
285   case 2:
286     thePrs->SetDirection(VISU::StreamLines::BOTH);
287   }
288   thePrs->ShowColored(myUseScalar->isChecked());
289   if(!thePrs->IsColored()){
290     SALOMEDS::Color aColor;
291     aColor.R = myColor.red()/255.;
292     aColor.G = myColor.green()/255.;
293     aColor.B = myColor.blue()/255.;
294     thePrs->SetColor(aColor);
295   }
296   thePrs->SetUsedPoints(myPntPercent->value());
297
298   int aSrcSelection = myUseSrcCombo->currentItem();
299   int aSrcItem = (mySrcCombo->count() > 0)? mySrcCombo->currentItem() : -1;
300   if ((aSrcSelection > 0) && (aSrcItem > -1)) {
301     VISU::Prs3d_var aPrs;
302     VISU::VISUType aType;
303     QString aName;
304     switch (aSrcSelection){
305     case 1: // Entity
306       aPrs = myEntityList[aSrcItem];
307       aType = VISU::TENTITY;
308       aName = myEntitiesLst[aSrcItem];
309       break;
310     case 2: // Family
311       aPrs = myFamilyList[aSrcItem];
312       aType = VISU::TFAMILY;
313       aName = myFamilisLst[aSrcItem];
314       break;
315     case 3: // Group
316       aPrs = myGroupList[aSrcItem];
317       aType = VISU::TGROUP;
318       aName = myGroupsLst[aSrcItem];
319       break;
320     case 4: // Presentation
321       aPrs = myPrsList[aSrcItem];
322       break;
323     }
324     if (!CORBA::is_nil( aPrs )) {
325       thePrs->SetSource(aPrs);
326     } else if (aSrcSelection == 4) {
327       thePrs->SetSource(VISU::Prs3d::_nil());
328     } else { // Create Mesh presentation
329       thePrs->SetSource(createMesh(aType, aName));
330     }
331   } else 
332     thePrs->SetSource(VISU::Prs3d::_nil());  
333 }
334
335 /*!
336   Sets color
337 */
338 void VisuGUI_StreamLinesDlg::setColor( QColor color )
339 {
340   myColor = color;
341   SelColor->setPaletteBackgroundColor(myColor);
342 }
343
344
345 /*!
346   Called when "Select Color" buttonx clicked
347 */
348 void VisuGUI_StreamLinesDlg::setVColor()
349 {
350   QColor cnew = QColorDialog::getColor( myColor, this );
351   if ( cnew.isValid() )
352     setColor( cnew );
353 }
354
355
356 /*!
357   Enbled/disables magnitude coloring
358 */
359 void VisuGUI_StreamLinesDlg::enableMagnColor( bool enable )
360 {
361   myUseScalar->setEnabled( enable );
362   enableSetColor();
363 }
364
365 /*!
366   Called when "Magnitude Coloring" check box clicked
367 */
368 void VisuGUI_StreamLinesDlg::enableSetColor()
369 {
370   SelColor->setEnabled(!myUseScalar->isChecked() );
371 }
372
373 void VisuGUI_StreamLinesDlg::StepLengthChanged(double theValue){
374 }
375
376 void VisuGUI_StreamLinesDlg::IntegrationStepChanged(double theValue) {
377   PropagationTimeChanged(myPropTime->value());
378 }
379
380 void VisuGUI_StreamLinesDlg::PropagationTimeChanged(double theValue) {
381   myStepLen->setMinValue(VISU_StreamLinesPL::GetMinStepLength(myIntegStepLen->value()));
382   myStepLen->setMaxValue(VISU_StreamLinesPL::GetMaxStepLength(myPropTime->value()));
383 }
384
385 void VisuGUI_StreamLinesDlg::onSourceTypeChange(int theIndex) {
386   mySrcCombo->clear();
387   if (theIndex == 0) {
388     mySrcCombo->setEnabled(false);
389     return;
390   } else
391     mySrcCombo->setEnabled(true);
392
393   switch(theIndex) {
394   case 1: // Entity
395     mySrcCombo->insertStringList(myEntitiesLst);
396     return;
397   case 2: // Family
398     mySrcCombo->insertStringList(myFamilisLst);
399     return;
400   case 3: // Group
401     mySrcCombo->insertStringList(myGroupsLst);
402     return;
403   case 4: // Presentation
404     mySrcCombo->insertStringList(myPrsLst);
405     return;
406   }
407 }
408
409
410 VISU::Mesh_var VisuGUI_StreamLinesDlg::createMesh(VISU::VISUType theType, QString theName) {
411   CORBA::Object_var anObject = VISU::SObjectToObject(mySelectionObj);
412   VISU::Result_var aResult;
413   if (!CORBA::is_nil(anObject)) {
414     aResult = VISU::Result::_narrow(anObject);
415   }
416   if(CORBA::is_nil(aResult)) {
417     QAD_MessageBox::warn1(QAD_Application::getDesktop(), tr ("VISU_WARNING"),
418                           tr ("WRN_NO_AVAILABLE_DATA"),
419                           tr ("VISU_BUT_OK"));
420     return VISU::Mesh::_nil();
421   }
422   SALOMEDS::ChildIterator_var aIter = VisuGUI::GetStudyDocument()->NewChildIterator( mySelectionObj );
423   VISU::Storable::TRestoringMap aMap;
424
425   for (aIter->InitEx(true); aIter->More(); aIter->Next() ) {
426     SALOMEDS::SObject_var aChildSObj = aIter->Value();
427     SALOMEDS::GenericAttribute_var anAttr;
428     if (aChildSObj->FindAttribute(anAttr, "AttributeComment")) {
429       SALOMEDS::AttributeComment_var aComment = SALOMEDS::AttributeComment::_narrow(anAttr);
430       CORBA::String_var aComm = aComment->Value();
431       QString strIn(aComm.in());
432       aMap.clear();
433       VISU::Storable::StrToMap(strIn,aMap);
434       bool isExist;
435       VISU::VISUType aType = (VISU::VISUType)VISU::Storable::FindValue(aMap,"myType",&isExist).toInt();
436       if (isExist) {
437         if (aType != theType) continue;
438
439         SALOMEDS::GenericAttribute_var aNameAttr;
440         if ( aChildSObj->FindAttribute(aNameAttr, "AttributeName") ) {      
441           SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(aNameAttr);
442           if (QString(aName->Value()) == theName) break; //use current map
443         }
444       }
445     }
446   }
447   CORBA::Object_var aMesh;
448   QString aMeshName = VISU::Storable::FindValue(aMap,"myMeshName");
449   switch (theType) {
450   case VISU::TFAMILY: 
451     {
452       VISU::Entity anEntity = (VISU::Entity)VISU::Storable::FindValue(aMap,"myEntityId").toInt();
453       aMesh = VisuGUI::GetVisuGen()->FamilyMeshOnEntity(aResult,aMeshName.latin1(),anEntity,theName.latin1());
454     }
455     break;
456   case VISU::TGROUP:
457     {
458       aMesh = VisuGUI::GetVisuGen()->GroupMesh(aResult,aMeshName.latin1(), theName.latin1());
459     }
460     break;
461   case VISU::TENTITY:
462     {
463       VISU::Entity anEntity = (VISU::Entity)VISU::Storable::FindValue(aMap,"myId").toInt();
464       aMesh = VisuGUI::GetVisuGen()->MeshOnEntity(aResult,aMeshName.latin1(),anEntity);
465     }
466     break;
467   }
468   VISU::Mesh_i* pPresent = NULL;
469   if(!CORBA::is_nil(aMesh)) 
470     pPresent = dynamic_cast<VISU::Mesh_i*>(VISU::GetServant(aMesh));
471   if (pPresent == NULL) {
472     QAD_MessageBox::warn1 ( QAD_Application::getDesktop(), tr ("VISU_WARNING"), 
473                             tr ("ERR_CANT_BUILD_PRESENTATION"), 
474                             tr ("VISU_BUT_OK") ); 
475     return VISU::Mesh::_nil();
476   }
477   // Create Actor
478   if(VTKViewer_ViewFrame* vf = visuGUI->GetVtkViewFrame()){
479     try{
480       pPresent->SetPresentationType(VISU::POINT);
481       VISU_Actor *anActor = pPresent->CreateActor();
482       vf->AddActor(anActor);
483     }catch(std::runtime_error& exc){
484       QAD_MessageBox::warn1 ( QAD_Application::getDesktop(), tr ("VISU_WARNING"), 
485                               tr ("ERR_CANT_CREATE_ACTOR"), 
486                               tr ("VISU_BUT_OK") ); 
487     }
488   } 
489   return pPresent->_this();
490 }