Debian Codegraph @

src/Gui/SoFCColorBar.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #include <boost/core/ignore_unused.hpp>
25 #include <Inventor/actions/SoGetBoundingBoxAction.h>
26 #include <Inventor/actions/SoGLRenderAction.h>
27 #include <Inventor/events/SoMouseButtonEvent.h>
28 #include <Inventor/nodes/SoEventCallback.h>
29 #include <Inventor/nodes/SoOrthographicCamera.h>
30 #include <Inventor/nodes/SoSwitch.h>
31 #include <QApplication>
32 #include <QMenu>
33
34
35 #include "SoFCColorBar.h"
36 #include "SoFCColorGradient.h"
37 #include "SoFCColorLegend.h"
38
39
40 using namespace Gui;
41
42 SO_NODE_ABSTRACT_SOURCE(SoFCColorBarBase)
43
44 /*!
45 Constructor.
46 */
47 SoFCColorBarBase::SoFCColorBarBase()
48 : _windowSize(0, 0)
49 {
50 SO_NODE_CONSTRUCTOR(SoFCColorBarBase);
51 }
52
53 /*!
54 Destructor.
55 */
56 SoFCColorBarBase::~SoFCColorBarBase() = default;
57
58 // doc from parent
59 void SoFCColorBarBase::initClass()
60 {
61 SO_NODE_INIT_ABSTRACT_CLASS(SoFCColorBarBase, SoSeparator, "Separator");
62 }
63
64 void SoFCColorBarBase::finish()
65 {
66 atexit_cleanup();
67 }
68
69 void SoFCColorBarBase::GLRenderBelowPath(SoGLRenderAction* action)
70 {
71 const SbViewportRegion& vp = action->getViewportRegion();
72 const SbVec2s& size = vp.getWindowSize();
73 if (_windowSize != size) {
74 _windowSize = size;
75 setViewportSize(size);
76 }
77 SoSeparator::GLRenderBelowPath(action);
78 }
79
80 void SoFCColorBarBase::setModified()
81 {
82 _boxWidth = -1.0f;
83 }
84
85 void SoFCColorBarBase::setFormat(const SoLabelTextFormat& fmt)
86 {
87 format = fmt;
88 applyFormat(fmt);
89 }
90
91 SoLabelTextFormat SoFCColorBarBase::getFormat() const
92 {
93 return format;
94 }
95
96 void SoFCColorBarBase::applyFormat(const SoLabelTextFormat& fmt)
97 {
98 boost::ignore_unused(fmt);
99 }
100
101 float SoFCColorBarBase::getBoundingWidth(const SbVec2s& size)
102 {
103 float fRatio = static_cast<float>(size[0]) / static_cast<float>(size[1]);
104 if (fRatio >= 1.0f && _boxWidth >= 0.0f) {
105 return _boxWidth;
106 }
107
108 // These are the same camera settings for front nodes as defined in the 3d view
109 auto cam = new SoOrthographicCamera;
110 cam->position = SbVec3f(0, 0, 5); // the 5 is just a value > 0
111 cam->height = 10; // sets the coordinate range of the screen to [-5, +5]
112 cam->nearDistance = 0;
113 cam->farDistance = 10;
114
115 auto group = new SoGroup();
116 group->ref();
117 group->addChild(cam);
118 group->addChild(this);
119
120 SbViewportRegion vpr(size);
121 SoGetBoundingBoxAction bbact(vpr);
122 bbact.apply(group);
123 SbBox3f box = bbact.getBoundingBox();
124 SbVec3f minPt, maxPt;
125 box.getBounds(minPt, maxPt);
126 group->unref();
127
128 float boxWidth = maxPt[0] - minPt[0];
129 _boxWidth = boxWidth;
130 return boxWidth;
131 }
132
133 float SoFCColorBarBase::getBounds(const SbVec2s& size, float& fMinX, float& fMinY, float& fMaxX, float& fMaxY)
134 {
135 // ratio of window width / height
136 float fRatio = static_cast<float>(size[0]) / static_cast<float>(size[1]);
137
138 // The cam height is set in SoFCColorBarBase::getBoundingWidth to 10.
139 // Therefore the normalized coordinates are in the range [-5, +5] x [-5ratio, +5ratio] if ratio > 1
140 // and [-5ratio, +5ratio] x [-5, +5] if ratio < 1.
141 // We don't want the whole height covered by the color bar (to have e.g space to the axis cross
142 // and the Navigation Cube) thus we take as base 3 or if the height reduces significantly it is 2.5.
143
144 float baseYValue;
145 if (fRatio > 3.0f) {
146 baseYValue = 2.5f;
147 }
148 else {
149 baseYValue = 3.0f;
150 }
151 float barWidth = 0.5f;
152
153 // we want the color bar at the rightmost position, therefore we take 4.95 as base
154 fMinX = 4.95f * fRatio; // must be scaled with the ratio to assure it stays at the right
155
156 fMaxX = fMinX + barWidth;
157 fMinY = -baseYValue - 0.6f; // Extend shortened bar towards axis cross
158 fMaxY = baseYValue; // bar has the height of almost whole window height
159
160 if (fRatio < 1.0f) {
161 // must be adjusted to assure that the size of the bar doesn't shrink
162 fMinX /= fRatio;
163 fMaxX /= fRatio;
164 fMinY = -baseYValue / fRatio;
165 fMaxY = baseYValue / fRatio;
166 }
167
168 // get the bounding box width of the color bar and labels
169 float boxWidth = getBoundingWidth(size);
170 return boxWidth;
171 }
172
173 // --------------------------------------------------------------------------
174
175 namespace Gui
176 {
177 // Proxy class that receives an asynchronous custom event
178 class SoFCColorBarProxyObject: public QObject
179 {
180 public:
181 explicit SoFCColorBarProxyObject(SoFCColorBar* b)
182 : QObject(nullptr)
183 , bar(b)
184 {}
185 ~SoFCColorBarProxyObject() override = default;
186 void customEvent(QEvent*) override
187 {
188 bar->customize(bar->getActiveBar());
189 this->deleteLater();
190 }
191
192 private:
193 SoFCColorBar* bar;
194 };
195 } // namespace Gui
196
197 SO_NODE_SOURCE(SoFCColorBar)
198
199 /*!
200 Constructor.
201 */
202 SoFCColorBar::SoFCColorBar()
203 {
204 SO_NODE_CONSTRUCTOR(SoFCColorBar);
205
206 // SoEventCallback * cb = new SoEventCallback;
207 // cb->addEventCallback(SoMouseButtonEvent::getClassTypeId(), eventCallback, this);
208 // insertChild(cb, 0);
209
210 pColorMode = new SoSwitch;
211 addChild(pColorMode);
212
213 _colorBars.push_back(new SoFCColorGradient);
214 _colorBars.push_back(new SoFCColorLegend);
215
216 for (auto it : _colorBars) {
217 pColorMode->addChild(it);
218 }
219 pColorMode->whichChild = 0;
220 }
221
222 /*!
223 Destructor.
224 */
225 SoFCColorBar::~SoFCColorBar() = default;
226
227 // doc from parent
228 void SoFCColorBar::initClass()
229 {
230 SO_NODE_INIT_CLASS(SoFCColorBar, SoFCColorBarBase, "Separator");
231 }
232
233 void SoFCColorBar::finish()
234 {
235 atexit_cleanup();
236 }
237
238 SoFCColorBarBase* SoFCColorBar::getActiveBar() const
239 {
240 int child = pColorMode->whichChild.getValue();
241 return _colorBars[child];
242 }
243
244 void SoFCColorBar::setFormat(const SoLabelTextFormat& fmt)
245 {
246 for (auto it : _colorBars) {
247 it->setFormat(fmt);
248 }
249 }
250
251 void SoFCColorBar::setViewportSize(const SbVec2s& size)
252 {
253 boost::ignore_unused(size);
254 }
255
256 void SoFCColorBar::setRange(float fMin, float fMax, int prec)
257 {
258 for (auto it : _colorBars) {
259 it->setRange(fMin, fMax, prec);
260 }
261 }
262
263 void SoFCColorBar::setOutsideGrayed(bool bVal)
264 {
265 for (auto it : _colorBars) {
266 it->setOutsideGrayed(bVal);
267 }
268 }
269
270 bool SoFCColorBar::isVisible(float fVal) const
271 {
272 return this->getActiveBar()->isVisible(fVal);
273 }
274
275 float SoFCColorBar::getMinValue() const
276 {
277 return this->getActiveBar()->getMinValue();
278 }
279
280 float SoFCColorBar::getMaxValue() const
281 {
282 return this->getActiveBar()->getMaxValue();
283 }
284
285 void SoFCColorBar::triggerChange(SoFCColorBarBase*)
286 {
287 Notify(0);
288 }
289
290 void SoFCColorBar::customize(SoFCColorBarBase* child)
291 {
292 try {
293 return child->customize(this);
294 }
295 catch (const Base::ValueError& e) {
296 e.reportException();
297 }
298 }
299
300 Base::Color SoFCColorBar::getColor(float fVal) const
301 {
302 return this->getActiveBar()->getColor(fVal);
303 }
304
305 void SoFCColorBar::eventCallback(void* /*userdata*/, SoEventCallback* node)
306 {
307 const SoEvent* event = node->getEvent();
308 if (event->getTypeId().isDerivedFrom(SoMouseButtonEvent::getClassTypeId())) {
309 const auto e = static_cast<const SoMouseButtonEvent*>(event);
310 if ((e->getButton() == SoMouseButtonEvent::BUTTON2)) {
311 if (e->getState() == SoButtonEvent::UP) {
312 // do nothing here
313 }
314 }
315 }
316 }
317
318 void SoFCColorBar::handleEvent(SoHandleEventAction* action)
319 {
320 const SoEvent* event = action->getEvent();
321
322 // check for mouse button events
323 if (event->getTypeId().isDerivedFrom(SoMouseButtonEvent::getClassTypeId())) {
324 const auto e = static_cast<const SoMouseButtonEvent*>(event);
325
326 // check if the cursor is near to the color bar
327 if (!action->getPickedPoint()) {
328 return; // not inside the rectangle
329 }
330
331 // left mouse pressed
332 action->setHandled();
333 if ((e->getButton() == SoMouseButtonEvent::BUTTON1)) {
334 if (e->getState() == SoButtonEvent::DOWN) {
335 // double click event
336 if (!_timer.isValid()) {
337 _timer.start();
338 }
339 else if (_timer.restart() < QApplication::doubleClickInterval()) {
340 QApplication::postEvent(new SoFCColorBarProxyObject(this), new QEvent(QEvent::User));
341 }
342 }
343 }
344 // right mouse pressed
345 else if ((e->getButton() == SoMouseButtonEvent::BUTTON2)) {
346 if (e->getState() == SoButtonEvent::UP) {
347 SoFCColorBarBase* current = getActiveBar();
348 QMenu menu;
349 int i = 0;
350 for (auto it : _colorBars) {
351 QAction* item = menu.addAction(QObject::tr(it->getColorBarName()));
352 item->setCheckable(true);
353 item->setChecked(it == current);
354 item->setData(QVariant(i++));
355 }
356
357 menu.addSeparator();
358 QAction* option = menu.addAction(QObject::tr("Options"));
359 QAction* select = menu.exec(QCursor::pos());
360
361 if (select == option) {
362 QApplication::postEvent(new SoFCColorBarProxyObject(this), new QEvent(QEvent::User));
363 }
364 else if (select) {
365 int id = select->data().toInt();
366 pColorMode->whichChild = id;
367 }
368 }
369 }
370 }
371 }