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 view submenu with usual visualization functions
94 label = tr("LAB_VISUALIZE_SCALARMAP");
95 icon = tr("ICO_DATASOURCE_VIEW");
96 actionId = _salomeModule->createStandardAction(label,this,SLOT(OnVisualizeScalarMap()),icon);
97 _salomeModule->addActionInPopupMenu(actionId, tr("LAB_VISUALIZE"));
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::visualize(DatasourceEvent::EventType eventType) {
286 // We need a _studyEditor updated on the active study
287 _studyEditor->updateActiveStudy();
289 // Get the selected objects in the study (SObject)
290 SALOME_StudyEditor::SObjectList * listOfSObject = _studyEditor->getSelectedObjects();
292 // For each object, emit a signal to the workspace to request a
293 // visualisation using the tui command (so that the user can see how
294 // to make a view of an object from the tui console).
295 for (int i=0; i<listOfSObject->size(); i++) {
296 SALOMEDS::SObject_var soField = listOfSObject->at(i);
297 int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
298 // If fieldId equals -1, then it means that it is not a field
299 // managed by the MED module, and we stop this function process.
303 MEDOP::FieldHandler * fieldHandler = MEDOPFactoryClient::getDataManager()->getFieldHandler(fieldId);
304 if (! fieldHandler) {
305 QMessageBox::warning(_salomeModule->getApp()->desktop(),
306 tr("Operation not allowed"),
307 tr("No field is defined"));
311 DatasourceEvent * event = new DatasourceEvent();
312 event->eventtype = eventType;
313 XmedDataObject * dataObject = new XmedDataObject();
314 dataObject->setFieldHandler(*fieldHandler);
315 event->objectdata = dataObject;
316 emit datasourceSignal(event);
320 void DatasourceController::OnVisualizeScalarMap() {
321 this->visualize(DatasourceEvent::EVENT_VIEW_OBJECT_SCALAR_MAP);
324 void DatasourceController::OnUseInWorkspace() {
325 // We need a studyEditor updated on the active study
326 _studyEditor->updateActiveStudy();
328 // Get the selected objects in the study (SObject)
329 SALOME_StudyEditor::SObjectList * listOfSObject = _studyEditor->getSelectedObjects();
330 if ( listOfSObject->size() == 1 ) {
331 // In this case we ask the name of the variable for the python
335 // _GBO_ Note that it works only for a single field but the
336 // XmedDataObject will be improved to deal with mesh, timeseries
337 // and single field in a futur version. We suppose here that a
338 // single field has been selected.
341 SALOMEDS::SObject_var soField = listOfSObject->at(0);
343 bool isInWorkspace = _studyEditor->getParameterBool(soField,OBJECT_IS_IN_WORKSPACE);
344 if ( isInWorkspace ) {
345 QMessageBox::warning(_salomeModule->getApp()->desktop(),
346 tr("Operation not allowed"),
347 tr("This field is already defined in the workspace"));
351 int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
353 // If fieldId equals -1, then it means that it is not a field
354 // managed by the MED module, and we stop this function process.
356 QMessageBox::warning(_salomeModule->getApp()->desktop(),
357 tr("Operation not allowed"),
358 tr("This element is not a field object"));
362 MEDOP::FieldHandler * fieldHandler =
363 MEDOPFactoryClient::getDataManager()->getFieldHandler(fieldId);
365 if (! fieldHandler) {
366 QMessageBox::warning(_salomeModule->getApp()->desktop(),
367 tr("Operation not allowed"),
368 tr("No field is defined"));
372 QString alias(fieldHandler->fieldname);
374 dialog.setAlias(alias);
375 int choice = dialog.exec();
376 if ( choice == QDialog::Rejected ) {
377 // The user decides to cancel the operation
380 alias = dialog.getAlias();
382 DatasourceEvent * event = new DatasourceEvent();
383 event->eventtype = DatasourceEvent::EVENT_USE_OBJECT;
384 XmedDataObject * dataObject = new XmedDataObject();
385 dataObject->setFieldHandler(*fieldHandler);
386 event->objectdata = dataObject;
387 event->objectalias = alias;
388 emit datasourceSignal(event);
389 // Tag the item to prevent double import
390 // _studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);
391 // Tag the field as persistent on the server. It means that a
392 // saving of the workspace will save at least this field (maybe it
393 // should be an option?)
394 MEDOPFactoryClient::getDataManager()->markAsPersistent(fieldId, true);
397 // In this case, we don't ask the user to specify an alias for
398 // each item, we just import the whole set of items.
399 for (int i=0; i<listOfSObject->size(); i++) {
400 SALOMEDS::SObject_var soField = listOfSObject->at(i);
402 bool isInWorkspace = _studyEditor->getParameterBool(soField,OBJECT_IS_IN_WORKSPACE);
403 if ( !isInWorkspace ) {
404 int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
405 MEDOP::FieldHandler * fieldHandler =
406 MEDOPFactoryClient::getDataManager()->getFieldHandler(fieldId);
407 DatasourceEvent * event = new DatasourceEvent();
408 event->eventtype = DatasourceEvent::EVENT_IMPORT_OBJECT;
409 XmedDataObject * dataObject = new XmedDataObject();
410 dataObject->setFieldHandler(*fieldHandler);
411 event->objectdata = dataObject;
412 emit datasourceSignal(event);
413 // Note that this signal is processed by the WorkspaceController
415 // Tag the item to prevent double import
416 // _studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);
417 // Tag the field as persistent on the server. It means that a
418 // saving of the workspace will save at least this field (maybe it
419 // should be an option?)
420 MEDOPFactoryClient::getDataManager()->markAsPersistent(fieldId, true);
423 STDLOG("The field "<<_studyEditor->getName(soField)<<
424 " is already defined in the workspace");
430 void DatasourceController::OnChangeUnderlyingMesh() {
431 // We need a studyEditor updated on the active study
432 _studyEditor->updateActiveStudy();
434 // Get the selected objects in the study (SObject). In cas of a
435 // multiple selection, we consider only the first item. At least one
436 // item must be selected.
437 SALOME_StudyEditor::SObjectList * listOfSObject = _studyEditor->getSelectedObjects();
438 if ( listOfSObject->size() > 0 ) {
439 SALOMEDS::SObject_var soField = listOfSObject->at(0);
440 int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
441 // _GBO_ : the dialog should not be modal, so that we can choose a
442 // mesh in the browser. Then we have to emit a signal from the
443 // dialog.accept, connected to a slot of the DatasourceControler
444 _dlgChangeUnderlyingMesh->setFieldId(fieldId);
445 Qt::WindowFlags flags = _dlgChangeUnderlyingMesh->windowFlags();
446 _dlgChangeUnderlyingMesh->setWindowFlags(flags | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
447 _dlgChangeUnderlyingMesh->open();
451 void DatasourceController::OnChangeUnderlyingMeshInputValidated() {
452 int meshId = _dlgChangeUnderlyingMesh->getMeshId();
453 STDLOG("meshId = " << ToString(meshId));
454 int fieldId = _dlgChangeUnderlyingMesh->getFieldId();
455 MEDOP::FieldHandler * fieldHandler =
456 MEDOPFactoryClient::getDataManager()->getFieldHandler(fieldId);
458 // We don't modify the original field but create first a duplicate
459 MEDOP::FieldHandler * duplicate = MEDOPFactoryClient::getCalculator()->dup(*fieldHandler);
460 MEDOPFactoryClient::getDataManager()->changeUnderlyingMesh(duplicate->id, meshId);
462 // Request once more the duplicate to update the meta-data on this
464 duplicate = MEDOPFactoryClient::getDataManager()->getFieldHandler(duplicate->id);
467 // WARN: the following is a temporary code for test purpose
468 // Automatically add in ws
469 DatasourceEvent * event = new DatasourceEvent();
470 event->eventtype = DatasourceEvent::EVENT_IMPORT_OBJECT;
471 XmedDataObject * dataObject = new XmedDataObject();
472 dataObject->setFieldHandler(*duplicate);
473 event->objectdata = dataObject;
474 emit datasourceSignal(event);
475 // Note that this signal is processed by the WorkspaceController
477 // Tag the item to prevent double import
478 //_studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);
481 void DatasourceController::OnInterpolateField() {
482 // We need a studyEditor updated on the active study
483 _studyEditor->updateActiveStudy();
485 // Get the selected objects in the study (SObject). In case of a
486 // multiple selection, we consider only the first item. At least one
487 // item must be selected.
488 SALOME_StudyEditor::SObjectList * listOfSObject = _studyEditor->getSelectedObjects();
489 if ( listOfSObject->size() > 0 ) {
490 SALOMEDS::SObject_var soField = listOfSObject->at(0);
491 int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
492 // _GBO_ : the dialog should not be modal, so that we can choose a
493 // mesh in the browser. Then we have to emit a signal from the
494 // dialog.accept, connected to a slot of the DatasourceControler
495 _dlgInterpolateField->setFieldId(fieldId);
496 Qt::WindowFlags flags = _dlgInterpolateField->windowFlags();
497 _dlgInterpolateField->setWindowFlags(flags | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
498 _dlgInterpolateField->open();
502 void DatasourceController::OnInterpolateFieldInputValidated() {
503 MEDOP::InterpolationParameters params;
504 params.precision = _dlgInterpolateField->getPrecision();
505 STDLOG("precision = " << params.precision);
506 params.defaultValue = _dlgInterpolateField->getDefaultValue();
507 STDLOG("defaultValue = " << params.defaultValue);
508 params.reverse = _dlgInterpolateField->getReverse();
509 STDLOG("reverse = " << params.reverse);
510 params.intersectionType = _dlgInterpolateField->getIntersectionType().c_str();
511 STDLOG("intersectionType = " << params.intersectionType);
512 params.method = _dlgInterpolateField->getMethod().c_str();
513 STDLOG("method = " << params.method);
514 params.nature = _dlgInterpolateField->getFieldNature().c_str();
515 STDLOG("nature = " << params.nature);
517 int meshId = _dlgInterpolateField->getMeshId();
518 STDLOG("meshId = " << ToString(meshId));
519 int fieldId = _dlgInterpolateField->getFieldId();
520 MEDOP::FieldHandler* fieldHandler = MEDOPFactoryClient::getDataManager()->getFieldHandler(fieldId);
522 // We don't modify the original field but create first a duplicate
523 // MEDOP::FieldHandler* duplicate = MEDOPFactoryClient::getCalculator()->dup(*fieldHandler);
524 //MEDOPFactoryClient::getDataManager()->changeUnderlyingMesh(duplicate->id, meshId);
525 MEDOP::FieldHandler* result = NULL;
527 result = MEDOPFactoryClient::getDataManager()->interpolateField(fieldId, meshId, params);
530 STDLOG("Unable to process field interpolation; please check interpolation parameters");
531 QMessageBox::critical(_salomeModule->getApp()->desktop(),
532 tr("Operation failed"),
533 tr("Unable to process field interpolation; please check interpolation parameters"));
537 // Request once more the duplicate to update the meta-data on this
539 // duplicate = MEDOPFactoryClient::getDataManager()->getFieldHandler(duplicate->id);
542 // WARN: the following is a temporary code for test purpose
543 // Automatically add in ws
544 DatasourceEvent * event = new DatasourceEvent();
545 event->eventtype = DatasourceEvent::EVENT_IMPORT_OBJECT;
546 XmedDataObject * dataObject = new XmedDataObject();
547 dataObject->setFieldHandler(*result);
548 event->objectdata = dataObject;
549 emit datasourceSignal(event);
550 // Note that this signal is processed by the WorkspaceController
552 // // Tag the item to prevent double import
553 // //_studyEditor->setParameterBool(soField,OBJECT_IS_IN_WORKSPACE,true);