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);
307 DatasourceEvent * event = new DatasourceEvent();
308 event->eventtype = DatasourceEvent::EVENT_VIEW_OBJECT;
309 XmedDataObject * dataObject = new XmedDataObject();
310 dataObject->setFieldHandler(*fieldHandler);
311 event->objectdata = dataObject;
312 emit datasourceSignal(event);
317 void DatasourceController::OnUseInWorkspace() {
318 // We need a studyEditor updated on the active study
319 _studyEditor->updateActiveStudy();
321 // Get the selected objects in the study (SObject)
322 SALOME_StudyEditor::SObjectList * listOfSObject = _studyEditor->getSelectedObjects();
323 if ( listOfSObject->size() == 1 ) {
324 // In this case we ask the name of the variable for the python
328 // _GBO_ Note that it works only for a single field but the
329 // XmedDataObject will be improved to deal with mesh, timeseries
330 // and single field in a futur version. We suppose here that a
331 // single field has been selected.
334 SALOMEDS::SObject_var soField = listOfSObject->at(0);
336 bool isInWorkspace = _studyEditor->getParameterBool(soField,OBJECT_IS_IN_WORKSPACE);
337 if ( isInWorkspace ) {
338 QMessageBox::warning(_salomeModule->getApp()->desktop(),
339 tr("Operation not allowed"),
340 tr("This field is already defined in the workspace"));
344 int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
346 // If fieldId equals -1, then it means that it is not a field
347 // managed by the MED module, and we stop this function process.
349 QMessageBox::warning(_salomeModule->getApp()->desktop(),
350 tr("Operation not allowed"),
351 tr("This element is not a field object"));
355 MEDOP::FieldHandler * fieldHandler =
356 MEDOPFactoryClient::getDataManager()->getFieldHandler(fieldId);
358 if (! fieldHandler) {
359 QMessageBox::warning(_salomeModule->getApp()->desktop(),
360 tr("Operation not allowed"),
361 tr("No field is defined"));
365 QString alias(fieldHandler->fieldname);
367 dialog.setAlias(alias);
368 int choice = dialog.exec();
369 if ( choice == QDialog::Rejected ) {
370 // The user decides to cancel the operation
373 alias = dialog.getAlias();
375 DatasourceEvent * event = new DatasourceEvent();
376 event->eventtype = DatasourceEvent::EVENT_USE_OBJECT;
377 XmedDataObject * dataObject = new XmedDataObject();
378 dataObject->setFieldHandler(*fieldHandler);
379 event->objectdata = dataObject;
380 event->objectalias = alias;
381 emit datasourceSignal(event);
382 // Tag the item to prevent double import
383 // _studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);
384 // Tag the field as persistent on the server. It means that a
385 // saving of the workspace will save at least this field (maybe it
386 // should be an option?)
387 MEDOPFactoryClient::getDataManager()->markAsPersistent(fieldId, true);
390 // In this case, we don't ask the user to specify an alias for
391 // each item, we just import the whole set of items.
392 for (int i=0; i<listOfSObject->size(); i++) {
393 SALOMEDS::SObject_var soField = listOfSObject->at(i);
395 bool isInWorkspace = _studyEditor->getParameterBool(soField,OBJECT_IS_IN_WORKSPACE);
396 if ( !isInWorkspace ) {
397 int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
398 MEDOP::FieldHandler * fieldHandler =
399 MEDOPFactoryClient::getDataManager()->getFieldHandler(fieldId);
400 DatasourceEvent * event = new DatasourceEvent();
401 event->eventtype = DatasourceEvent::EVENT_IMPORT_OBJECT;
402 XmedDataObject * dataObject = new XmedDataObject();
403 dataObject->setFieldHandler(*fieldHandler);
404 event->objectdata = dataObject;
405 emit datasourceSignal(event);
406 // Note that this signal is processed by the WorkspaceController
408 // Tag the item to prevent double import
409 // _studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);
410 // Tag the field as persistent on the server. It means that a
411 // saving of the workspace will save at least this field (maybe it
412 // should be an option?)
413 MEDOPFactoryClient::getDataManager()->markAsPersistent(fieldId, true);
416 STDLOG("The field "<<_studyEditor->getName(soField)<<
417 " is already defined in the workspace");
423 void DatasourceController::OnChangeUnderlyingMesh() {
424 // We need a studyEditor updated on the active study
425 _studyEditor->updateActiveStudy();
427 // Get the selected objects in the study (SObject). In cas of a
428 // multiple selection, we consider only the first item. At least one
429 // item must be selected.
430 SALOME_StudyEditor::SObjectList * listOfSObject = _studyEditor->getSelectedObjects();
431 if ( listOfSObject->size() > 0 ) {
432 SALOMEDS::SObject_var soField = listOfSObject->at(0);
433 int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
434 // _GBO_ : the dialog should not be modal, so that we can choose a
435 // mesh in the browser. Then we have to emit a signal from the
436 // dialog.accept, connected to a slot of the DatasourceControler
437 _dlgChangeUnderlyingMesh->setFieldId(fieldId);
438 Qt::WindowFlags flags = _dlgChangeUnderlyingMesh->windowFlags();
439 _dlgChangeUnderlyingMesh->setWindowFlags(flags | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
440 _dlgChangeUnderlyingMesh->open();
444 void DatasourceController::OnChangeUnderlyingMeshInputValidated() {
445 int meshId = _dlgChangeUnderlyingMesh->getMeshId();
446 STDLOG("meshId = " << ToString(meshId));
447 int fieldId = _dlgChangeUnderlyingMesh->getFieldId();
448 MEDOP::FieldHandler * fieldHandler =
449 MEDOPFactoryClient::getDataManager()->getFieldHandler(fieldId);
451 // We don't modify the original field but create first a duplicate
452 MEDOP::FieldHandler * duplicate = MEDOPFactoryClient::getCalculator()->dup(*fieldHandler);
453 MEDOPFactoryClient::getDataManager()->changeUnderlyingMesh(duplicate->id, meshId);
455 // Request once more the duplicate to update the meta-data on this
457 duplicate = MEDOPFactoryClient::getDataManager()->getFieldHandler(duplicate->id);
460 // WARN: the following is a temporary code for test purpose
461 // Automatically add in ws
462 DatasourceEvent * event = new DatasourceEvent();
463 event->eventtype = DatasourceEvent::EVENT_IMPORT_OBJECT;
464 XmedDataObject * dataObject = new XmedDataObject();
465 dataObject->setFieldHandler(*duplicate);
466 event->objectdata = dataObject;
467 emit datasourceSignal(event);
468 // Note that this signal is processed by the WorkspaceController
470 // Tag the item to prevent double import
471 //_studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);
474 void DatasourceController::OnInterpolateField() {
475 // We need a studyEditor updated on the active study
476 _studyEditor->updateActiveStudy();
478 // Get the selected objects in the study (SObject). In case of a
479 // multiple selection, we consider only the first item. At least one
480 // item must be selected.
481 SALOME_StudyEditor::SObjectList * listOfSObject = _studyEditor->getSelectedObjects();
482 if ( listOfSObject->size() > 0 ) {
483 SALOMEDS::SObject_var soField = listOfSObject->at(0);
484 int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
485 // _GBO_ : the dialog should not be modal, so that we can choose a
486 // mesh in the browser. Then we have to emit a signal from the
487 // dialog.accept, connected to a slot of the DatasourceControler
488 _dlgInterpolateField->setFieldId(fieldId);
489 Qt::WindowFlags flags = _dlgInterpolateField->windowFlags();
490 _dlgInterpolateField->setWindowFlags(flags | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
491 _dlgInterpolateField->open();
495 void DatasourceController::OnInterpolateFieldInputValidated() {
496 MEDOP::InterpolationParameters params;
497 params.precision = _dlgInterpolateField->getPrecision();
498 STDLOG("precision = " << params.precision);
499 params.defaultValue = _dlgInterpolateField->getDefaultValue();
500 STDLOG("defaultValue = " << params.defaultValue);
501 params.reverse = _dlgInterpolateField->getReverse();
502 STDLOG("reverse = " << params.reverse);
503 params.intersectionType = _dlgInterpolateField->getIntersectionType().c_str();
504 STDLOG("intersectionType = " << params.intersectionType);
505 params.method = _dlgInterpolateField->getMethod().c_str();
506 STDLOG("method = " << params.method);
507 params.nature = _dlgInterpolateField->getFieldNature().c_str();
508 STDLOG("nature = " << params.nature);
510 int meshId = _dlgInterpolateField->getMeshId();
511 STDLOG("meshId = " << ToString(meshId));
512 int fieldId = _dlgInterpolateField->getFieldId();
513 MEDOP::FieldHandler* fieldHandler = MEDOPFactoryClient::getDataManager()->getFieldHandler(fieldId);
515 // We don't modify the original field but create first a duplicate
516 // MEDOP::FieldHandler* duplicate = MEDOPFactoryClient::getCalculator()->dup(*fieldHandler);
517 //MEDOPFactoryClient::getDataManager()->changeUnderlyingMesh(duplicate->id, meshId);
518 MEDOP::FieldHandler* result = NULL;
520 result = MEDOPFactoryClient::getDataManager()->interpolateField(fieldId, meshId, params);
523 STDLOG("Unable to process field interpolation; please check interpolation parameters");
524 QMessageBox::critical(_salomeModule->getApp()->desktop(),
525 tr("Operation failed"),
526 tr("Unable to process field interpolation; please check interpolation parameters"));
530 // Request once more the duplicate to update the meta-data on this
532 // duplicate = MEDOPFactoryClient::getDataManager()->getFieldHandler(duplicate->id);
535 // WARN: the following is a temporary code for test purpose
536 // Automatically add in ws
537 DatasourceEvent * event = new DatasourceEvent();
538 event->eventtype = DatasourceEvent::EVENT_IMPORT_OBJECT;
539 XmedDataObject * dataObject = new XmedDataObject();
540 dataObject->setFieldHandler(*result);
541 event->objectdata = dataObject;
542 emit datasourceSignal(event);
543 // Note that this signal is processed by the WorkspaceController
545 // // Tag the item to prevent double import
546 // //_studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);