7a324550 |
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 | #include <AIS_ColorScale.hxx> |
16 | #include <AIS_InteractiveContext.hxx> |
17 | #include <Aspect_TypeOfColorScaleData.hxx> |
18 | #include <Aspect_TypeOfColorScalePosition.hxx> |
19 | #include <Aspect_Window.hxx> |
20 | #include <Geom_Line.hxx> |
21 | #include <GeomAdaptor_Curve.hxx> |
22 | #include <Graphic3d_ArrayOfPolygons.hxx> |
23 | #include <Graphic3d_ArrayOfPolylines.hxx> |
24 | #include <Graphic3d_AspectFillArea3d.hxx> |
25 | #include <Graphic3d_AspectText3d.hxx> |
26 | #include <Graphic3d_GraphicDriver.hxx> |
27 | #include <Graphic3d_ArrayOfTriangles.hxx> |
28 | #include <Prs3d_LineAspect.hxx> |
29 | #include <Prs3d_Root.hxx> |
30 | #include <Prs3d_ShadingAspect.hxx> |
31 | #include <Prs3d_Text.hxx> |
32 | #include <Prs3d_TextAspect.hxx> |
33 | #include <SelectMgr_EntityOwner.hxx> |
34 | #include <SelectMgr_Selection.hxx> |
35 | #include <Select3D_SensitiveBox.hxx> |
36 | #include <Select3D_SensitiveSegment.hxx> |
37 | #include <StdPrs_Curve.hxx> |
38 | #include <V3d_Viewer.hxx> |
39 | #include <V3d_View.hxx> |
40 | |
41 | |
42 | //======================================================================= |
43 | //function : AIS_ColorScale |
44 | //purpose : |
45 | //======================================================================= |
46 | AIS_ColorScale::AIS_ColorScale() : |
47 | myMin (0.0), |
48 | myMax (1.0), |
49 | myTitle (""), |
50 | myFormat ("%.4g"), |
51 | myInterval (10), |
52 | myColorType (Aspect_TOCSD_AUTO), |
53 | myLabelType (Aspect_TOCSD_AUTO), |
54 | myAtBorder (Standard_True), |
55 | myReversed (Standard_False), |
56 | myLabelPos (Aspect_TOCSP_RIGHT), |
57 | myTitlePos (Aspect_TOCSP_CENTER), |
58 | myXPos (0), |
59 | myYPos (0), |
60 | myWidth (0.2), |
61 | myHeight (1), |
62 | myTextHeight(20) |
63 | { |
64 | } |
65 | |
66 | //======================================================================= |
67 | //function : GetRange |
68 | //purpose : |
69 | //======================================================================= |
70 | void AIS_ColorScale::GetRange (Standard_Real& theMin, Standard_Real& theMax) const |
71 | { |
72 | theMin = myMin; |
73 | theMax = myMax; |
74 | } |
75 | |
76 | //======================================================================= |
77 | //function : GetLabel |
78 | //purpose : |
79 | //======================================================================= |
80 | TCollection_ExtendedString AIS_ColorScale::GetLabel (const Standard_Integer theIndex) const |
81 | { |
82 | if (GetLabelType() == Aspect_TOCSD_USER) |
83 | { |
84 | if (theIndex < 0 |
85 | || theIndex >= myLabels.Length()) |
86 | { |
87 | return ""; |
88 | } |
89 | |
90 | return myLabels.Value (theIndex + 1); |
91 | } |
92 | |
93 | const Standard_Real aVal = GetNumber (theIndex); |
94 | const TCollection_AsciiString aFormat = Format(); |
95 | Standard_Character aBuf[1024]; |
96 | sprintf (aBuf, aFormat.ToCString(), aVal); |
97 | return TCollection_ExtendedString (aBuf); |
98 | } |
99 | |
100 | //======================================================================= |
101 | //function : GetColor |
102 | //purpose : |
103 | //======================================================================= |
104 | Quantity_Color AIS_ColorScale::GetColor (const Standard_Integer theIndex) const |
105 | { |
106 | if (GetColorType() == Aspect_TOCSD_USER) |
107 | { |
108 | if (theIndex < 0 |
109 | || theIndex >= myColors.Length()) |
110 | { |
111 | return Quantity_Color(); |
112 | } |
113 | |
114 | return myColors.Value (theIndex + 1); |
115 | } |
116 | return Quantity_Color (HueFromValue (theIndex, 0, GetNumberOfIntervals() - 1), 1.0, 1.0, Quantity_TOC_HLS); |
117 | } |
118 | |
119 | //======================================================================= |
120 | //function : GetLabels |
121 | //purpose : |
122 | //======================================================================= |
123 | void AIS_ColorScale::GetLabels (TColStd_SequenceOfExtendedString& theLabels) const |
124 | { |
125 | theLabels.Clear(); |
126 | for (Standard_Integer i = 1; i <= myLabels.Length(); i++) |
127 | theLabels.Append (myLabels.Value (i)); |
128 | } |
129 | |
130 | //======================================================================= |
131 | //function : GetColors |
132 | //purpose : |
133 | //======================================================================= |
134 | void AIS_ColorScale::GetColors (Aspect_SequenceOfColor& theColors) const |
135 | { |
136 | theColors.Clear(); |
137 | for (Standard_Integer i = 1; i <= myColors.Length(); i++) |
138 | theColors.Append (myColors.Value (i)); |
139 | } |
140 | |
141 | //======================================================================= |
142 | //function : SetMin |
143 | //purpose : |
144 | //======================================================================= |
145 | void AIS_ColorScale::SetMin (const Standard_Real theMin) |
146 | { |
147 | SetRange (theMin, GetMax()); |
148 | } |
149 | |
150 | //======================================================================= |
151 | //function : SetMax |
152 | //purpose : |
153 | //======================================================================= |
154 | void AIS_ColorScale::SetMax (const Standard_Real theMax) |
155 | { |
156 | SetRange (GetMin(), theMax); |
157 | } |
158 | |
159 | //======================================================================= |
160 | //function : SetRange |
161 | //purpose : |
162 | //======================================================================= |
163 | void AIS_ColorScale::SetRange (const Standard_Real theMin, const Standard_Real theMax) |
164 | { |
165 | if (myMin == theMin && myMax == theMax) |
166 | return; |
167 | |
168 | myMin = Min (theMin, theMax); |
169 | myMax = Max (theMin, theMax); |
170 | } |
171 | |
172 | //======================================================================= |
173 | //function : SetLabelType |
174 | //purpose : |
175 | //======================================================================= |
176 | void AIS_ColorScale::SetLabelType (const Aspect_TypeOfColorScaleData theType) |
177 | { |
178 | if (myLabelType == theType) |
179 | return; |
180 | |
181 | myLabelType = theType; |
182 | } |
183 | |
184 | //======================================================================= |
185 | //function : SetColorType |
186 | //purpose : |
187 | //======================================================================= |
188 | void AIS_ColorScale::SetColorType (const Aspect_TypeOfColorScaleData theType) |
189 | { |
190 | if (myColorType == theType) |
191 | return; |
192 | |
193 | myColorType = theType; |
194 | } |
195 | |
196 | //======================================================================= |
197 | //function : SetNumberOfIntervals |
198 | //purpose : |
199 | //======================================================================= |
200 | void AIS_ColorScale::SetNumberOfIntervals (const Standard_Integer theNum) |
201 | { |
202 | if (myInterval == theNum || theNum < 1) |
203 | return; |
204 | |
205 | myInterval = theNum; |
206 | } |
207 | |
208 | //======================================================================= |
209 | //function : SetTitle |
210 | //purpose : |
211 | //======================================================================= |
212 | void AIS_ColorScale::SetTitle (const TCollection_ExtendedString& theTitle) |
213 | { |
214 | if (myTitle == theTitle) |
215 | return; |
216 | |
217 | myTitle = theTitle; |
218 | } |
219 | |
220 | //======================================================================= |
221 | //function : SetFormat |
222 | //purpose : |
223 | //======================================================================= |
224 | void AIS_ColorScale::SetFormat (const TCollection_AsciiString& theFormat) |
225 | { |
226 | if (myFormat == theFormat) |
227 | return; |
228 | |
229 | myFormat = theFormat; |
230 | } |
231 | |
232 | //======================================================================= |
233 | //function : SetLabel |
234 | //purpose : |
235 | //======================================================================= |
236 | void AIS_ColorScale::SetLabel (const TCollection_ExtendedString& theLabel, const Standard_Integer theIndex) |
237 | { |
238 | Standard_Integer i = theIndex < 0 ? myLabels.Length() + 1 : theIndex + 1; |
239 | if (i <= myLabels.Length()) |
240 | { |
241 | myLabels.SetValue (i, theLabel); |
242 | } |
243 | else |
244 | { |
245 | while (i > myLabels.Length()) |
246 | myLabels.Append (TCollection_ExtendedString()); |
247 | myLabels.SetValue (i, theLabel); |
248 | } |
249 | } |
250 | |
251 | //======================================================================= |
252 | //function : SetColor |
253 | //purpose : |
254 | //======================================================================= |
255 | void AIS_ColorScale::SetColor (const Quantity_Color& theColor, const Standard_Integer theIndex) |
256 | { |
257 | Standard_Integer i = theIndex < 0 ? myColors.Length() + 1 : theIndex + 1; |
258 | if (i <= myColors.Length()) |
259 | { |
260 | myColors.SetValue (i, theColor); |
261 | } |
262 | else |
263 | { |
264 | while (i > myColors.Length()) |
265 | myColors.Append (Quantity_Color()); |
266 | myColors.SetValue (i, theColor); |
267 | } |
268 | } |
269 | |
270 | //======================================================================= |
271 | //function : SetLabels |
272 | //purpose : |
273 | //======================================================================= |
274 | void AIS_ColorScale::SetLabels (const TColStd_SequenceOfExtendedString& theSeq) |
275 | { |
276 | myLabels.Clear(); |
277 | for (Standard_Integer i = 1; i <= theSeq.Length(); i++) |
278 | myLabels.Append (theSeq.Value (i)); |
279 | } |
280 | |
281 | //======================================================================= |
282 | //function : SetColors |
283 | //purpose : |
284 | //======================================================================= |
285 | void AIS_ColorScale::SetColors (const Aspect_SequenceOfColor& theSeq) |
286 | { |
287 | myColors.Clear(); |
288 | for (Standard_Integer i = 1; i <= theSeq.Length(); i++) |
289 | myColors.Append (theSeq.Value (i)); |
290 | } |
291 | |
292 | //======================================================================= |
293 | //function : SetLabelPosition |
294 | //purpose : |
295 | //======================================================================= |
296 | void AIS_ColorScale::SetLabelPosition (const Aspect_TypeOfColorScalePosition thePos) |
297 | { |
298 | if (myLabelPos == thePos) |
299 | return; |
300 | |
301 | myLabelPos = thePos; |
302 | } |
303 | |
304 | //======================================================================= |
305 | //function : SetTitlePosition |
306 | //purpose : |
307 | //======================================================================= |
308 | void AIS_ColorScale::SetTitlePosition (const Aspect_TypeOfColorScalePosition thePos) |
309 | { |
310 | if (myTitlePos == thePos) |
311 | return; |
312 | |
313 | myTitlePos = thePos; |
314 | } |
315 | |
316 | //======================================================================= |
317 | //function : SetReversed |
318 | //purpose : |
319 | //======================================================================= |
320 | void AIS_ColorScale::SetReversed (const Standard_Boolean theReverse) |
321 | { |
322 | if (myReversed == theReverse) |
323 | return; |
324 | |
325 | myReversed = theReverse; |
326 | } |
327 | |
328 | //======================================================================= |
329 | //function : SetLabelAtBorder |
330 | //purpose : |
331 | //======================================================================= |
332 | void AIS_ColorScale::SetLabelAtBorder (const Standard_Boolean theOn) |
333 | { |
334 | if (myAtBorder == theOn) |
335 | return; |
336 | |
337 | myAtBorder = theOn; |
338 | } |
339 | |
340 | //======================================================================= |
341 | //function : GetPosition |
342 | //purpose : |
343 | //======================================================================= |
344 | void AIS_ColorScale::GetPosition (Standard_Real& theX, Standard_Real& theY) const |
345 | { |
346 | theX = myXPos; |
347 | theY = myYPos; |
348 | } |
349 | |
350 | //======================================================================= |
351 | //function : SetPosition |
352 | //purpose : |
353 | //======================================================================= |
354 | void AIS_ColorScale::SetPosition (const Standard_Real theX, const Standard_Real theY) |
355 | { |
356 | if (myXPos == theX && myYPos == theY) |
357 | return; |
358 | |
359 | myXPos = theX; |
360 | myYPos = theY; |
361 | } |
362 | |
363 | //======================================================================= |
364 | //function : SetXPosition |
365 | //purpose : |
366 | //======================================================================= |
367 | void AIS_ColorScale::SetXPosition (const Standard_Real theX) |
368 | { |
369 | SetPosition (theX, GetYPosition()); |
370 | } |
371 | |
372 | //======================================================================= |
373 | //function : SetYPosition |
374 | //purpose : |
375 | //======================================================================= |
376 | void AIS_ColorScale::SetYPosition (const Standard_Real theY) |
377 | { |
378 | SetPosition (GetXPosition(), theY); |
379 | } |
380 | |
381 | //======================================================================= |
382 | //function : GetSize |
383 | //purpose : |
384 | //======================================================================= |
385 | void AIS_ColorScale::GetSize (Standard_Real& theWidth, Standard_Real& theHeight) const |
386 | { |
387 | theWidth = myWidth; |
388 | theHeight = myHeight; |
389 | } |
390 | |
391 | //======================================================================= |
392 | //function : SetSize |
393 | //purpose : |
394 | //======================================================================= |
395 | void AIS_ColorScale::SetSize (const Standard_Real theWidth, const Standard_Real theHeight) |
396 | { |
397 | if (myWidth == theWidth && myHeight == theHeight) |
398 | return; |
399 | |
400 | myWidth = theWidth; |
401 | myHeight = theHeight; |
402 | } |
403 | |
404 | //======================================================================= |
405 | //function : SetWidth |
406 | //purpose : |
407 | //======================================================================= |
408 | void AIS_ColorScale::SetWidth (const Standard_Real theWidth) |
409 | { |
410 | SetSize (theWidth, GetHeight()); |
411 | } |
412 | |
413 | //======================================================================= |
414 | //function : SetHeight |
415 | //purpose : |
416 | //======================================================================= |
417 | void AIS_ColorScale::SetHeight (const Standard_Real theHeight) |
418 | { |
419 | SetSize (GetWidth(), theHeight); |
420 | } |
421 | |
422 | //======================================================================= |
423 | //function : SizeHint |
424 | //purpose : |
425 | //======================================================================= |
426 | void AIS_ColorScale::SizeHint (Standard_Integer& theWidth, Standard_Integer& theHeight) const |
427 | { |
428 | Standard_Integer aNum = GetNumberOfIntervals(); |
429 | |
430 | Standard_Integer aSpacer = 5; |
431 | Standard_Integer aTextWidth = 0; |
432 | Standard_Integer aTextHeight = TextHeight (""); |
433 | Standard_Integer aColorWidth = 20; |
434 | |
435 | if (GetLabelPosition() != Aspect_TOCSP_NONE) |
436 | for (Standard_Integer idx = 0; idx < aNum; idx++) |
437 | aTextWidth = Max (aTextWidth, TextWidth (GetLabel (idx + 1))); |
438 | |
439 | Standard_Integer aScaleWidth = 0; |
440 | Standard_Integer aScaleHeight = 0; |
441 | |
442 | Standard_Integer aTitleWidth = 0; |
443 | Standard_Integer aTitleHeight = 0; |
444 | |
445 | if (IsLabelAtBorder()) |
446 | { |
447 | aNum++; |
448 | if (GetTitle().Length()) |
449 | aTitleHeight += 10; |
450 | } |
451 | |
452 | aScaleWidth = aColorWidth + aTextWidth + ( aTextWidth ? 3 : 2 ) * aSpacer; |
453 | aScaleHeight = (Standard_Integer)( 1.5 * ( aNum + 1 ) * aTextHeight ); |
454 | |
455 | if (GetTitle().Length()) |
456 | { |
457 | aTitleHeight = TextHeight (GetTitle()) + aSpacer; |
458 | aTitleWidth = TextWidth (GetTitle()) + 10; |
459 | } |
460 | |
461 | theWidth = Max (aTitleWidth, aScaleWidth); |
462 | theHeight = aScaleHeight + aTitleHeight; |
463 | } |
464 | |
465 | //======================================================================= |
466 | //function : Format |
467 | //purpose : |
468 | //======================================================================= |
469 | TCollection_AsciiString AIS_ColorScale::Format() const |
470 | { |
471 | return GetFormat(); |
472 | } |
473 | |
474 | //======================================================================= |
475 | //function : GetNumber |
476 | //purpose : |
477 | //======================================================================= |
478 | Standard_Real AIS_ColorScale::GetNumber (const Standard_Integer theIndex) const |
479 | { |
480 | Standard_Real aNum = 0; |
481 | if (GetNumberOfIntervals() > 0) |
482 | aNum = GetMin() + theIndex * ( Abs (GetMax() - GetMin()) / GetNumberOfIntervals() ); |
483 | return aNum; |
484 | } |
485 | |
486 | //======================================================================= |
487 | //function : HueFromValue |
488 | //purpose : |
489 | //======================================================================= |
490 | Standard_Integer AIS_ColorScale::HueFromValue (const Standard_Integer theValue, |
491 | const Standard_Integer theMin, const Standard_Integer theMax) |
492 | { |
493 | Standard_Integer aMinLimit (0), aMaxLimit (230); |
494 | |
495 | Standard_Integer aHue = aMaxLimit; |
496 | if (theMin != theMax) |
497 | aHue = (Standard_Integer)( aMaxLimit - ( aMaxLimit - aMinLimit ) * ( theValue - theMin ) / ( theMax - theMin ) ); |
498 | |
499 | aHue = Min (Max (aMinLimit, aHue), aMaxLimit); |
500 | |
501 | return aHue; |
502 | } |
503 | |
504 | //======================================================================= |
505 | //function : FindColor |
506 | //purpose : |
507 | //======================================================================= |
508 | Standard_Boolean AIS_ColorScale::FindColor (const Standard_Real theValue, |
509 | Quantity_Color& theColor) const |
510 | { |
511 | return FindColor (theValue, myMin, myMax, myInterval, theColor); |
512 | } |
513 | |
514 | //======================================================================= |
515 | //function : FindColor |
516 | //purpose : |
517 | //======================================================================= |
518 | Standard_Boolean AIS_ColorScale::FindColor (const Standard_Real theValue, |
519 | const Standard_Real theMin, |
520 | const Standard_Real theMax, |
521 | const Standard_Integer theColorsCount, |
522 | Quantity_Color& theColor) |
523 | { |
524 | if (theValue < theMin || theValue > theMax || theMax < theMin) |
525 | return Standard_False; |
526 | |
527 | else |
528 | { |
529 | Standard_Real anIntervNumber = 0; |
530 | if(Abs (theMax-theMin) > Precision::Approximation()) |
531 | anIntervNumber = Floor (Standard_Real (theColorsCount) * ( theValue - theMin ) / ( theMax - theMin )); |
532 | |
533 | Standard_Integer anInterv = Standard_Integer (anIntervNumber); |
534 | |
535 | theColor = Quantity_Color (HueFromValue (anInterv, 0, theColorsCount - 1), 1.0, 1.0, Quantity_TOC_HLS); |
536 | |
537 | return Standard_True; |
538 | } |
539 | } |
540 | |
541 | //======================================================================= |
542 | //function : Compute |
543 | //purpose : |
544 | //======================================================================= |
545 | void AIS_ColorScale::Compute(const Handle(PrsMgr_PresentationManager3d)& /*thePresentationManager*/, |
546 | const Handle(Prs3d_Presentation)& thePresentation, |
547 | const Standard_Integer /*theMode*/) |
548 | { |
549 | Standard_Integer aWinWidth(0), aWinHeight(0); |
550 | Handle(V3d_Viewer) aViewer= GetContext()->CurrentViewer(); |
551 | aViewer->InitActiveViews(); |
552 | aViewer->ActiveView()->Window()->Size (aWinWidth, aWinHeight); |
553 | Quantity_Color aBgColor = aViewer->ActiveView()->BackgroundColor(); |
554 | Standard_Integer aNum = GetNumberOfIntervals(); |
555 | Aspect_TypeOfColorScalePosition aLabPos = GetLabelPosition(); |
556 | |
557 | Standard_Integer aSpacer = 5; |
558 | Standard_Integer aTextWidth = 0; |
559 | Standard_Integer aTextHeight = myTextHeight; |
560 | Standard_Boolean toDrawLabel = GetLabelPosition() != Aspect_TOCSP_NONE; |
561 | TCollection_ExtendedString aTitle = GetTitle(); |
562 | Standard_Integer aTitleHeight = aSpacer; |
563 | Standard_Integer aGray = (Standard_Integer)(255 * ( aBgColor.Red() * 11 + aBgColor.Green() * 16 + aBgColor.Blue() * 5 ) / 32); |
564 | Quantity_Color aFgColor (aGray < 128 ? Quantity_NOC_WHITE : Quantity_NOC_BLACK); |
565 | |
566 | // Draw title |
567 | if (aTitle.Length()) |
568 | { |
569 | aTitleHeight += myTextHeight + aSpacer; |
570 | DrawText (thePresentation, aTitle, (Standard_Integer)myXPos + aSpacer, aWinHeight - ((Standard_Integer)myYPos - 2 * aSpacer + aTitleHeight), aFgColor); |
571 | } |
572 | |
573 | Standard_Boolean toReverse = IsReversed(); |
574 | |
575 | Aspect_SequenceOfColor aColors; |
576 | TColStd_SequenceOfExtendedString aLabels; |
577 | for (Standard_Integer i = 0; i < aNum; i++) |
578 | { |
579 | if (toReverse) |
580 | { |
581 | aColors.Prepend (GetColor (i)); |
582 | aLabels.Prepend (GetLabel (i)); |
583 | } |
584 | else |
585 | { |
586 | aColors.Append (GetColor (i)); |
587 | aLabels.Append (GetLabel (i)); |
588 | } |
589 | } |
590 | |
591 | if (IsLabelAtBorder()) |
592 | { |
593 | if (toReverse) |
594 | aLabels.Prepend (GetLabel (aNum)); |
595 | else |
596 | aLabels.Append (GetLabel (aNum)); |
597 | } |
598 | |
599 | if (toDrawLabel) |
600 | for (Standard_Integer i = 1; i <= aLabels.Length(); i++) |
601 | aTextWidth = Max (aTextWidth, TextWidth (aLabels.Value (i))); |
602 | |
603 | Standard_Integer aLabCount = aLabels.Length(); |
604 | |
605 | Standard_Real aSpc = ( aWinHeight - ( ( Min (aLabCount, 2) + Abs (aLabCount - aNum - 1) ) * aTextHeight ) - aTitleHeight ); |
606 | Standard_Real aVal = aSpc != 0 ? 1.0 * ( aLabCount - Min (aLabCount, 0) ) * aTextHeight / aSpc : 0; |
607 | Standard_Real anIPart; |
608 | Standard_Real anFPart = modf (aVal, &anIPart); |
609 | Standard_Integer aFilter = (Standard_Integer)anIPart + ( anFPart != 0 ? 1 : 0 ); |
610 | |
611 | Standard_Real aStep = 1.0 * ( aWinHeight - (aLabCount - aNum + Abs (aLabCount - aNum - 1)) * aTextHeight - aTitleHeight ) / aNum; |
612 | |
613 | Standard_Integer anAscent = 0; |
614 | Standard_Integer aColorWidth = Max (5, Min (20, aWinWidth - aTextWidth - 3 * aSpacer)); |
615 | if (aLabPos == Aspect_TOCSP_CENTER || !toDrawLabel) |
616 | aColorWidth = aWinWidth - 2 * aSpacer; |
617 | |
618 | // Draw colors |
619 | Standard_Integer aX = (Standard_Integer)myXPos + aSpacer; |
620 | if (aLabPos == Aspect_TOCSP_LEFT) |
621 | aX += aTextWidth + ( aTextWidth ? 1 : 0 ) * aSpacer; |
622 | |
623 | Standard_Real anOffset = 1.0 * aTextHeight / 2 * ( aLabCount - aNum + Abs (aLabCount - aNum - 1) ); |
624 | anOffset += 2*aSpacer; |
625 | Handle (Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (thePresentation); |
626 | Handle (Graphic3d_ArrayOfTriangles) aPrim; |
627 | Standard_Integer anEdgesPerColor = 6; |
628 | Standard_Integer aVerticiesPerColor = 4; |
629 | aPrim = new Graphic3d_ArrayOfTriangles (aColors.Length()*aVerticiesPerColor, aColors.Length()*anEdgesPerColor, 0, 1); |
630 | Standard_Integer aVertIndex = 1; |
631 | for (Standard_Integer i = 1; i <= aColors.Length() && aStep > 0; i++) |
632 | { |
633 | Standard_Integer aY = (Standard_Integer)( myYPos + ( i - 1 )* aStep + anOffset ); |
634 | Standard_Integer aColorHeight = (Standard_Integer)( myYPos + ( i ) * aStep + anOffset ) - aY; |
635 | aPrim->AddVertex (gp_Pnt (aX, aY, 0.0), aColors.Value( i )); |
636 | aPrim->AddVertex (gp_Pnt (aX+aColorWidth, aY, 0.0), aColors.Value( i )); |
637 | aPrim->AddVertex (gp_Pnt (aX, aY+aColorHeight, 0.0), aColors.Value( i )); |
638 | aPrim->AddVertex (gp_Pnt (aX+aColorWidth, aY+aColorHeight, 0.0), aColors.Value( i )); |
639 | aPrim->AddEdge(aVertIndex); |
640 | aPrim->AddEdge(aVertIndex+1); |
641 | aPrim->AddEdge(aVertIndex+2); |
642 | aPrim->AddEdge(aVertIndex+1); |
643 | aPrim->AddEdge(aVertIndex+2); |
644 | aPrim->AddEdge(aVertIndex+3); |
645 | aVertIndex += 4; |
646 | } |
647 | aGroup->AddPrimitiveArray (aPrim); |
648 | |
649 | if (aStep > 0) |
650 | DrawFrame (thePresentation, aX - 1, (Standard_Integer)(myYPos + anOffset - 1), aColorWidth + 2, (Standard_Integer)(aColors.Length() * aStep + 2), aFgColor); |
651 | |
652 | // Draw Labels |
653 | anOffset = 1.0 * Abs (aLabCount - aNum - 1) * ( aStep - aTextHeight ) / 2 + 1.0 * Abs (aLabCount - aNum - 1) * aTextHeight / 2; |
654 | anOffset += 2*aSpacer; |
655 | if (toDrawLabel && aLabels.Length() && aFilter > 0) |
656 | { |
657 | Standard_Integer i1 = 0; |
658 | Standard_Integer i2 = aLabCount - 1; |
659 | Standard_Integer aLast1( i1 ), aLast2( i2 ); |
660 | aX = (Standard_Integer)myXPos + aSpacer; |
661 | switch (aLabPos) |
662 | { |
663 | case Aspect_TOCSP_NONE: |
664 | case Aspect_TOCSP_LEFT: |
665 | break; |
666 | case Aspect_TOCSP_CENTER: |
667 | aX += ( aColorWidth - aTextWidth ) / 2; |
668 | break; |
669 | case Aspect_TOCSP_RIGHT: |
670 | aX += aColorWidth + aSpacer; |
671 | break; |
672 | } |
673 | while (i2 - i1 >= aFilter || ( i2 == 0 && i1 == 0 )) |
674 | { |
675 | Standard_Integer aPos1 = i1; |
676 | Standard_Integer aPos2 = aLabCount - 1 - i2; |
677 | if (aFilter && !( aPos1 % aFilter )) |
678 | { |
679 | DrawText (thePresentation, aLabels.Value (i1 + 1), aX, (Standard_Integer)( myYPos + i1 * aStep + anAscent + anOffset ), aFgColor); |
680 | aLast1 = i1; |
681 | } |
682 | if (aFilter && !( aPos2 % aFilter )) |
683 | { |
684 | DrawText (thePresentation, aLabels.Value (i2 + 1), aX, (Standard_Integer)( myYPos + i2 * aStep + anAscent + anOffset ), aFgColor); |
685 | aLast2 = i2; |
686 | } |
687 | i1++; |
688 | i2--; |
689 | } |
690 | Standard_Integer aPos = i1; |
691 | Standard_Integer i0 = -1; |
692 | while (aPos <= i2 && i0 == -1) |
693 | { |
694 | if (aFilter && !( aPos % aFilter ) && Abs (aPos - aLast1) >= aFilter && Abs (aPos - aLast2) >= aFilter) |
695 | i0 = aPos; |
696 | aPos++; |
697 | } |
698 | |
699 | if (i0 != -1) |
700 | DrawText (thePresentation, aLabels.Value (i0 + 1), aX, (Standard_Integer)( myYPos + i0 * aStep + anAscent + anOffset ), aFgColor); |
701 | } |
702 | } |
703 | |
704 | //======================================================================= |
705 | //function : DrawFrame |
706 | //purpose : |
707 | //======================================================================= |
708 | void AIS_ColorScale::DrawFrame (const Handle(Prs3d_Presentation)& thePresentation, |
709 | const Standard_Integer theX, const Standard_Integer theY, |
710 | const Standard_Integer theWidth, const Standard_Integer theHeight, |
711 | const Quantity_Color& theColor) |
712 | { |
713 | Handle (Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (thePresentation); |
714 | Handle(Graphic3d_ArrayOfPolylines) aPrim = new Graphic3d_ArrayOfPolylines(5); |
715 | aPrim->AddVertex (theX,theY,0.0); |
716 | aPrim->AddVertex (theX+theWidth,theY,0.0); |
717 | aPrim->AddVertex (theX+theWidth,theY+theHeight,0.0); |
718 | aPrim->AddVertex (theX,theY+theHeight,0.0); |
719 | aPrim->AddVertex (theX,theY,0.0); |
720 | Handle(Prs3d_LineAspect) anAspect = new Prs3d_LineAspect (theColor, Aspect_TOL_SOLID, 1.0); |
721 | anAspect->SetColor (theColor); |
722 | aGroup->SetPrimitivesAspect (anAspect->Aspect()); |
723 | aGroup->AddPrimitiveArray (aPrim); |
724 | } |
725 | |
726 | //======================================================================= |
727 | //function : DrawText |
728 | //purpose : |
729 | //======================================================================= |
730 | void AIS_ColorScale::DrawText (const Handle(Prs3d_Presentation)& thePresentation, |
731 | const TCollection_ExtendedString& theText, |
732 | const Standard_Integer theX, const Standard_Integer theY, |
733 | const Quantity_Color& theColor) |
734 | { |
735 | if (!myDrawer->HasOwnTextAspect()) |
736 | { |
737 | myDrawer->SetTextAspect (new Prs3d_TextAspect()); |
738 | *myDrawer->TextAspect()->Aspect() = *myDrawer->Link()->TextAspect()->Aspect(); |
739 | } |
740 | Handle(Prs3d_TextAspect) anAspect = myDrawer->TextAspect(); |
741 | anAspect->SetColor (theColor); |
742 | anAspect->SetHeight (myTextHeight); |
743 | anAspect->SetHorizontalJustification (Graphic3d_HTA_LEFT); |
744 | anAspect->SetVerticalJustification (Graphic3d_VTA_BOTTOM); |
745 | anAspect->Aspect()->SetTextZoomable (Standard_True); |
746 | anAspect->Aspect()->SetTextAngle (0.0); |
747 | anAspect->Aspect()->SetTextFontAspect (Font_FA_Regular); |
748 | Prs3d_Text::Draw (thePresentation, anAspect, theText,gp_Pnt (theX,theY,0.0)); |
749 | } |
750 | |
751 | //======================================================================= |
752 | //function : TextWidth |
753 | //purpose : |
754 | //======================================================================= |
755 | Standard_Integer AIS_ColorScale::TextWidth (const TCollection_ExtendedString& theText) const |
756 | { |
757 | Standard_Integer aWidth, anAscent, aDescent; |
758 | TextSize (theText, GetTextHeight(), aWidth, anAscent, aDescent); |
759 | return aWidth; |
760 | } |
761 | |
762 | //======================================================================= |
763 | //function : TextHeight |
764 | //purpose : |
765 | //======================================================================= |
766 | Standard_Integer AIS_ColorScale::TextHeight (const TCollection_ExtendedString& theText) const |
767 | { |
768 | Standard_Integer aWidth, anAscent, aDescent; |
769 | TextSize (theText, GetTextHeight(), aWidth, anAscent, aDescent); |
770 | return anAscent+aDescent; |
771 | } |
772 | |
773 | //======================================================================= |
774 | //function : TextSize |
775 | //purpose : |
776 | //======================================================================= |
777 | void AIS_ColorScale::TextSize (const TCollection_ExtendedString& theText, const Standard_Integer theHeight, Standard_Integer& theWidth, Standard_Integer& theAscent, Standard_Integer& theDescent) const |
778 | { |
779 | Standard_ShortReal aWidth(10.0), anAscent(1.0), aDescent(1.0); |
780 | TCollection_AsciiString aText (theText.ToExtString(), '?'); |
781 | GetContext()->CurrentViewer()->Driver()->TextSize (aText.ToCString(),(Standard_ShortReal)theHeight,aWidth,anAscent,aDescent); |
782 | theWidth = (Standard_Integer)aWidth; |
783 | theAscent = (Standard_Integer)anAscent; |
784 | theDescent = (Standard_Integer)aDescent; |
785 | } |