1 // Copyright (C) 2007-2014 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 //==================================================================
42 vtkStandardNewMacro(VTKViewer_FramedTextActor);
44 //==================================================================
45 // function : VTKViewer_FramedTextActor
47 //==================================================================
48 VTKViewer_FramedTextActor::VTKViewer_FramedTextActor()
50 PositionCoordinate->SetCoordinateSystemToNormalizedViewport();
53 myBar = vtkPolyData::New();
54 myBarMapper = vtkPolyDataMapper2D::New();
55 myBarMapper->SetInputData(myBar);
56 myBarActor = vtkActor2D::New();
57 myBarActor->SetMapper(myBarMapper);
58 myBarActor->GetProperty()->SetOpacity(1.-myTransparency);
59 myBarActor->GetProperty()->SetColor(.5, .5, .5);
61 myTextProperty = vtkTextProperty::New();
62 myTextProperty->SetFontSize(12);
63 myTextProperty->SetBold(0);
64 myTextProperty->SetItalic(0);
65 myTextProperty->SetShadow(1);
66 myTextProperty->SetFontFamilyToArial();
68 myTextActor=vtkTextActor::New();
69 myTextActor->SetTextProperty(myTextProperty);
71 myBarActor->SetVisibility(1);
72 myTextActor->SetVisibility(1);
73 myBarActor->SetPickable(0);
74 myTextActor->SetPickable(0);
76 myModePosition = BelowPoint;
77 myLayoutType = Vertical;
79 for(int i=0; i<4; i++) {
84 myTextMargin = TEXT_MARGIN;
86 myHorizontalOffset = 0;
92 //==================================================================
95 //==================================================================
96 VTKViewer_FramedTextActor::~VTKViewer_FramedTextActor()
98 myTextActor->Delete();
99 myTextProperty->Delete();
100 myBarActor->Delete();
101 myBarMapper->Delete();
105 //==================================================================
106 // function : SetVisibility
108 //==================================================================
109 void VTKViewer_FramedTextActor::SetVisibility (int theVisibility)
111 myBarActor->SetVisibility(theVisibility);
112 myTextActor->SetVisibility(theVisibility);
115 //==================================================================
116 // function : GetVisibility
118 //==================================================================
119 int VTKViewer_FramedTextActor::GetVisibility()
121 return myBarActor->GetVisibility();
124 //==================================================================
125 // function : SetPickable
127 //==================================================================
128 void VTKViewer_FramedTextActor::SetPickable (int thePickability)
130 myBarActor->SetPickable(thePickability);
131 myTextActor->SetPickable(thePickability);
134 //==================================================================
135 // function : GetPickable
137 //==================================================================
138 int VTKViewer_FramedTextActor::GetPickable()
140 return myBarActor->GetPickable();
143 //==================================================================
144 // function : GetSize
146 //==================================================================
147 void VTKViewer_FramedTextActor::GetSize(vtkRenderer* vport, double theSize[2]) const
149 myTextActor->GetSize(vport, theSize);
150 theSize[0] = theSize[0] + 2 * GetTextMargin() + OFFSET_SPACING;
151 theSize[1] = theSize[1] + 2 * GetTextMargin() + OFFSET_SPACING;
154 //==================================================================
155 // function : SetForegroundColor
157 //==================================================================
158 void VTKViewer_FramedTextActor::SetForegroundColor(const double r,
162 myTextProperty->SetColor(r, g, b);
163 myTextActor->GetTextProperty()->ShallowCopy(myTextProperty);
167 //==================================================================
168 // function : GetForegroundColor
170 //==================================================================
171 void VTKViewer_FramedTextActor::GetForegroundColor(double& r,
176 myTextProperty->GetColor(aColor);
182 //==================================================================
183 // function : SetBackgroundColor
185 //==================================================================
186 void VTKViewer_FramedTextActor::SetBackgroundColor(const double r,
190 myBarActor->GetProperty()->SetColor(r, g, b);
194 //==================================================================
195 // function : GetBackgroundColor
197 //==================================================================
198 void VTKViewer_FramedTextActor::GetBackgroundColor(double& r,
203 myBarActor->GetProperty()->GetColor(aColor);
209 //==================================================================
210 // function : SetTransparency
212 //==================================================================
213 void VTKViewer_FramedTextActor::SetTransparency(const double theTransparency)
215 if (theTransparency>=0. && theTransparency<=1.){
216 myTransparency=theTransparency;
217 myBarActor->GetProperty()->SetOpacity(1.-myTransparency);
222 //==================================================================
223 // function : GetTransparency
225 //==================================================================
226 double VTKViewer_FramedTextActor::GetTransparency()const
228 return myTransparency;
231 //==================================================================
232 // function : SetTextMargin
234 //==================================================================
235 void VTKViewer_FramedTextActor::SetTextMargin(const int theMargin)
237 if( theMargin >= 0 ) {
238 myTextMargin = theMargin;
243 //==================================================================
244 // function : GetTextMargin
246 //==================================================================
247 int VTKViewer_FramedTextActor::GetTextMargin() const
252 //==================================================================
253 // function : SetOffset
255 //==================================================================
256 void VTKViewer_FramedTextActor::SetOffset(const double theOffset[2])
258 myHorizontalOffset = theOffset[0];
259 myVerticalOffset = theOffset[1];
263 //==================================================================
264 // function : SetText
266 //==================================================================
267 void VTKViewer_FramedTextActor::SetText(const char* theText)
269 // remove whitespaces from from the start and the end
270 // additionally, consider a case of multi-string text
271 QString aString(QString::fromUtf8(theText));
273 QStringList aTrimmedStringList;
274 QStringList aStringList = aString.split("\n");
275 QStringListIterator anIter(aStringList);
276 while(anIter.hasNext())
277 aTrimmedStringList.append(anIter.next().trimmed());
279 myTextActor->SetInput(aTrimmedStringList.join("\n").toUtf8().constData());
283 //==================================================================
284 // function : GetText
286 //==================================================================
287 char* VTKViewer_FramedTextActor::GetText()
289 return myTextActor->GetInput();
292 //==================================================================
293 // function : SetModePosition
295 //==================================================================
296 void VTKViewer_FramedTextActor::SetModePosition(const int theMode)
298 myModePosition = theMode;
302 //==================================================================
303 // function : GetModePosition
305 //==================================================================
306 int VTKViewer_FramedTextActor::GetModePosition()const
308 return myModePosition;
311 //==================================================================
312 // function : SetLayoutType
314 //==================================================================
315 void VTKViewer_FramedTextActor::SetLayoutType(const int theType)
317 myLayoutType = theType;
321 //==================================================================
322 // function : GetLayoutType
324 //==================================================================
325 int VTKViewer_FramedTextActor::GetLayoutType() const
330 //==================================================================
331 // function : SetWorldPoint
333 //==================================================================
334 void VTKViewer_FramedTextActor::SetWorldPoint(const double theWorldPoint[4])
336 for(int i = 0; i<4; ++i) {
337 myWorldPoint[i] = theWorldPoint[i];
342 //==================================================================
343 // function : GetWorldPoint
345 //==================================================================
346 const double* VTKViewer_FramedTextActor::GetWorldPoint()const
351 //==================================================================
352 // function : SetDistance
354 //==================================================================
355 void VTKViewer_FramedTextActor::SetDistance(const double theDistance)
357 myDistance=theDistance;
360 //==================================================================
361 // function : GetDistance
363 //==================================================================
364 double VTKViewer_FramedTextActor::GetDistance()const
369 //==================================================================
370 // function : SetMoveFrameFlag
371 // purpose : If moveFrameFlag is true, then frame with text is moved
373 //==================================================================
374 void VTKViewer_FramedTextActor::SetMoveFrameFlag(const int theMoveFrameFlag)
376 if(myMoveFrameFlag != theMoveFrameFlag) {
377 myMoveFrameFlag = theMoveFrameFlag;
382 //==================================================================
383 // function : GetDistance
385 //==================================================================
386 int VTKViewer_FramedTextActor::GetMoveFrameFlag() const
388 return myMoveFrameFlag;
392 //==================================================================
393 // function : ReleaseGraphicsResources
395 //==================================================================
396 void VTKViewer_FramedTextActor::ReleaseGraphicsResources(vtkWindow *win)
398 myTextActor->ReleaseGraphicsResources(win);
399 myBarActor->ReleaseGraphicsResources(win);
402 //==================================================================
403 // function : RenderOverlay
405 //==================================================================
406 int VTKViewer_FramedTextActor::RenderOverlay(vtkViewport *viewport)
408 int renderedSomething = 0;
409 renderedSomething +=myTextActor->RenderOverlay(viewport);
410 renderedSomething +=myBarActor->RenderOverlay(viewport);
411 return renderedSomething;
414 //==================================================================
415 // function : RenderOpaqueGeometry
417 //==================================================================
419 VTKViewer_FramedTextActor
420 ::RenderOpaqueGeometry(vtkViewport *theViewport)
422 int anIsRenderedSomething = 0;
424 int* aViewportSize = theViewport->GetSize();
425 int aViewPortWidth = aViewportSize[0];
426 int aViewPortHeight = aViewportSize[1];
427 if(aViewPortWidth == 1 || aViewPortHeight == 1)
428 return anIsRenderedSomething;
430 if(!myTextActor->GetInput())
431 return anIsRenderedSomething;
436 vtkPoints *aPoints = vtkPoints::New();
437 aPoints->SetNumberOfPoints(aNbPoints);
438 myBar->SetPoints(aPoints);
441 vtkCellArray *aPolys = vtkCellArray::New();
442 aPolys->Allocate(aPolys->EstimateSize(1,4));
443 vtkIdType aPointsIds[4] = {0, 1, 3, 2};
444 aPolys->InsertNextCell(4,aPointsIds);
445 myBar->SetPolys(aPolys);
449 myTextActor->GetSize(theViewport, aTextSize);
450 int aBarWidth = aTextSize[0];
451 int aBarHeight = aTextSize[1];
453 int aTextMargin = GetTextMargin();
457 double yMin = -aBarHeight/2 - aTextMargin;
458 double yMax = aBarHeight/2 + aTextMargin;
460 int aHorizontalOffset = GetLayoutType() == Horizontal ? myHorizontalOffset : 0;
461 int aVerticalOffset = GetLayoutType() == Vertical ? myVerticalOffset : 0;
463 if( myModePosition == BelowPoint )
465 theViewport->SetWorldPoint(myWorldPoint);
466 theViewport->WorldToDisplay();
468 double aSelectionPoint[3];
469 theViewport->GetDisplayPoint(aSelectionPoint);
470 double u = aSelectionPoint[0];
471 double v = aSelectionPoint[1] - myDistance;
474 theViewport->ViewportToNormalizedViewport(u, v);
475 PositionCoordinate->SetValue(u, v);
477 myTextProperty->SetJustificationToCentered();
479 xMin = -aBarWidth/2 - aTextMargin;
480 xMax = aBarWidth/2 + aTextMargin;
482 else // except BelowPoint, only TopLeft and TopRight modes are supported at this moment
484 double x = 0, xOffset = aHorizontalOffset + aTextMargin + OFFSET_SPACING;
485 double y = 0, yOffset = aVerticalOffset + aTextMargin + OFFSET_SPACING;
487 if( myModePosition == TopLeft )
490 y = aViewPortHeight - yOffset - aBarHeight/2;
491 myTextProperty->SetJustificationToLeft();
493 xMin = - aTextMargin;
494 xMax = aBarWidth + aTextMargin;
496 else if( myModePosition == TopRight )
498 x = aViewPortWidth - xOffset;
499 y = aViewPortHeight - yOffset - aBarHeight/2;
500 myTextProperty->SetJustificationToRight();
502 xMin = -aBarWidth - aTextMargin;
506 PositionCoordinate->SetValue(x / (double)aViewPortWidth,
507 y / (double)aViewPortHeight);
511 aPoints->SetPoint(0, xMin, yMax, 0.0);
512 aPoints->SetPoint(1, xMin, yMin, 0.0);
513 aPoints->SetPoint(2, xMax, yMax, 0.0);
514 aPoints->SetPoint(3, xMax, yMin, 0.0);
516 myTextProperty->SetVerticalJustificationToCentered();
518 myBarActor ->GetPositionCoordinate()->SetReferenceCoordinate(PositionCoordinate);
519 myTextActor->GetPositionCoordinate()->SetReferenceCoordinate(PositionCoordinate);
521 myBuildTime.Modified();
523 return anIsRenderedSomething;