Salome HOME
Add missing resource item
[modules/gui.git] / src / VTKViewer / VTKViewer_FramedTextActor.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "VTKViewer_FramedTextActor.h"
21
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>
35
36 #include <QStringList>
37
38 #define TEXT_MARGIN    4
39 #define OFFSET_SPACING 2
40
41 //==================================================================
42 vtkStandardNewMacro(VTKViewer_FramedTextActor);
43
44 //==================================================================
45 // function : VTKViewer_FramedTextActor
46 // purpose  :
47 //==================================================================
48 VTKViewer_FramedTextActor::VTKViewer_FramedTextActor()
49 {
50   PositionCoordinate->SetCoordinateSystemToNormalizedViewport();
51
52   myTransparency=0.;
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); 
60
61   myTextProperty = vtkTextProperty::New();
62   myTextProperty->SetFontSize(12);
63   myTextProperty->SetBold(0);
64   myTextProperty->SetItalic(0);
65   myTextProperty->SetShadow(1);
66   myTextProperty->SetFontFamilyToArial();
67
68   myTextMapper=vtkTextMapper::New();
69   myTextMapper->SetInput("");
70   myTextMapper->GetTextProperty()->ShallowCopy(myTextProperty);
71   myTextActor=vtkActor2D::New();
72   myTextActor->SetMapper(myTextMapper);
73
74   myBarActor->SetVisibility(1);
75   myTextActor->SetVisibility(1);
76   myBarActor->SetPickable(0);
77   myTextActor->SetPickable(0);
78
79   myModePosition = BelowPoint;
80   myLayoutType = Vertical;
81
82   for(int i=0; i<4; i++) {
83     myWorldPoint[i] = 0.;
84   }
85   myDistance=10.;
86
87   myTextMargin = TEXT_MARGIN;
88
89   myHorizontalOffset = 0;
90   myVerticalOffset = 0;
91
92   myMoveFrameFlag = 0;
93 }
94
95 //==================================================================
96 // function : ~
97 // purpose  :
98 //==================================================================
99 VTKViewer_FramedTextActor::~VTKViewer_FramedTextActor()
100 {
101   myTextActor->Delete();
102   myTextMapper->Delete();
103   myTextProperty->Delete();
104   myBarActor->Delete();
105   myBarMapper->Delete();
106   myBar->Delete();
107 }
108
109 //==================================================================
110 // function : SetVisibility
111 // purpose  :
112 //==================================================================
113 void VTKViewer_FramedTextActor::SetVisibility (int theVisibility)
114 {
115   myBarActor->SetVisibility(theVisibility);
116   myTextActor->SetVisibility(theVisibility);
117 }
118
119 //==================================================================
120 // function : GetVisibility
121 // purpose  :
122 //==================================================================
123 int VTKViewer_FramedTextActor::GetVisibility() 
124 {
125   return myBarActor->GetVisibility();
126 }
127
128 //==================================================================
129 // function : SetPickable
130 // purpose  :
131 //==================================================================
132 void VTKViewer_FramedTextActor::SetPickable (int thePickability) 
133 {
134   myBarActor->SetPickable(thePickability);
135   myTextActor->SetPickable(thePickability);
136 }
137
138 //==================================================================
139 // function : GetPickable
140 // purpose  :
141 //==================================================================
142 int VTKViewer_FramedTextActor::GetPickable()
143 {
144   return myBarActor->GetPickable();
145 }
146
147 //==================================================================
148 // function : GetSize
149 // purpose  :
150 //==================================================================
151 void VTKViewer_FramedTextActor::GetSize(vtkRenderer* theRenderer, int theSize[2]) const
152 {
153   myTextMapper->GetSize(theRenderer, theSize);
154   theSize[0] = theSize[0] + 2 * GetTextMargin() + OFFSET_SPACING;
155   theSize[1] = theSize[1] + 2 * GetTextMargin() + OFFSET_SPACING;
156 }
157
158 //==================================================================
159 // function : SetForegroundColor
160 // purpose  :
161 //==================================================================
162 void VTKViewer_FramedTextActor::SetForegroundColor(const double r,
163                                                    const double g,
164                                                    const double b)
165 {
166   myTextProperty->SetColor(r, g, b);
167   myTextMapper->GetTextProperty()->ShallowCopy(myTextProperty);
168   Modified();
169 }
170
171 //==================================================================
172 // function : GetForegroundColor
173 // purpose  :
174 //==================================================================
175 void VTKViewer_FramedTextActor::GetForegroundColor(double& r,
176                                                    double& g,
177                                                    double& b)
178 {
179   double aColor[3];
180   myTextProperty->GetColor(aColor);
181   r = aColor[0];
182   g = aColor[1];
183   b = aColor[2];
184 }
185
186 //==================================================================
187 // function : SetBackgroundColor
188 // purpose  :
189 //==================================================================
190 void VTKViewer_FramedTextActor::SetBackgroundColor(const double r,
191                                                    const double g,
192                                                    const double b)
193 {
194   myBarActor->GetProperty()->SetColor(r, g, b);
195   Modified();
196 }
197
198 //==================================================================
199 // function : GetBackgroundColor
200 // purpose  :
201 //==================================================================
202 void VTKViewer_FramedTextActor::GetBackgroundColor(double& r,
203                                                    double& g,
204                                                    double& b)
205 {
206   double aColor[3];
207   myBarActor->GetProperty()->GetColor(aColor);
208   r = aColor[0];
209   g = aColor[1];
210   b = aColor[2];
211 }
212
213 //==================================================================
214 // function : SetTransparency
215 // purpose  :
216 //==================================================================
217 void VTKViewer_FramedTextActor::SetTransparency(const double theTransparency)
218 {
219   if (theTransparency>=0.  && theTransparency<=1.){
220     myTransparency=theTransparency;
221     myBarActor->GetProperty()->SetOpacity(1.-myTransparency);  
222     Modified();
223   }
224 }
225
226 //==================================================================
227 // function : GetTransparency
228 // purpose  :
229 //==================================================================
230 double VTKViewer_FramedTextActor::GetTransparency()const
231 {
232   return myTransparency;
233 }
234
235 //==================================================================
236 // function : SetTextMargin
237 // purpose  :
238 //==================================================================
239 void VTKViewer_FramedTextActor::SetTextMargin(const int theMargin)
240 {
241   if( theMargin >= 0 ) {
242     myTextMargin = theMargin;
243     Modified();
244   }
245 }
246
247 //==================================================================
248 // function : GetTextMargin
249 // purpose  :
250 //==================================================================
251 int VTKViewer_FramedTextActor::GetTextMargin() const
252 {
253   return myTextMargin;
254 }
255
256 //==================================================================
257 // function : SetOffset
258 // purpose  :
259 //==================================================================
260 void VTKViewer_FramedTextActor::SetOffset(const int theOffset[2])
261 {
262   myHorizontalOffset = theOffset[0];
263   myVerticalOffset = theOffset[1];
264   Modified();
265 }
266
267 //==================================================================
268 // function : SetText
269 // purpose  :
270 //==================================================================
271 void VTKViewer_FramedTextActor::SetText(const char* theText)
272 {
273   // remove whitespaces from from the start and the end
274   // additionally, consider a case of multi-string text
275   QString aString(theText);
276
277   QStringList aTrimmedStringList;
278   QStringList aStringList = aString.split("\n");
279   QStringListIterator anIter(aStringList);
280   while(anIter.hasNext())
281     aTrimmedStringList.append(anIter.next().trimmed());
282
283   myTextMapper->SetInput(aTrimmedStringList.join("\n").toLatin1().constData());
284   Modified();
285 }
286
287 //==================================================================
288 // function : GetText
289 // purpose  :
290 //==================================================================
291 char* VTKViewer_FramedTextActor::GetText()
292 {
293   return myTextMapper->GetInput();
294 }
295
296 //==================================================================
297 // function : SetModePosition
298 // purpose  :
299 //==================================================================
300 void VTKViewer_FramedTextActor::SetModePosition(const int theMode)
301 {
302   myModePosition = theMode;
303   Modified();
304 }
305
306 //==================================================================
307 // function : GetModePosition
308 // purpose  :
309 //==================================================================
310 int VTKViewer_FramedTextActor::GetModePosition()const
311 {
312   return myModePosition;
313 }
314
315 //==================================================================
316 // function : SetLayoutType
317 // purpose  :
318 //==================================================================
319 void VTKViewer_FramedTextActor::SetLayoutType(const int theType)
320 {
321   myLayoutType = theType;
322   Modified();
323 }
324
325 //==================================================================
326 // function : GetLayoutType
327 // purpose  :
328 //==================================================================
329 int VTKViewer_FramedTextActor::GetLayoutType() const
330 {
331   return myLayoutType;
332 }
333
334 //==================================================================
335 // function : SetWorldPoint
336 // purpose  :
337 //==================================================================
338 void VTKViewer_FramedTextActor::SetWorldPoint(const double theWorldPoint[4])
339 {
340   for(int i = 0; i<4; ++i) {
341     myWorldPoint[i] = theWorldPoint[i];
342   } 
343   Modified();
344 }
345
346 //==================================================================
347 // function : GetWorldPoint
348 // purpose  :
349 //==================================================================
350 const double* VTKViewer_FramedTextActor::GetWorldPoint()const 
351 {
352   return myWorldPoint;
353 }
354
355 //==================================================================
356 // function : SetDistance
357 // purpose  :
358 //==================================================================
359 void VTKViewer_FramedTextActor::SetDistance(const double theDistance)
360 {
361   myDistance=theDistance;
362 }
363
364 //==================================================================
365 // function : GetDistance
366 // purpose  :
367 //==================================================================
368 double VTKViewer_FramedTextActor::GetDistance()const
369 {
370   return myDistance;
371 }
372
373 //==================================================================
374 // function : SetMoveFrameFlag
375 // purpose  : If moveFrameFlag is true, then frame with text is moved
376 //            under world point
377 //==================================================================
378 void VTKViewer_FramedTextActor::SetMoveFrameFlag(const int theMoveFrameFlag)
379 {
380   if(myMoveFrameFlag != theMoveFrameFlag) {
381     myMoveFrameFlag = theMoveFrameFlag;
382     Modified();
383   }
384 }
385
386 //==================================================================
387 // function : GetDistance
388 // purpose  :
389 //==================================================================
390 int VTKViewer_FramedTextActor::GetMoveFrameFlag() const
391 {
392   return myMoveFrameFlag;
393 }
394
395
396 //==================================================================
397 // function : ReleaseGraphicsResources
398 // purpose  :
399 //==================================================================
400 void VTKViewer_FramedTextActor::ReleaseGraphicsResources(vtkWindow *win)
401 {
402   myTextActor->ReleaseGraphicsResources(win);
403   myBarActor->ReleaseGraphicsResources(win);
404 }
405
406 //==================================================================
407 // function : RenderOverlay
408 // purpose  :
409 //==================================================================
410 int VTKViewer_FramedTextActor::RenderOverlay(vtkViewport *viewport)
411 {
412   int renderedSomething = 0;
413   myBarActor->RenderOverlay(viewport);
414   renderedSomething +=myTextActor->RenderOverlay(viewport);
415   return renderedSomething;
416 }
417
418 //==================================================================
419 // function : RenderOpaqueGeometry
420 // purpose  :
421 //==================================================================
422 int 
423 VTKViewer_FramedTextActor
424 ::RenderOpaqueGeometry(vtkViewport *theViewport)
425 {
426   int anIsRenderedSomething = 0;
427
428   int* aViewportSize = theViewport->GetSize();
429   int aViewPortWidth = aViewportSize[0];
430   int aViewPortHeight = aViewportSize[1];
431   if(aViewPortWidth == 1 || aViewPortHeight == 1)
432     return anIsRenderedSomething;
433
434   if(!myTextMapper->GetInput())
435     return anIsRenderedSomething;
436
437   myBar->Initialize();
438
439   int aNbPoints = 4;
440   vtkPoints *aPoints = vtkPoints::New();
441   aPoints->SetNumberOfPoints(aNbPoints);
442   myBar->SetPoints(aPoints);
443   aPoints->Delete();
444
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);
450   aPolys->Delete(); 
451
452   int aTextSize[2]; 
453   myTextMapper->GetSize(theViewport, aTextSize);
454   int aBarWidth = aTextSize[0];
455   int aBarHeight = aTextSize[1];
456
457   int aTextMargin = GetTextMargin();
458
459   double xMin = 0.0;
460   double xMax = 0.0;
461   double yMin = -aBarHeight/2 - aTextMargin;
462   double yMax =  aBarHeight/2 + aTextMargin;
463
464   int aHorizontalOffset = GetLayoutType() == Horizontal ? myHorizontalOffset : 0;
465   int aVerticalOffset = GetLayoutType() == Vertical ? myVerticalOffset : 0;
466
467   if( myModePosition == BelowPoint )
468   {
469     theViewport->SetWorldPoint(myWorldPoint);
470     theViewport->WorldToDisplay();
471
472     double aSelectionPoint[3];
473     theViewport->GetDisplayPoint(aSelectionPoint);
474     double u = aSelectionPoint[0];
475     double v = aSelectionPoint[1] - myDistance;
476     if(myMoveFrameFlag)
477       v -= aBarHeight/2.;
478     theViewport->ViewportToNormalizedViewport(u, v);
479     PositionCoordinate->SetValue(u, v);
480
481     myTextProperty->SetJustificationToCentered();
482
483     xMin = -aBarWidth/2 - aTextMargin;
484     xMax =  aBarWidth/2 + aTextMargin;
485   }
486   else // except BelowPoint, only TopLeft and TopRight modes are supported at this moment
487   {
488     double x = 0, xOffset = aHorizontalOffset + aTextMargin + OFFSET_SPACING;
489     double y = 0, yOffset = aVerticalOffset + aTextMargin + OFFSET_SPACING;
490
491     if( myModePosition == TopLeft )
492     {
493       x = xOffset;
494       y = aViewPortHeight - yOffset - aBarHeight/2;
495       myTextProperty->SetJustificationToLeft();
496
497       xMin =            - aTextMargin;
498       xMax =  aBarWidth + aTextMargin;
499     }
500     else if( myModePosition == TopRight )
501     {
502       x = aViewPortWidth - xOffset;
503       y = aViewPortHeight - yOffset - aBarHeight/2;
504       myTextProperty->SetJustificationToRight();
505
506       xMin = -aBarWidth - aTextMargin;
507       xMax =              aTextMargin;
508     }
509
510     PositionCoordinate->SetValue(x / (double)aViewPortWidth,
511                                  y / (double)aViewPortHeight);
512   }
513
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);
518
519   myTextProperty->SetVerticalJustificationToCentered();
520
521   myTextMapper->GetTextProperty()->ShallowCopy(myTextProperty);
522   myBarActor ->GetPositionCoordinate()->SetReferenceCoordinate(PositionCoordinate);
523   myTextActor->GetPositionCoordinate()->SetReferenceCoordinate(PositionCoordinate);
524
525   myBuildTime.Modified();
526
527   return anIsRenderedSomething;
528 }