Salome HOME
Update copyrights
[modules/med.git] / src / MEDCalc / gui / MEDModule.cxx
1 // Copyright (C) 2007-2019  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 "MEDModule.hxx"
23 #include "QtHelper.hxx"
24 #include <MEDCalcConstants.hxx>
25
26 #include "SALOME_LifeCycleCORBA.hxx"
27 #include "QtxPopupMgr.h"
28
29 #include <LightApp_Preferences.h>
30 #include <SUIT_Desktop.h>
31 #include <SUIT_ResourceMgr.h>
32 #include <SUIT_Session.h>
33 #include <SUIT_DataBrowser.h>
34 #include <SalomeApp_Study.h>
35 #include <SalomeApp_DataObject.h>
36 #include <SalomeApp_DataModel.h>
37
38 #include <SALOMEconfig.h>
39 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
40 #include <SALOMEDS_SObject.hxx>
41 #include <SALOMEDS_Study.hxx>
42
43 #ifndef DISABLE_PVVIEWER
44 #include <PVViewer_ViewModel.h>
45 #include <PVViewer_GUIElements.h>
46 #endif
47
48 #include "MEDFactoryClient.hxx"
49 #include "MEDPresentationManager_i.hxx"
50
51 #include <QTimer>
52 #include <sstream>
53
54 #include <pqAnimationManager.h>
55 #include <pqPVApplicationCore.h>
56
57 //! The only instance of the reference to engine
58 MED_ORB::MED_Gen_var MEDModule::_MED_engine;
59
60 MEDModule::MEDModule() :
61   SalomeApp_Module("FIELDS"), _studyEditor(0),
62   _datasourceController(0), _workspaceController(0), _presentationController(0),
63   _processingController(0), _pvGuiElements(0)
64 {
65   STDLOG("MEDModule::MEDModule()");
66   // Note also that we can't use the getApp() function here because
67   // the initialize(...) function has not been called yet.
68
69   init(); // internal initialization
70 }
71
72 MEDModule::~MEDModule()
73 {
74   // Clean up engine:
75   STDLOG("MEDModule::~MEDModule(): cleaning up engine side.");
76   _MED_engine->cleanUp();
77   MEDFactoryClient::getFactory()->getPresentationManager()->cleanUp();
78   MEDFactoryClient::getFactory()->getDataManager()->cleanUp();
79
80   if (_studyEditor)
81     delete _studyEditor;
82   if (_datasourceController)
83     delete _datasourceController;
84   //if (_workspaceController)
85   //  delete _workspaceController;
86   if (_presentationController)
87     delete _presentationController;
88   if (_processingController)
89     delete _processingController;
90 }
91
92 MED_ORB::MED_Gen_var
93 MEDModule::engine()
94 {
95   init(); // initialize engine, if necessary
96   return _MED_engine;
97 }
98
99 void
100 MEDModule::init()
101 {
102   // initialize FIELDS module engine (load, if necessary)
103   if ( CORBA::is_nil( _MED_engine ) ) {
104     Engines::EngineComponent_var comp =
105       SalomeApp_Application::lcc()->FindOrLoad_Component( "FactoryServer", "FIELDS" );
106     if (CORBA::is_nil( comp ))
107         STDLOG("Could not FindOrLoad_Component FIELDS");
108     _MED_engine = MED_ORB::MED_Gen::_narrow( comp );
109     if (CORBA::is_nil( _MED_engine ))
110         STDLOG("Could not narrow FIELDS engine");
111   }
112 }
113
114 //void MEDModule::onEventLoopStarted()
115 //{
116 //  if(!getApp()->isMainEventLoopStarted())
117 //    {
118 //      QTimer::singleShot(100, this, SLOT(onEventLoopStarted()));
119 //      return;
120 //    }
121 //}
122
123 void
124 MEDModule::initialize( CAM_Application* app )
125 {
126   STDLOG("MEDModule::initialize()");
127   // call the parent implementation
128   SalomeApp_Module::initialize( app );
129
130   if (! getApp()->objectBrowser())
131     getApp()->getWindow(SalomeApp_Application::WT_ObjectBrowser);
132
133   getApp()->objectBrowser()->setAutoOpenLevel(5);
134
135   if (app && app->desktop()) {
136     connect((QObject*) (getApp()->objectBrowser()->treeView()), SIGNAL(doubleClicked(const QModelIndex&)),
137             this, SLOT(onDblClick(const QModelIndex&)));
138     connect((QObject*) (getApp()->objectBrowser()->treeView()), SIGNAL(clicked(const QModelIndex&)),
139                 this, SLOT(onClick(const QModelIndex&)));
140   }
141
142   // The following initializes the GUI widget and associated actions
143   this->createModuleWidgets();
144   this->createModuleActions();
145
146   // Now that the workspace controller is created, ParaView core application has normally been started,
147   // and hidden GUI elements have been created.  We can fire the VCR toolbar activation:
148   initToolbars();
149 }
150
151 QString
152 MEDModule::engineIOR() const
153 {
154   init(); // initialize engine, if necessary
155   CORBA::String_var anIOR = getApp()->orb()->object_to_string( _MED_engine.in() );
156   return QString( anIOR.in() );
157 }
158
159 QString
160 MEDModule::iconName() const
161 {
162   return tr("ICO_MED_SMALL");
163 }
164
165 void
166 MEDModule::windows( QMap<int, int>& theMap ) const
167 {
168   STDLOG("MEDModule::windows()");
169   // want Object browser, in the left area
170   theMap.insert( SalomeApp_Application::WT_ObjectBrowser,
171                  Qt::LeftDockWidgetArea );
172 #ifndef DISABLE_PYCONSOLE
173   // want Python console, in the bottom area
174   theMap.insert( SalomeApp_Application::WT_PyConsole,
175                  Qt::BottomDockWidgetArea );
176 #endif
177 }
178
179 void
180 MEDModule::viewManagers( QStringList& list ) const
181 {
182 #ifndef DISABLE_PVVIEWER
183   list.append( PVViewer_Viewer::Type() );
184 #endif
185 }
186
187 void
188 MEDModule::createPreferences()
189 {
190   int genTab = addPreference(tr("PREF_TAB_GENERAL"));
191
192   int themeGroup = addPreference(tr("PREF_THEME_GROUP"), genTab);
193   setPreferenceProperty(themeGroup, "columns", 2);
194   int icons = addPreference(tr("PREF_ICONS"), themeGroup, LightApp_Preferences::Selector, "MEDCalc", "icons" );
195   QStringList iconsThemes;
196   iconsThemes.append(tr("PREF_ICON_THEME_MODERN"));
197   iconsThemes.append(tr("PREF_ICON_THEME_CLASSIC"));
198   QList<QVariant> indices;
199   indices.append(0);
200   indices.append(1);
201   setPreferenceProperty(icons, "strings", iconsThemes);
202   setPreferenceProperty(icons, "indexes", indices);
203 }
204
205 bool
206 MEDModule::activateModule( SUIT_Study* theStudy )
207 {
208   STDLOG("MEDModule::activateModule()");
209   if ( CORBA::is_nil( _MED_engine ) )
210     return false;
211
212   // call parent implementation
213   bool bOk = SalomeApp_Module::activateModule( theStudy );
214   if (!bOk)
215     return false;
216
217   // show own menus
218   setMenuShown( true );
219   // show own toolbars
220   setToolShown( true );
221
222   //this->createStudyComponent(theStudy);
223   _workspaceController->showDockWidgets(true);
224   _presentationController->showDockWidgets(true);
225   //this->setDockLayout(StandardApp_Module::DOCKLAYOUT_LEFT_VLARGE);
226
227   // Mark the start of the main event loop - important for test playback:
228 //  QObject::connect(getApp(), SIGNAL(activated(SUIT_Application *)), this, SLOT(onEventLoopStarted(SUIT_Application *)));
229 //  QTimer::singleShot(0, this, SLOT(onEventLoopStarted()));
230
231   // return the activation status
232   return bOk;
233 }
234
235 bool
236 MEDModule::deactivateModule( SUIT_Study* theStudy )
237 {
238   STDLOG("MEDModule::deactivateModule()");
239   _workspaceController->showDockWidgets(false);
240   _presentationController->showDockWidgets(false);
241   //this->unsetDockLayout();
242
243   // hide own menus
244   setMenuShown( false );
245   // hide own toolbars
246   setToolShown( false );
247
248   // call parent implementation and return the activation status
249   return SalomeApp_Module::deactivateModule( theStudy );
250 }
251
252 //
253 // =====================================================================
254 // This part add custom widgets (a dockwidget containing a tree view
255 // in this example) and add special actions in the toolbox of the
256 // module.
257 // =====================================================================
258 //
259
260 /*!
261  * This function implements the interface StandardApp_Module. It
262  * creates the widgets specific for this module, in particular the
263  * workspace widget and the dataspace widget.
264  */
265 void
266 MEDModule::createModuleWidgets() {
267   _studyEditor = new SALOME_AppStudyEditor(getApp());
268   _datasourceController = new DatasourceController(this);
269   _workspaceController = new WorkspaceController(this);
270   _xmedDataModel  = new XmedDataModel();
271   _workspaceController->setDataModel(_xmedDataModel);
272   _presentationController = new PresentationController(this);
273   // ABN: ultimately console driver should be owned by module: everyone needs it, not only WorkspaceController
274   _presentationController->setConsoleDriver(_workspaceController->getConsoleDriver());
275   _processingController = new ProcessingController(this);
276 #ifdef MED_HAS_QTTESTING
277   _testController = new TestController(this);
278 #endif
279
280   connect(_datasourceController, SIGNAL(datasourceSignal(const DatasourceEvent*)),
281     _workspaceController, SLOT(processDatasourceEvent(const DatasourceEvent*)));
282
283   connect(_presentationController, SIGNAL(presentationSignal(const PresentationEvent*)),
284     _presentationController, SLOT(processPresentationEvent(const PresentationEvent*)));
285
286   connect(_processingController, SIGNAL(processingSignal(const ProcessingEvent*)),
287     _workspaceController, SLOT(processProcessingEvent(const ProcessingEvent*)));
288
289   connect(_workspaceController, SIGNAL(workspaceSignal(const MEDCALC::MedEvent*)),
290     _datasourceController, SLOT(processWorkspaceEvent(const MEDCALC::MedEvent*)));
291
292   connect(_workspaceController, SIGNAL(workspaceSignal(const MEDCALC::MedEvent*)),
293     _presentationController, SLOT(processWorkspaceEvent(const MEDCALC::MedEvent*)));
294 }
295
296 void
297 MEDModule::initToolbars()
298 {
299   // VCR and Time toolbars:
300   SUIT_Desktop* dsk = getApp()->desktop();
301   _pvGuiElements = PVViewer_GUIElements::GetInstance(dsk);
302
303   _pvGuiElements->getVCRToolbar();  // make sure VCR toolbar is built
304   _pvGuiElements->setToolBarVisible(false);
305   _pvGuiElements->setVCRTimeToolBarVisible(true);
306
307   // Emit signal in order to make sure that animation scene is set - same trick as in PARAVIS module activation
308   QMetaObject::invokeMethod( pqPVApplicationCore::instance()->animationManager(),
309                              "activeSceneChanged",
310                              Q_ARG( pqAnimationScene*, pqPVApplicationCore::instance()->animationManager()->getActiveScene() ) );
311
312 #ifdef MED_HAS_QTTESTING
313   connect(_workspaceController, SIGNAL(workspaceSignal(const MEDCALC::MedEvent*)),
314     _testController, SLOT(processWorkspaceEvent(const MEDCALC::MedEvent*)));
315 #endif
316 }
317
318 void
319 MEDModule::createModuleActions() {
320   _datasourceController->createActions();
321   _workspaceController->createActions();
322   _presentationController->createActions();
323   _processingController->createActions();
324 #ifdef MED_HAS_QTTESTING
325   _testController->createActions();
326 #endif
327 }
328
329 int
330 MEDModule::createStandardAction(const QString& label,
331                                 QObject* slotobject,
332                                 const char* slotmember,
333                                 const QString& iconName,
334                                 const QString& tooltip)
335 {
336   SUIT_Desktop* dsk = getApp()->desktop();
337   SUIT_ResourceMgr* resMgr = getApp()->resourceMgr();
338
339   // If the tooltip is not defined, we choose instead the label text.
340   QString effToolTip(tooltip);
341   if ( effToolTip.isEmpty() )
342     effToolTip = label;
343
344   QIcon ico;
345   if (iconName.isEmpty())
346     ico = QIcon();
347   else
348     ico = QIcon(resMgr->loadPixmap("FIELDS", iconName));
349
350   QAction* action = createAction(-1,
351                                  label,
352                                  ico,
353                                  label,
354                                  effToolTip,
355                                  0,
356                                  dsk,
357                                  false,
358                                  slotobject,
359                                  slotmember
360                                  );
361   return actionId(action);
362 }
363
364 void
365 MEDModule::addActionInPopupMenu(int actionId,const QString& menus,const QString& rule)
366 {
367   // _GBO_ for a fine customization of the rule (for example with a
368   // test on the type of the selected object), see the LIGHT module:
369   // implement "LightApp_Selection*    createSelection() const;"
370   int parentId = -1;
371   QtxPopupMgr* mgr = this->popupMgr();
372   this->action( actionId )->setIconVisibleInMenu(true);
373   if (! menus.isEmpty())
374     mgr->insert ( this->action( actionId ), menus, parentId, 0 );
375   else
376     mgr->insert ( this->action( actionId ), parentId, 0 );
377   mgr->setRule( this->action( actionId ), rule, QtxPopupMgr::VisibleRule );
378 }
379
380 //MEDCALC::ViewModeType
381 //MEDModule::getSelectedViewMode() const
382 //{
383 //  return _presentationController->getSelectedViewMode();
384 //}
385 //
386 //MEDCALC::ColorMapType
387 //MEDModule::getSelectedColorMap() const
388 //{
389 //  return _presentationController->getSelectedColorMap();
390 //}
391 //
392 //MEDCALC::ScalarBarRangeType
393 //MEDModule::getSelectedScalarBarRange() const
394 //{
395 //  return _presentationController->getSelectedScalarBarRange();
396 //}
397
398
399 /**
400  * Returns presentation name, type and ID from the currently selected presentation in the object
401  * browser.
402  */
403 bool
404 MEDModule::itemClickGeneric(std::string & name, std::string & type, int & presId) const
405 {
406   DataObjectList dol = getApp()->objectBrowser()->getSelected();
407   if (dol.isEmpty())
408     return false;
409   SalomeApp_DataObject* item = dynamic_cast<SalomeApp_DataObject*>(dol[0]);
410   if (!item)
411     return false;
412   SalomeApp_DataModel *model = dynamic_cast<SalomeApp_DataModel*>(dataModel());
413   if (!model)
414     return false;
415
416   if (item->componentDataType().toStdString() != "FIELDS")
417     return false;
418   _PTR(SObject) obj = item->object();
419   _PTR(GenericAttribute) anAttribute;
420
421   if (! obj->FindAttribute(anAttribute, "AttributeName"))
422     return false;
423   _PTR(AttributeName) attrName(anAttribute);
424   name = attrName->Value();
425
426   if (! obj->FindAttribute(anAttribute, "AttributeParameter"))
427     return false;
428   _PTR(AttributeParameter) attrParam(anAttribute);
429   if (! attrParam->IsSet(IS_PRESENTATION, PT_BOOLEAN)
430       || ! attrParam->GetBool(IS_PRESENTATION)) { // Not a presentation
431       return false;
432   }
433 //  if (!attrParam->IsSet(FIELD_ID, PT_INTEGER))
434 //    return false;
435 //  fieldId = attrParam->GetInt(FIELD_ID);
436   if (!attrParam->IsSet(PRESENTATION_ID, PT_INTEGER))
437       return false;
438   presId = attrParam->GetInt(PRESENTATION_ID);
439   if (!attrParam->IsSet(PRESENTATION_TYPE, PT_STRING))
440     return false;
441   type = attrParam->GetString(PRESENTATION_TYPE);
442   return true;
443 }
444
445 void
446 MEDModule::onClick(const QModelIndex & index)
447 {
448   int presId;
449   std::string name, type;
450   if (!itemClickGeneric(name, type, presId))
451     {
452       // Not a presentation - clear widget:
453       emit presentationSelected(-1, QString(""), QString(""));
454       return;
455     }
456
457 //  STDLOG("Presentation selection");
458 //  std::ostringstream oss;
459 //  oss << fieldId << " / " << presId;
460 //  STDLOG("    - Field id / pres id:   " + oss.str());
461 //  STDLOG("    - Presentation type: " + type);
462 //  STDLOG("    - Presentation name: " + name);
463
464   emit presentationSelected(presId, QString::fromStdString(type), QString::fromStdString(name) );  // caught by PresentationController
465 }
466
467 void
468 MEDModule::onDblClick(const QModelIndex& index)
469 {
470   int presId;
471   std::string name, type;
472   if (!itemClickGeneric(name, type, presId))
473     return;
474
475 //  STDLOG("Presentation double click");
476 //  STDLOG("  Presentation infos:");
477 //  STDLOG("    - Component:         " + item->componentDataType().toStdString());
478 //  STDLOG("    - Item entry:        " + item->entry().toStdString());
479 //  STDLOG("    - Item name:         " + item->name().toStdString());
480 //  std::ostringstream oss;
481 //  oss << fieldId;
482 //  STDLOG("    - Field id:          " + oss.str());
483 //  STDLOG("    - Presentation name: " + name);
484 }
485
486 void
487 MEDModule::requestSALOMETermination() const
488 {
489   STDLOG("Requesting SALOME termination!!");
490   SUIT_Session::session()->closeSession( SUIT_Session::DONT_SAVE, 1 );  // killServers = True
491 }
492
493
494 //bool MEDModule::hasMainEventLoopStarted() const
495 //{
496 //  return _eventLoopStarted;
497 //}
498
499 int
500 MEDModule::getIntParamFromStudyEditor(SALOMEDS::SObject_var obj, const char* name)
501 {
502   if (obj->_is_nil())
503     return -1;
504
505   SALOMEDS::GenericAttribute_var anAttr;
506   SALOMEDS::AttributeParameter_var aParam;
507   if ( obj->FindAttribute(anAttr,"AttributeParameter") ) {
508     aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
509     if (aParam->IsSet(name, PT_INTEGER))
510       return aParam->GetInt(name);
511   }
512   return -1;
513 }