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