Salome HOME
Merge branch 'V8_0_0_BR'
[modules/med.git] / src / MEDCalc / gui / PresentationController.cxx
1 // Copyright (C) 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 #include "PresentationController.hxx"
21 #include "DatasourceConstants.hxx"
22 #include "MEDModule.hxx"
23 #include "Basics_Utils.hxx"
24 #include "QtxActionGroup.h"
25 #include "QtxActionToolMgr.h"
26 #include "MEDFactoryClient.hxx"
27
28 #include <SalomeApp_Application.h>
29 #include <SalomeApp_Study.h>
30 #include <SalomeApp_DataObject.h>
31
32 #include <SALOMEDS_SObject.hxx>
33 #include <SALOMEDS_Study.hxx>
34
35 #include <SUIT_Desktop.h>
36 #include <SUIT_Session.h>
37 #include <SUIT_ResourceMgr.h>
38 #include <QMessageBox>
39
40 PresentationController::PresentationController(MEDModule* salomeModule)
41 {
42   STDLOG("Creating a PresentationController");
43   _salomeModule = salomeModule;
44   _studyEditor = _salomeModule->getStudyEditor();
45
46   _widgetPresentationParameters = new WidgetPresentationParameters();
47
48   QMainWindow* parent = salomeModule->getApp()->desktop();
49   _dockWidget = new QDockWidget(parent);
50   _dockWidget->setVisible(false);
51   _dockWidget->setWindowTitle(tr("TITLE_PRESENTATION_PARAMETERS"));
52   _dockWidget->setObjectName(tr("TITLE_PRESENTATION_PARAMETERS"));
53   _dockWidget->setFeatures(QDockWidget::AllDockWidgetFeatures);
54   _dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
55   _dockWidget->setWidget(_widgetPresentationParameters);
56   parent->addDockWidget(Qt::LeftDockWidgetArea, _dockWidget);
57   //_dockWidget->show();
58 }
59
60 PresentationController::~PresentationController()
61 {
62   STDLOG("Deleting the PresentationController");
63 }
64
65 std::string
66 PresentationController::_getIconName(const std::string& name)
67 {
68   SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
69   if (!mgr)
70     return name;
71
72   // Read value from preferences and suffix name to select icon theme
73   int theme = mgr->integerValue("MEDCalc", "icons");
74   if (theme == 0) {
75     return name + "_MODERN";
76   } else if (theme == 1) {
77     return name + "_CLASSIC";
78   }
79   return name + "_DEFAULT";
80 }
81
82 void
83 PresentationController::createActions()
84 {
85   STDLOG("Creating PresentationController actions");
86
87   int presentationToolbarId = _salomeModule->createTool("Presentations", "PresentationToolbar");
88   int presentationMenuId = _salomeModule->createMenu(tr("MENU_PRESENTATIONS"), -1, 1);
89
90   // Presentations
91   QString label   = tr("LAB_PRESENTATION_SCALAR_MAP");
92   QString tooltip = tr("TIP_PRESENTATION_SCALAR_MAP");
93   QString icon = tr(_getIconName("ICO_PRESENTATION_SCALAR_MAP").c_str());
94   int actionId;
95   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnVisualizeScalarMap()),icon,tooltip);
96   _salomeModule->createTool(actionId, presentationToolbarId);
97   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
98   _salomeModule->createMenu(actionId, presentationMenuId);
99
100   label   = tr("LAB_PRESENTATION_CONTOUR");
101   tooltip = tr("TIP_PRESENTATION_CONTOUR");
102   icon    = tr(_getIconName("ICO_PRESENTATION_CONTOUR").c_str());
103   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnVisualizeContour()),icon,tooltip);
104   _salomeModule->createTool(actionId, presentationToolbarId);
105   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
106   _salomeModule->createMenu(actionId, presentationMenuId);
107
108   label   = tr("LAB_PRESENTATION_VECTOR_FIELD");
109   tooltip = tr("TIP_PRESENTATION_VECTOR_FIELD");
110   icon    = tr(_getIconName("ICO_PRESENTATION_VECTOR_FIELD").c_str());
111   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnVisualizeVectorField()),icon,tooltip);
112   _salomeModule->createTool(actionId, presentationToolbarId);
113   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
114   _salomeModule->createMenu(actionId, presentationMenuId);
115
116   label   = tr("LAB_PRESENTATION_SLICES");
117   tooltip = tr("TIP_PRESENTATION_SLICES");
118   icon    = tr(_getIconName("ICO_PRESENTATION_SLICES").c_str());
119   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnVisualizeSlices()),icon,tooltip);
120   _salomeModule->createTool(actionId, presentationToolbarId);
121   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
122   _salomeModule->createMenu(actionId, presentationMenuId);
123
124   label   = tr("LAB_PRESENTATION_DEFLECTION_SHAPE");
125   tooltip = tr("TIP_PRESENTATION_DEFLECTION_SHAPE");
126   icon    = tr(_getIconName("ICO_PRESENTATION_DEFLECTION_SHAPE").c_str());
127   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnVisualizeDeflectionShape()),icon,tooltip);
128   _salomeModule->createTool(actionId, presentationToolbarId);
129   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
130   _salomeModule->createMenu(actionId, presentationMenuId);
131
132   label   = tr("LAB_PRESENTATION_POINT_SPRITE");
133   tooltip = tr("TIP_PRESENTATION_POINT_SPRITE");
134   icon    = tr(_getIconName("ICO_PRESENTATION_POINT_SPRITE").c_str());
135   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnVisualizePointSprite()),icon,tooltip);
136   _salomeModule->createTool(actionId, presentationToolbarId);
137   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
138   _salomeModule->createMenu(actionId, presentationMenuId);
139 }
140
141 MEDCALC::MEDPresentationViewMode
142 PresentationController::getSelectedViewMode()
143 {
144   return _widgetPresentationParameters->getViewMode();
145 }
146
147 MEDCALC::MEDPresentationColorMap
148 PresentationController::getSelectedColorMap()
149 {
150   return _widgetPresentationParameters->getColorMap();
151 }
152
153 void
154 PresentationController::visualize(PresentationEvent::EventType eventType)
155 {
156   // We need a _studyEditor updated on the active study
157   _studyEditor->updateActiveStudy();
158
159   // Get the selected objects in the study (SObject)
160   SALOME_StudyEditor::SObjectList* listOfSObject = _studyEditor->getSelectedObjects();
161
162   // For each object, emit a signal to the workspace to request a
163   // visualisation using the tui command (so that the user can see how
164   // to make a view of an object from the tui console).
165   for (int i=0; i<listOfSObject->size(); i++) {
166     SALOMEDS::SObject_var soField = listOfSObject->at(i);
167     int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
168     // If fieldId equals -1, then it means that it is not a field
169     // managed by the MED module, and we stop this function process.
170     if ( fieldId < 0 )
171       continue;
172
173     MEDCALC::FieldHandler* fieldHandler = MEDFactoryClient::getDataManager()->getFieldHandler(fieldId);
174     if (! fieldHandler) {
175       QMessageBox::warning(_salomeModule->getApp()->desktop(),
176          tr("Operation not allowed"),
177          tr("No field is defined"));
178       return;
179     }
180
181     PresentationEvent* event = new PresentationEvent();
182     event->eventtype = eventType;
183     XmedDataObject* dataObject = new XmedDataObject();
184     dataObject->setFieldHandler(*fieldHandler);
185     event->objectdata = dataObject;
186     emit presentationSignal(event); // --> WorkspaceController::processPresentationEvent
187   }
188 }
189
190 void
191 PresentationController::OnVisualizeScalarMap()
192 {
193   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_SCALAR_MAP);
194 }
195
196 void
197 PresentationController::OnVisualizeContour()
198 {
199   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_CONTOUR);
200 }
201
202 void
203 PresentationController::OnVisualizeVectorField()
204 {
205   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_VECTOR_FIELD);
206 }
207
208 void
209 PresentationController::OnVisualizeSlices()
210 {
211   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_SLICES);
212 }
213
214 void
215 PresentationController::OnVisualizeDeflectionShape()
216 {
217   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_DEFLECTION_SHAPE);
218 }
219
220 void
221 PresentationController::OnVisualizePointSprite()
222 {
223   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_POINT_SPRITE);
224 }
225
226 void
227 PresentationController::updateTreeViewWithNewPresentation(long fieldId, long presentationId)
228 {
229   if (presentationId < 0) {
230     std::cerr << "Unknown presentation\n";
231     return;
232   }
233
234   std::string name = MEDFactoryClient::getPresentationManager()->getPresentationProperty(presentationId, "name");
235   std::string icon = std::string("ICO_") + name;
236   icon = _getIconName(icon);
237   name = tr(name.c_str()).toStdString();
238   std::string label = tr(icon.c_str()).toStdString();
239
240   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(_salomeModule->application()->activeStudy());
241   _PTR(Study) studyDS = study->studyDS();
242
243   _salomeModule->engine()->registerPresentation(_CAST(Study, studyDS)->GetStudy(), fieldId, name.c_str(), label.c_str());
244
245   // update Object browser
246   _salomeModule->getApp()->updateObjectBrowser(true);
247 }
248
249 void
250 PresentationController::processWorkspaceEvent(const MEDCALC::MedEvent* event)
251 {
252   if ( event->type == MEDCALC::EVENT_ADD_PRESENTATION ) {
253     this->updateTreeViewWithNewPresentation(event->dataId, event->presentationId);
254   }
255 }
256
257 void
258 PresentationController::showDockWidgets(bool isVisible)
259 {
260   STDLOG("Switching PresentationController visibility to: " << isVisible);
261   _dockWidget->setVisible(isVisible);
262 }