1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "VTKViewer_FramedTextActor.h"
22 #include <vtkCellArray.h>
23 #include <vtkObjectFactory.h>
24 #include <vtkPoints.h>
25 #include <vtkPolyData.h>
26 #include <vtkPolyDataMapper2D.h>
27 #include <vtkProperty2D.h>
28 #include <vtkRenderer.h>
29 #include <vtkTextActor.h>
30 #include <vtkTextMapper.h>
31 #include <vtkTextProperty.h>
32 #include <vtkTimeStamp.h>
33 #include <vtkViewport.h>
34 #include <vtkWindow.h>
36 #include <QStringList>
39 #define OFFSET_SPACING 2
41 //VSR: uncomment below macro to support unicode text properly in SALOME
42 // current commented out due to regressions
43 //#define PAL22528_UNICODE
47 QString fromUtf8( const char* txt )
49 #ifdef PAL22528_UNICODE
50 return QString::fromUtf8( txt );
52 return QString( txt );
55 const char* toUtf8( const QString& txt )
57 #ifdef PAL22528_UNICODE
58 return txt.toUtf8().constData();
60 return txt.toLatin1().constData();
65 //==================================================================
66 vtkStandardNewMacro(VTKViewer_FramedTextActor);
68 //==================================================================
69 // function : VTKViewer_FramedTextActor
71 //==================================================================
72 VTKViewer_FramedTextActor::VTKViewer_FramedTextActor()
74 PositionCoordinate->SetCoordinateSystemToNormalizedViewport();
77 myBar = vtkPolyData::New();
78 myBarMapper = vtkPolyDataMapper2D::New();
79 myBarMapper->SetInputData(myBar);
80 myBarActor = vtkActor2D::New();
81 myBarActor->SetMapper(myBarMapper);
82 myBarActor->GetProperty()->SetOpacity(1.-myTransparency);
83 myBarActor->GetProperty()->SetColor(.5, .5, .5);
85 myTextProperty = vtkTextProperty::New();
86 myTextProperty->SetFontSize(12);
87 myTextProperty->SetBold(0);
88 myTextProperty->SetItalic(0);
89 myTextProperty->SetShadow(1);
90 myTextProperty->SetFontFamilyToArial();
92 myTextActor=vtkTextActor::New();
93 myTextActor->SetTextProperty(myTextProperty);
95 myBarActor->SetVisibility(1);
96 myTextActor->SetVisibility(1);
97 myBarActor->SetPickable(0);
98 myTextActor->SetPickable(0);
100 myModePosition = BelowPoint;
101 myLayoutType = Vertical;
103 for(int i=0; i<4; i++) {
104 myWorldPoint[i] = 0.;
108 myTextMargin = TEXT_MARGIN;
110 myHorizontalOffset = 0;
111 myVerticalOffset = 0;
116 //==================================================================
119 //==================================================================
120 VTKViewer_FramedTextActor::~VTKViewer_FramedTextActor()
122 myTextActor->Delete();
123 myTextProperty->Delete();
124 myBarActor->Delete();
125 myBarMapper->Delete();
129 //==================================================================
130 // function : SetVisibility
132 //==================================================================
133 void VTKViewer_FramedTextActor::SetVisibility (int theVisibility)
135 myBarActor->SetVisibility(theVisibility);
136 myTextActor->SetVisibility(theVisibility);
139 //==================================================================
140 // function : GetVisibility
142 //==================================================================
143 int VTKViewer_FramedTextActor::GetVisibility()
145 return myBarActor->GetVisibility();
148 //==================================================================
149 // function : SetPickable
151 //==================================================================
152 void VTKViewer_FramedTextActor::SetPickable (int thePickability)
154 myBarActor->SetPickable(thePickability);
155 myTextActor->SetPickable(thePickability);
158 //==================================================================
159 // function : GetPickable
161 //==================================================================
162 int VTKViewer_FramedTextActor::GetPickable()
164 return myBarActor->GetPickable();
167 //==================================================================
168 // function : GetSize
170 //==================================================================
171 void VTKViewer_FramedTextActor::GetSize(vtkRenderer* vport, double theSize[2]) const
173 myTextActor->GetSize(vport, theSize);
174 theSize[0] = theSize[0] + 2 * GetTextMargin() + OFFSET_SPACING;
175 theSize[1] = theSize[1] + 2 * GetTextMargin() + OFFSET_SPACING;
178 //==================================================================
179 // function : SetForegroundColor
181 //==================================================================
182 void VTKViewer_FramedTextActor::SetForegroundColor(const double r,
186 myTextProperty->SetColor(r, g, b);
187 myTextActor->GetTextProperty()->ShallowCopy(myTextProperty);
191 //==================================================================
192 // function : GetForegroundColor
194 //==================================================================
195 void VTKViewer_FramedTextActor::GetForegroundColor(double& r,
200 myTextProperty->GetColor(aColor);
206 //==================================================================
207 // function : SetBackgroundColor
209 //==================================================================
210 void VTKViewer_FramedTextActor::SetBackgroundColor(const double r,
214 myBarActor->GetProperty()->SetColor(r, g, b);
218 //==================================================================
219 // function : GetBackgroundColor
221 //==================================================================
222 void VTKViewer_FramedTextActor::GetBackgroundColor(double& r,
227 myBarActor->GetProperty()->GetColor(aColor);
233 //==================================================================
234 // function : SetTransparency
236 //==================================================================
237 void VTKViewer_FramedTextActor::SetTransparency(const double theTransparency)
239 if (theTransparency>=0. && theTransparency<=1.){
240 myTransparency=theTransparency;
241 myBarActor->GetProperty()->SetOpacity(1.-myTransparency);
246 //==================================================================
247 // function : GetTransparency
249 //==================================================================
250 double VTKViewer_FramedTextActor::GetTransparency()const
252 return myTransparency;
255 //==================================================================
256 // function : SetTextMargin
258 //==================================================================
259 void VTKViewer_FramedTextActor::SetTextMargin(const int theMargin)
261 if( theMargin >= 0 ) {
262 myTextMargin = theMargin;
267 //==================================================================
268 // function : GetTextMargin
270 //==================================================================
271 int VTKViewer_FramedTextActor::GetTextMargin() const
276 //==================================================================
277 // function : SetOffset
279 //==================================================================
280 void VTKViewer_FramedTextActor::SetOffset(const double theOffset[2])
282 myHorizontalOffset = theOffset[0];
283 myVerticalOffset = theOffset[1];
287 //==================================================================
288 // function : SetText
290 //==================================================================
291 void VTKViewer_FramedTextActor::SetText(const char* theText)
293 // remove whitespaces from from the start and the end
294 // additionally, consider a case of multi-string text
295 QString aString(fromUtf8(theText));
297 QStringList aTrimmedStringList;
298 QStringList aStringList = aString.split("\n");
299 QStringListIterator anIter(aStringList);
300 while(anIter.hasNext())
301 aTrimmedStringList.append(anIter.next().trimmed());
303 myTextActor->SetInput(toUtf8(aTrimmedStringList.join("\n")));
307 //==================================================================
308 // function : GetText
310 //==================================================================
311 char* VTKViewer_FramedTextActor::GetText()
313 return myTextActor->GetInput();
316 //==================================================================
317 // function : SetModePosition
319 //==================================================================
320 void VTKViewer_FramedTextActor::SetModePosition(const int theMode)
322 myModePosition = theMode;
326 //==================================================================
327 // function : GetModePosition
329 //==================================================================
330 int VTKViewer_FramedTextActor::GetModePosition()const
332 return myModePosition;
335 //==================================================================
336 // function : SetLayoutType
338 //==================================================================
339 void VTKViewer_FramedTextActor::SetLayoutType(const int theType)
341 myLayoutType = theType;
345 //==================================================================
346 // function : GetLayoutType
348 //==================================================================
349 int VTKViewer_FramedTextActor::GetLayoutType() const
354 //==================================================================
355 // function : SetWorldPoint
357 //==================================================================
358 void VTKViewer_FramedTextActor::SetWorldPoint(const double theWorldPoint[4])
360 for(int i = 0; i<4; ++i) {
361 myWorldPoint[i] = theWorldPoint[i];
366 //==================================================================
367 // function : GetWorldPoint
369 //==================================================================
370 const double* VTKViewer_FramedTextActor::GetWorldPoint()const
375 //==================================================================
376 // function : SetDistance
378 //==================================================================
379 void VTKViewer_FramedTextActor::SetDistance(const double theDistance)
381 myDistance=theDistance;
384 //==================================================================
385 // function : GetDistance
387 //==================================================================
388 double VTKViewer_FramedTextActor::GetDistance()const
393 //==================================================================
394 // function : SetMoveFrameFlag
395 // purpose : If moveFrameFlag is true, then frame with text is moved
397 //==================================================================
398 void VTKViewer_FramedTextActor::SetMoveFrameFlag(const int theMoveFrameFlag)
400 if(myMoveFrameFlag != theMoveFrameFlag) {
401 myMoveFrameFlag = theMoveFrameFlag;
406 //==================================================================
407 // function : GetDistance
409 //==================================================================
410 int VTKViewer_FramedTextActor::GetMoveFrameFlag() const
412 return myMoveFrameFlag;
416 //==================================================================
417 // function : ReleaseGraphicsResources
419 //==================================================================
420 void VTKViewer_FramedTextActor::ReleaseGraphicsResources(vtkWindow *win)
422 myTextActor->ReleaseGraphicsResources(win);
423 myBarActor->ReleaseGraphicsResources(win);
426 //==================================================================
427 // function : RenderOverlay
429 //==================================================================
430 int VTKViewer_FramedTextActor::RenderOverlay(vtkViewport *viewport)
432 int renderedSomething = 0;
433 renderedSomething +=myTextActor->RenderOverlay(viewport);
434 renderedSomething +=myBarActor->RenderOverlay(viewport);
435 return renderedSomething;
438 //==================================================================
439 // function : RenderOpaqueGeometry
441 //==================================================================
443 VTKViewer_FramedTextActor
444 ::RenderOpaqueGeometry(vtkViewport *theViewport)
446 int anIsRenderedSomething = 0;
448 int* aViewportSize = theViewport->GetSize();
449 int aViewPortWidth = aViewportSize[0];
450 int aViewPortHeight = aViewportSize[1];
451 if(aViewPortWidth == 1 || aViewPortHeight == 1)
452 return anIsRenderedSomething;
454 if(!myTextActor->GetInput())
455 return anIsRenderedSomething;
460 vtkPoints *aPoints = vtkPoints::New();
461 aPoints->SetNumberOfPoints(aNbPoints);
462 myBar->SetPoints(aPoints);
465 vtkCellArray *aPolys = vtkCellArray::New();
466 aPolys->Allocate(aPolys->EstimateSize(1,4));
467 vtkIdType aPointsIds[4] = {0, 1, 3, 2};
468 aPolys->InsertNextCell(4,aPointsIds);
469 myBar->SetPolys(aPolys);
473 myTextActor->GetSize(theViewport, aTextSize);
474 int aBarWidth = aTextSize[0];
475 int aBarHeight = aTextSize[1];
477 int aTextMargin = GetTextMargin();
481 double yMin = -aBarHeight/2 - aTextMargin;
482 double yMax = aBarHeight/2 + aTextMargin;
484 int aHorizontalOffset = GetLayoutType() == Horizontal ? myHorizontalOffset : 0;
485 int aVerticalOffset = GetLayoutType() == Vertical ? myVerticalOffset : 0;
487 if( myModePosition == BelowPoint )
489 theViewport->SetWorldPoint(myWorldPoint);
490 theViewport->WorldToDisplay();
492 double aSelectionPoint[3];
493 theViewport->GetDisplayPoint(aSelectionPoint);
494 double u = aSelectionPoint[0];
495 double v = aSelectionPoint[1] - myDistance;
498 theViewport->ViewportToNormalizedViewport(u, v);
499 PositionCoordinate->SetValue(u, v);
501 myTextProperty->SetJustificationToCentered();
503 xMin = -aBarWidth/2 - aTextMargin;
504 xMax = aBarWidth/2 + aTextMargin;
506 else // except BelowPoint, only TopLeft and TopRight modes are supported at this moment
508 double x = 0, xOffset = aHorizontalOffset + aTextMargin + OFFSET_SPACING;
509 double y = 0, yOffset = aVerticalOffset + aTextMargin + OFFSET_SPACING;
511 if( myModePosition == TopLeft )
514 y = aViewPortHeight - yOffset - aBarHeight/2;
515 myTextProperty->SetJustificationToLeft();
517 xMin = - aTextMargin;
518 xMax = aBarWidth + aTextMargin;
520 else if( myModePosition == TopRight )
522 x = aViewPortWidth - xOffset;
523 y = aViewPortHeight - yOffset - aBarHeight/2;
524 myTextProperty->SetJustificationToRight();
526 xMin = -aBarWidth - aTextMargin;
530 PositionCoordinate->SetValue(x / (double)aViewPortWidth,
531 y / (double)aViewPortHeight);
535 aPoints->SetPoint(0, xMin, yMax, 0.0);
536 aPoints->SetPoint(1, xMin, yMin, 0.0);
537 aPoints->SetPoint(2, xMax, yMax, 0.0);
538 aPoints->SetPoint(3, xMax, yMin, 0.0);
540 myTextProperty->SetVerticalJustificationToCentered();
542 myBarActor ->GetPositionCoordinate()->SetReferenceCoordinate(PositionCoordinate);
543 myTextActor->GetPositionCoordinate()->SetReferenceCoordinate(PositionCoordinate);
545 myBuildTime.Modified();
547 return anIsRenderedSomething;