Salome HOME
Merge 'abn/V8_1_fix' branch into V8_1_BR.
[modules/med.git] / src / MEDCalc / gui / ProcessingController.cxx
1 // Copyright (C) 2016  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ProcessingController.hxx"
21 #include <MEDCalcConstants.hxx>
22
23 #include <SALOMEDS_SObject.hxx>
24 #include <SALOMEDS_Study.hxx>
25 #include CORBA_CLIENT_HEADER(SALOMEDS)
26 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
27
28 #include "MEDFactoryClient.hxx"
29 #include "MEDModule.hxx"
30 #include "Basics_Utils.hxx"
31
32 #include <SUIT_Desktop.h>
33 #include <QMessageBox>
34
35 ProcessingController::ProcessingController(MEDModule* salomeModule)
36 {
37   STDLOG("Creating a ProcessingController");
38   _salomeModule = salomeModule;
39   _studyEditor = _salomeModule->getStudyEditor();
40
41   _dlgChangeUnderlyingMesh = new DlgChangeUnderlyingMesh(_studyEditor);
42   connect(_dlgChangeUnderlyingMesh,SIGNAL(inputValidated()),
43           this, SLOT(OnChangeUnderlyingMeshInputValidated()));
44
45   _dlgInterpolateField = new DlgInterpolateField(_studyEditor);
46   connect(_dlgInterpolateField,SIGNAL(inputValidated()),
47           this, SLOT(OnInterpolateFieldInputValidated()));
48 }
49
50 ProcessingController::~ProcessingController()
51 {
52   STDLOG("Deleting the ProcessingController");
53 }
54
55 void
56 ProcessingController::createActions()
57 {
58   STDLOG("Creating ProcessingController actions");
59
60   int processingToolbarId = _salomeModule->createTool("Processing", "ProcessingToolbar");
61   int processingMenuId = _salomeModule->createMenu(tr("MENU_PROCESSING"), -1, -1, 12);
62
63   // Change underlying mesh (note that this action creates a new field in
64   // the workspace that corresponds to a copy of the selected field
65   // modified by the change of the underlying mesh.
66   QString label   = tr("LAB_PROCESSING_CHANGE_MESH");
67   QString icon = tr("ICO_PROCESSING_CHANGE_MESH");
68   int actionId;
69   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnChangeUnderlyingMesh()),icon,label);
70   _salomeModule->createTool(actionId, processingToolbarId);
71   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
72   _salomeModule->createMenu(actionId, processingMenuId);
73
74   label   = tr("LAB_PROCESSING_INTERPOLATE_FIELD");
75   icon = tr("ICO_PROCESSING_INTERPOLATE_FIELD");
76   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnInterpolateField()),icon,label);
77   _salomeModule->createTool(actionId, processingToolbarId);
78   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
79   _salomeModule->createMenu(actionId, processingMenuId);
80 }
81
82 void
83 ProcessingController::OnChangeUnderlyingMesh()
84 {
85   // We need a studyEditor updated on the active study
86   _studyEditor->updateActiveStudy();
87
88   // Get the selected objects in the study (SObject). In cas of a
89   // multiple selection, we consider only the first item. At least one
90   // item must be selected.
91   SALOME_StudyEditor::SObjectList* listOfSObject = _studyEditor->getSelectedObjects();
92   if ( listOfSObject->size() > 0 ) {
93     SALOMEDS::SObject_var soField = listOfSObject->at(0);
94     std::string name(_studyEditor->getName(soField));
95     if (soField->_is_nil() || name == "MEDCalc")
96       return;
97     SALOMEDS::GenericAttribute_var anAttr;
98     SALOMEDS::AttributeParameter_var aParam;
99     if ( soField->FindAttribute(anAttr,"AttributeParameter") ) {
100       aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
101       if (! aParam->IsSet(FIELD_SERIES_ID, PT_INTEGER))
102         return;
103     }
104     int fieldId = aParam->GetInt(FIELD_SERIES_ID);
105     // _GBO_ : the dialog should not be modal, so that we can choose a
106     // mesh in the browser. Then we have to emit a signal from the
107     // dialog.accept, connected to a slot of the DatasourceControler
108     _dlgChangeUnderlyingMesh->setFieldId(fieldId);
109     Qt::WindowFlags flags = _dlgChangeUnderlyingMesh->windowFlags();
110     _dlgChangeUnderlyingMesh->setWindowFlags(flags | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
111     _dlgChangeUnderlyingMesh->open();
112   }
113 }
114
115 void
116 ProcessingController::OnChangeUnderlyingMeshInputValidated()
117 {
118   int meshId = _dlgChangeUnderlyingMesh->getMeshId();
119   STDLOG("meshId = " << ToString(meshId));
120   int fieldId = _dlgChangeUnderlyingMesh->getFieldId();
121   MEDCALC::FieldHandler* fieldHandler =
122     MEDFactoryClient::getDataManager()->getFieldHandler(fieldId);
123
124   // We don't modify the original field but create first a duplicate
125   MEDCALC::FieldHandler* duplicate = MEDFactoryClient::getCalculator()->dup(*fieldHandler);
126   MEDFactoryClient::getDataManager()->changeUnderlyingMesh(duplicate->id, meshId);
127
128   // Request once more the duplicate to update the meta-data on this
129   // client side
130   duplicate = MEDFactoryClient::getDataManager()->getFieldHandler(duplicate->id);
131
132   // >>>
133   // WARN: the following is a temporary code for test purpose
134   // Automatically add in ws
135   ProcessingEvent* event = new ProcessingEvent();
136   event->eventtype = ProcessingEvent::EVENT_IMPORT_OBJECT;
137   XmedDataObject* dataObject = new XmedDataObject();
138   dataObject->setFieldHandler(*duplicate);
139   event->objectdata = dataObject;
140   emit processingSignal(event);
141   // Note that this signal is processed by the WorkspaceController
142
143   // Tag the item to prevent double import
144   //_studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);
145 }
146
147 void
148 ProcessingController::OnInterpolateField()
149 {
150   // We need a studyEditor updated on the active study
151   _studyEditor->updateActiveStudy();
152
153   // Get the selected objects in the study (SObject). In case of a
154   // multiple selection, we consider only the first item. At least one
155   // item must be selected.
156   SALOME_StudyEditor::SObjectList* listOfSObject = _studyEditor->getSelectedObjects();
157   if ( listOfSObject->size() > 0 ) {
158     SALOMEDS::SObject_var soField = listOfSObject->at(0);
159     std::string name(_studyEditor->getName(soField));
160     if (soField->_is_nil() || name == "MEDCalc")
161       return;
162     SALOMEDS::GenericAttribute_var anAttr;
163     SALOMEDS::AttributeParameter_var aParam;
164     if ( soField->FindAttribute(anAttr,"AttributeParameter") ) {
165       aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
166       if (! aParam->IsSet(FIELD_SERIES_ID, PT_INTEGER))
167         return;
168     }
169     int fieldId = aParam->GetInt(FIELD_SERIES_ID);
170     // _GBO_ : the dialog should not be modal, so that we can choose a
171     // mesh in the browser. Then we have to emit a signal from the
172     // dialog.accept, connected to a slot of the DatasourceControler
173     _dlgInterpolateField->setFieldId(fieldId);
174     Qt::WindowFlags flags = _dlgInterpolateField->windowFlags();
175     _dlgInterpolateField->setWindowFlags(flags | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
176     _dlgInterpolateField->open();
177   }
178 }
179
180 void
181 ProcessingController::OnInterpolateFieldInputValidated()
182 {
183   MEDCALC::InterpolationParameters params;
184   params.precision = _dlgInterpolateField->getPrecision();
185   STDLOG("precision = " << params.precision);
186   params.defaultValue = _dlgInterpolateField->getDefaultValue();
187   STDLOG("defaultValue = " << params.defaultValue);
188   params.reverse = _dlgInterpolateField->getReverse();
189   STDLOG("reverse = " << params.reverse);
190   params.intersectionType = _dlgInterpolateField->getIntersectionType().c_str();
191   STDLOG("intersectionType = " << params.intersectionType);
192   params.method = _dlgInterpolateField->getMethod().c_str();
193   STDLOG("method = " << params.method);
194   params.nature = _dlgInterpolateField->getFieldNature().c_str();
195   STDLOG("nature = " << params.nature);
196
197   int meshId = _dlgInterpolateField->getMeshId();
198   STDLOG("meshId = " << ToString(meshId));
199   int fieldId = _dlgInterpolateField->getFieldId();
200   MEDCALC::FieldHandler* fieldHandler = MEDFactoryClient::getDataManager()->getFieldHandler(fieldId);
201
202   // We don't modify the original field but create first a duplicate
203   // MEDCALC::FieldHandler* duplicate = MEDFactoryClient::getCalculator()->dup(*fieldHandler);
204   //MEDFactoryClient::getDataManager()->changeUnderlyingMesh(duplicate->id, meshId);
205   MEDCALC::FieldHandler* result = NULL;
206   try {
207     result = MEDFactoryClient::getDataManager()->interpolateField(fieldId, meshId, params);
208   }
209   catch(...) {
210     STDLOG("Unable to process field interpolation; please check interpolation parameters");
211     QMessageBox::critical(_salomeModule->getApp()->desktop(),
212                           tr("Operation failed"),
213                           tr("Unable to process field interpolation; please check interpolation parameters"));
214     return;
215   }
216
217   // Request once more the duplicate to update the meta-data on this
218   // client side
219   // duplicate = MEDFactoryClient::getDataManager()->getFieldHandler(duplicate->id);
220
221   // >>>
222   // WARN: the following is a temporary code for test purpose
223   // Automatically add in ws
224   ProcessingEvent* event = new ProcessingEvent();
225   event->eventtype = ProcessingEvent::EVENT_IMPORT_OBJECT;
226   XmedDataObject* dataObject = new XmedDataObject();
227   dataObject->setFieldHandler(*result);
228   event->objectdata = dataObject;
229   emit processingSignal(event);
230   // Note that this signal is processed by the WorkspaceController
231
232   // // Tag the item to prevent double import
233   // //_studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);
234 }