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