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