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