Debian Codegraph @

src/Gui/SoFCColorBar.h

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 #ifndef GUI_SOFCCOLORBAR_H
24 #define GUI_SOFCCOLORBAR_H
25
26 #include <QElapsedTimer>
27 #include <vector>
28 #include <Inventor/SbVec2s.h>
29 #include <Inventor/nodes/SoSeparator.h>
30
31 #include <App/ColorModel.h>
32 #include <Base/Observer.h>
33
34
35 class SoSwitch;
36 class SoEventCallback;
37 class SbVec2s;
38 class SoHandleEventAction;
39 class SoGLRenderAction;
40
41 namespace Gui
42 {
43 class SoFCColorGradient;
44
45 struct SoLabelTextFormat
46 {
47 // NOLINTBEGIN
48 int textSize = 13;
49 uint32_t textColor = 0xffffffff;
50 // NOLINTEND
51 };
52
53 /**
54 * The abstract color bar base class to get most important information on how to convert a scalar to
55 * an RGB color.
56 * @author Werner Mayer
57 */
58 class GuiExport SoFCColorBarBase: public SoSeparator, public App::ValueFloatToRGB
59 {
60 using inherited = SoSeparator;
61
62 SO_NODE_ABSTRACT_HEADER(Gui::SoFCColorBarBase);
63
64 public:
65 static void initClass();
66 static void finish();
67
68 void GLRenderBelowPath(SoGLRenderAction* action) override;
69
70 /**
71 * Sets the range of the colorbar from the maximum \a fMax to the minimum \a fMin.
72 * \a prec indicates the post decimal positions, \a prec should be in between 0 and 6.
73 *
74 * This method must be implemented in subclasses.
75 */
76 virtual void setRange(float fMin, float fMax, int prec = 3) = 0;
77 /**
78 * Returns the associated color to the value \a fVal.
79 *
80 * This method must be implemented in subclasses.
81 */
82 Base::Color getColor(float fVal) const override = 0;
83 /**
84 * Returns always true if the color bar is in mode to show colors to arbitrary values of \a
85 * fVal, otherwise true is returned if \a fVal is within the specified parameter range, if not
86 * false is returned.
87 *
88 * This method must be implemented in subclasses.
89 */
90 virtual bool isVisible(float fVal) const = 0;
91 /**
92 * Sets whether values outside the range should be in gray,
93 *
94 * This method must be implemented in subclasses.
95 */
96 virtual void setOutsideGrayed(bool bVal) = 0;
97 /** Returns the current minimum of the parameter range.
98 *
99 * This method must be implemented in subclasses.
100 */
101 virtual float getMinValue() const = 0;
102 /** Returns the current maximum of the parameter range.
103 *
104 * This method must be implemented in subclasses.
105 */
106 virtual float getMaxValue() const = 0;
107 /**
108 * Opens a dialog to customize the current settings of the color bar.
109 *
110 * This method must be implemented in subclasses.
111 */
112 virtual void customize(SoFCColorBarBase*) = 0;
113 /**
114 * Forward a triggered change
115 */
116 virtual void triggerChange(SoFCColorBarBase* base)
117 {
118 base->triggerChange(this);
119 }
120 /** Returns the name of the color bar.
121 *
122 * This method must be implemented in subclasses.
123 */
124 virtual const char* getColorBarName() const = 0;
125 /** Sets the format for the label text.
126 */
127 virtual void setFormat(const SoLabelTextFormat& fmt);
128 /** Returns the format for the label text.
129 */
130 SoLabelTextFormat getFormat() const;
131
132 protected:
133 /** Applies the format to the label text */
134 virtual void applyFormat(const SoLabelTextFormat& fmt);
135 /** Computes the dimensions of the color bar and labels in coordinates with
136 * respect to the defined height of the camera.
137 * Returns the width of the bounding box
138 */
139 float getBounds(const SbVec2s& size, float& fMinX, float& fMinY, float& fMaxX, float& fMaxY);
140 /** Returns the width of the color bar and labels
141 *
142 * Computes the occupied width of the color bar and its labels.
143 * It therefore determines the bounding box.
144 */
145 float getBoundingWidth(const SbVec2s& size);
146 /**
147 * Sets the current viewer size to recalculate the new position.
148 *
149 * This method must be implemented in subclasses.
150 */
151 virtual void setViewportSize(const SbVec2s& size) = 0;
152 /**
153 * Mark the object as modified.
154 */
155 void setModified();
156
157 SoFCColorBarBase();
158 ~SoFCColorBarBase() override;
159
160 private:
161 float _boxWidth {-1.0F};
162 SbVec2s _windowSize;
163 SoLabelTextFormat format;
164 };
165
166 // --------------------------------------------------------------------------
167
168 /**
169 * The color bar class that redirects all calls to its handled color bars.
170 * @author Werner Mayer
171 */
172 class GuiExport SoFCColorBar: public SoFCColorBarBase, public Base::Subject<int>
173 {
174 using inherited = SoFCColorBarBase;
175
176 SO_NODE_HEADER(Gui::SoFCColorBar);
177
178 public:
179 static void initClass();
180 static void finish();
181 SoFCColorBar();
182
183 /**
184 * Returns the currently active color bar object.
185 */
186 SoFCColorBarBase* getActiveBar() const;
187 /**
188 * Handles the mouse button events and checks if the user has clicked on the area of the
189 * currently active color bar.
190 */
191 void handleEvent(SoHandleEventAction* action) override;
192 /**
193 * Sets the range of all color bars from the maximum \a fMax to the minimum \a fMin.
194 * \a prec indicates the post decimal positions, \a prec should be in between 0 and 6.
195 */
196 void setRange(float fMin, float fMax, int prec = 3) override;
197 /**
198 * Returns the associated color to the value \a fVal of the currently active color bar.
199 */
200 Base::Color getColor(float fVal) const override;
201 /**
202 * Sets whether values outside the range should be in gray,
203 */
204 void setOutsideGrayed(bool bVal) override;
205 /**
206 * Returns the return value of the currently active color bar.
207 */
208 bool isVisible(float fVal) const override;
209 /**
210 * Returns the current minimum of the parameter range of the currently active color bar.
211 */
212 float getMinValue() const override;
213 /**
214 * Returns the current maximum of the parameter range of the currently active color bar.
215 */
216 float getMaxValue() const override;
217 /**
218 * Customizes the currently active color bar.
219 */
220 void customize(SoFCColorBarBase*) override;
221 /**
222 * Notify observers
223 */
224 void triggerChange(SoFCColorBarBase*) override;
225 /** Returns the name of the color bar.
226 */
227 const char* getColorBarName() const override
228 {
229 return "Color Bar";
230 }
231 void setFormat(const SoLabelTextFormat& fmt) override;
232
233 protected:
234 /**
235 * Sets the current viewer size to all color bars to recalculate their new position.
236 */
237 void setViewportSize(const SbVec2s& size) override;
238
239 ~SoFCColorBar() override;
240
241 private:
242 static void eventCallback(void* userdata, SoEventCallback* node);
243
244 private:
245 QElapsedTimer _timer;
246
247 SoSwitch* pColorMode;
248 std::vector<SoFCColorBarBase*> _colorBars;
249 };
250
251 } // namespace Gui
252
253
254 #endif // GUI_SOFCCOLORBAR_H