Salome HOME
Merge remote-tracking branch 'origin/V8_0_0_BR'
[modules/med.git] / src / MEDCalc / gui / DatasourceController.cxx
1 // Copyright (C) 2007-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 // Author : Guillaume Boulant (EDF)
21
22 #include "DatasourceController.hxx"
23 #include "DatasourceConstants.hxx"
24
25 #include <SalomeApp_Application.h>
26 #include <SalomeApp_Study.h>
27 #include <SalomeApp_DataObject.h>
28
29 #include <SALOME_ListIO.hxx>
30 #include <LightApp_SelectionMgr.h>
31
32 #include <SALOME_LifeCycleCORBA.hxx>
33 #include <SALOMEDS_SObject.hxx>
34 #include <SALOMEDS_Study.hxx>
35
36 #include "MEDFactoryClient.hxx"
37 #include "MEDModule.hxx"
38 #include "QtHelper.hxx"
39
40 #include CORBA_CLIENT_HEADER(SALOMEDS)
41 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
42 #include <SUIT_FileDlg.h>
43 #include <SUIT_Desktop.h>
44
45 #include <QStringList>
46 #include <QString>
47 #include <QMessageBox>
48
49 #include "DlgAlias.hxx"
50
51 //
52 // ==============================================================
53 // Datasource controller
54 // ==============================================================
55 //
56 //DatasourceController::DatasourceController(StandardApp_Module * salomeModule)
57 DatasourceController::DatasourceController(MEDModule * salomeModule)
58 {
59   STDLOG("Creating a DatasourceController");
60   _salomeModule = salomeModule;
61   _studyEditor = new SALOME_AppStudyEditor(_salomeModule->getApp());
62
63   _dlgChangeUnderlyingMesh = new DlgChangeUnderlyingMesh(_studyEditor);
64   connect(_dlgChangeUnderlyingMesh,SIGNAL(inputValidated()),
65           this, SLOT(OnChangeUnderlyingMeshInputValidated()));
66
67   _dlgInterpolateField = new DlgInterpolateField(_studyEditor);
68   connect(_dlgInterpolateField,SIGNAL(inputValidated()),
69           this, SLOT(OnInterpolateFieldInputValidated()));
70
71 }
72
73 DatasourceController::~DatasourceController() {
74   STDLOG("Deleting the DatasourceController");
75   delete _studyEditor;
76 }
77
78 void DatasourceController::createActions() {
79   //QWidget* dsk = _salomeModule->getApp()->desktop();
80   //SUIT_ResourceMgr* resMgr = _salomeModule->getApp()->resourceMgr();
81   int toolbarId = _salomeModule->createTool("Datasource", "DatasourceToolbar");
82
83   //
84   // Main actions (toolbar and menubar)
85   //
86   QString label   = tr("LAB_ADD_DATA_SOURCE");
87   QString tooltip = tr("TIP_ADD_DATA_SOURCE");
88   QString icon    = tr("ICO_DATASOURCE_ADD");
89   int actionId;
90   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnAddDatasource()),icon,tooltip);
91   //_salomeModule->addActionInToolbar(actionId);
92   _salomeModule->createTool(actionId, toolbarId);
93
94   // This action has to be placed in the general file menu with the label "Import MED file"
95   int menuId = _salomeModule->createMenu( tr( "MEN_FILE" ), -1,  1 );
96   //_salomeModule->addActionInMenubar(actionId, menuId);
97   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
98   _salomeModule->createMenu(actionId, menuId, 10);
99
100   label   = tr("LAB_ADD_IMAGE_SOURCE");
101   tooltip = tr("TIP_ADD_IMAGE_SOURCE");
102   icon    = tr("ICO_IMAGE_ADD");
103   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnAddImagesource()),icon,tooltip);
104   // _salomeModule->addActionInToolbar(actionId);
105   _salomeModule->createTool(actionId, toolbarId);
106
107   //
108   // Actions for popup menu only
109   //
110   // Expand field timeseries
111   label = tr("LAB_EXPAND_FIELD");
112   icon  = tr("ICO_DATASOURCE_EXPAND_FIELD");
113   actionId = _salomeModule->createStandardAction(label,this,SLOT(OnExpandField()),icon);
114   _salomeModule->addActionInPopupMenu(actionId);
115
116   // Create a view submenu with usual visualization functions
117   label = tr("LAB_VISUALIZE_SCALARMAP");
118   icon  = tr("ICO_DATASOURCE_VIEW");
119   actionId = _salomeModule->createStandardAction(label,this,SLOT(OnVisualizeScalarMap()),icon);
120   _salomeModule->addActionInPopupMenu(actionId, tr("LAB_VISUALIZE"));
121
122   label = tr("LAB_VISUALIZE_CONTOUR");
123   icon  = tr("ICO_DATASOURCE_VIEW");
124   actionId = _salomeModule->createStandardAction(label,this,SLOT(OnVisualizeContour()),icon);
125   _salomeModule->addActionInPopupMenu(actionId, tr("LAB_VISUALIZE"));
126
127   label = tr("LAB_VISUALIZE_VECTORFIELD");
128   icon  = tr("ICO_DATASOURCE_VIEW");
129   actionId = _salomeModule->createStandardAction(label,this,SLOT(OnVisualizeVectorField()),icon);
130   _salomeModule->addActionInPopupMenu(actionId, tr("LAB_VISUALIZE"));
131
132   label = tr("LAB_VISUALIZE_SLICES");
133   icon  = tr("ICO_DATASOURCE_VIEW");
134   actionId = _salomeModule->createStandardAction(label,this,SLOT(OnVisualizeSlices()),icon);
135   _salomeModule->addActionInPopupMenu(actionId, tr("LAB_VISUALIZE"));
136
137   label = tr("LAB_VISUALIZE_DEFLECTIONSHAPE");
138   icon  = tr("ICO_DATASOURCE_VIEW");
139   actionId = _salomeModule->createStandardAction(label,this,SLOT(OnVisualizeDeflectionShape()),icon);
140   _salomeModule->addActionInPopupMenu(actionId, tr("LAB_VISUALIZE"));
141
142   label = tr("LAB_VISUALIZE_POINTSPRITE");
143   icon  = tr("ICO_DATASOURCE_VIEW");
144   actionId = _salomeModule->createStandardAction(label,this,SLOT(OnVisualizePointSprite()),icon);
145   _salomeModule->addActionInPopupMenu(actionId, tr("LAB_VISUALIZE"));
146
147   // Use in workspace
148   label = tr("LAB_USE_IN_WORKSPACE");
149   icon  = tr("ICO_DATASOURCE_USE");
150   actionId = _salomeModule->createStandardAction(label,this,SLOT(OnUseInWorkspace()),icon);
151   _salomeModule->addActionInPopupMenu(actionId);
152
153   // Change underlying mesh (note that this action creates a new field in
154   // the workspace that corresponds to a copy of the selected field
155   // modified by the change of the underlying mesh.
156   label = tr("LAB_CHANGE_MESH");
157   icon  = tr("ICO_DATASOURCE_CHANGE_MESH");
158   actionId = _salomeModule->createStandardAction(label,this,SLOT(OnChangeUnderlyingMesh()),icon);
159   _salomeModule->addActionInPopupMenu(actionId);
160
161   label = tr("LAB_INTERPOLATE_FIELD");
162   icon  = tr("ICO_DATASOURCE_INTERPOLATE_FIELD");
163   actionId = _salomeModule->createStandardAction(label,this,SLOT(OnInterpolateField()),icon);
164   _salomeModule->addActionInPopupMenu(actionId);
165 }
166
167 /**
168  * This function adds the specified MED file as a datasource in the
169  * dataspace. Technically speaking, the engine loads the
170  * meta-information concerning med data from the file, gives this
171  * informations to the GUI, and the GUI creates a tree view of these
172  * data in the study object browser.
173  */
174 // This function emits a signal that will be caught by workspace to delegate command (datasource creation) to python console.
175 void
176 DatasourceController::addDatasource(const char* filename)
177 {
178   DatasourceEvent* event = new DatasourceEvent();
179   event->eventtype = DatasourceEvent::EVENT_ADD_DATASOURCE;
180   event->objectalias = filename;
181   emit datasourceSignal(event);
182 }
183 // After above data source creation, python console emits a signal, forwarded by workspace, to update the GUI
184 void
185 DatasourceController::updateTreeViewWithNewDatasource(const MEDCALC::DatasourceHandler* datasourceHandler)
186 {
187   if (!datasourceHandler) {
188     return;
189   }
190
191   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(_salomeModule->application()->activeStudy());
192   _PTR(Study) studyDS = study->studyDS();
193
194   _salomeModule->engine()->addDatasourceToStudy(_CAST(Study, studyDS)->GetStudy(), *datasourceHandler);
195
196   // update Object browser
197   _salomeModule->getApp()->updateObjectBrowser(true);
198 }
199
200 void
201 DatasourceController::updateTreeViewWithNewPresentation(long fieldId, long presentationId)
202 {
203   if (presentationId < 0) {
204     std::cerr << "Unknown presentation\n";
205     return;
206   }
207
208   std::string name = MEDFactoryClient::getPresentationManager()->getPresentationProperty(presentationId, "name");
209   name = tr(name.c_str()).toStdString();
210   std::string label = tr("ICO_MED_PRESENTATION").toStdString();
211
212   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(_salomeModule->application()->activeStudy());
213   _PTR(Study) studyDS = study->studyDS();
214
215   _salomeModule->engine()->registerPresentation(_CAST(Study, studyDS)->GetStudy(), fieldId, name.c_str(), label.c_str());
216
217   // update Object browser
218   _salomeModule->getApp()->updateObjectBrowser(true);
219 }
220
221 void DatasourceController::OnAddDatasource()
222 {
223   // Dialog to get the filename where the input data are read from
224   QStringList filter;
225   filter.append(tr("FILE_FILTER_MED"));
226
227   QString anInitialPath = "";
228   if ( SUIT_FileDlg::getLastVisitedPath().isEmpty() )
229     anInitialPath = QDir::currentPath();
230
231   QStringList filenames = SUIT_FileDlg::getOpenFileNames( _salomeModule->getApp()->desktop(),
232                                                           anInitialPath,
233                                                           filter,
234                                                           tr("IMPORT_MED_FIELDS") );
235
236   if ( filenames.count() <= 0 ) return;
237   for ( QStringList::ConstIterator itFile = filenames.begin();
238         itFile != filenames.end(); ++itFile ) {
239     QString filename = *itFile;
240     this->addDatasource(QCHARSTAR(filename));
241     _salomeModule->updateObjBrowser(true);
242   }
243 }
244
245 #include "DlgImageToMed.hxx"
246 //#include <stdio.h>
247 //#include <stdlib.h>
248 void DatasourceController::OnAddImagesource()
249 {
250
251   DlgImageToMed dialog;
252   dialog.setAutoLoaded(true);
253   int choice = dialog.exec();
254   if ( choice == QDialog::Rejected ) {
255     // The user decides to cancel the operation
256     return;
257   }
258
259   QString imageFilename = dialog.getImageFilepath();
260   /*
261   QString medFilename   = dialog.getMedFilepath();
262   bool autoLoad         = dialog.isAutoLoaded();
263
264   std::string ROOT_DIR(getenv("MED_ROOT_DIR"));
265   std::string command(ROOT_DIR+"/bin/salome/med/image2med.py");
266   command += " -i "+QS2S(imageFilename);
267   command += " -m "+QS2S(medFilename);
268   int error = system(command.c_str());
269   if ( error != 0 ) {
270     QMessageBox::critical(_salomeModule->getApp()->desktop(),
271            tr("Operation failed"),
272            tr("The creation of med data from the image file failed"));
273     return;
274   }
275
276   if ( autoLoad ) {
277     this->addDatasource(QCHARSTAR(medFilename));
278     _salomeModule->updateObjBrowser(true);
279   }
280   */
281
282   DatasourceEvent* event = new DatasourceEvent();
283   event->eventtype = DatasourceEvent::EVENT_ADD_IMAGE_AS_DATASOURCE;
284   event->objectalias = imageFilename;
285   emit datasourceSignal(event);
286 }
287
288 void DatasourceController::OnExpandField()
289 {
290   // We need a studyEditor updated on the active study
291   _studyEditor->updateActiveStudy();
292
293   // Get the selected objects in the study (SObject)
294   SALOME_StudyEditor::SObjectList * listOfSObject = _studyEditor->getSelectedObjects();
295   for (int i=0; i<listOfSObject->size(); i++) {
296     SALOMEDS::SObject_var soFieldseries = listOfSObject->at(i);
297
298     // First retrieve the fieldseries id associated to this study object
299     long fieldseriesId = _studyEditor->getParameterInt(soFieldseries,OBJECT_ID);
300     STDLOG("Expand the field timeseries "<<fieldseriesId);
301
302     // If fieldseriesId equals -1, then it means that it is not a
303     // fieldseries managed by the MED module, and we stop this
304     // function process.
305     if ( fieldseriesId < 0 )
306       continue;
307     // _GBO_ A better correction should be to no display the
308     // contextual menu if the selected object is not conform
309
310     // Then retrieve the list of fields in this timeseries
311     MEDCALC::FieldHandlerList * fieldHandlerList =
312       MEDFactoryClient::getDataManager()->getFieldListInFieldseries(fieldseriesId);
313
314     // Finally, create an entry for each of the field
315     for(CORBA::ULong iField=0; iField<fieldHandlerList->length(); iField++) {
316       MEDCALC::FieldHandler fieldHandler = (*fieldHandlerList)[iField];
317       SALOMEDS::SObject_var soField = _studyEditor->newObject(soFieldseries);
318       std::string label("it="); label += ToString(fieldHandler.iteration);
319       _studyEditor->setName(soField,label.c_str());
320       _studyEditor->setParameterInt(soField, OBJECT_ID, fieldHandler.id);
321       _studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,false);
322     }
323   }
324   _salomeModule->updateObjBrowser(true);
325 }
326
327 void DatasourceController::visualize(DatasourceEvent::EventType eventType) {
328   // We need a _studyEditor updated on the active study
329   _studyEditor->updateActiveStudy();
330
331   // Get the selected objects in the study (SObject)
332   SALOME_StudyEditor::SObjectList * listOfSObject = _studyEditor->getSelectedObjects();
333
334   // For each object, emit a signal to the workspace to request a
335   // visualisation using the tui command (so that the user can see how
336   // to make a view of an object from the tui console).
337   for (int i=0; i<listOfSObject->size(); i++) {
338     SALOMEDS::SObject_var soField = listOfSObject->at(i);
339     int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
340     // If fieldId equals -1, then it means that it is not a field
341     // managed by the MED module, and we stop this function process.
342     if ( fieldId < 0 )
343       continue;
344
345     MEDCALC::FieldHandler * fieldHandler = MEDFactoryClient::getDataManager()->getFieldHandler(fieldId);
346     if (! fieldHandler) {
347       QMessageBox::warning(_salomeModule->getApp()->desktop(),
348          tr("Operation not allowed"),
349          tr("No field is defined"));
350       return;
351     }
352
353     DatasourceEvent * event = new DatasourceEvent();
354     event->eventtype = eventType;
355     XmedDataObject * dataObject = new XmedDataObject();
356     dataObject->setFieldHandler(*fieldHandler);
357     event->objectdata  = dataObject;
358     emit datasourceSignal(event);
359   }
360 }
361
362 void DatasourceController::OnVisualizeScalarMap() {
363   this->visualize(DatasourceEvent::EVENT_VIEW_OBJECT_SCALAR_MAP);
364 }
365
366 void DatasourceController::OnVisualizeContour() {
367   this->visualize(DatasourceEvent::EVENT_VIEW_OBJECT_CONTOUR);
368 }
369
370 void DatasourceController::OnVisualizeVectorField() {
371   this->visualize(DatasourceEvent::EVENT_VIEW_OBJECT_VECTOR_FIELD);
372 }
373
374 void DatasourceController::OnVisualizeSlices() {
375   this->visualize(DatasourceEvent::EVENT_VIEW_OBJECT_SLICES);
376 }
377
378 void DatasourceController::OnVisualizeDeflectionShape() {
379   this->visualize(DatasourceEvent::EVENT_VIEW_OBJECT_DEFLECTION_SHAPE);
380 }
381
382 void DatasourceController::OnVisualizePointSprite() {
383   this->visualize(DatasourceEvent::EVENT_VIEW_OBJECT_POINT_SPRITE);
384 }
385
386 void DatasourceController::OnUseInWorkspace() {
387   // We need a studyEditor updated on the active study
388   _studyEditor->updateActiveStudy();
389
390   // Get the selected objects in the study (SObject)
391   SALOME_StudyEditor::SObjectList * listOfSObject = _studyEditor->getSelectedObjects();
392   if ( listOfSObject->size() == 1 ) {
393     // In this case we ask the name of the variable for the python
394     // console
395
396     // >>>
397     // _GBO_ Note that it works only for a single field but the
398     // XmedDataObject will be improved to deal with mesh, timeseries
399     // and single field in a futur version. We suppose here that a
400     // single field has been selected.
401     // <<<
402
403     SALOMEDS::SObject_var soField = listOfSObject->at(0);
404
405     bool isInWorkspace = _studyEditor->getParameterBool(soField,OBJECT_IS_IN_WORKSPACE);
406     if ( isInWorkspace ) {
407       QMessageBox::warning(_salomeModule->getApp()->desktop(),
408          tr("Operation not allowed"),
409          tr("This field is already defined in the workspace"));
410       return;
411     }
412
413     int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
414
415     // If fieldId equals -1, then it means that it is not a field
416     // managed by the MED module, and we stop this function process.
417     if ( fieldId < 0 ) {
418       QMessageBox::warning(_salomeModule->getApp()->desktop(),
419          tr("Operation not allowed"),
420          tr("This element is not a field object"));
421       return;
422     }
423
424     MEDCALC::FieldHandler * fieldHandler =
425       MEDFactoryClient::getDataManager()->getFieldHandler(fieldId);
426
427     if (! fieldHandler) {
428       QMessageBox::warning(_salomeModule->getApp()->desktop(),
429          tr("Operation not allowed"),
430          tr("No field is defined"));
431       return;
432     }
433
434     QString alias(fieldHandler->fieldname);
435     DlgAlias dialog;
436     dialog.setAlias(alias);
437     int choice = dialog.exec();
438     if ( choice == QDialog::Rejected ) {
439       // The user decides to cancel the operation
440       return;
441     }
442     alias = dialog.getAlias();
443
444     DatasourceEvent * event = new DatasourceEvent();
445     event->eventtype = DatasourceEvent::EVENT_USE_OBJECT;
446     XmedDataObject * dataObject = new XmedDataObject();
447     dataObject->setFieldHandler(*fieldHandler);
448     event->objectdata  = dataObject;
449     event->objectalias = alias;
450     emit datasourceSignal(event);
451     // Tag the item to prevent double import
452     //    _studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);
453     // Tag the field as persistent on the server. It means that a
454     // saving of the workspace will save at least this field (maybe it
455     // should be an option?)
456     MEDFactoryClient::getDataManager()->markAsPersistent(fieldId, true);
457   }
458   else {
459     // In this case, we don't ask the user to specify an alias for
460     // each item, we just import the whole set of items.
461     for (int i=0; i<listOfSObject->size(); i++) {
462       SALOMEDS::SObject_var soField = listOfSObject->at(i);
463
464       bool isInWorkspace = _studyEditor->getParameterBool(soField,OBJECT_IS_IN_WORKSPACE);
465       if ( !isInWorkspace ) {
466         int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
467         MEDCALC::FieldHandler * fieldHandler =
468           MEDFactoryClient::getDataManager()->getFieldHandler(fieldId);
469         DatasourceEvent * event = new DatasourceEvent();
470         event->eventtype = DatasourceEvent::EVENT_IMPORT_OBJECT;
471         XmedDataObject * dataObject = new XmedDataObject();
472         dataObject->setFieldHandler(*fieldHandler);
473         event->objectdata  = dataObject;
474         emit datasourceSignal(event);
475         // Note that this signal is processed by the WorkspaceController
476
477         // Tag the item to prevent double import
478         //        _studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);
479         // Tag the field as persistent on the server. It means that a
480         // saving of the workspace will save at least this field (maybe it
481         // should be an option?)
482         MEDFactoryClient::getDataManager()->markAsPersistent(fieldId, true);
483       }
484       else {
485         STDLOG("The field "<<_studyEditor->getName(soField)<<
486                " is already defined in the workspace");
487       }
488     }
489   }
490 }
491
492 void DatasourceController::OnChangeUnderlyingMesh() {
493   // We need a studyEditor updated on the active study
494   _studyEditor->updateActiveStudy();
495
496   // Get the selected objects in the study (SObject). In cas of a
497   // multiple selection, we consider only the first item. At least one
498   // item must be selected.
499   SALOME_StudyEditor::SObjectList * listOfSObject = _studyEditor->getSelectedObjects();
500   if ( listOfSObject->size() > 0 ) {
501     SALOMEDS::SObject_var soField = listOfSObject->at(0);
502     int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
503     // _GBO_ : the dialog should not be modal, so that we can choose a
504     // mesh in the browser. Then we have to emit a signal from the
505     // dialog.accept, connected to a slot of the DatasourceControler
506     _dlgChangeUnderlyingMesh->setFieldId(fieldId);
507     Qt::WindowFlags flags = _dlgChangeUnderlyingMesh->windowFlags();
508     _dlgChangeUnderlyingMesh->setWindowFlags(flags | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
509     _dlgChangeUnderlyingMesh->open();
510   }
511 }
512
513 void DatasourceController::OnChangeUnderlyingMeshInputValidated() {
514   int meshId = _dlgChangeUnderlyingMesh->getMeshId();
515   STDLOG("meshId = " << ToString(meshId));
516   int fieldId = _dlgChangeUnderlyingMesh->getFieldId();
517   MEDCALC::FieldHandler * fieldHandler =
518     MEDFactoryClient::getDataManager()->getFieldHandler(fieldId);
519
520   // We don't modify the original field but create first a duplicate
521   MEDCALC::FieldHandler * duplicate = MEDFactoryClient::getCalculator()->dup(*fieldHandler);
522   MEDFactoryClient::getDataManager()->changeUnderlyingMesh(duplicate->id, meshId);
523
524   // Request once more the duplicate to update the meta-data on this
525   // client side
526   duplicate = MEDFactoryClient::getDataManager()->getFieldHandler(duplicate->id);
527
528   // >>>
529   // WARN: the following is a temporary code for test purpose
530   // Automatically add in ws
531   DatasourceEvent * event = new DatasourceEvent();
532   event->eventtype = DatasourceEvent::EVENT_IMPORT_OBJECT;
533   XmedDataObject * dataObject = new XmedDataObject();
534   dataObject->setFieldHandler(*duplicate);
535   event->objectdata = dataObject;
536   emit datasourceSignal(event);
537   // Note that this signal is processed by the WorkspaceController
538
539   // Tag the item to prevent double import
540   //_studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);
541 }
542
543 void DatasourceController::OnInterpolateField() {
544   // We need a studyEditor updated on the active study
545   _studyEditor->updateActiveStudy();
546
547   // Get the selected objects in the study (SObject). In case of a
548   // multiple selection, we consider only the first item. At least one
549   // item must be selected.
550   SALOME_StudyEditor::SObjectList * listOfSObject = _studyEditor->getSelectedObjects();
551   if ( listOfSObject->size() > 0 ) {
552     SALOMEDS::SObject_var soField = listOfSObject->at(0);
553     int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
554     // _GBO_ : the dialog should not be modal, so that we can choose a
555     // mesh in the browser. Then we have to emit a signal from the
556     // dialog.accept, connected to a slot of the DatasourceControler
557     _dlgInterpolateField->setFieldId(fieldId);
558     Qt::WindowFlags flags = _dlgInterpolateField->windowFlags();
559     _dlgInterpolateField->setWindowFlags(flags | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
560     _dlgInterpolateField->open();
561   }
562 }
563
564 void DatasourceController::OnInterpolateFieldInputValidated() {
565   MEDCALC::InterpolationParameters params;
566   params.precision = _dlgInterpolateField->getPrecision();
567   STDLOG("precision = " << params.precision);
568   params.defaultValue = _dlgInterpolateField->getDefaultValue();
569   STDLOG("defaultValue = " << params.defaultValue);
570   params.reverse = _dlgInterpolateField->getReverse();
571   STDLOG("reverse = " << params.reverse);
572   params.intersectionType = _dlgInterpolateField->getIntersectionType().c_str();
573   STDLOG("intersectionType = " << params.intersectionType);
574   params.method = _dlgInterpolateField->getMethod().c_str();
575   STDLOG("method = " << params.method);
576   params.nature = _dlgInterpolateField->getFieldNature().c_str();
577   STDLOG("nature = " << params.nature);
578
579   int meshId = _dlgInterpolateField->getMeshId();
580   STDLOG("meshId = " << ToString(meshId));
581   int fieldId = _dlgInterpolateField->getFieldId();
582   MEDCALC::FieldHandler* fieldHandler = MEDFactoryClient::getDataManager()->getFieldHandler(fieldId);
583
584   // We don't modify the original field but create first a duplicate
585   // MEDCALC::FieldHandler* duplicate = MEDFactoryClient::getCalculator()->dup(*fieldHandler);
586   //MEDFactoryClient::getDataManager()->changeUnderlyingMesh(duplicate->id, meshId);
587   MEDCALC::FieldHandler* result = NULL;
588   try {
589     result = MEDFactoryClient::getDataManager()->interpolateField(fieldId, meshId, params);
590   }
591   catch(...) {
592     STDLOG("Unable to process field interpolation; please check interpolation parameters");
593     QMessageBox::critical(_salomeModule->getApp()->desktop(),
594                           tr("Operation failed"),
595                           tr("Unable to process field interpolation; please check interpolation parameters"));
596     return;
597   }
598
599   // Request once more the duplicate to update the meta-data on this
600   // client side
601   // duplicate = MEDFactoryClient::getDataManager()->getFieldHandler(duplicate->id);
602
603   // >>>
604   // WARN: the following is a temporary code for test purpose
605   // Automatically add in ws
606   DatasourceEvent * event = new DatasourceEvent();
607   event->eventtype = DatasourceEvent::EVENT_IMPORT_OBJECT;
608   XmedDataObject * dataObject = new XmedDataObject();
609   dataObject->setFieldHandler(*result);
610   event->objectdata = dataObject;
611   emit datasourceSignal(event);
612   // Note that this signal is processed by the WorkspaceController
613
614   // // Tag the item to prevent double import
615   // //_studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);
616 }
617
618 void
619 DatasourceController::processWorkspaceEvent(const MEDCALC::MedEvent* event)
620 {
621   if ( event->type == MEDCALC::EVENT_ADD_DATASOURCE ) {
622     MEDCALC::DatasourceHandler* datasourceHandler = MEDFactoryClient::getDataManager()->getDatasourceHandler(event->filename);
623     this->updateTreeViewWithNewDatasource(datasourceHandler);
624   }
625   else if ( event->type == MEDCALC::EVENT_ADD_PRESENTATION ) {
626     this->updateTreeViewWithNewPresentation(event->dataId, event->presentationId);
627   }
628 }