Back to index

salome-gui  6.5.0
VTKViewer_FramedTextActor.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "VTKViewer_FramedTextActor.h"
00021 
00022 #include <vtkCellArray.h>
00023 #include <vtkObjectFactory.h>
00024 #include <vtkPoints.h>
00025 #include <vtkPolyData.h>
00026 #include <vtkPolyDataMapper2D.h>
00027 #include <vtkProperty2D.h>
00028 #include <vtkRenderer.h>
00029 #include <vtkTextActor.h>
00030 #include <vtkTextMapper.h>
00031 #include <vtkTextProperty.h>
00032 #include <vtkTimeStamp.h>
00033 #include <vtkViewport.h>
00034 #include <vtkWindow.h>
00035 
00036 #include <QStringList>
00037 
00038 #define TEXT_MARGIN    4
00039 #define OFFSET_SPACING 2
00040 
00041 //==================================================================
00042 vtkCxxRevisionMacro(VTKViewer_FramedTextActor, "$Revision: 1.1.2.3.8.2.14.1 $");
00043 vtkStandardNewMacro(VTKViewer_FramedTextActor);
00044 
00045 //==================================================================
00046 // function : VTKViewer_FramedTextActor
00047 // purpose  :
00048 //==================================================================
00049 VTKViewer_FramedTextActor::VTKViewer_FramedTextActor()
00050 {
00051   PositionCoordinate->SetCoordinateSystemToNormalizedViewport();
00052 
00053   myTransparency=0.;
00054   myBar = vtkPolyData::New();
00055   myBarMapper = vtkPolyDataMapper2D::New();
00056   myBarMapper->SetInput(myBar);
00057   myBarActor = vtkActor2D::New();
00058   myBarActor->SetMapper(myBarMapper);
00059   myBarActor->GetProperty()->SetOpacity(1.-myTransparency);  
00060   myBarActor->GetProperty()->SetColor(.5, .5, .5); 
00061 
00062   myTextProperty = vtkTextProperty::New();
00063   myTextProperty->SetFontSize(12);
00064   myTextProperty->SetBold(0);
00065   myTextProperty->SetItalic(0);
00066   myTextProperty->SetShadow(1);
00067   myTextProperty->SetFontFamilyToArial();
00068 
00069   myTextMapper=vtkTextMapper::New();
00070   myTextMapper->SetInput("");
00071   myTextMapper->GetTextProperty()->ShallowCopy(myTextProperty);
00072   myTextActor=vtkActor2D::New();
00073   myTextActor->SetMapper(myTextMapper);
00074 
00075   myBarActor->SetVisibility(1);
00076   myTextActor->SetVisibility(1);
00077   myBarActor->SetPickable(0);
00078   myTextActor->SetPickable(0);
00079 
00080   myModePosition = BelowPoint;
00081   myLayoutType = Vertical;
00082 
00083   for(int i=0; i<4; i++) {
00084     myWorldPoint[i] = 0.;
00085   }
00086   myDistance=10.;
00087 
00088   myTextMargin = TEXT_MARGIN;
00089 
00090   myHorizontalOffset = 0;
00091   myVerticalOffset = 0;
00092 
00093   myMoveFrameFlag = 0;
00094 }
00095 
00096 //==================================================================
00097 // function : ~
00098 // purpose  :
00099 //==================================================================
00100 VTKViewer_FramedTextActor::~VTKViewer_FramedTextActor()
00101 {
00102   myTextActor->Delete();
00103   myTextMapper->Delete();
00104   myTextProperty->Delete();
00105   myBarActor->Delete();
00106   myBarMapper->Delete();
00107   myBar->Delete();
00108 }
00109 
00110 //==================================================================
00111 // function : SetVisibility
00112 // purpose  :
00113 //==================================================================
00114 void VTKViewer_FramedTextActor::SetVisibility (int theVisibility)
00115 {
00116   myBarActor->SetVisibility(theVisibility);
00117   myTextActor->SetVisibility(theVisibility);
00118 }
00119 
00120 //==================================================================
00121 // function : GetVisibility
00122 // purpose  :
00123 //==================================================================
00124 int VTKViewer_FramedTextActor::GetVisibility() 
00125 {
00126   return myBarActor->GetVisibility();
00127 }
00128 
00129 //==================================================================
00130 // function : SetPickable
00131 // purpose  :
00132 //==================================================================
00133 void VTKViewer_FramedTextActor::SetPickable (int thePickability) 
00134 {
00135   myBarActor->SetPickable(thePickability);
00136   myTextActor->SetPickable(thePickability);
00137 }
00138 
00139 //==================================================================
00140 // function : GetPickable
00141 // purpose  :
00142 //==================================================================
00143 int VTKViewer_FramedTextActor::GetPickable()
00144 {
00145   return myBarActor->GetPickable();
00146 }
00147 
00148 //==================================================================
00149 // function : GetSize
00150 // purpose  :
00151 //==================================================================
00152 void VTKViewer_FramedTextActor::GetSize(vtkRenderer* theRenderer, int theSize[2]) const
00153 {
00154   myTextMapper->GetSize(theRenderer, theSize);
00155   theSize[0] = theSize[0] + 2 * GetTextMargin() + OFFSET_SPACING;
00156   theSize[1] = theSize[1] + 2 * GetTextMargin() + OFFSET_SPACING;
00157 }
00158 
00159 //==================================================================
00160 // function : SetForegroundColor
00161 // purpose  :
00162 //==================================================================
00163 void VTKViewer_FramedTextActor::SetForegroundColor(const vtkFloatingPointType r,
00164                                                    const vtkFloatingPointType g,
00165                                                    const vtkFloatingPointType b)
00166 {
00167   myTextProperty->SetColor(r, g, b);
00168   myTextMapper->GetTextProperty()->ShallowCopy(myTextProperty);
00169   Modified();
00170 }
00171 
00172 //==================================================================
00173 // function : GetForegroundColor
00174 // purpose  :
00175 //==================================================================
00176 void VTKViewer_FramedTextActor::GetForegroundColor(vtkFloatingPointType& r,
00177                                                    vtkFloatingPointType& g,
00178                                                    vtkFloatingPointType& b)
00179 {
00180   vtkFloatingPointType aColor[3];
00181   myTextProperty->GetColor(aColor);
00182   r = aColor[0];
00183   g = aColor[1];
00184   b = aColor[2];
00185 }
00186 
00187 //==================================================================
00188 // function : SetBackgroundColor
00189 // purpose  :
00190 //==================================================================
00191 void VTKViewer_FramedTextActor::SetBackgroundColor(const vtkFloatingPointType r,
00192                                                    const vtkFloatingPointType g,
00193                                                    const vtkFloatingPointType b)
00194 {
00195   myBarActor->GetProperty()->SetColor(r, g, b);
00196   Modified();
00197 }
00198 
00199 //==================================================================
00200 // function : GetBackgroundColor
00201 // purpose  :
00202 //==================================================================
00203 void VTKViewer_FramedTextActor::GetBackgroundColor(vtkFloatingPointType& r,
00204                                                    vtkFloatingPointType& g,
00205                                                    vtkFloatingPointType& b)
00206 {
00207   vtkFloatingPointType aColor[3];
00208   myBarActor->GetProperty()->GetColor(aColor);
00209   r = aColor[0];
00210   g = aColor[1];
00211   b = aColor[2];
00212 }
00213 
00214 //==================================================================
00215 // function : SetTransparency
00216 // purpose  :
00217 //==================================================================
00218 void VTKViewer_FramedTextActor::SetTransparency(const vtkFloatingPointType theTransparency)
00219 {
00220   if (theTransparency>=0.  && theTransparency<=1.){
00221     myTransparency=theTransparency;
00222     myBarActor->GetProperty()->SetOpacity(1.-myTransparency);  
00223     Modified();
00224   }
00225 }
00226 
00227 //==================================================================
00228 // function : GetTransparency
00229 // purpose  :
00230 //==================================================================
00231 vtkFloatingPointType VTKViewer_FramedTextActor::GetTransparency()const
00232 {
00233   return myTransparency;
00234 }
00235 
00236 //==================================================================
00237 // function : SetTextMargin
00238 // purpose  :
00239 //==================================================================
00240 void VTKViewer_FramedTextActor::SetTextMargin(const int theMargin)
00241 {
00242   if( theMargin >= 0 ) {
00243     myTextMargin = theMargin;
00244     Modified();
00245   }
00246 }
00247 
00248 //==================================================================
00249 // function : GetTextMargin
00250 // purpose  :
00251 //==================================================================
00252 int VTKViewer_FramedTextActor::GetTextMargin() const
00253 {
00254   return myTextMargin;
00255 }
00256 
00257 //==================================================================
00258 // function : SetOffset
00259 // purpose  :
00260 //==================================================================
00261 void VTKViewer_FramedTextActor::SetOffset(const int theOffset[2])
00262 {
00263   myHorizontalOffset = theOffset[0];
00264   myVerticalOffset = theOffset[1];
00265   Modified();
00266 }
00267 
00268 //==================================================================
00269 // function : SetText
00270 // purpose  :
00271 //==================================================================
00272 void VTKViewer_FramedTextActor::SetText(const char* theText)
00273 {
00274   // remove whitespaces from from the start and the end
00275   // additionally, consider a case of multi-string text
00276   QString aString(theText);
00277 
00278   QStringList aTrimmedStringList;
00279   QStringList aStringList = aString.split("\n");
00280   QStringListIterator anIter(aStringList);
00281   while(anIter.hasNext())
00282     aTrimmedStringList.append(anIter.next().trimmed());
00283 
00284   myTextMapper->SetInput(aTrimmedStringList.join("\n").toLatin1().constData());
00285   Modified();
00286 }
00287 
00288 //==================================================================
00289 // function : GetText
00290 // purpose  :
00291 //==================================================================
00292 char* VTKViewer_FramedTextActor::GetText()
00293 {
00294   return myTextMapper->GetInput();
00295 }
00296 
00297 //==================================================================
00298 // function : SetModePosition
00299 // purpose  :
00300 //==================================================================
00301 void VTKViewer_FramedTextActor::SetModePosition(const int theMode)
00302 {
00303   myModePosition = theMode;
00304   Modified();
00305 }
00306 
00307 //==================================================================
00308 // function : GetModePosition
00309 // purpose  :
00310 //==================================================================
00311 int VTKViewer_FramedTextActor::GetModePosition()const
00312 {
00313   return myModePosition;
00314 }
00315 
00316 //==================================================================
00317 // function : SetLayoutType
00318 // purpose  :
00319 //==================================================================
00320 void VTKViewer_FramedTextActor::SetLayoutType(const int theType)
00321 {
00322   myLayoutType = theType;
00323   Modified();
00324 }
00325 
00326 //==================================================================
00327 // function : GetLayoutType
00328 // purpose  :
00329 //==================================================================
00330 int VTKViewer_FramedTextActor::GetLayoutType() const
00331 {
00332   return myLayoutType;
00333 }
00334 
00335 //==================================================================
00336 // function : SetWorldPoint
00337 // purpose  :
00338 //==================================================================
00339 void VTKViewer_FramedTextActor::SetWorldPoint(const vtkFloatingPointType theWorldPoint[4])
00340 {
00341   for(int i = 0; i<4; ++i) {
00342     myWorldPoint[i] = theWorldPoint[i];
00343   } 
00344   Modified();
00345 }
00346 
00347 //==================================================================
00348 // function : GetWorldPoint
00349 // purpose  :
00350 //==================================================================
00351 const vtkFloatingPointType* VTKViewer_FramedTextActor::GetWorldPoint()const 
00352 {
00353   return myWorldPoint;
00354 }
00355 
00356 //==================================================================
00357 // function : SetDistance
00358 // purpose  :
00359 //==================================================================
00360 void VTKViewer_FramedTextActor::SetDistance(const vtkFloatingPointType theDistance)
00361 {
00362   myDistance=theDistance;
00363 }
00364 
00365 //==================================================================
00366 // function : GetDistance
00367 // purpose  :
00368 //==================================================================
00369 vtkFloatingPointType VTKViewer_FramedTextActor::GetDistance()const
00370 {
00371   return myDistance;
00372 }
00373 
00374 //==================================================================
00375 // function : SetMoveFrameFlag
00376 // purpose  : If moveFrameFlag is true, then frame with text is moved
00377 //            under world point
00378 //==================================================================
00379 void VTKViewer_FramedTextActor::SetMoveFrameFlag(const int theMoveFrameFlag)
00380 {
00381   if(myMoveFrameFlag != theMoveFrameFlag) {
00382     myMoveFrameFlag = theMoveFrameFlag;
00383     Modified();
00384   }
00385 }
00386 
00387 //==================================================================
00388 // function : GetDistance
00389 // purpose  :
00390 //==================================================================
00391 int VTKViewer_FramedTextActor::GetMoveFrameFlag() const
00392 {
00393   return myMoveFrameFlag;
00394 }
00395 
00396 
00397 //==================================================================
00398 // function : ReleaseGraphicsResources
00399 // purpose  :
00400 //==================================================================
00401 void VTKViewer_FramedTextActor::ReleaseGraphicsResources(vtkWindow *win)
00402 {
00403   myTextActor->ReleaseGraphicsResources(win);
00404   myBarActor->ReleaseGraphicsResources(win);
00405 }
00406 
00407 //==================================================================
00408 // function : RenderOverlay
00409 // purpose  :
00410 //==================================================================
00411 int VTKViewer_FramedTextActor::RenderOverlay(vtkViewport *viewport)
00412 {
00413   int renderedSomething = 0;
00414   myBarActor->RenderOverlay(viewport);
00415   renderedSomething +=myTextActor->RenderOverlay(viewport);
00416   return renderedSomething;
00417 }
00418 
00419 //==================================================================
00420 // function : RenderOpaqueGeometry
00421 // purpose  :
00422 //==================================================================
00423 int 
00424 VTKViewer_FramedTextActor
00425 ::RenderOpaqueGeometry(vtkViewport *theViewport)
00426 {
00427   int anIsRenderedSomething = 0;
00428 
00429   int* aViewportSize = theViewport->GetSize();
00430   int aViewPortWidth = aViewportSize[0];
00431   int aViewPortHeight = aViewportSize[1];
00432   if(aViewPortWidth == 1 || aViewPortHeight == 1)
00433     return anIsRenderedSomething;
00434 
00435   if(!myTextMapper->GetInput())
00436     return anIsRenderedSomething;
00437 
00438   myBar->Initialize();
00439 
00440   int aNbPoints = 4;
00441   vtkPoints *aPoints = vtkPoints::New();
00442   aPoints->SetNumberOfPoints(aNbPoints);
00443   myBar->SetPoints(aPoints);
00444   aPoints->Delete();
00445 
00446   vtkCellArray *aPolys = vtkCellArray::New();
00447   aPolys->Allocate(aPolys->EstimateSize(1,4));
00448   vtkIdType aPointsIds[4] = {0, 1, 3, 2};
00449   aPolys->InsertNextCell(4,aPointsIds);
00450   myBar->SetPolys(aPolys);
00451   aPolys->Delete(); 
00452 
00453   int aTextSize[2]; 
00454   myTextMapper->GetSize(theViewport, aTextSize);
00455   int aBarWidth = aTextSize[0];
00456   int aBarHeight = aTextSize[1];
00457 
00458   int aTextMargin = GetTextMargin();
00459 
00460   vtkFloatingPointType xMin = 0.0;
00461   vtkFloatingPointType xMax = 0.0;
00462   vtkFloatingPointType yMin = -aBarHeight/2 - aTextMargin;
00463   vtkFloatingPointType yMax =  aBarHeight/2 + aTextMargin;
00464 
00465   int aHorizontalOffset = GetLayoutType() == Horizontal ? myHorizontalOffset : 0;
00466   int aVerticalOffset = GetLayoutType() == Vertical ? myVerticalOffset : 0;
00467 
00468   if( myModePosition == BelowPoint )
00469   {
00470     theViewport->SetWorldPoint(myWorldPoint);
00471     theViewport->WorldToDisplay();
00472 
00473     vtkFloatingPointType aSelectionPoint[3];
00474     theViewport->GetDisplayPoint(aSelectionPoint);
00475     vtkFloatingPointType u = aSelectionPoint[0];
00476     vtkFloatingPointType v = aSelectionPoint[1] - myDistance;
00477     if(myMoveFrameFlag)
00478       v -= aBarHeight/2.;
00479     theViewport->ViewportToNormalizedViewport(u, v);
00480     PositionCoordinate->SetValue(u, v);
00481 
00482     myTextProperty->SetJustificationToCentered();
00483 
00484     xMin = -aBarWidth/2 - aTextMargin;
00485     xMax =  aBarWidth/2 + aTextMargin;
00486   }
00487   else // except BelowPoint, only TopLeft and TopRight modes are supported at this moment
00488   {
00489     vtkFloatingPointType x = 0, xOffset = aHorizontalOffset + aTextMargin + OFFSET_SPACING;
00490     vtkFloatingPointType y = 0, yOffset = aVerticalOffset + aTextMargin + OFFSET_SPACING;
00491 
00492     if( myModePosition == TopLeft )
00493     {
00494       x = xOffset;
00495       y = aViewPortHeight - yOffset - aBarHeight/2;
00496       myTextProperty->SetJustificationToLeft();
00497 
00498       xMin =            - aTextMargin;
00499       xMax =  aBarWidth + aTextMargin;
00500     }
00501     else if( myModePosition == TopRight )
00502     {
00503       x = aViewPortWidth - xOffset;
00504       y = aViewPortHeight - yOffset - aBarHeight/2;
00505       myTextProperty->SetJustificationToRight();
00506 
00507       xMin = -aBarWidth - aTextMargin;
00508       xMax =              aTextMargin;
00509     }
00510 
00511     PositionCoordinate->SetValue(x / (vtkFloatingPointType)aViewPortWidth,
00512                                  y / (vtkFloatingPointType)aViewPortHeight);
00513   }
00514 
00515   aPoints->SetPoint(0, xMin, yMax, 0.0);
00516   aPoints->SetPoint(1, xMin, yMin, 0.0);
00517   aPoints->SetPoint(2, xMax, yMax, 0.0);
00518   aPoints->SetPoint(3, xMax, yMin, 0.0);
00519 
00520   myTextProperty->SetVerticalJustificationToCentered();
00521 
00522   myTextMapper->GetTextProperty()->ShallowCopy(myTextProperty);
00523   myBarActor ->GetPositionCoordinate()->SetReferenceCoordinate(PositionCoordinate);
00524   myTextActor->GetPositionCoordinate()->SetReferenceCoordinate(PositionCoordinate);
00525 
00526   myBuildTime.Modified();
00527 
00528   return anIsRenderedSomething;
00529 }