1 // Copyright (C) 2007-2016 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 <MEDCalcConstants.hxx>
25 #include <SalomeApp_Application.h>
26 #include <SalomeApp_Study.h>
27 #include <SalomeApp_DataObject.h>
29 #include <SALOME_ListIO.hxx>
30 #include <LightApp_SelectionMgr.h>
32 #include <SALOME_LifeCycleCORBA.hxx>
33 #include <SALOMEDS_SObject.hxx>
34 #include <SALOMEDS_Study.hxx>
36 #include "MEDFactoryClient.hxx"
37 #include "MEDModule.hxx"
38 #include "QtHelper.hxx"
40 #include CORBA_CLIENT_HEADER(SALOMEDS)
41 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
42 #include <SUIT_FileDlg.h>
43 #include <SUIT_Desktop.h>
45 #include <QStringList>
47 #include <QMessageBox>
48 #include <QFileDialog>
50 #include "DlgAlias.hxx"
53 // ==============================================================
54 // Datasource controller
55 // ==============================================================
57 DatasourceController::DatasourceController(MEDModule* salomeModule)
59 STDLOG("Creating a DatasourceController");
60 _salomeModule = salomeModule;
61 _studyEditor = _salomeModule->getStudyEditor();
64 DatasourceController::~DatasourceController() {
65 STDLOG("Deleting the DatasourceController");
68 void DatasourceController::createActions() {
69 int toolbarId = _salomeModule->createTool("Datasource", "DatasourceToolbar");
72 // Main actions (toolbar and menubar)
74 QString label = tr("LAB_ADD_DATA_SOURCE");
75 QString tooltip = tr("TIP_ADD_DATA_SOURCE");
76 QString icon = tr("ICO_DATASOURCE_ADD");
78 actionId = _salomeModule->createStandardAction(label,this, SLOT(OnAddDatasource()),icon,tooltip);
79 _salomeModule->createTool(actionId, toolbarId);
81 // This action has to be placed in the general file menu with the label "Import MED file"
82 int menuId = _salomeModule->createMenu( tr( "MEN_FILE" ), -1, 1 );
83 _salomeModule->action(actionId)->setIconVisibleInMenu(true);
84 _salomeModule->createMenu(actionId, menuId, 10);
86 label = tr("LAB_ADD_IMAGE_SOURCE");
87 tooltip = tr("TIP_ADD_IMAGE_SOURCE");
88 icon = tr("ICO_IMAGE_ADD");
89 actionId = _salomeModule->createStandardAction(label,this, SLOT(OnAddImagesource()),icon,tooltip);
90 _salomeModule->createTool(actionId, toolbarId);
91 _salomeModule->action(actionId)->setIconVisibleInMenu(true);
92 _salomeModule->createMenu(actionId, menuId, 20);
95 // Actions for popup menu only
97 // Expand field timeseries
98 label = tr("LAB_EXPAND_FIELD");
99 icon = tr("ICO_DATASOURCE_EXPAND_FIELD");
100 actionId = _salomeModule->createStandardAction(label,this,SLOT(OnExpandField()),icon);
101 _salomeModule->addActionInPopupMenu(actionId);
104 label = tr("LAB_USE_IN_WORKSPACE");
105 icon = tr("ICO_DATASOURCE_USE");
106 actionId = _salomeModule->createStandardAction(label,this,SLOT(OnUseInWorkspace()),icon);
107 _salomeModule->addActionInPopupMenu(actionId);
112 * This function adds the specified MED file as a datasource in the
113 * dataspace. Technically speaking, the engine loads the
114 * meta-information concerning med data from the file, gives this
115 * information to the GUI, and the GUI creates a tree view of these
116 * data in the study object browser.
118 // This function emits a signal that will be caught by workspace to delegate command (datasource creation) to python console.
120 DatasourceController::addDatasource(const char* filename)
122 DatasourceEvent* event = new DatasourceEvent();
123 event->eventtype = DatasourceEvent::EVENT_ADD_DATASOURCE;
124 event->objectalias = filename;
125 emit datasourceSignal(event); // --> WorkspaceController::processDatasourceEvent()
126 //#ifdef MED_WITH_QTTESTING
127 // _dirtyAddDataSource = true;
128 // while(_dirtyAddDataSource)
129 // QApplication::processEvents();
133 // After above data source creation, python console emits a signal, forwarded by workspace, to update the GUI
135 DatasourceController::updateTreeViewWithNewDatasource(const MEDCALC::DatasourceHandler* datasourceHandler)
137 if (!datasourceHandler) {
141 _salomeModule->engine()->addDatasourceToStudy(*datasourceHandler);
143 // update Object browser
144 _salomeModule->getApp()->updateObjectBrowser(true);
146 //#ifdef MED_WITH_QTTESTING
147 // _dirtyAddDataSource = false;
151 void DatasourceController::OnAddDatasource()
153 // Dialog to get the filename where the input data are read from
155 filter.append(tr("FILE_FILTER_MED"));
157 QString anInitialPath = "";
158 if ( SUIT_FileDlg::getLastVisitedPath().isEmpty() )
159 anInitialPath = QDir::currentPath();
161 // QStringList filenames = SUIT_FileDlg::getOpenFileNames( _salomeModule->getApp()->desktop(),
164 // tr("IMPORT_MED_FIELDS") );
165 // [ABN] the below to be compatible with QtTesting:
166 QStringList filenames = QFileDialog::getOpenFileNames( _salomeModule->getApp()->desktop(),
167 tr("IMPORT_MED_FIELDS"),
169 tr("FILE_FILTER_MED") );
171 if ( filenames.count() <= 0 ) return;
172 for ( QStringList::ConstIterator itFile = filenames.begin();
173 itFile != filenames.end(); ++itFile ) {
174 QString filename = *itFile;
175 this->addDatasource(QCHARSTAR(filename));
176 _salomeModule->updateObjBrowser(true);
180 #include "DlgImageToMed.hxx"
181 void DatasourceController::OnAddImagesource()
184 DlgImageToMed dialog;
185 dialog.setAutoLoaded(true);
186 int choice = dialog.exec();
187 if ( choice == QDialog::Rejected ) {
188 // The user decides to cancel the operation
192 QString imageFilename = dialog.getImageFilepath();
194 QString medFilename = dialog.getMedFilepath();
195 bool autoLoad = dialog.isAutoLoaded();
197 std::string ROOT_DIR(getenv("FIELDS_ROOT_DIR"));
198 std::string command(ROOT_DIR+"/bin/salome/fields/image2med.py");
199 command += " -i "+QS2S(imageFilename);
200 command += " -m "+QS2S(medFilename);
201 int error = system(command.c_str());
203 QMessageBox::critical(_salomeModule->getApp()->desktop(),
204 tr("Operation failed"),
205 tr("The creation of med data from the image file failed"));
210 this->addDatasource(QCHARSTAR(medFilename));
211 _salomeModule->updateObjBrowser(true);
215 DatasourceEvent* event = new DatasourceEvent();
216 event->eventtype = DatasourceEvent::EVENT_ADD_IMAGE_AS_DATASOURCE;
217 event->objectalias = imageFilename;
218 emit datasourceSignal(event); // --> WorkspaceController::processDatasourceEvent()
221 void DatasourceController::OnExpandField()
223 // Get the selected objects in the study (SObject)
224 SALOME_StudyEditor::SObjectList* listOfSObject = _studyEditor->getSelectedObjects();
225 for (int i=0; i<listOfSObject->size(); i++) {
226 SALOMEDS::SObject_var soFieldseries = listOfSObject->at(i);
227 std::string name(_studyEditor->getName(soFieldseries));
228 if (soFieldseries->_is_nil() || name == "MEDCalc")
231 // First retrieve the fieldseries id associated to this study object
232 SALOMEDS::GenericAttribute_var anAttr;
233 SALOMEDS::AttributeParameter_var aParam;
234 if ( soFieldseries->FindAttribute(anAttr,"AttributeParameter") ) {
235 aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
236 if (! aParam->IsSet(FIELD_SERIES_ID, PT_INTEGER))
239 long fieldseriesId = aParam->GetInt(FIELD_SERIES_ID);
240 STDLOG("Expand the field timeseries "<<fieldseriesId);
242 // If fieldseriesId equals -1, then it means that it is not a
243 // fieldseries managed by the MED module, and we stop this
245 if ( fieldseriesId < 0 )
247 // _GBO_ A better correction should be to no display the
248 // contextual menu if the selected object is not conform
250 // Then retrieve the list of fields in this timeseries
251 MEDCALC::FieldHandlerList* fieldHandlerList =
252 MEDFactoryClient::getDataManager()->getFieldListInFieldseries(fieldseriesId);
254 // Finally, create an entry for each of the field
255 for(CORBA::ULong iField=0; iField<fieldHandlerList->length(); iField++) {
256 MEDCALC::FieldHandler fieldHandler = (*fieldHandlerList)[iField];
257 SALOMEDS::SObject_var soField = _studyEditor->newObject(soFieldseries);
258 std::string label("it="); label += ToString(fieldHandler.iteration);
259 _studyEditor->setName(soField,label.c_str());
260 _studyEditor->setParameterInt(soField, FIELD_ID, fieldHandler.id);
261 _studyEditor->setParameterBool(soField,IS_IN_WORKSPACE,false);
264 _salomeModule->updateObjBrowser(true);
267 void DatasourceController::OnUseInWorkspace() {
268 // Get the selected objects in the study (SObject)
269 SALOME_StudyEditor::SObjectList* listOfSObject = _studyEditor->getSelectedObjects();
270 if ( listOfSObject->size() == 1 ) {
271 // In this case we ask the name of the variable for the python
275 // _GBO_ Note that it works only for a single field but the
276 // XmedDataObject will be improved to deal with mesh, timeseries
277 // and single field in a futur version. We suppose here that a
278 // single field has been selected.
281 SALOMEDS::SObject_var soField = listOfSObject->at(0);
282 std::string name(_studyEditor->getName(soField));
283 if (soField->_is_nil() || name == "MEDCalc")
285 SALOMEDS::GenericAttribute_var anAttr;
286 SALOMEDS::AttributeParameter_var aParam;
287 if ( soField->FindAttribute(anAttr,"AttributeParameter") ) {
288 aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
289 if (! aParam->IsSet(IS_IN_WORKSPACE, PT_BOOLEAN))
292 bool isInWorkspace = aParam->GetBool(IS_IN_WORKSPACE);
293 if ( isInWorkspace ) {
294 QMessageBox::warning(_salomeModule->getApp()->desktop(),
295 tr("Operation not allowed"),
296 tr("This field is already defined in the workspace"));
300 if (! aParam->IsSet(FIELD_ID, PT_INTEGER))
302 int fieldId = aParam->GetInt(FIELD_ID);
304 // If fieldId equals -1, then it means that it is not a field
305 // managed by the MED module, and we stop this function process.
307 QMessageBox::warning(_salomeModule->getApp()->desktop(),
308 tr("Operation not allowed"),
309 tr("This element is not a field object"));
313 MEDCALC::FieldHandler* fieldHandler =
314 MEDFactoryClient::getDataManager()->getFieldHandler(fieldId);
316 if (! fieldHandler) {
317 QMessageBox::warning(_salomeModule->getApp()->desktop(),
318 tr("Operation not allowed"),
319 tr("No field is defined"));
323 QString alias(fieldHandler->fieldname);
325 dialog.setAlias(alias);
326 int choice = dialog.exec();
327 if ( choice == QDialog::Rejected ) {
328 // The user decides to cancel the operation
331 alias = dialog.getAlias();
333 DatasourceEvent* event = new DatasourceEvent();
334 event->eventtype = DatasourceEvent::EVENT_USE_OBJECT;
335 XmedDataObject* dataObject = new XmedDataObject();
336 dataObject->setFieldHandler(*fieldHandler);
337 event->objectdata = dataObject;
338 event->objectalias = alias;
339 emit datasourceSignal(event); // --> WorkspaceController::processDatasourceEvent()
340 // Tag the item to prevent double import
341 // _studyEditor->setParameterBool(soField,IS_IN_WORKSPACE,true);
342 // Tag the field as persistent on the server. It means that a
343 // saving of the workspace will save at least this field (maybe it
344 // should be an option?)
345 MEDFactoryClient::getDataManager()->markAsPersistent(fieldId, true);
348 // In this case, we don't ask the user to specify an alias for
349 // each item, we just import the whole set of items.
350 for (int i=0; i<listOfSObject->size(); i++) {
351 SALOMEDS::SObject_var soField = listOfSObject->at(i);
352 if (soField->_is_nil())
354 SALOMEDS::GenericAttribute_var anAttr;
355 SALOMEDS::AttributeParameter_var aParam;
356 if ( soField->FindAttribute(anAttr,"AttributeParameter") ) {
357 aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
358 if (! aParam->IsSet(IS_IN_WORKSPACE, PT_BOOLEAN))
361 bool isInWorkspace = aParam->GetBool(IS_IN_WORKSPACE);
362 if ( !isInWorkspace ) {
363 if (! aParam->IsSet(FIELD_ID, PT_INTEGER))
365 int fieldId = aParam->GetInt(FIELD_ID);
366 MEDCALC::FieldHandler* fieldHandler =
367 MEDFactoryClient::getDataManager()->getFieldHandler(fieldId);
368 DatasourceEvent* event = new DatasourceEvent();
369 event->eventtype = DatasourceEvent::EVENT_IMPORT_OBJECT;
370 XmedDataObject* dataObject = new XmedDataObject();
371 dataObject->setFieldHandler(*fieldHandler);
372 event->objectdata = dataObject;
373 emit datasourceSignal(event); // --> WorkspaceController::processDatasourceEvent()
374 // Note that this signal is processed by the WorkspaceController
376 // Tag the item to prevent double import
377 // _studyEditor->setParameterBool(soField,IS_IN_WORKSPACE,true);
378 // Tag the field as persistent on the server. It means that a
379 // saving of the workspace will save at least this field (maybe it
380 // should be an option?)
381 MEDFactoryClient::getDataManager()->markAsPersistent(fieldId, true);
384 STDLOG("The field "<<_studyEditor->getName(soField)<<
385 " is already defined in the workspace");
392 DatasourceController::processWorkspaceEvent(const MEDCALC::MedEvent* event)
394 if ( event->type == MEDCALC::EVENT_ADD_DATASOURCE ) {
395 MEDCALC::DatasourceHandler* datasourceHandler = MEDFactoryClient::getDataManager()->getDatasourceHandler(event->filename);
396 this->updateTreeViewWithNewDatasource(datasourceHandler);