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