1 // Copyright (C) 2007-2013 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.
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 myTextMapper=vtkTextMapper::New();
69 myTextMapper->SetInput("");
70 myTextMapper->GetTextProperty()->ShallowCopy(myTextProperty);
71 myTextActor=vtkActor2D::New();
72 myTextActor->SetMapper(myTextMapper);
74 myBarActor->SetVisibility(1);
75 myTextActor->SetVisibility(1);
76 myBarActor->SetPickable(0);
77 myTextActor->SetPickable(0);
79 myModePosition = BelowPoint;
80 myLayoutType = Vertical;
82 for(int i=0; i<4; i++) {
87 myTextMargin = TEXT_MARGIN;
89 myHorizontalOffset = 0;
95 //==================================================================
98 //==================================================================
99 VTKViewer_FramedTextActor::~VTKViewer_FramedTextActor()
101 myTextActor->Delete();
102 myTextMapper->Delete();
103 myTextProperty->Delete();
104 myBarActor->Delete();
105 myBarMapper->Delete();
109 //==================================================================
110 // function : SetVisibility
112 //==================================================================
113 void VTKViewer_FramedTextActor::SetVisibility (int theVisibility)
115 myBarActor->SetVisibility(theVisibility);
116 myTextActor->SetVisibility(theVisibility);
119 //==================================================================
120 // function : GetVisibility
122 //==================================================================
123 int VTKViewer_FramedTextActor::GetVisibility()
125 return myBarActor->GetVisibility();
128 //==================================================================
129 // function : SetPickable
131 //==================================================================
132 void VTKViewer_FramedTextActor::SetPickable (int thePickability)
134 myBarActor->SetPickable(thePickability);
135 myTextActor->SetPickable(thePickability);
138 //==================================================================
139 // function : GetPickable
141 //==================================================================
142 int VTKViewer_FramedTextActor::GetPickable()
144 return myBarActor->GetPickable();
147 //==================================================================
148 // function : GetSize
150 //==================================================================
151 void VTKViewer_FramedTextActor::GetSize(vtkRenderer* theRenderer, int theSize[2]) const
153 myTextMapper->GetSize(theRenderer, theSize);
154 theSize[0] = theSize[0] + 2 * GetTextMargin() + OFFSET_SPACING;
155 theSize[1] = theSize[1] + 2 * GetTextMargin() + OFFSET_SPACING;
158 //==================================================================
159 // function : SetForegroundColor
161 //==================================================================
162 void VTKViewer_FramedTextActor::SetForegroundColor(const double r,
166 myTextProperty->SetColor(r, g, b);
167 myTextMapper->GetTextProperty()->ShallowCopy(myTextProperty);
171 //==================================================================
172 // function : GetForegroundColor
174 //==================================================================
175 void VTKViewer_FramedTextActor::GetForegroundColor(double& r,
180 myTextProperty->GetColor(aColor);
186 //==================================================================
187 // function : SetBackgroundColor
189 //==================================================================
190 void VTKViewer_FramedTextActor::SetBackgroundColor(const double r,
194 myBarActor->GetProperty()->SetColor(r, g, b);
198 //==================================================================
199 // function : GetBackgroundColor
201 //==================================================================
202 void VTKViewer_FramedTextActor::GetBackgroundColor(double& r,
207 myBarActor->GetProperty()->GetColor(aColor);
213 //==================================================================
214 // function : SetTransparency
216 //==================================================================
217 void VTKViewer_FramedTextActor::SetTransparency(const double theTransparency)
219 if (theTransparency>=0. && theTransparency<=1.){
220 myTransparency=theTransparency;
221 myBarActor->GetProperty()->SetOpacity(1.-myTransparency);
226 //==================================================================
227 // function : GetTransparency
229 //==================================================================
230 double VTKViewer_FramedTextActor::GetTransparency()const
232 return myTransparency;
235 //==================================================================
236 // function : SetTextMargin
238 //==================================================================
239 void VTKViewer_FramedTextActor::SetTextMargin(const int theMargin)
241 if( theMargin >= 0 ) {
242 myTextMargin = theMargin;
247 //==================================================================
248 // function : GetTextMargin
250 //==================================================================
251 int VTKViewer_FramedTextActor::GetTextMargin() const
256 //==================================================================
257 // function : SetOffset
259 //==================================================================
260 void VTKViewer_FramedTextActor::SetOffset(const int theOffset[2])
262 myHorizontalOffset = theOffset[0];
263 myVerticalOffset = theOffset[1];
267 //==================================================================
268 // function : SetText
270 //==================================================================
271 void VTKViewer_FramedTextActor::SetText(const char* theText)
273 // remove whitespaces from from the start and the end
274 // additionally, consider a case of multi-string text
275 QString aString(theText);
277 QStringList aTrimmedStringList;
278 QStringList aStringList = aString.split("\n");
279 QStringListIterator anIter(aStringList);
280 while(anIter.hasNext())
281 aTrimmedStringList.append(anIter.next().trimmed());
283 myTextMapper->SetInput(aTrimmedStringList.join("\n").toLatin1().constData());
287 //==================================================================
288 // function : GetText
290 //==================================================================
291 char* VTKViewer_FramedTextActor::GetText()
293 return myTextMapper->GetInput();
296 //==================================================================
297 // function : SetModePosition
299 //==================================================================
300 void VTKViewer_FramedTextActor::SetModePosition(const int theMode)
302 myModePosition = theMode;
306 //==================================================================
307 // function : GetModePosition
309 //==================================================================
310 int VTKViewer_FramedTextActor::GetModePosition()const
312 return myModePosition;
315 //==================================================================
316 // function : SetLayoutType
318 //==================================================================
319 void VTKViewer_FramedTextActor::SetLayoutType(const int theType)
321 myLayoutType = theType;
325 //==================================================================
326 // function : GetLayoutType
328 //==================================================================
329 int VTKViewer_FramedTextActor::GetLayoutType() const
334 //==================================================================
335 // function : SetWorldPoint
337 //==================================================================
338 void VTKViewer_FramedTextActor::SetWorldPoint(const double theWorldPoint[4])
340 for(int i = 0; i<4; ++i) {
341 myWorldPoint[i] = theWorldPoint[i];
346 //==================================================================
347 // function : GetWorldPoint
349 //==================================================================
350 const double* VTKViewer_FramedTextActor::GetWorldPoint()const
355 //==================================================================
356 // function : SetDistance
358 //==================================================================
359 void VTKViewer_FramedTextActor::SetDistance(const double theDistance)
361 myDistance=theDistance;
364 //==================================================================
365 // function : GetDistance
367 //==================================================================
368 double VTKViewer_FramedTextActor::GetDistance()const
373 //==================================================================
374 // function : SetMoveFrameFlag
375 // purpose : If moveFrameFlag is true, then frame with text is moved
377 //==================================================================
378 void VTKViewer_FramedTextActor::SetMoveFrameFlag(const int theMoveFrameFlag)
380 if(myMoveFrameFlag != theMoveFrameFlag) {
381 myMoveFrameFlag = theMoveFrameFlag;
386 //==================================================================
387 // function : GetDistance
389 //==================================================================
390 int VTKViewer_FramedTextActor::GetMoveFrameFlag() const
392 return myMoveFrameFlag;
396 //==================================================================
397 // function : ReleaseGraphicsResources
399 //==================================================================
400 void VTKViewer_FramedTextActor::ReleaseGraphicsResources(vtkWindow *win)
402 myTextActor->ReleaseGraphicsResources(win);
403 myBarActor->ReleaseGraphicsResources(win);
406 //==================================================================
407 // function : RenderOverlay
409 //==================================================================
410 int VTKViewer_FramedTextActor::RenderOverlay(vtkViewport *viewport)
412 int renderedSomething = 0;
413 myBarActor->RenderOverlay(viewport);
414 renderedSomething +=myTextActor->RenderOverlay(viewport);
415 return renderedSomething;
418 //==================================================================
419 // function : RenderOpaqueGeometry
421 //==================================================================
423 VTKViewer_FramedTextActor
424 ::RenderOpaqueGeometry(vtkViewport *theViewport)
426 int anIsRenderedSomething = 0;
428 int* aViewportSize = theViewport->GetSize();
429 int aViewPortWidth = aViewportSize[0];
430 int aViewPortHeight = aViewportSize[1];
431 if(aViewPortWidth == 1 || aViewPortHeight == 1)
432 return anIsRenderedSomething;
434 if(!myTextMapper->GetInput())
435 return anIsRenderedSomething;
440 vtkPoints *aPoints = vtkPoints::New();
441 aPoints->SetNumberOfPoints(aNbPoints);
442 myBar->SetPoints(aPoints);
445 vtkCellArray *aPolys = vtkCellArray::New();
446 aPolys->Allocate(aPolys->EstimateSize(1,4));
447 vtkIdType aPointsIds[4] = {0, 1, 3, 2};
448 aPolys->InsertNextCell(4,aPointsIds);
449 myBar->SetPolys(aPolys);
453 myTextMapper->GetSize(theViewport, aTextSize);
454 int aBarWidth = aTextSize[0];
455 int aBarHeight = aTextSize[1];
457 int aTextMargin = GetTextMargin();
461 double yMin = -aBarHeight/2 - aTextMargin;
462 double yMax = aBarHeight/2 + aTextMargin;
464 int aHorizontalOffset = GetLayoutType() == Horizontal ? myHorizontalOffset : 0;
465 int aVerticalOffset = GetLayoutType() == Vertical ? myVerticalOffset : 0;
467 if( myModePosition == BelowPoint )
469 theViewport->SetWorldPoint(myWorldPoint);
470 theViewport->WorldToDisplay();
472 double aSelectionPoint[3];
473 theViewport->GetDisplayPoint(aSelectionPoint);
474 double u = aSelectionPoint[0];
475 double v = aSelectionPoint[1] - myDistance;
478 theViewport->ViewportToNormalizedViewport(u, v);
479 PositionCoordinate->SetValue(u, v);
481 myTextProperty->SetJustificationToCentered();
483 xMin = -aBarWidth/2 - aTextMargin;
484 xMax = aBarWidth/2 + aTextMargin;
486 else // except BelowPoint, only TopLeft and TopRight modes are supported at this moment
488 double x = 0, xOffset = aHorizontalOffset + aTextMargin + OFFSET_SPACING;
489 double y = 0, yOffset = aVerticalOffset + aTextMargin + OFFSET_SPACING;
491 if( myModePosition == TopLeft )
494 y = aViewPortHeight - yOffset - aBarHeight/2;
495 myTextProperty->SetJustificationToLeft();
497 xMin = - aTextMargin;
498 xMax = aBarWidth + aTextMargin;
500 else if( myModePosition == TopRight )
502 x = aViewPortWidth - xOffset;
503 y = aViewPortHeight - yOffset - aBarHeight/2;
504 myTextProperty->SetJustificationToRight();
506 xMin = -aBarWidth - aTextMargin;
510 PositionCoordinate->SetValue(x / (double)aViewPortWidth,
511 y / (double)aViewPortHeight);
514 aPoints->SetPoint(0, xMin, yMax, 0.0);
515 aPoints->SetPoint(1, xMin, yMin, 0.0);
516 aPoints->SetPoint(2, xMax, yMax, 0.0);
517 aPoints->SetPoint(3, xMax, yMin, 0.0);
519 myTextProperty->SetVerticalJustificationToCentered();
521 myTextMapper->GetTextProperty()->ShallowCopy(myTextProperty);
522 myBarActor ->GetPositionCoordinate()->SetReferenceCoordinate(PositionCoordinate);
523 myTextActor->GetPositionCoordinate()->SetReferenceCoordinate(PositionCoordinate);
525 myBuildTime.Modified();
527 return anIsRenderedSomething;