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