1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // Author : Guillaume Boulant (EDF)
22 #include "DatasourceController.hxx"
23 #include "DatasourceConstants.hxx"
25 #include "MEDOPFactoryClient.hxx"
26 #include "QtHelper.hxx"
28 #include CORBA_CLIENT_HEADER(SALOMEDS)
29 #include <SUIT_FileDlg.h>
30 #include <SUIT_Desktop.h>
32 #include <QStringList>
34 #include <QMessageBox>
36 #include "DlgAlias.hxx"
39 // ==============================================================
40 // Datasource controller
41 // ==============================================================
43 DatasourceController::DatasourceController(StandardApp_Module * salomeModule) {
44 STDLOG("Creating a DatasourceController");
45 _salomeModule = salomeModule;
46 _studyEditor = new SALOME_AppStudyEditor(_salomeModule->getApp());
48 _dlgChangeUnderlyingMesh = new DlgChangeUnderlyingMesh(_studyEditor);
49 connect(_dlgChangeUnderlyingMesh,SIGNAL(inputValidated()),
50 this, SLOT(OnChangeUnderlyingMeshInputValidated()));
52 _dlgInterpolateField = new DlgInterpolateField(_studyEditor);
53 connect(_dlgInterpolateField,SIGNAL(inputValidated()),
54 this, SLOT(OnInterpolateFieldInputValidated()));
58 DatasourceController::~DatasourceController() {
59 STDLOG("Deleting the DatasourceController");
63 void DatasourceController::createActions() {
65 // Main actions (toolbar and menubar)
67 QString label = tr("LAB_ADD_DATA_SOURCE");
68 QString tooltip = tr("TIP_ADD_DATA_SOURCE");
69 QString icon = tr("ICO_DATASOURCE_ADD");
71 actionId = _salomeModule->createStandardAction(label,this, SLOT(OnAddDatasource()),icon,tooltip);
72 _salomeModule->addActionInToolbar(actionId);
74 // This action has to be placed in the general file menu with the label "Import MED file"
75 int menuId = _salomeModule->createMenu( tr( "MEN_FILE" ), -1, 1 );
76 _salomeModule->addActionInMenubar(actionId, menuId);
78 label = tr("LAB_ADD_IMAGE_SOURCE");
79 tooltip = tr("TIP_ADD_IMAGE_SOURCE");
80 icon = tr("ICO_IMAGE_ADD");
81 actionId = _salomeModule->createStandardAction(label,this, SLOT(OnAddImagesource()),icon,tooltip);
82 _salomeModule->addActionInToolbar(actionId);
85 // Actions for popup menu only
87 // Expand field timeseries
88 label = tr("LAB_EXPAND_FIELD");
89 icon = tr("ICO_DATASOURCE_EXPAND_FIELD");
90 actionId = _salomeModule->createStandardAction(label,this,SLOT(OnExpandField()),icon);
91 _salomeModule->addActionInPopupMenu(actionId);
93 // Create a control view
94 label = tr("LAB_VISUALIZE");
95 icon = tr("ICO_DATASOURCE_VIEW");
96 actionId = _salomeModule->createStandardAction(label,this,SLOT(OnVisualize()),icon);
97 _salomeModule->addActionInPopupMenu(actionId);
100 label = tr("LAB_USE_IN_WORKSPACE");
101 icon = tr("ICO_DATASOURCE_USE");
102 actionId = _salomeModule->createStandardAction(label,this,SLOT(OnUseInWorkspace()),icon);
103 _salomeModule->addActionInPopupMenu(actionId);
105 // Change underlying mesh (note that this action creates a new field in
106 // the workspace that corresponds to a copy of the selected field
107 // modified by the change of the underlying mesh.
108 label = tr("LAB_CHANGE_MESH");
109 icon = tr("ICO_DATASOURCE_CHANGE_MESH");
110 actionId = _salomeModule->createStandardAction(label,this,SLOT(OnChangeUnderlyingMesh()),icon);
111 _salomeModule->addActionInPopupMenu(actionId);
113 label = tr("LAB_INTERPOLATE_FIELD");
114 icon = tr("ICO_DATASOURCE_INTERPOLATE_FIELD");
115 actionId = _salomeModule->createStandardAction(label,this,SLOT(OnInterpolateField()),icon);
116 _salomeModule->addActionInPopupMenu(actionId);
120 * This function adds the specified MED file as a datasource in the
121 * dataspace. Technically speaking, the engine loads the
122 * meta-information concerning med data from the file, gives this
123 * informations to the GUI, and the GUI creates a tree view of these
124 * data in the study object browser.
126 MEDOP::DatasourceHandler * DatasourceController::addDatasource(const char * filename) {
128 MEDOP::DatasourceHandler * datasourceHandler =
129 MEDOPFactoryClient::getDataManager()->addDatasource(filename);
131 // We need a studyEditor updated on the active study
132 _studyEditor->updateActiveStudy();
134 // Create a datasource SObject as a father of the module root
135 SALOMEDS::SComponent_var root = _studyEditor->findRoot(QCHARSTAR(_salomeModule->moduleName()));
136 SALOMEDS::SObject_var soDatasource = _studyEditor->newObject(root);
137 _studyEditor->setName(soDatasource,datasourceHandler->name);
138 _studyEditor->setIcon(soDatasource,tr("ICO_DATASOURCE").toStdString().c_str());
139 _studyEditor->setParameterInt(soDatasource,OBJECT_ID,datasourceHandler->id);
142 // We can add the meshes as children of the datasource
143 MEDOP::MeshHandlerList * meshHandlerList =
144 MEDOPFactoryClient::getDataManager()->getMeshList(datasourceHandler->id);
146 for(CORBA::ULong iMesh=0; iMesh<meshHandlerList->length(); iMesh++) {
147 MEDOP::MeshHandler meshHandler = (*meshHandlerList)[iMesh];
148 SALOMEDS::SObject_var soMesh = _studyEditor->newObject(soDatasource);
149 _studyEditor->setName(soMesh,meshHandler.name);
150 _studyEditor->setIcon(soMesh,tr("ICO_DATASOURCE_MESH").toStdString().c_str());
151 _studyEditor->setParameterInt(soMesh,OBJECT_ID,meshHandler.id);
152 _studyEditor->setParameterBool(soMesh,OBJECT_IS_IN_WORKSPACE,false);
154 // We add the field timeseries defined on this mesh, as children
155 // of the mesh SObject
156 MEDOP::FieldseriesHandlerList * fieldseriesHandlerList =
157 MEDOPFactoryClient::getDataManager()->getFieldseriesListOnMesh(meshHandler.id);
159 for(CORBA::ULong iFieldseries=0; iFieldseries<fieldseriesHandlerList->length(); iFieldseries++) {
160 MEDOP::FieldseriesHandler fieldseriesHandler = (*fieldseriesHandlerList)[iFieldseries];
161 SALOMEDS::SObject_var soFieldseries = _studyEditor->newObject(soMesh);
163 std::string label(fieldseriesHandler.name);
164 label +=" ("+std::string(XmedDataObject::mapTypeOfFieldLabel[fieldseriesHandler.type])+")";
165 _studyEditor->setName(soFieldseries,label.c_str());
167 _studyEditor->setIcon(soFieldseries,tr("ICO_DATASOURCE_FIELD").toStdString().c_str());
168 _studyEditor->setParameterInt(soFieldseries,OBJECT_ID,fieldseriesHandler.id);
169 _studyEditor->setParameterBool(soFieldseries,OBJECT_IS_IN_WORKSPACE,false);
173 return datasourceHandler;
177 void DatasourceController::OnAddDatasource()
179 // Dialog to get the filename where the input data are read from
181 filter.append(tr("FILE_FILTER_MED"));
183 QString anInitialPath = "";
184 if ( SUIT_FileDlg::getLastVisitedPath().isEmpty() )
185 anInitialPath = QDir::currentPath();
188 QString filename = SUIT_FileDlg::getFileName(_salomeModule->getApp()->desktop(),
191 tr("IMPORT_MED_FIELDS"),
195 QStringList filenames = SUIT_FileDlg::getOpenFileNames( _salomeModule->getApp()->desktop(),
198 tr("IMPORT_MED_FIELDS") );
200 if ( filenames.count() <= 0 ) return;
201 for ( QStringList::ConstIterator itFile = filenames.begin();
202 itFile != filenames.end(); ++itFile ) {
203 QString filename = *itFile;
204 this->addDatasource(QCHARSTAR(filename));
205 _salomeModule->updateObjBrowser(true);
209 #include "DlgImageToMed.hxx"
212 void DatasourceController::OnAddImagesource()
215 DlgImageToMed dialog;
216 dialog.setAutoLoaded(true);
217 int choice = dialog.exec();
218 if ( choice == QDialog::Rejected ) {
219 // The user decides to cancel the operation
223 QString imageFilename = dialog.getImageFilepath();
224 QString medFilename = dialog.getMedFilepath();
225 bool autoLoad = dialog.isAutoLoaded();
227 std::string ROOT_DIR(getenv("MED_ROOT_DIR"));
228 std::string command(ROOT_DIR+"/bin/salome/med/image2med.py");
229 command += " -i "+QS2S(imageFilename);
230 command += " -m "+QS2S(medFilename);
231 int error = system(command.c_str());
233 QMessageBox::critical(_salomeModule->getApp()->desktop(),
234 tr("Operation failed"),
235 tr("The creation of med data from the image file failed"));
240 this->addDatasource(QCHARSTAR(medFilename));
241 _salomeModule->updateObjBrowser(true);
246 void DatasourceController::OnExpandField()
248 // We need a studyEditor updated on the active study
249 _studyEditor->updateActiveStudy();
251 // Get the selected objects in the study (SObject)
252 SALOME_StudyEditor::SObjectList * listOfSObject = _studyEditor->getSelectedObjects();
253 for (int i=0; i<listOfSObject->size(); i++) {
254 SALOMEDS::SObject_var soFieldseries = listOfSObject->at(i);
256 // First retrieve the fieldseries id associated to this study object
257 long fieldseriesId = _studyEditor->getParameterInt(soFieldseries,OBJECT_ID);
258 STDLOG("Expand the field timeseries "<<fieldseriesId);
260 // If fieldseriesId equals -1, then it means that it is not a
261 // fieldseries managed by the MED module, and we stop this
263 if ( fieldseriesId < 0 )
265 // _GBO_ A better correction should be to no display the
266 // contextual menu if the selected object is not conform
268 // Then retrieve the list of fields in this timeseries
269 MEDOP::FieldHandlerList * fieldHandlerList =
270 MEDOPFactoryClient::getDataManager()->getFieldListInFieldseries(fieldseriesId);
272 // Finally, create an entry for each of the field
273 for(CORBA::ULong iField=0; iField<fieldHandlerList->length(); iField++) {
274 MEDOP::FieldHandler fieldHandler = (*fieldHandlerList)[iField];
275 SALOMEDS::SObject_var soField = _studyEditor->newObject(soFieldseries);
276 std::string label("it="); label += ToString(fieldHandler.iteration);
277 _studyEditor->setName(soField,label.c_str());
278 _studyEditor->setParameterInt(soField, OBJECT_ID, fieldHandler.id);
279 _studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,false);
282 _salomeModule->updateObjBrowser(true);
285 void DatasourceController::OnVisualize() {
286 STDLOG("OnVisualize: To Be Implemented");
288 // We need a _studyEditor updated on the active study
289 _studyEditor->updateActiveStudy();
291 // Get the selected objects in the study (SObject)
292 SALOME_StudyEditor::SObjectList * listOfSObject = _studyEditor->getSelectedObjects();
294 // For each object, emit a signal to the workspace to request a
295 // visualisation using the tui command (so that the user can see how
296 // to make a view of an object from the tui console).
297 for (int i=0; i<listOfSObject->size(); i++) {
298 SALOMEDS::SObject_var soField = listOfSObject->at(i);
299 int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
300 // If fieldId equals -1, then it means that it is not a field
301 // managed by the MED module, and we stop this function process.
305 MEDOP::FieldHandler * fieldHandler = MEDOPFactoryClient::getDataManager()->getFieldHandler(fieldId);
306 if (! fieldHandler) {
307 QMessageBox::warning(_salomeModule->getApp()->desktop(),
308 tr("Operation not allowed"),
309 tr("No field is defined"));
313 DatasourceEvent * event = new DatasourceEvent();
314 event->eventtype = DatasourceEvent::EVENT_VIEW_OBJECT;
315 XmedDataObject * dataObject = new XmedDataObject();
316 dataObject->setFieldHandler(*fieldHandler);
317 event->objectdata = dataObject;
318 emit datasourceSignal(event);
323 void DatasourceController::OnUseInWorkspace() {
324 // We need a studyEditor updated on the active study
325 _studyEditor->updateActiveStudy();
327 // Get the selected objects in the study (SObject)
328 SALOME_StudyEditor::SObjectList * listOfSObject = _studyEditor->getSelectedObjects();
329 if ( listOfSObject->size() == 1 ) {
330 // In this case we ask the name of the variable for the python
334 // _GBO_ Note that it works only for a single field but the
335 // XmedDataObject will be improved to deal with mesh, timeseries
336 // and single field in a futur version. We suppose here that a
337 // single field has been selected.
340 SALOMEDS::SObject_var soField = listOfSObject->at(0);
342 bool isInWorkspace = _studyEditor->getParameterBool(soField,OBJECT_IS_IN_WORKSPACE);
343 if ( isInWorkspace ) {
344 QMessageBox::warning(_salomeModule->getApp()->desktop(),
345 tr("Operation not allowed"),
346 tr("This field is already defined in the workspace"));
350 int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
352 // If fieldId equals -1, then it means that it is not a field
353 // managed by the MED module, and we stop this function process.
355 QMessageBox::warning(_salomeModule->getApp()->desktop(),
356 tr("Operation not allowed"),
357 tr("This element is not a field object"));
361 MEDOP::FieldHandler * fieldHandler =
362 MEDOPFactoryClient::getDataManager()->getFieldHandler(fieldId);
364 if (! fieldHandler) {
365 QMessageBox::warning(_salomeModule->getApp()->desktop(),
366 tr("Operation not allowed"),
367 tr("No field is defined"));
371 QString alias(fieldHandler->fieldname);
373 dialog.setAlias(alias);
374 int choice = dialog.exec();
375 if ( choice == QDialog::Rejected ) {
376 // The user decides to cancel the operation
379 alias = dialog.getAlias();
381 DatasourceEvent * event = new DatasourceEvent();
382 event->eventtype = DatasourceEvent::EVENT_USE_OBJECT;
383 XmedDataObject * dataObject = new XmedDataObject();
384 dataObject->setFieldHandler(*fieldHandler);
385 event->objectdata = dataObject;
386 event->objectalias = alias;
387 emit datasourceSignal(event);
388 // Tag the item to prevent double import
389 // _studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);
390 // Tag the field as persistent on the server. It means that a
391 // saving of the workspace will save at least this field (maybe it
392 // should be an option?)
393 MEDOPFactoryClient::getDataManager()->markAsPersistent(fieldId, true);
396 // In this case, we don't ask the user to specify an alias for
397 // each item, we just import the whole set of items.
398 for (int i=0; i<listOfSObject->size(); i++) {
399 SALOMEDS::SObject_var soField = listOfSObject->at(i);
401 bool isInWorkspace = _studyEditor->getParameterBool(soField,OBJECT_IS_IN_WORKSPACE);
402 if ( !isInWorkspace ) {
403 int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
404 MEDOP::FieldHandler * fieldHandler =
405 MEDOPFactoryClient::getDataManager()->getFieldHandler(fieldId);
406 DatasourceEvent * event = new DatasourceEvent();
407 event->eventtype = DatasourceEvent::EVENT_IMPORT_OBJECT;
408 XmedDataObject * dataObject = new XmedDataObject();
409 dataObject->setFieldHandler(*fieldHandler);
410 event->objectdata = dataObject;
411 emit datasourceSignal(event);
412 // Note that this signal is processed by the WorkspaceController
414 // Tag the item to prevent double import
415 // _studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);
416 // Tag the field as persistent on the server. It means that a
417 // saving of the workspace will save at least this field (maybe it
418 // should be an option?)
419 MEDOPFactoryClient::getDataManager()->markAsPersistent(fieldId, true);
422 STDLOG("The field "<<_studyEditor->getName(soField)<<
423 " is already defined in the workspace");
429 void DatasourceController::OnChangeUnderlyingMesh() {
430 // We need a studyEditor updated on the active study
431 _studyEditor->updateActiveStudy();
433 // Get the selected objects in the study (SObject). In cas of a
434 // multiple selection, we consider only the first item. At least one
435 // item must be selected.
436 SALOME_StudyEditor::SObjectList * listOfSObject = _studyEditor->getSelectedObjects();
437 if ( listOfSObject->size() > 0 ) {
438 SALOMEDS::SObject_var soField = listOfSObject->at(0);
439 int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
440 // _GBO_ : the dialog should not be modal, so that we can choose a
441 // mesh in the browser. Then we have to emit a signal from the
442 // dialog.accept, connected to a slot of the DatasourceControler
443 _dlgChangeUnderlyingMesh->setFieldId(fieldId);
444 Qt::WindowFlags flags = _dlgChangeUnderlyingMesh->windowFlags();
445 _dlgChangeUnderlyingMesh->setWindowFlags(flags | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
446 _dlgChangeUnderlyingMesh->open();
450 void DatasourceController::OnChangeUnderlyingMeshInputValidated() {
451 int meshId = _dlgChangeUnderlyingMesh->getMeshId();
452 STDLOG("meshId = " << ToString(meshId));
453 int fieldId = _dlgChangeUnderlyingMesh->getFieldId();
454 MEDOP::FieldHandler * fieldHandler =
455 MEDOPFactoryClient::getDataManager()->getFieldHandler(fieldId);
457 // We don't modify the original field but create first a duplicate
458 MEDOP::FieldHandler * duplicate = MEDOPFactoryClient::getCalculator()->dup(*fieldHandler);
459 MEDOPFactoryClient::getDataManager()->changeUnderlyingMesh(duplicate->id, meshId);
461 // Request once more the duplicate to update the meta-data on this
463 duplicate = MEDOPFactoryClient::getDataManager()->getFieldHandler(duplicate->id);
466 // WARN: the following is a temporary code for test purpose
467 // Automatically add in ws
468 DatasourceEvent * event = new DatasourceEvent();
469 event->eventtype = DatasourceEvent::EVENT_IMPORT_OBJECT;
470 XmedDataObject * dataObject = new XmedDataObject();
471 dataObject->setFieldHandler(*duplicate);
472 event->objectdata = dataObject;
473 emit datasourceSignal(event);
474 // Note that this signal is processed by the WorkspaceController
476 // Tag the item to prevent double import
477 //_studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);
480 void DatasourceController::OnInterpolateField() {
481 // We need a studyEditor updated on the active study
482 _studyEditor->updateActiveStudy();
484 // Get the selected objects in the study (SObject). In case of a
485 // multiple selection, we consider only the first item. At least one
486 // item must be selected.
487 SALOME_StudyEditor::SObjectList * listOfSObject = _studyEditor->getSelectedObjects();
488 if ( listOfSObject->size() > 0 ) {
489 SALOMEDS::SObject_var soField = listOfSObject->at(0);
490 int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
491 // _GBO_ : the dialog should not be modal, so that we can choose a
492 // mesh in the browser. Then we have to emit a signal from the
493 // dialog.accept, connected to a slot of the DatasourceControler
494 _dlgInterpolateField->setFieldId(fieldId);
495 Qt::WindowFlags flags = _dlgInterpolateField->windowFlags();
496 _dlgInterpolateField->setWindowFlags(flags | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
497 _dlgInterpolateField->open();
501 void DatasourceController::OnInterpolateFieldInputValidated() {
502 MEDOP::InterpolationParameters params;
503 params.precision = _dlgInterpolateField->getPrecision();
504 STDLOG("precision = " << params.precision);
505 params.defaultValue = _dlgInterpolateField->getDefaultValue();
506 STDLOG("defaultValue = " << params.defaultValue);
507 params.reverse = _dlgInterpolateField->getReverse();
508 STDLOG("reverse = " << params.reverse);
509 params.intersectionType = _dlgInterpolateField->getIntersectionType().c_str();
510 STDLOG("intersectionType = " << params.intersectionType);
511 params.method = _dlgInterpolateField->getMethod().c_str();
512 STDLOG("method = " << params.method);
513 params.nature = _dlgInterpolateField->getFieldNature().c_str();
514 STDLOG("nature = " << params.nature);
516 int meshId = _dlgInterpolateField->getMeshId();
517 STDLOG("meshId = " << ToString(meshId));
518 int fieldId = _dlgInterpolateField->getFieldId();
519 MEDOP::FieldHandler* fieldHandler = MEDOPFactoryClient::getDataManager()->getFieldHandler(fieldId);
521 // We don't modify the original field but create first a duplicate
522 // MEDOP::FieldHandler* duplicate = MEDOPFactoryClient::getCalculator()->dup(*fieldHandler);
523 //MEDOPFactoryClient::getDataManager()->changeUnderlyingMesh(duplicate->id, meshId);
524 MEDOP::FieldHandler* result = NULL;
526 result = MEDOPFactoryClient::getDataManager()->interpolateField(fieldId, meshId, params);
529 STDLOG("Unable to process field interpolation; please check interpolation parameters");
530 QMessageBox::critical(_salomeModule->getApp()->desktop(),
531 tr("Operation failed"),
532 tr("Unable to process field interpolation; please check interpolation parameters"));
536 // Request once more the duplicate to update the meta-data on this
538 // duplicate = MEDOPFactoryClient::getDataManager()->getFieldHandler(duplicate->id);
541 // WARN: the following is a temporary code for test purpose
542 // Automatically add in ws
543 DatasourceEvent * event = new DatasourceEvent();
544 event->eventtype = DatasourceEvent::EVENT_IMPORT_OBJECT;
545 XmedDataObject * dataObject = new XmedDataObject();
546 dataObject->setFieldHandler(*result);
547 event->objectdata = dataObject;
548 emit datasourceSignal(event);
549 // Note that this signal is processed by the WorkspaceController
551 // // Tag the item to prevent double import
552 // //_studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);