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