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