0027724: Visualization, TKV3d - Null handle check missing in AIS_InteractiveContext...
[occt.git] / src / AIS / AIS_ColorScale.hxx
1 // Created on: 2015-02-03
2 // Copyright (c) 2015 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #ifndef _AIS_ColorScale_HeaderFile
16 #define _AIS_ColorScale_HeaderFile
17
18 #include <AIS_InteractiveObject.hxx>
19 #include <Aspect_TypeOfColorScaleData.hxx>
20 #include <Aspect_TypeOfColorScalePosition.hxx>
21 #include <Aspect_SequenceOfColor.hxx>
22 #include <Standard.hxx>
23 #include <Standard_DefineHandle.hxx>
24 #include <TCollection_ExtendedString.hxx>
25 #include <TColStd_SequenceOfExtendedString.hxx>
26
27 //! Class for drawing a custom color scale.
28 //!
29 //! The color scale consists of rectangular color bar (composed of fixed
30 //! number of color intervals), optional labels, and title.
31 //! The labels can be positioned either at the boundaries of the intervals,
32 //! or at the middle of each interval.
33 //! Colors and labels can be either defined automatically or set by the user.
34 //! Automatic labels are calculated from numerical limits of the scale,
35 //! its type (logarithmic or plain), and formatted by specified format string.
36
37 class AIS_ColorScale : public AIS_InteractiveObject {
38
39 public:
40
41   //! Default constructor.
42   Standard_EXPORT AIS_ColorScale();
43
44   //! Calculate color according passed value; returns true if value is in range or false, if isn't
45   Standard_EXPORT Standard_Boolean FindColor (const Standard_Real theValue, Quantity_Color& theColor) const;
46
47   Standard_EXPORT static Standard_Boolean FindColor (const Standard_Real theValue, const Standard_Real theMin, const Standard_Real theMax, const Standard_Integer theColorsCount, Quantity_Color& theColor);
48
49   //! Returns minimal value of color scale;
50   Standard_EXPORT Standard_Real GetMin() const { return myMin; }
51
52   //! Returns maximal value of color scale;
53   Standard_EXPORT Standard_Real GetMax() const { return myMax; }
54
55   //! Returns minimal and maximal values of color scale;
56   Standard_EXPORT void GetRange (Standard_Real& theMin, Standard_Real& theMax) const;
57
58   //! Returns the type of labels;
59   //! Aspect_TOCSD_AUTO - labels as boundary values for intervals
60   //! Aspect_TOCSD_USER - user specified label is used
61   Standard_EXPORT Aspect_TypeOfColorScaleData GetLabelType() const { return myLabelType; }
62
63   //! Returns the type of colors;
64   //! Aspect_TOCSD_AUTO - value between Red and Blue
65   //! Aspect_TOCSD_USER - user specified color from color map
66   Standard_EXPORT Aspect_TypeOfColorScaleData GetColorType() const { return myColorType; }
67
68   //! Returns the number of color scale intervals;
69   Standard_EXPORT Standard_Integer GetNumberOfIntervals() const { return myInterval; }
70
71   //! Returns the color scale title string;
72   Standard_EXPORT TCollection_ExtendedString GetTitle() const { return myTitle; }
73
74   //! Returns the format for numbers.
75   //! The same like format for function printf().
76   //! Used if GetLabelType() is TOCSD_AUTO;
77   Standard_EXPORT TCollection_AsciiString GetFormat() const { return myFormat; }
78
79   //! Returns the user specified label with index theIndex.
80   //! Index is in range from 1 to GetNumberOfIntervals() or to
81   //! GetNumberOfIntervals() + 1 if IsLabelAtBorder() is true.
82   //! Returns empty string if label not defined.
83   Standard_EXPORT TCollection_ExtendedString GetLabel (const Standard_Integer theIndex) const;
84
85   //! Returns the user specified color from color map with index <anIndex> (starts at 1).
86   //! Returns default color if index is out of range in color map.
87   Standard_EXPORT Quantity_Color GetIntervalColor (const Standard_Integer theIndex) const;
88
89   //! Returns the user specified labels.
90   Standard_EXPORT void GetLabels (TColStd_SequenceOfExtendedString& theLabels) const;
91
92   //! Returns the user specified colors.
93   Standard_EXPORT void GetColors (Aspect_SequenceOfColor& theColors) const;
94
95   //! Returns the position of labels concerning color filled rectangles.
96   Standard_EXPORT Aspect_TypeOfColorScalePosition GetLabelPosition() const { return myLabelPos; }
97
98   //! Returns the position of color scale title.
99   Standard_EXPORT Aspect_TypeOfColorScalePosition GetTitlePosition() const { return myTitlePos; }
100
101   //! Returns true if the labels and colors used in reversed order.
102   Standard_EXPORT Standard_Boolean IsReversed() const { return myReversed; }
103
104   //! Returns true if the labels are placed at border of color intervals.
105   Standard_EXPORT Standard_Boolean IsLabelAtBorder() const { return myAtBorder; }
106
107   //! Returns true if the color scale has logarithmic intervals
108   Standard_Boolean IsLogarithmic() const { return myIsLogarithmic; }
109
110   //! Sets the minimal value of color scale.
111   Standard_EXPORT void SetMin (const Standard_Real theMin);
112
113   //! Sets the maximal value of color scale.
114   Standard_EXPORT void SetMax (const Standard_Real theMax);
115
116   //! Sets the minimal and maximal value of color scale.
117   Standard_EXPORT void SetRange (const Standard_Real theMin, const Standard_Real theMax);
118
119   //! Sets the type of labels.
120   //! Aspect_TOCSD_AUTO - labels as boundary values for intervals
121   //! Aspect_TOCSD_USER - user specified label is used
122   Standard_EXPORT void SetLabelType (const Aspect_TypeOfColorScaleData theType);
123
124   //! Sets the type of colors.
125   //! Aspect_TOCSD_AUTO - value between Red and Blue
126   //! Aspect_TOCSD_USER - user specified color from color map
127   Standard_EXPORT void SetColorType (const Aspect_TypeOfColorScaleData theType);
128
129   //! Sets the number of color scale intervals.
130   Standard_EXPORT void SetNumberOfIntervals (const Standard_Integer theNum);
131
132   //! Sets the color scale title string.
133   Standard_EXPORT void SetTitle (const TCollection_ExtendedString& theTitle);
134
135   //! Sets the color scale auto label format specification.
136   Standard_EXPORT void SetFormat (const TCollection_AsciiString& theFormat);
137
138   //! Sets the color scale label at index.
139   //! Index is in range from 1 to GetNumberOfIntervals() or to
140   //! GetNumberOfIntervals() + 1 if IsLabelAtBorder() is true.
141   Standard_EXPORT void SetLabel (const TCollection_ExtendedString& theLabel, const Standard_Integer anIndex);
142
143   //! Sets the color of the specified interval. 
144   //! Index is in range from 1 to GetNumberOfIntervals().
145   Standard_EXPORT void SetIntervalColor (const Quantity_Color& theColor, const Standard_Integer theIndex);
146
147   //! Sets the color scale labels.
148   //! The length of the sequence should be equal to GetNumberOfIntervals() or to
149   //! GetNumberOfIntervals() + 1 if IsLabelAtBorder() is true.
150   Standard_EXPORT void SetLabels (const TColStd_SequenceOfExtendedString& theSeq);
151
152   //! Sets the color scale colors.
153   //! The length of the sequence should be equal to GetNumberOfIntervals().
154   Standard_EXPORT void SetColors (const Aspect_SequenceOfColor& theSeq);
155
156   //! Sets the color scale labels position relative to color bar.
157   Standard_EXPORT void SetLabelPosition (const Aspect_TypeOfColorScalePosition thePos);
158
159   //! Sets the color scale title position.
160   Standard_EXPORT void SetTitlePosition (const Aspect_TypeOfColorScalePosition thePos);
161
162   //! Sets true if the labels and colors used in reversed order.
163   Standard_EXPORT void SetReversed (const Standard_Boolean theReverse);
164
165   //! Sets true if the labels are placed at border of color intervals (true by default).
166   //! If set to False, labels will be drawn at color intervals rather than at borders.
167   Standard_EXPORT void SetLabelAtBorder (const Standard_Boolean theOn);
168
169   //! Sets true if the color scale has logarithmic intervals.
170   void SetLogarithmic (const Standard_Boolean isLogarithmic) { myIsLogarithmic = isLogarithmic; };
171
172   //! Returns the size of color bar.
173   Standard_EXPORT void GetSize (Standard_Integer& theBreadth, Standard_Integer& theHeight) const;
174
175   //! Returns the breadth of color bar.
176   Standard_EXPORT Standard_Integer GetBreadth() const { return myBreadth; }
177
178   //! Returns the height of color bar.
179   Standard_EXPORT Standard_Integer GetHeight() const { return myHeight; }
180
181   //! Sets the size of color bar.
182   Standard_EXPORT void SetSize (const Standard_Integer theWidth, const Standard_Integer theHeight);
183
184   //! Sets the width of color bar.
185   Standard_EXPORT void SetBreadth (const Standard_Integer theBreadth);
186
187   //! Sets the height of color bar.
188   Standard_EXPORT void SetHeight (const Standard_Integer theHeight);
189
190   //! Returns the position of color scale.
191   Standard_EXPORT void GetPosition (Standard_Real& theX, Standard_Real& theY) const;
192
193   //! Returns the X position of color scale.
194   Standard_EXPORT Standard_Integer GetXPosition() const { return myXPos; }
195
196   //! Returns the height of color scale.
197   Standard_EXPORT Standard_Integer GetYPosition() const { return myYPos; }
198
199   //! Sets the position of color scale.
200   Standard_EXPORT void SetPosition (const Standard_Integer theX, const Standard_Integer theY);
201
202   //! Sets the X position of color scale.
203   Standard_EXPORT void SetXPosition (const Standard_Integer theX);
204
205   //! Sets the Y position of color scale.
206   Standard_EXPORT void SetYPosition (const Standard_Integer theY);
207
208   //! Returns the height of text of color scale.
209   Standard_EXPORT Standard_Integer GetTextHeight() const { return myTextHeight; }
210
211   //! Sets the height of text of color scale.
212   Standard_EXPORT void SetTextHeight (const Standard_Integer theHeight) { myTextHeight = theHeight; }
213
214   //! Returns the width of text.
215   //! @param theText [in] the text of which to calculate width.
216   Standard_EXPORT Standard_Integer TextWidth (const TCollection_ExtendedString& theText) const;
217
218   //! Returns the height of text.
219   //! @param theText [in] the text of which to calculate height.
220   Standard_EXPORT Standard_Integer TextHeight (const TCollection_ExtendedString& theText) const;
221
222   Standard_EXPORT void TextSize (const TCollection_ExtendedString& theText, const Standard_Integer theHeight, Standard_Integer& theWidth, Standard_Integer& theAscent, Standard_Integer& theDescent) const;
223
224
225   DEFINE_STANDARD_RTTIEXT(AIS_ColorScale,AIS_InteractiveObject)
226
227 protected:
228
229   //! Draws a frame.
230   //! @param theX [in] the X coordinate of frame position.
231   //! @param theY [in] the Y coordinate of frame position.
232   //! @param theWidth [in] the width of frame.
233   //! @param theHeight [in] the height of frame.
234   //! @param theColor [in] the color of frame.
235   Standard_EXPORT void drawFrame (const Handle(Prs3d_Presentation)& thePresentation,
236                        const Standard_Integer theX, const Standard_Integer theY,
237                        const Standard_Integer theWidth, const Standard_Integer theHeight,
238                        const Quantity_Color& theColor);
239
240   //! Draws a text.
241   //! @param theText [in] the text to draw.
242   //! @param theX [in] the X coordinate of text position.
243   //! @param theY [in] the Y coordinate of text position.
244   //! @param theColor [in] the color of text.
245   Standard_EXPORT void drawText (const Handle(Prs3d_Presentation)& thePresentation,
246                   const TCollection_ExtendedString& theText,
247                   const Standard_Integer theX, const Standard_Integer theY,
248                   const Quantity_Color& theColor);
249
250 private:
251
252   //! Returns the size of color scale.
253   //! @param theWidth [out] the width of color scale.
254   //! @param theHeight [out] the height of color scale.
255   void SizeHint (Standard_Integer& theWidth, Standard_Integer& theHeight) const;
256
257   void Compute (const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
258                   const Handle(Prs3d_Presentation)& thePresentation,
259                   const Standard_Integer theMode) Standard_OVERRIDE;
260
261   void ComputeSelection (const Handle(SelectMgr_Selection)& /*aSelection*/,
262                            const Standard_Integer /*aMode*/) Standard_OVERRIDE
263   {}
264
265   //! Returns the format of text.
266   TCollection_AsciiString Format() const;
267
268   //! Returns the upper value of given interval, or minimum for theIndex = 0.
269   Standard_Real GetIntervalValue (const Standard_Integer theIndex) const;
270
271   //! Returns the color's hue for the given value in the given interval.
272   //! @param theValue [in] the current value of interval.
273   //! @param theMin [in] the min value of interval.
274   //! @param theMax [in] the max value of interval.
275   static Standard_Integer HueFromValue (const Standard_Integer theValue, const Standard_Integer theMin, const Standard_Integer theMax);
276
277 private:
278
279   Standard_Real myMin;
280   Standard_Real myMax;
281   TCollection_ExtendedString myTitle;
282   TCollection_AsciiString myFormat;
283   Standard_Integer myInterval;
284   Aspect_TypeOfColorScaleData myColorType;
285   Aspect_TypeOfColorScaleData myLabelType;
286   Standard_Boolean myAtBorder;
287   Standard_Boolean myReversed;
288   Standard_Boolean myIsLogarithmic;
289   Aspect_SequenceOfColor myColors;
290   TColStd_SequenceOfExtendedString myLabels;
291   Aspect_TypeOfColorScalePosition myLabelPos;
292   Aspect_TypeOfColorScalePosition myTitlePos;
293   Standard_Integer myXPos;
294   Standard_Integer myYPos;
295   Standard_Integer myBreadth;
296   Standard_Integer myHeight;
297   Standard_Integer myTextHeight;
298   Quantity_Color myBgColor;
299 };
300 #endif