Salome HOME
e774bbe1295d1eaeb06a3e607d21c5d37ba9951d
[modules/med.git] / src / MEDCalc / gui / MEDWidgetHelper.cxx
1 // Copyright (C) 2016-2023  CEA, EDF
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 "MEDWidgetHelper.hxx"
21 #include "PresentationController.hxx"
22 #include "MEDPresentation.hxx"
23 #include <Basics_Utils.hxx>
24
25 MEDWidgetHelper::MEDWidgetHelper(const PresentationController * presController,
26                                  MEDCALC::MEDPresentationManager_ptr presManager, int presId,
27                                  const std::string & presName,  WidgetPresentationParameters * paramWidget):
28   _presManager(presManager),
29   _presController(presController),
30   _presId(presId),
31   _presName(presName),
32   _paramWidget(paramWidget)
33 {}
34
35 MEDWidgetHelper::~MEDWidgetHelper()
36 {}
37
38 void MEDWidgetHelper::loadParametersFromEngine()
39 {
40   _selectedCompo = _presManager->getPresentationIntProperty(_presId, MEDPresentation::PROP_SELECTED_COMPONENT.c_str());
41   _nbCompos = _presManager->getPresentationIntProperty(_presId, MEDPresentation::PROP_NB_COMPONENTS.c_str());
42   _allCompos.clear();
43   _allCompos.reserve(_nbCompos);
44   for (int i = 0; i < _nbCompos; i++)
45     {
46       std::ostringstream oss;
47       oss << MEDPresentation::PROP_COMPONENT << i;
48       _allCompos.push_back(_presManager->getPresentationStringProperty(_presId, oss.str().c_str()));
49     }
50   _colorMap = static_cast<MEDCALC::ColorMapType>(
51       _presManager->getPresentationIntProperty(_presId, MEDPresentation::PROP_COLOR_MAP.c_str()));
52   _scalarBarRange = static_cast<MEDCALC::ScalarBarRangeType>(
53       _presManager->getPresentationIntProperty(_presId, MEDPresentation::PROP_SCALAR_BAR_RANGE.c_str()));
54
55   _scalarBarVisibility = static_cast<bool>(
56     _presManager->getPresentationIntProperty(_presId, MEDPresentation::PROP_SCALAR_BAR_VISIBILITY.c_str()));
57
58   _scalarBarRangeArray[0] = _presManager->getPresentationDoubleProperty(_presId, MEDPresentation::PROP_SCALAR_BAR_MIN_VALUE.c_str());
59   _scalarBarRangeArray[1] = _presManager->getPresentationDoubleProperty(_presId, MEDPresentation::PROP_SCALAR_BAR_MAX_VALUE.c_str());
60
61   _hideDataOutsideCustomRange = _presManager->getPresentationIntProperty(_presId, MEDPresentation::PROP_HIDE_DATA_OUTSIDE_CUSTOM_RANGE.c_str());
62 }
63
64 void MEDWidgetHelper::updateWidget(bool connect)
65 {
66   // Set presentation name
67   _paramWidget->setPresName(_presName);
68
69   // Show dynamic part of the widget (i.e. everything else than color map and scalar bar range)
70   _paramWidget->toggleWidget(true);
71
72   loadParametersFromEngine();
73
74   // Set properly color map and scalar bar range
75   STDLOG("MEDWidgetHelper::udpateWidget() colorMap is " << _colorMap);
76   _paramWidget->setColorMap(_colorMap);
77   STDLOG("MEDWidgetHelper::udpateWidget() scalarBarRange is " << _scalarBarRange);
78   _paramWidget->setScalarBarRange(_scalarBarRange);
79
80   _paramWidget->setScalarBarVisibility(_scalarBarVisibility);
81
82   _paramWidget->setScalarBarRangeValue(_scalarBarRangeArray[0], _scalarBarRangeArray[1]);
83  
84   _paramWidget->setHideDataOutsideCustomRange(_hideDataOutsideCustomRange);
85
86   if (connect)
87     {
88       QObject::connect( this, SIGNAL(presentationUpdateSignal(const PresentationEvent *)),
89                               _presController, SIGNAL(presentationSignal(const PresentationEvent *)) );
90       QObject::connect( _paramWidget, SIGNAL(comboScalarBarRangeIndexChanged(int)), this, SLOT(onScalarBarRangeChanged(int)) );
91       QObject::connect( _paramWidget, SIGNAL(comboColorMapIndexChanged(int)), this, SLOT(onColorMapChanged(int)) );
92       QObject::connect( _paramWidget, SIGNAL(checkboxScalarBarVisibilityChanged(int)), this, SLOT(onScalarBarVisibilityChanged(int)) );
93       QObject::connect( _paramWidget, SIGNAL(checkboxCustomRangeChanged(int)), this, SLOT(onUseCustomRangeChanged(int)));
94       QObject::connect( _paramWidget, SIGNAL(spinboxCustomRangeChanged(double, double)), this, SLOT(onCustomRangeValueChanged(double, double)));
95       QObject::connect( _paramWidget, SIGNAL(checkboxHideDataOutsideCustomRangeChanged(int)), this, SLOT(onHideDataOutsideCustomRangeChanged(int)));
96   }
97 }
98
99 void MEDWidgetHelper::releaseWidget()
100 {
101   QObject::disconnect( this, SIGNAL(presentationUpdateSignal(const PresentationEvent *)),
102                           _presController, SIGNAL(presentationSignal(const PresentationEvent *)) );
103   QObject::disconnect( _paramWidget, SIGNAL(comboScalarBarRangeIndexChanged(int)), this, SLOT(onScalarBarRangeChanged(int)) );
104   QObject::disconnect( _paramWidget, SIGNAL(comboColorMapIndexChanged(int)), this, SLOT(onColorMapChanged(int)) );
105   QObject::disconnect( _paramWidget, SIGNAL(checkboxScalarBarVisibilityChanged(int)), this, SLOT(onScalarBarVisibilityChanged(int)));
106   QObject::disconnect( _paramWidget, SIGNAL(checkboxCustomRangeChanged(int)), this, SLOT(onUseCustomRangeChanged(int)));
107   QObject::disconnect( _paramWidget, SIGNAL(spinboxCustomRangeChanged(double, double)), this, SLOT(onCustomRangeChanged(double, double)));
108   QObject::disconnect( _paramWidget, SIGNAL(checkboxHideDataOutsideCustomRangeChanged(int)), this, SLOT(onHideDataOutsideCustomRangeChanged(int)));
109
110
111
112   // Reset default for color map and scalar bar range
113   _paramWidget->setColorMap(MEDCALC::COLOR_MAP_DEFAULT);
114   _paramWidget->setScalarBarRange(MEDCALC::SCALAR_BAR_RANGE_DEFAULT);
115 }
116
117 void MEDWidgetHelper::onScalarBarVisibilityChanged(int flag) {
118   STDLOG("MEDWidgetHelper::onScalarBarVisibilityChanged");
119   PresentationEvent* event = new PresentationEvent();
120   event->eventtype = PresentationEvent::EVENT_SCALAR_BAR_VISIBILITY_CHANGED;
121   event->presentationId = _presId;
122   event->anInteger = flag;
123
124   emit presentationUpdateSignal(event); // --> PresentationController::processPresentationEvent
125 }
126
127 void MEDWidgetHelper::onComponentChanged(int idx)
128 {
129   STDLOG("MEDWidgetHelper::onComponentChanged");
130   PresentationEvent* event = new PresentationEvent();
131   event->eventtype = PresentationEvent::EVENT_CHANGE_COMPONENT;
132   event->presentationId = _presId;
133   event->anInteger = idx;
134   event->aString = _paramWidget->getComponent();
135
136   emit presentationUpdateSignal(event); // --> PresentationController::processPresentationEvent
137 }
138
139 void MEDWidgetHelper::onColorMapChanged(int /*idx*/)
140 {
141   STDLOG("MEDWidgetHelper::onColorMapChanged");
142   PresentationEvent* event = new PresentationEvent();
143   event->eventtype = PresentationEvent::EVENT_CHANGE_COLORMAP;
144   event->presentationId = _presId;
145   // The actual selected item in the combo list is retrieved by the workspace controller _getColorMap()
146 //  event->anInteger = idx;
147
148   emit presentationUpdateSignal(event); // --> PresentationController::processPresentationEvent
149 }
150
151 void MEDWidgetHelper::onScalarBarRangeChanged(int idx)
152 {
153   STDLOG("MEDWidgetHelper::onScalarBarRangeChanged");
154   PresentationEvent* event = new PresentationEvent();
155   event->eventtype = PresentationEvent::EVENT_CHANGE_TIME_RANGE;
156   event->presentationId = _presId;
157   event->anInteger = idx;
158   // The actual selected item in the combo list is retrieved by the workspace controller _getScalarBarRange()
159   //event->aString = _paramWidget->getScalarBarRange();
160
161   emit presentationUpdateSignal(event); // --> PresentationController::processPresentationEvent
162 }
163
164 void MEDWidgetHelper::onUseCustomRangeChanged(int flag) {
165   STDLOG("MEDWidgetHelper::onUseCustomRangeChanged");
166   PresentationEvent* event = new PresentationEvent();
167   event->eventtype = PresentationEvent::EVENT_CHANGE_TIME_RANGE;
168   event->presentationId = _presId;
169   event->anInteger = flag ? _paramWidget->getRangeComboBox()->count() : _paramWidget->getRangeComboBox()->currentIndex();
170   event->aDouble1 = _paramWidget->getMixCustomRange();
171   event->aDouble2 = _paramWidget->getMaxCustomRange();
172
173   emit presentationUpdateSignal(event); // --> PresentationController::processPresentationEvent
174 }
175
176 void MEDWidgetHelper::onCustomRangeValueChanged(double min, double max) {
177   STDLOG("MEDWidgetHelper::onCustomRangeChanged");
178   PresentationEvent* event = new PresentationEvent();
179   event->eventtype = PresentationEvent::EVENT_CUSTOM_RANGE_CHANGED;
180   event->presentationId = _presId;
181   event->aDouble1 = min;
182   event->aDouble2 = max;
183
184   emit presentationUpdateSignal(event); // --> PresentationController::processPresentationEvent
185 }
186
187 void MEDWidgetHelper::onHideDataOutsideCustomRangeChanged(int flag) {
188   STDLOG("MEDWidgetHelper::onCustomRangeFlagChanged");
189   PresentationEvent* event = new PresentationEvent();
190   event->eventtype = PresentationEvent::EVENT_HIDE_DATA_OUTSIDE_CUSTOM_RANGE_CHANGED;
191   event->presentationId = _presId;
192   event->anInteger = flag;
193   event->aDouble1 = _paramWidget->getMixCustomRange();
194   event->aDouble2 = _paramWidget->getMaxCustomRange();
195
196   emit presentationUpdateSignal(event); // --> PresentationController::processPresentationEvent
197 }