Salome HOME
Merge branch 'V8_0_0_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 "DatasourceConstants.hxx"
22
23 #include "MEDFactoryClient.hxx"
24 #include "MEDModule.hxx"
25 #include "Basics_Utils.hxx"
26
27 #include <SUIT_Desktop.h>
28
29 ProcessingController::ProcessingController(MEDModule* salomeModule)
30 {
31   STDLOG("Creating a ProcessingController");
32   _salomeModule = salomeModule;
33   _studyEditor = _salomeModule->getStudyEditor();
34
35   _dlgChangeUnderlyingMesh = new DlgChangeUnderlyingMesh(_studyEditor);
36   connect(_dlgChangeUnderlyingMesh,SIGNAL(inputValidated()),
37           this, SLOT(OnChangeUnderlyingMeshInputValidated()));
38
39   _dlgInterpolateField = new DlgInterpolateField(_studyEditor);
40   connect(_dlgInterpolateField,SIGNAL(inputValidated()),
41           this, SLOT(OnInterpolateFieldInputValidated()));
42 }
43
44 ProcessingController::~ProcessingController()
45 {
46   STDLOG("Deleting the ProcessingController");
47 }
48
49 void
50 ProcessingController::createActions()
51 {
52   STDLOG("Creating ProcessingController actions");
53
54   int processingToolbarId = _salomeModule->createTool("Processing", "ProcessingToolbar");
55   int processingMenuId = _salomeModule->createMenu(tr("MENU_PROCESSING"), -1, 1);
56
57   // Change underlying mesh (note that this action creates a new field in
58   // the workspace that corresponds to a copy of the selected field
59   // modified by the change of the underlying mesh.
60   QString label   = tr("LAB_PROCESSING_CHANGE_MESH");
61   QString icon = tr("ICO_PROCESSING_CHANGE_MESH");
62   int actionId;
63   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnChangeUnderlyingMesh()),icon,label);
64   _salomeModule->createTool(actionId, processingToolbarId);
65   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
66   _salomeModule->createMenu(actionId, processingMenuId);
67
68   label   = tr("LAB_PROCESSING_INTERPOLATE_FIELD");
69   icon = tr("ICO_PROCESSING_INTERPOLATE_FIELD");
70   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnInterpolateField()),icon,label);
71   _salomeModule->createTool(actionId, processingToolbarId);
72   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
73   _salomeModule->createMenu(actionId, processingMenuId);
74 }
75
76 void
77 ProcessingController::OnChangeUnderlyingMesh()
78 {
79   // We need a studyEditor updated on the active study
80   _studyEditor->updateActiveStudy();
81
82   // Get the selected objects in the study (SObject). In cas of a
83   // multiple selection, we consider only the first item. At least one
84   // item must be selected.
85   SALOME_StudyEditor::SObjectList* listOfSObject = _studyEditor->getSelectedObjects();
86   if ( listOfSObject->size() > 0 ) {
87     SALOMEDS::SObject_var soField = listOfSObject->at(0);
88     int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
89     // _GBO_ : the dialog should not be modal, so that we can choose a
90     // mesh in the browser. Then we have to emit a signal from the
91     // dialog.accept, connected to a slot of the DatasourceControler
92     _dlgChangeUnderlyingMesh->setFieldId(fieldId);
93     Qt::WindowFlags flags = _dlgChangeUnderlyingMesh->windowFlags();
94     _dlgChangeUnderlyingMesh->setWindowFlags(flags | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
95     _dlgChangeUnderlyingMesh->open();
96   }
97 }
98
99 void
100 ProcessingController::OnChangeUnderlyingMeshInputValidated()
101 {
102   int meshId = _dlgChangeUnderlyingMesh->getMeshId();
103   STDLOG("meshId = " << ToString(meshId));
104   int fieldId = _dlgChangeUnderlyingMesh->getFieldId();
105   MEDCALC::FieldHandler* fieldHandler =
106     MEDFactoryClient::getDataManager()->getFieldHandler(fieldId);
107
108   // We don't modify the original field but create first a duplicate
109   MEDCALC::FieldHandler* duplicate = MEDFactoryClient::getCalculator()->dup(*fieldHandler);
110   MEDFactoryClient::getDataManager()->changeUnderlyingMesh(duplicate->id, meshId);
111
112   // Request once more the duplicate to update the meta-data on this
113   // client side
114   duplicate = MEDFactoryClient::getDataManager()->getFieldHandler(duplicate->id);
115
116   // >>>
117   // WARN: the following is a temporary code for test purpose
118   // Automatically add in ws
119   ProcessingEvent* event = new ProcessingEvent();
120   event->eventtype = ProcessingEvent::EVENT_IMPORT_OBJECT;
121   XmedDataObject* dataObject = new XmedDataObject();
122   dataObject->setFieldHandler(*duplicate);
123   event->objectdata = dataObject;
124   emit processingSignal(event);
125   // Note that this signal is processed by the WorkspaceController
126
127   // Tag the item to prevent double import
128   //_studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);
129 }
130
131 void
132 ProcessingController::OnInterpolateField()
133 {
134   // We need a studyEditor updated on the active study
135   _studyEditor->updateActiveStudy();
136
137   // Get the selected objects in the study (SObject). In case of a
138   // multiple selection, we consider only the first item. At least one
139   // item must be selected.
140   SALOME_StudyEditor::SObjectList* listOfSObject = _studyEditor->getSelectedObjects();
141   if ( listOfSObject->size() > 0 ) {
142     SALOMEDS::SObject_var soField = listOfSObject->at(0);
143     int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
144     // _GBO_ : the dialog should not be modal, so that we can choose a
145     // mesh in the browser. Then we have to emit a signal from the
146     // dialog.accept, connected to a slot of the DatasourceControler
147     _dlgInterpolateField->setFieldId(fieldId);
148     Qt::WindowFlags flags = _dlgInterpolateField->windowFlags();
149     _dlgInterpolateField->setWindowFlags(flags | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
150     _dlgInterpolateField->open();
151   }
152 }
153
154 void
155 ProcessingController::OnInterpolateFieldInputValidated()
156 {
157   MEDCALC::InterpolationParameters params;
158   params.precision = _dlgInterpolateField->getPrecision();
159   STDLOG("precision = " << params.precision);
160   params.defaultValue = _dlgInterpolateField->getDefaultValue();
161   STDLOG("defaultValue = " << params.defaultValue);
162   params.reverse = _dlgInterpolateField->getReverse();
163   STDLOG("reverse = " << params.reverse);
164   params.intersectionType = _dlgInterpolateField->getIntersectionType().c_str();
165   STDLOG("intersectionType = " << params.intersectionType);
166   params.method = _dlgInterpolateField->getMethod().c_str();
167   STDLOG("method = " << params.method);
168   params.nature = _dlgInterpolateField->getFieldNature().c_str();
169   STDLOG("nature = " << params.nature);
170
171   int meshId = _dlgInterpolateField->getMeshId();
172   STDLOG("meshId = " << ToString(meshId));
173   int fieldId = _dlgInterpolateField->getFieldId();
174   MEDCALC::FieldHandler* fieldHandler = MEDFactoryClient::getDataManager()->getFieldHandler(fieldId);
175
176   // We don't modify the original field but create first a duplicate
177   // MEDCALC::FieldHandler* duplicate = MEDFactoryClient::getCalculator()->dup(*fieldHandler);
178   //MEDFactoryClient::getDataManager()->changeUnderlyingMesh(duplicate->id, meshId);
179   MEDCALC::FieldHandler* result = NULL;
180   try {
181     result = MEDFactoryClient::getDataManager()->interpolateField(fieldId, meshId, params);
182   }
183   catch(...) {
184     STDLOG("Unable to process field interpolation; please check interpolation parameters");
185     QMessageBox::critical(_salomeModule->getApp()->desktop(),
186                           tr("Operation failed"),
187                           tr("Unable to process field interpolation; please check interpolation parameters"));
188     return;
189   }
190
191   // Request once more the duplicate to update the meta-data on this
192   // client side
193   // duplicate = MEDFactoryClient::getDataManager()->getFieldHandler(duplicate->id);
194
195   // >>>
196   // WARN: the following is a temporary code for test purpose
197   // Automatically add in ws
198   ProcessingEvent* event = new ProcessingEvent();
199   event->eventtype = ProcessingEvent::EVENT_IMPORT_OBJECT;
200   XmedDataObject* dataObject = new XmedDataObject();
201   dataObject->setFieldHandler(*result);
202   event->objectdata = dataObject;
203   emit processingSignal(event);
204   // Note that this signal is processed by the WorkspaceController
205
206   // // Tag the item to prevent double import
207   // //_studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);
208 }