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