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