1 // Copyright (C) 2007-2012 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 vtkCxxRevisionMacro(VTKViewer_FramedTextActor, "$Revision$");
43 vtkStandardNewMacro(VTKViewer_FramedTextActor);
45 //==================================================================
46 // function : VTKViewer_FramedTextActor
48 //==================================================================
49 VTKViewer_FramedTextActor::VTKViewer_FramedTextActor()
51 PositionCoordinate->SetCoordinateSystemToNormalizedViewport();
54 myBar = vtkPolyData::New();
55 myBarMapper = vtkPolyDataMapper2D::New();
56 myBarMapper->SetInput(myBar);
57 myBarActor = vtkActor2D::New();
58 myBarActor->SetMapper(myBarMapper);
59 myBarActor->GetProperty()->SetOpacity(1.-myTransparency);
60 myBarActor->GetProperty()->SetColor(.5, .5, .5);
62 myTextProperty = vtkTextProperty::New();
63 myTextProperty->SetFontSize(12);
64 myTextProperty->SetBold(0);
65 myTextProperty->SetItalic(0);
66 myTextProperty->SetShadow(1);
67 myTextProperty->SetFontFamilyToArial();
69 myTextMapper=vtkTextMapper::New();
70 myTextMapper->SetInput("");
71 myTextMapper->GetTextProperty()->ShallowCopy(myTextProperty);
72 myTextActor=vtkActor2D::New();
73 myTextActor->SetMapper(myTextMapper);
75 myBarActor->SetVisibility(1);
76 myTextActor->SetVisibility(1);
77 myBarActor->SetPickable(0);
78 myTextActor->SetPickable(0);
80 myModePosition = BelowPoint;
81 myLayoutType = Vertical;
83 for(int i=0; i<4; i++) {
88 myTextMargin = TEXT_MARGIN;
90 myHorizontalOffset = 0;
96 //==================================================================
99 //==================================================================
100 VTKViewer_FramedTextActor::~VTKViewer_FramedTextActor()
102 myTextActor->Delete();
103 myTextMapper->Delete();
104 myTextProperty->Delete();
105 myBarActor->Delete();
106 myBarMapper->Delete();
110 //==================================================================
111 // function : SetVisibility
113 //==================================================================
114 void VTKViewer_FramedTextActor::SetVisibility (int theVisibility)
116 myBarActor->SetVisibility(theVisibility);
117 myTextActor->SetVisibility(theVisibility);
120 //==================================================================
121 // function : GetVisibility
123 //==================================================================
124 int VTKViewer_FramedTextActor::GetVisibility()
126 return myBarActor->GetVisibility();
129 //==================================================================
130 // function : SetPickable
132 //==================================================================
133 void VTKViewer_FramedTextActor::SetPickable (int thePickability)
135 myBarActor->SetPickable(thePickability);
136 myTextActor->SetPickable(thePickability);
139 //==================================================================
140 // function : GetPickable
142 //==================================================================
143 int VTKViewer_FramedTextActor::GetPickable()
145 return myBarActor->GetPickable();
148 //==================================================================
149 // function : GetSize
151 //==================================================================
152 void VTKViewer_FramedTextActor::GetSize(vtkRenderer* theRenderer, int theSize[2]) const
154 myTextMapper->GetSize(theRenderer, theSize);
155 theSize[0] = theSize[0] + 2 * GetTextMargin() + OFFSET_SPACING;
156 theSize[1] = theSize[1] + 2 * GetTextMargin() + OFFSET_SPACING;
159 //==================================================================
160 // function : SetForegroundColor
162 //==================================================================
163 void VTKViewer_FramedTextActor::SetForegroundColor(const vtkFloatingPointType r,
164 const vtkFloatingPointType g,
165 const vtkFloatingPointType b)
167 myTextProperty->SetColor(r, g, b);
168 myTextMapper->GetTextProperty()->ShallowCopy(myTextProperty);
172 //==================================================================
173 // function : GetForegroundColor
175 //==================================================================
176 void VTKViewer_FramedTextActor::GetForegroundColor(vtkFloatingPointType& r,
177 vtkFloatingPointType& g,
178 vtkFloatingPointType& b)
180 vtkFloatingPointType aColor[3];
181 myTextProperty->GetColor(aColor);
187 //==================================================================
188 // function : SetBackgroundColor
190 //==================================================================
191 void VTKViewer_FramedTextActor::SetBackgroundColor(const vtkFloatingPointType r,
192 const vtkFloatingPointType g,
193 const vtkFloatingPointType b)
195 myBarActor->GetProperty()->SetColor(r, g, b);
199 //==================================================================
200 // function : GetBackgroundColor
202 //==================================================================
203 void VTKViewer_FramedTextActor::GetBackgroundColor(vtkFloatingPointType& r,
204 vtkFloatingPointType& g,
205 vtkFloatingPointType& b)
207 vtkFloatingPointType aColor[3];
208 myBarActor->GetProperty()->GetColor(aColor);
214 //==================================================================
215 // function : SetTransparency
217 //==================================================================
218 void VTKViewer_FramedTextActor::SetTransparency(const vtkFloatingPointType theTransparency)
220 if (theTransparency>=0. && theTransparency<=1.){
221 myTransparency=theTransparency;
222 myBarActor->GetProperty()->SetOpacity(1.-myTransparency);
227 //==================================================================
228 // function : GetTransparency
230 //==================================================================
231 vtkFloatingPointType VTKViewer_FramedTextActor::GetTransparency()const
233 return myTransparency;
236 //==================================================================
237 // function : SetTextMargin
239 //==================================================================
240 void VTKViewer_FramedTextActor::SetTextMargin(const int theMargin)
242 if( theMargin >= 0 ) {
243 myTextMargin = theMargin;
248 //==================================================================
249 // function : GetTextMargin
251 //==================================================================
252 int VTKViewer_FramedTextActor::GetTextMargin() const
257 //==================================================================
258 // function : SetOffset
260 //==================================================================
261 void VTKViewer_FramedTextActor::SetOffset(const int theOffset[2])
263 myHorizontalOffset = theOffset[0];
264 myVerticalOffset = theOffset[1];
268 //==================================================================
269 // function : SetText
271 //==================================================================
272 void VTKViewer_FramedTextActor::SetText(const char* theText)
274 // remove whitespaces from from the start and the end
275 // additionally, consider a case of multi-string text
276 QString aString(theText);
278 QStringList aTrimmedStringList;
279 QStringList aStringList = aString.split("\n");
280 QStringListIterator anIter(aStringList);
281 while(anIter.hasNext())
282 aTrimmedStringList.append(anIter.next().trimmed());
284 myTextMapper->SetInput(aTrimmedStringList.join("\n").toLatin1().constData());
288 //==================================================================
289 // function : GetText
291 //==================================================================
292 char* VTKViewer_FramedTextActor::GetText()
294 return myTextMapper->GetInput();
297 //==================================================================
298 // function : SetModePosition
300 //==================================================================
301 void VTKViewer_FramedTextActor::SetModePosition(const int theMode)
303 myModePosition = theMode;
307 //==================================================================
308 // function : GetModePosition
310 //==================================================================
311 int VTKViewer_FramedTextActor::GetModePosition()const
313 return myModePosition;
316 //==================================================================
317 // function : SetLayoutType
319 //==================================================================
320 void VTKViewer_FramedTextActor::SetLayoutType(const int theType)
322 myLayoutType = theType;
326 //==================================================================
327 // function : GetLayoutType
329 //==================================================================
330 int VTKViewer_FramedTextActor::GetLayoutType() const
335 //==================================================================
336 // function : SetWorldPoint
338 //==================================================================
339 void VTKViewer_FramedTextActor::SetWorldPoint(const vtkFloatingPointType theWorldPoint[4])
341 for(int i = 0; i<4; ++i) {
342 myWorldPoint[i] = theWorldPoint[i];
347 //==================================================================
348 // function : GetWorldPoint
350 //==================================================================
351 const vtkFloatingPointType* VTKViewer_FramedTextActor::GetWorldPoint()const
356 //==================================================================
357 // function : SetDistance
359 //==================================================================
360 void VTKViewer_FramedTextActor::SetDistance(const vtkFloatingPointType theDistance)
362 myDistance=theDistance;
365 //==================================================================
366 // function : GetDistance
368 //==================================================================
369 vtkFloatingPointType VTKViewer_FramedTextActor::GetDistance()const
374 //==================================================================
375 // function : SetMoveFrameFlag
376 // purpose : If moveFrameFlag is true, then frame with text is moved
378 //==================================================================
379 void VTKViewer_FramedTextActor::SetMoveFrameFlag(const int theMoveFrameFlag)
381 if(myMoveFrameFlag != theMoveFrameFlag) {
382 myMoveFrameFlag = theMoveFrameFlag;
387 //==================================================================
388 // function : GetDistance
390 //==================================================================
391 int VTKViewer_FramedTextActor::GetMoveFrameFlag() const
393 return myMoveFrameFlag;
397 //==================================================================
398 // function : ReleaseGraphicsResources
400 //==================================================================
401 void VTKViewer_FramedTextActor::ReleaseGraphicsResources(vtkWindow *win)
403 myTextActor->ReleaseGraphicsResources(win);
404 myBarActor->ReleaseGraphicsResources(win);
407 //==================================================================
408 // function : RenderOverlay
410 //==================================================================
411 int VTKViewer_FramedTextActor::RenderOverlay(vtkViewport *viewport)
413 int renderedSomething = 0;
414 myBarActor->RenderOverlay(viewport);
415 renderedSomething +=myTextActor->RenderOverlay(viewport);
416 return renderedSomething;
419 //==================================================================
420 // function : RenderOpaqueGeometry
422 //==================================================================
424 VTKViewer_FramedTextActor
425 ::RenderOpaqueGeometry(vtkViewport *theViewport)
427 int anIsRenderedSomething = 0;
429 int* aViewportSize = theViewport->GetSize();
430 int aViewPortWidth = aViewportSize[0];
431 int aViewPortHeight = aViewportSize[1];
432 if(aViewPortWidth == 1 || aViewPortHeight == 1)
433 return anIsRenderedSomething;
435 if(!myTextMapper->GetInput())
436 return anIsRenderedSomething;
441 vtkPoints *aPoints = vtkPoints::New();
442 aPoints->SetNumberOfPoints(aNbPoints);
443 myBar->SetPoints(aPoints);
446 vtkCellArray *aPolys = vtkCellArray::New();
447 aPolys->Allocate(aPolys->EstimateSize(1,4));
448 vtkIdType aPointsIds[4] = {0, 1, 3, 2};
449 aPolys->InsertNextCell(4,aPointsIds);
450 myBar->SetPolys(aPolys);
454 myTextMapper->GetSize(theViewport, aTextSize);
455 int aBarWidth = aTextSize[0];
456 int aBarHeight = aTextSize[1];
458 int aTextMargin = GetTextMargin();
460 vtkFloatingPointType xMin = 0.0;
461 vtkFloatingPointType xMax = 0.0;
462 vtkFloatingPointType yMin = -aBarHeight/2 - aTextMargin;
463 vtkFloatingPointType yMax = aBarHeight/2 + aTextMargin;
465 int aHorizontalOffset = GetLayoutType() == Horizontal ? myHorizontalOffset : 0;
466 int aVerticalOffset = GetLayoutType() == Vertical ? myVerticalOffset : 0;
468 if( myModePosition == BelowPoint )
470 theViewport->SetWorldPoint(myWorldPoint);
471 theViewport->WorldToDisplay();
473 vtkFloatingPointType aSelectionPoint[3];
474 theViewport->GetDisplayPoint(aSelectionPoint);
475 vtkFloatingPointType u = aSelectionPoint[0];
476 vtkFloatingPointType v = aSelectionPoint[1] - myDistance;
479 theViewport->ViewportToNormalizedViewport(u, v);
480 PositionCoordinate->SetValue(u, v);
482 myTextProperty->SetJustificationToCentered();
484 xMin = -aBarWidth/2 - aTextMargin;
485 xMax = aBarWidth/2 + aTextMargin;
487 else // except BelowPoint, only TopLeft and TopRight modes are supported at this moment
489 vtkFloatingPointType x = 0, xOffset = aHorizontalOffset + aTextMargin + OFFSET_SPACING;
490 vtkFloatingPointType y = 0, yOffset = aVerticalOffset + aTextMargin + OFFSET_SPACING;
492 if( myModePosition == TopLeft )
495 y = aViewPortHeight - yOffset - aBarHeight/2;
496 myTextProperty->SetJustificationToLeft();
498 xMin = - aTextMargin;
499 xMax = aBarWidth + aTextMargin;
501 else if( myModePosition == TopRight )
503 x = aViewPortWidth - xOffset;
504 y = aViewPortHeight - yOffset - aBarHeight/2;
505 myTextProperty->SetJustificationToRight();
507 xMin = -aBarWidth - aTextMargin;
511 PositionCoordinate->SetValue(x / (vtkFloatingPointType)aViewPortWidth,
512 y / (vtkFloatingPointType)aViewPortHeight);
515 aPoints->SetPoint(0, xMin, yMax, 0.0);
516 aPoints->SetPoint(1, xMin, yMin, 0.0);
517 aPoints->SetPoint(2, xMax, yMax, 0.0);
518 aPoints->SetPoint(3, xMax, yMin, 0.0);
520 myTextProperty->SetVerticalJustificationToCentered();
522 myTextMapper->GetTextProperty()->ShallowCopy(myTextProperty);
523 myBarActor ->GetPositionCoordinate()->SetReferenceCoordinate(PositionCoordinate);
524 myTextActor->GetPositionCoordinate()->SetReferenceCoordinate(PositionCoordinate);
526 myBuildTime.Modified();
528 return anIsRenderedSomething;