Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/visu.git] / src / VVTK / VVTK_SegmentationCursorDlg.cxx
1 //  VISU VISUGUI : GUI of VISU component
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : VVTK_SegmentationCursorDlg.cxx
8 //  Author : Oleg Uvarov
9 //  Module : VISU
10
11 #include "VVTK_SegmentationCursorDlg.h"
12 #include "VVTK_PrimitiveBox.h"
13 #include "VVTK_SizeBox.h"
14
15 #include "VISU_GaussPtsAct.h"
16 #include "VISU_GaussPtsSettings.h"
17
18 #include "VISU_WidgetCtrl.hxx"
19 #include "VISU_PlanesWidget.hxx"
20 #include "VISU_SphereWidget.hxx"
21
22 #include "VISU_GaussPointsPL.hxx"
23 #include "VISU_OpenGLPointSpriteMapper.hxx"
24
25 #include "LightApp_Application.h"
26 #include "SUIT_MessageBox.h"
27 #include "SUIT_ResourceMgr.h"
28 #include "SUIT_Session.h"
29
30 #include "SVTK_RenderWindowInteractor.h"
31
32 #include <vtkActorCollection.h>
33 #include <vtkCallbackCommand.h>
34 #include <vtkObjectFactory.h>
35 #include <vtkRenderer.h>
36 #include <vtkRenderWindowInteractor.h>
37 #include <vtkSmartPointer.h>
38 #include <vtkImageData.h>
39
40 #include "utilities.h"
41
42 #include <qbuttongroup.h>
43 #include <qcolordialog.h>
44 #include <qfiledialog.h>
45 #include <qgroupbox.h>
46 #include <qlabel.h>
47 #include <qlayout.h>
48 #include <qlineedit.h>
49 #include <qpushbutton.h>
50 #include <qradiobutton.h>
51 #include <qtabwidget.h>
52 #include <qvbox.h>
53
54 #include "QtxAction.h"
55 #include "QtxDblSpinBox.h"
56 #include "QtxIntSpinBox.h"
57
58 using namespace std;
59
60
61 //----------------------------------------------------------------
62 namespace VISU
63 {
64   inline
65   QString
66   Image2VTI(const QString& theImageFileName)
67   {
68     QFileInfo aFileInfo(theImageFileName);
69     QString aFormat = aFileInfo.extension(FALSE);
70     QString aVTIName = QString("/tmp/") + getenv("USER")  + "-" + aFileInfo.baseName(TRUE) + ".vti";
71     QString aCommand = QString( "VISU_img2vti " ) + aFormat + " " +  theImageFileName + " " + aVTIName;
72
73     if(system( aCommand.latin1() ) == 0)
74       return aVTIName;
75
76     return QString::null;
77   }
78
79   inline
80   void
81   RemoveFile(const QString& theFileName)
82   {
83     if( !theFileName.isNull() ){
84       QString aCommand = QString( "rm -fr " ) + theFileName;
85       system( aCommand.latin1() );
86     }
87   }
88   
89   
90   TTextureValue
91   GetTexture(const QString& theMainTexture, 
92              const QString& theAlphaTexture)
93   {
94     typedef std::pair<std::string,std::string> TTextureKey;
95     typedef std::map<TTextureKey,TTextureValue> TTextureMap;
96     
97     static TTextureMap aTextureMap;
98     
99     TTextureValue aTextureValue;
100     TTextureKey aTextureKey(theMainTexture.latin1(),theAlphaTexture.latin1());
101     TTextureMap::const_iterator anIter = aTextureMap.find(aTextureKey);
102     if(anIter != aTextureMap.end()){
103       aTextureValue = anIter->second;
104     }else{
105       QString aMainTextureVTI = Image2VTI(theMainTexture);
106       QString anAlphaTextureVTI = Image2VTI(theAlphaTexture);
107       
108       if( !aMainTextureVTI.isNull() && !anAlphaTextureVTI.isNull() ){
109         aTextureValue =
110           VISU_GaussPointsPL::MakeTexture( aMainTextureVTI.latin1(), 
111                                            anAlphaTextureVTI.latin1());
112
113         if( aTextureValue.GetPointer() )
114           aTextureMap[aTextureKey] = aTextureValue;
115       }
116
117       RemoveFile(aMainTextureVTI);
118       RemoveFile(anAlphaTextureVTI);
119     }
120
121     return aTextureValue;
122   }
123 }
124
125
126 //----------------------------------------------------------------
127 VVTK_SegmentationCursorDlg::VVTK_SegmentationCursorDlg( QWidget* parent, const char* name )
128   :QDialog( parent, name, false, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
129    myEventCallbackCommand( vtkCallbackCommand::New() ),
130    myInsideCursorSettings( VISU_InsideCursorSettings::New() ),
131    myOutsideCursorSettings( VISU_OutsideCursorSettings::New() ),
132    myIsPlaneSegmentation( true ),
133    myWidgetCtrl(NULL),
134    myInteractor(NULL)
135 {
136   myPriority = 0.0;
137   myEventCallbackCommand->Delete();
138   myEventCallbackCommand->SetClientData(this); 
139   myEventCallbackCommand->SetCallback(VVTK_SegmentationCursorDlg::ProcessEvents);
140
141   myInsideCursorSettings->AddObserver(VISU::UpdateFromSettingsEvent, 
142                                       myEventCallbackCommand.GetPointer(), 
143                                       myPriority);
144
145   setCaption( tr( "SEGMENTATION_CURSOR_DLG_TITLE" ) );
146   setSizeGripEnabled(TRUE);
147
148   QVBoxLayout* TopLayout = new QVBoxLayout( this );
149   TopLayout->setSpacing(6);
150   TopLayout->setMargin(11);
151
152   myTabBox = new QTabWidget( this );
153
154   // Segmentation cursor pane
155   mySegmentationCursorBox = new QVBox( this );
156   mySegmentationCursorBox->setMargin(11);
157   mySegmentationCursorBox->setSpacing(6);
158   mySegmentationCursorBox->layout()->setAlignment(Qt::AlignTop);
159
160   // Origin
161   myOriginGroup = new QGroupBox( tr( "ORIGIN_TITLE" ), mySegmentationCursorBox, "OriginGroup" );
162   myOriginGroup->setColumnLayout( 0, Qt::Vertical );
163   myOriginGroup->layout()->setSpacing( 0 );
164   myOriginGroup->layout()->setMargin( 0 );
165
166   QGridLayout* OriginGroupLayout = new QGridLayout( myOriginGroup->layout() );
167   OriginGroupLayout->setAlignment(Qt::AlignTop | Qt::AlignCenter);
168   OriginGroupLayout->setSpacing(6);
169   OriginGroupLayout->setMargin(11);
170
171   QLabel* XOriginLabel = new QLabel( tr( "ORIGIN_X" ), myOriginGroup );
172   myXOriginSpinBox = new QtxDblSpinBox( -1000.0, 1000.0, 0.1, myOriginGroup );
173   myXOriginSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
174   myXOriginSpinBox->setMinimumWidth( 100 );
175   myXOriginSpinBox->setValue( 0.0 );
176
177   QLabel* YOriginLabel = new QLabel( tr( "ORIGIN_Y" ), myOriginGroup );
178   myYOriginSpinBox = new QtxDblSpinBox( -1000.0, 1000.0, 0.1, myOriginGroup );
179   myYOriginSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
180   myYOriginSpinBox->setMinimumWidth( 100 );
181   myYOriginSpinBox->setValue( 0.0 );
182
183   QLabel* ZOriginLabel = new QLabel( tr( "ORIGIN_Z" ), myOriginGroup );
184   myZOriginSpinBox = new QtxDblSpinBox( -1000.0, 1000.0, 0.1, myOriginGroup );
185   //myZOriginSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
186   myZOriginSpinBox->setMinimumWidth( 100 );
187   myZOriginSpinBox->setValue( 1.0 );
188
189   OriginGroupLayout->addWidget(   XOriginLabel,   0, 0 );
190   OriginGroupLayout->addWidget( myXOriginSpinBox, 0, 1 );
191   OriginGroupLayout->addWidget(   YOriginLabel,   0, 2 );
192   OriginGroupLayout->addWidget( myYOriginSpinBox, 0, 3 );
193   OriginGroupLayout->addWidget(   ZOriginLabel,   0, 4 );
194   OriginGroupLayout->addWidget( myZOriginSpinBox, 0, 5 );
195
196   // Direction ( Plane Segmentation )
197   myDirectionGroup = new QGroupBox( tr( "DIRECTION_TITLE" ), mySegmentationCursorBox, "DirectionGroup" );
198   myDirectionGroup->setColumnLayout( 0, Qt::Vertical );
199   myDirectionGroup->layout()->setSpacing( 0 );
200   myDirectionGroup->layout()->setMargin( 0 );
201
202   QGridLayout* DirectionGroupLayout = new QGridLayout( myDirectionGroup->layout() );
203   DirectionGroupLayout->setAlignment(Qt::AlignTop | Qt::AlignCenter);
204   DirectionGroupLayout->setSpacing(6);
205   DirectionGroupLayout->setMargin(11);
206
207   QLabel* DXDirectionLabel = new QLabel( tr( "DIRECTION_DX" ), myDirectionGroup );
208   myDXDirectionSpinBox = new QtxDblSpinBox( -1.0, 1.0, 0.1, myDirectionGroup );
209   myDXDirectionSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
210   myDXDirectionSpinBox->setMinimumWidth( 100 );
211   myDXDirectionSpinBox->setValue( 0.0 );
212
213   QLabel* DYDirectionLabel = new QLabel( tr( "DIRECTION_DY" ), myDirectionGroup );
214   myDYDirectionSpinBox = new QtxDblSpinBox( -1.0, 1.0, 0.1, myDirectionGroup );
215   myDYDirectionSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
216   myDYDirectionSpinBox->setMinimumWidth( 100 );
217   myDYDirectionSpinBox->setValue( 0.0 );
218
219   QLabel* DZDirectionLabel = new QLabel( tr( "DIRECTION_DZ" ), myDirectionGroup );
220   myDZDirectionSpinBox = new QtxDblSpinBox( -1.0, 1.0, 0.1, myDirectionGroup );
221   myDZDirectionSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
222   myDZDirectionSpinBox->setMinimumWidth( 100 );
223   myDZDirectionSpinBox->setValue( 1.0 );
224
225   DirectionGroupLayout->addWidget(   DXDirectionLabel,   0, 0 );
226   DirectionGroupLayout->addWidget( myDXDirectionSpinBox, 0, 1 );
227   DirectionGroupLayout->addWidget(   DYDirectionLabel,   0, 2 );
228   DirectionGroupLayout->addWidget( myDYDirectionSpinBox, 0, 3 );
229   DirectionGroupLayout->addWidget(   DZDirectionLabel,   0, 4 );
230   DirectionGroupLayout->addWidget( myDZDirectionSpinBox, 0, 5 );
231
232   // Depth ( Plane Segmentation )
233   myDepthGroup = new QGroupBox( tr( "DEPTH_TITLE" ), mySegmentationCursorBox, "DepthGroup" );
234   myDepthGroup->setColumnLayout( 0, Qt::Vertical );
235   myDepthGroup->layout()->setSpacing( 0 );
236   myDepthGroup->layout()->setMargin( 0 );
237
238   QGridLayout* DepthGroupLayout = new QGridLayout( myDepthGroup->layout() );
239   DepthGroupLayout->setAlignment(Qt::AlignTop | Qt::AlignCenter);
240   DepthGroupLayout->setSpacing(6);
241   DepthGroupLayout->setMargin(11);
242
243   QLabel* DepthLabel = new QLabel( tr( "DEPTH" ), myDepthGroup );
244   myDepthSpinBox = new QtxDblSpinBox( 0.0, VTK_LARGE_FLOAT, 0.1, myDepthGroup );
245   myDepthSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
246   myDepthSpinBox->setMinimumWidth( 100 );
247   myDepthSpinBox->setValue( 1.0 );
248
249   DepthGroupLayout->addWidget(   DepthLabel,   0, 0 );
250   DepthGroupLayout->addWidget( myDepthSpinBox, 0, 1 );
251
252   // Radius ( Sphere Segmentation )
253   myRadiusGroup = new QGroupBox( tr( "RADIUS_TITLE" ), mySegmentationCursorBox, "RadiusGroup" );
254   myRadiusGroup->setColumnLayout( 0, Qt::Vertical );
255   myRadiusGroup->layout()->setSpacing( 0 );
256   myRadiusGroup->layout()->setMargin( 0 );
257
258   QGridLayout* RadiusGroupLayout = new QGridLayout( myRadiusGroup->layout() );
259   RadiusGroupLayout->setAlignment(Qt::AlignTop | Qt::AlignCenter);
260   RadiusGroupLayout->setSpacing(6);
261   RadiusGroupLayout->setMargin(11);
262
263   QLabel* RadiusLabel = new QLabel( tr( "RADIUS" ),myRadiusGroup  );
264   myRadiusSpinBox = new QtxDblSpinBox( 0.0, 1000.0, 1.0,myRadiusGroup  );
265   myRadiusSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
266   myRadiusSpinBox->setMinimumWidth( 100 );
267   myRadiusSpinBox->setValue( 100.0 );
268
269   QLabel* RatioLabel = new QLabel( tr( "RATIO" ), myRadiusGroup );
270   myRatioSpinBox = new QtxDblSpinBox( 0.1, 10.0, 0.1,myRadiusGroup  );
271   myRatioSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
272   myRatioSpinBox->setMinimumWidth( 100 );
273   myRatioSpinBox->setValue( 2.0 );
274
275   RadiusGroupLayout->addWidget( RadiusLabel,     0, 0 );
276   RadiusGroupLayout->addWidget( myRadiusSpinBox, 0, 1 );
277   RadiusGroupLayout->addWidget( RatioLabel,      0, 2 );
278   RadiusGroupLayout->addWidget( myRatioSpinBox,  0, 3 );
279
280
281   myTabBox->addTab( mySegmentationCursorBox, tr( "SEGMENTATION_CURSOR_TAB" ) );
282
283
284   // Gauss points pane
285   myGaussPointsBox = new QVBox( this );
286   myGaussPointsBox->setMargin(11);
287   myGaussPointsBox->setSpacing(6);
288
289   // Inside Gauss points
290   QGroupBox* anInsideGroup = new QGroupBox( tr( "INSIDE_GAUSS_POINTS" ), myGaussPointsBox );
291   anInsideGroup->setColumnLayout( 0, Qt::Vertical );
292   anInsideGroup->layout()->setSpacing( 0 );
293   anInsideGroup->layout()->setMargin( 0 );
294
295   QGridLayout* anInsideGroupLayout = new QGridLayout( anInsideGroup->layout() );
296   anInsideGroupLayout->setAlignment(Qt::AlignTop | Qt::AlignCenter);
297   anInsideGroupLayout->setSpacing(6);
298   anInsideGroupLayout->setMargin(11);
299
300   // Primitive
301   myInsidePrimitiveBox = new VVTK_PrimitiveBox( anInsideGroup );
302
303   // Size
304   myInsideSizeBox = new VVTK_SizeBox( anInsideGroup );
305   myInsideSizeBox->setType( VVTK_SizeBox::Inside );
306
307   anInsideGroupLayout->addWidget( myInsidePrimitiveBox, 0, 0 );
308   anInsideGroupLayout->addWidget( myInsideSizeBox, 1, 0 );
309
310   // Outside Gauss points
311   QGroupBox* anOutsideGroup = new QGroupBox( tr( "OUTSIDE_GAUSS_POINTS" ), myGaussPointsBox );
312   anOutsideGroup->setColumnLayout( 0, Qt::Vertical );
313   anOutsideGroup->layout()->setSpacing( 0 );
314   anOutsideGroup->layout()->setMargin( 0 );
315
316   QGridLayout* anOutsideGroupLayout = new QGridLayout( anOutsideGroup->layout() );
317   anOutsideGroupLayout->setAlignment(Qt::AlignTop | Qt::AlignCenter);
318   anOutsideGroupLayout->setSpacing(6);
319   anOutsideGroupLayout->setMargin(11);
320
321   // Primitive
322   myOutsidePrimitiveBox = new VVTK_PrimitiveBox( anOutsideGroup );
323
324   // Size
325   myOutsideSizeBox = new VVTK_SizeBox( anOutsideGroup );
326   myOutsideSizeBox->setType( VVTK_SizeBox::Outside );
327
328   anOutsideGroupLayout->addWidget( myOutsidePrimitiveBox, 0, 0 );
329   anOutsideGroupLayout->addWidget( myOutsideSizeBox, 1, 0 );
330
331
332   // Magnification
333   QGroupBox* MagnificationGroup = new QGroupBox ( tr( "MAGNIFICATION_TITLE" ), myGaussPointsBox, "MagnificationGroup" );
334   MagnificationGroup->setColumnLayout(0, Qt::Vertical );
335   MagnificationGroup->layout()->setSpacing( 0 );
336   MagnificationGroup->layout()->setMargin( 0 );
337
338   QGridLayout* MagnificationGroupLayout = new QGridLayout (MagnificationGroup->layout());
339   MagnificationGroupLayout->setAlignment(Qt::AlignTop | Qt::AlignCenter);
340   MagnificationGroupLayout->setSpacing(6);
341   MagnificationGroupLayout->setMargin(11);
342
343   myMagnificationLabel = new QLabel( tr( "MAGNIFICATION" ), MagnificationGroup );
344   myMagnificationSpinBox = new QtxIntSpinBox( 1, 10000, 10, MagnificationGroup );
345   myMagnificationSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
346
347   MagnificationGroupLayout->addWidget( myMagnificationLabel, 0, 0 );
348   MagnificationGroupLayout->addWidget( myMagnificationSpinBox, 0, 1 );
349
350   // Increment
351   myIncrementLabel = new QLabel( tr( "INCREMENT" ), MagnificationGroup );
352   myIncrementSpinBox = new QtxDblSpinBox( 0.01, 10, 0.1, MagnificationGroup );
353   myIncrementSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
354
355   MagnificationGroupLayout->addWidget( myIncrementLabel, 0, 2 );
356   MagnificationGroupLayout->addWidget( myIncrementSpinBox, 0, 3 );
357
358
359   myTabBox->addTab( myGaussPointsBox, tr( "GAUSS_POINTS_TAB" ) );
360   myTabBox->showPage( myGaussPointsBox );
361
362
363   // Common buttons ===========================================================
364   QGroupBox* GroupButtons = new QGroupBox( this, "GroupButtons" );
365   GroupButtons->setColumnLayout(0, Qt::Vertical );
366   GroupButtons->layout()->setSpacing( 0 );
367   GroupButtons->layout()->setMargin( 0 );
368   QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
369   GroupButtonsLayout->setAlignment( Qt::AlignTop );
370   GroupButtonsLayout->setSpacing( 6 );
371   GroupButtonsLayout->setMargin( 11 );
372
373   QPushButton* buttonApply = new QPushButton( tr( "&Apply" ), GroupButtons, "buttonApply" );
374   buttonApply->setAutoDefault( TRUE );
375   buttonApply->setDefault( TRUE );
376   GroupButtonsLayout->addWidget( buttonApply, 0, 0 );
377   GroupButtonsLayout->addItem( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
378
379   QPushButton* buttonClose = new QPushButton( tr( "&Close" ) , GroupButtons, "buttonClose" );
380   buttonClose->setAutoDefault( TRUE );
381   GroupButtonsLayout->addWidget( buttonClose, 0, 2 );
382
383   QPushButton* buttonHelp = new QPushButton( tr( "&Help" ) , GroupButtons, "buttonHelp" );
384   buttonHelp->setAutoDefault( TRUE );
385   GroupButtonsLayout->addWidget( buttonHelp, 0, 3 );
386
387   TopLayout->addWidget( myTabBox );
388   TopLayout->addWidget( GroupButtons );
389
390   connect( buttonApply, SIGNAL( clicked() ), this, SLOT( onClickApply() ) );
391   connect( buttonClose, SIGNAL( clicked() ), this, SLOT( onClickClose() ) );
392   connect( buttonHelp,  SIGNAL( clicked() ), this, SLOT( onClickHelp() ) );
393
394   connect(parent, SIGNAL(Show( QShowEvent * )), this, SLOT(onParentShow()));
395   connect(parent, SIGNAL(Hide( QHideEvent * )), this, SLOT(onParentHide()));
396 }
397
398 VVTK_SegmentationCursorDlg::~VVTK_SegmentationCursorDlg()
399 {
400   SetWidgetCtrl(NULL);
401 }
402
403 float VVTK_SegmentationCursorDlg::getMagnification() const
404 {
405   return myMagnificationSpinBox->value() / 100.0;
406 }
407
408 void VVTK_SegmentationCursorDlg::setMagnification( float theMagnification )
409 {
410   myMagnificationSpinBox->setValue( ( int )( theMagnification * 100 ) );
411 }
412
413 float VVTK_SegmentationCursorDlg::getIncrement() const
414 {
415   return myIncrementSpinBox->value();
416 }
417
418 void VVTK_SegmentationCursorDlg::setIncrement( float theIncrement )
419 {
420   myIncrementSpinBox->setValue( theIncrement );
421 }
422
423
424 void VVTK_SegmentationCursorDlg::SetWidgetCtrl( VISU_WidgetCtrl* theWidgetCtrl )
425 {
426   if(myWidgetCtrl == theWidgetCtrl)
427     return;
428
429   if(myWidgetCtrl)
430     myWidgetCtrl->RemoveObserver(myEventCallbackCommand.GetPointer());
431
432   myWidgetCtrl = theWidgetCtrl;
433
434   if(theWidgetCtrl)
435     theWidgetCtrl->AddObserver(vtkCommand::EndInteractionEvent, 
436                                myEventCallbackCommand.GetPointer(), 
437                                myPriority);
438 }
439
440 void VVTK_SegmentationCursorDlg::ProcessEvents(vtkObject* vtkNotUsed(theObject), 
441                                                unsigned long theEvent,
442                                                void* theClientData, 
443                                                void* vtkNotUsed(theCallData))
444 {
445   VVTK_SegmentationCursorDlg* self = reinterpret_cast<VVTK_SegmentationCursorDlg*>(theClientData);
446
447   switch(theEvent){
448   case vtkCommand::EndInteractionEvent:
449     self->UpdateSegmentation();
450     break;
451   case VISU::UpdateFromSettingsEvent:
452     self->GetOutsideCursorSettings()->SetMagnification( self->GetInsideCursorSettings()->GetMagnification() );
453     self->GetOutsideCursorSettings()->SetIncrement( self->GetInsideCursorSettings()->GetIncrement() );
454
455     self->UpdateInsideGaussPoints();
456     self->UpdateOutsideGaussPoints();
457
458     self->GetInsideCursorSettings()->InvokeEvent(VISU::UpdateInsideSettingsEvent,NULL);
459     self->GetOutsideCursorSettings()->InvokeEvent(VISU::UpdateOutsideSettingsEvent,NULL);
460     break;
461   }
462 }
463
464 void VVTK_SegmentationCursorDlg::UpdateSegmentation()
465 {
466   if( myIsPlaneSegmentation )
467   {
468     myDirectionGroup->show();
469     myDepthGroup->show();
470     myRadiusGroup->hide();
471
472     VISU_PlanesWidget *pPlanesWidget=myWidgetCtrl->GetPlanesWidget();
473     vtkFloatingPointType origin[3];
474     pPlanesWidget->GetOrigin( origin );
475     myXOriginSpinBox->setValue( origin[0] );
476     myYOriginSpinBox->setValue( origin[1] );
477     myZOriginSpinBox->setValue( origin[2] );
478
479     vtkFloatingPointType normal[3];
480     pPlanesWidget->GetNormal( normal );
481     myDXDirectionSpinBox->setValue( normal[0] );
482     myDYDirectionSpinBox->setValue( normal[1] );
483     myDZDirectionSpinBox->setValue( normal[2] );
484
485     myDepthSpinBox->setValue( pPlanesWidget->Distance() );
486   }
487   else
488   {
489     myDirectionGroup->hide();
490     myDepthGroup->hide();
491     myRadiusGroup->show();
492     
493     VISU_SphereWidget *pSphereWidget=myWidgetCtrl->GetSphereWidget();
494     vtkFloatingPointType origin[3], aRadius;
495     pSphereWidget->GetCenter(origin);
496     myXOriginSpinBox->setValue( origin[0] );
497     myYOriginSpinBox->setValue( origin[1] );
498     myZOriginSpinBox->setValue( origin[2] );
499     aRadius=pSphereWidget->GetRadius();
500     myRadiusSpinBox->setValue(aRadius);
501     myRatioSpinBox->setValue(pSphereWidget->GetRatio());
502   }
503 }
504
505 void VVTK_SegmentationCursorDlg::UpdateInsideGaussPoints()
506 {
507   int aPrimitiveType = VISU_OpenGLPointSpriteMapper::PointSprite;
508   vtkFloatingPointType aClamp = 200.0;
509   QString aMainTexture = QString( getenv( "VISU_ROOT_DIR") ) + "/share/salome/resources/sprite_texture.bmp";
510   QString anAlphaTexture = QString( getenv( "VISU_ROOT_DIR") ) + "/share/salome/resources/sprite_alpha.bmp";
511   vtkFloatingPointType anAlphaThreshold = 0.1;
512   int aResolution = 8;
513   int aMinSize = 3;
514   int aMaxSize = 33;
515   int aMagnification = 100;
516   vtkFloatingPointType anIncrement = 2.0;
517
518   if( !myInsideCursorSettings->GetInitial() )
519   {
520     myInsidePrimitiveBox->setPrimitiveType( myInsideCursorSettings->GetPrimitiveType() );
521     myInsidePrimitiveBox->setClamp( myInsideCursorSettings->GetClamp() );
522     myInsidePrimitiveBox->setMainTexture( myInsideMainTexture );
523     myInsidePrimitiveBox->setAlphaTexture( myInsideAlphaTexture );
524     myInsidePrimitiveBox->setAlphaThreshold( myInsideCursorSettings->GetAlphaThreshold() );
525     myInsidePrimitiveBox->setResolution( myInsideCursorSettings->GetResolution() );
526
527     myInsideSizeBox->setMinSize( myInsideCursorSettings->GetMinSize() );
528     myInsideSizeBox->setMaxSize( myInsideCursorSettings->GetMaxSize() );
529
530     this->setMagnification( myInsideCursorSettings->GetMagnification() );
531     this->setIncrement( myInsideCursorSettings->GetIncrement() );
532
533     return;
534   }
535
536   SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
537
538   aPrimitiveType = aResourceMgr->integerValue( "VISU", "inside_point_sprite_primitive_type", aPrimitiveType );
539   myInsidePrimitiveBox->setPrimitiveType( aPrimitiveType );
540
541   aClamp = aResourceMgr->doubleValue( "VISU", "inside_point_sprite_clamp", aClamp );
542   myInsidePrimitiveBox->setClamp( aClamp );
543
544   aMainTexture = aResourceMgr->stringValue( "VISU", "inside_point_sprite_main_texture", aMainTexture );
545   myInsidePrimitiveBox->setMainTexture( aMainTexture );
546
547   anAlphaTexture = aResourceMgr->stringValue( "VISU", "inside_point_sprite_alpha_texture", anAlphaTexture );
548   myInsidePrimitiveBox->setAlphaTexture( anAlphaTexture );
549
550   anAlphaThreshold = aResourceMgr->doubleValue( "VISU", "inside_point_sprite_alpha_threshold", anAlphaThreshold );
551   myInsidePrimitiveBox->setAlphaThreshold( anAlphaThreshold );
552
553   aResolution = aResourceMgr->integerValue( "VISU", "inside_geom_sphere_resolution", aResolution );
554   myInsidePrimitiveBox->setResolution( aResolution );
555
556   aMinSize = aResourceMgr->integerValue( "VISU", "inside_point_sprite_min_size", aMinSize );
557   myInsideSizeBox->setMinSize( aMinSize / 100.0 );
558
559   aMaxSize = aResourceMgr->integerValue( "VISU", "inside_point_sprite_max_size", aMaxSize );
560   myInsideSizeBox->setMaxSize( aMaxSize / 100.0 );
561
562   aMagnification = aResourceMgr->integerValue( "VISU", "inside_point_sprite_magnification", aMagnification );
563   this->setMagnification( aMagnification / 100.0 );
564
565   anIncrement = aResourceMgr->doubleValue( "VISU", "inside_point_sprite_increment", anIncrement );
566   this->setIncrement( anIncrement );
567
568   myInsidePrimitiveBox->setFaceLimit( 50000 );
569
570   ApplyInsideGaussPoints();
571 }
572
573 void VVTK_SegmentationCursorDlg::UpdateOutsideGaussPoints()
574 {
575   vtkFloatingPointType aClamp = 256.0;
576   int aPrimitiveType = VISU_OpenGLPointSpriteMapper::PointSprite;
577   QString aMainTexture = QString( getenv( "VISU_ROOT_DIR") ) + "/share/salome/resources/sprite_texture.bmp";
578   QString anAlphaTexture = QString( getenv( "VISU_ROOT_DIR") ) + "/share/salome/resources/sprite_alpha.bmp";
579   vtkFloatingPointType anAlphaThreshold = 0.1;
580   int aResolution = 8;
581   int aSize = 25;
582   bool aUniform = false;
583   QColor aColor = Qt::blue;
584
585   if( !myOutsideCursorSettings->GetInitial() )
586   {
587     myOutsidePrimitiveBox->setPrimitiveType( myOutsideCursorSettings->GetPrimitiveType() );
588     myOutsidePrimitiveBox->setClamp( myOutsideCursorSettings->GetClamp() );
589     myOutsidePrimitiveBox->setMainTexture( myOutsideMainTexture );
590     myOutsidePrimitiveBox->setAlphaTexture( myOutsideAlphaTexture );
591     myOutsidePrimitiveBox->setAlphaThreshold( myOutsideCursorSettings->GetAlphaThreshold() );
592     myOutsidePrimitiveBox->setResolution( myOutsideCursorSettings->GetResolution() );
593
594     myOutsideSizeBox->setOutsideSize( myOutsideCursorSettings->GetSize() );
595     myOutsideSizeBox->setUniform( myOutsideCursorSettings->GetUniform() );
596
597     vtkFloatingPointType* aColor = myOutsideCursorSettings->GetColor();
598     myOutsideSizeBox->setColor( QColor( ( int )( aColor[0] * 255.0 ),
599                                         ( int )( aColor[1] * 255.0 ),
600                                         ( int )( aColor[2] * 255.0 ) ) );
601
602     return;
603   }
604
605   SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
606
607   aPrimitiveType = aResourceMgr->integerValue( "VISU", "outside_point_sprite_primitive_type", aPrimitiveType );
608   myOutsidePrimitiveBox->setPrimitiveType( aPrimitiveType );
609
610   aClamp = aResourceMgr->doubleValue( "VISU", "outside_point_sprite_clamp", aClamp );
611   myOutsidePrimitiveBox->setClamp( aClamp );
612
613   aMainTexture = aResourceMgr->stringValue( "VISU", "outside_point_sprite_main_texture", aMainTexture );
614   myOutsidePrimitiveBox->setMainTexture( aMainTexture );
615
616   anAlphaTexture = aResourceMgr->stringValue( "VISU", "outside_point_sprite_alpha_texture", anAlphaTexture );
617   myOutsidePrimitiveBox->setAlphaTexture( anAlphaTexture );
618
619   anAlphaThreshold = aResourceMgr->doubleValue( "VISU", "outside_point_sprite_alpha_threshold", anAlphaThreshold );
620   myOutsidePrimitiveBox->setAlphaThreshold( anAlphaThreshold );
621
622   aResolution = aResourceMgr->integerValue( "VISU", "outside_geom_sphere_resolution", aResolution );
623   myOutsidePrimitiveBox->setResolution( aResolution );
624
625   aSize = aResourceMgr->integerValue( "VISU", "outside_point_sprite_size", aSize );
626   myOutsideSizeBox->setOutsideSize( aSize / 100.0 );
627
628   aUniform = aResourceMgr->booleanValue( "VISU", "outside_point_sprite_uniform", aUniform );
629   myOutsideSizeBox->setUniform( aUniform );
630
631   aColor = aResourceMgr->colorValue( "VISU", "outside_point_sprite_color", aColor );
632   myOutsideSizeBox->setColor( aColor );
633
634   myOutsidePrimitiveBox->setFaceLimit( 50000 );
635
636   ApplyOutsideGaussPoints();
637 }
638
639 VISU_InsideCursorSettings* VVTK_SegmentationCursorDlg::GetInsideCursorSettings()
640 {
641   return myInsideCursorSettings.GetPointer();
642 }
643
644 VISU_OutsideCursorSettings* VVTK_SegmentationCursorDlg::GetOutsideCursorSettings()
645 {
646   return myOutsideCursorSettings.GetPointer();
647 }
648
649 VISU::TTextureValue 
650 VVTK_SegmentationCursorDlg
651 ::MakeImageData( bool theInside,
652                  const QString& theMainTexture, 
653                  const QString& theAlphaTexture )
654 {
655   if( theInside )
656   {
657     bool updateMainTexture = myInsideMainTexture != theMainTexture;
658     bool updateAlphaTexture = myInsideAlphaTexture != theAlphaTexture;
659     if( !updateMainTexture && !updateAlphaTexture )
660       return 0;
661
662     myInsideMainTexture = theMainTexture;
663     myInsideAlphaTexture = theAlphaTexture;
664   }
665   else
666   {
667     bool updateMainTexture = myOutsideMainTexture != theMainTexture;
668     bool updateAlphaTexture = myOutsideAlphaTexture != theAlphaTexture;
669     if( !updateMainTexture && !updateAlphaTexture )
670       return 0;
671
672     myOutsideMainTexture = theMainTexture;
673     myOutsideAlphaTexture = theAlphaTexture;
674   }
675
676   return VISU::GetTexture( theMainTexture.latin1(), 
677                            theAlphaTexture.latin1());
678 }
679
680 void VVTK_SegmentationCursorDlg::onClickApply()
681 {
682   if( myTabBox->currentPage() == mySegmentationCursorBox )
683     ApplySegmentationCursor();
684   else
685   {
686     QString aWarning = "The number of faces needed to perform the 'Geometrical Sphere' primitive\n";
687     aWarning.append( "presentation might be too important to ensure an acceptable frame rate.\n\n" );
688     aWarning.append( "Can you please confirm that you want to continue anyway?" );
689     bool toApply = CheckNumberOfFaces() ||
690       SUIT_MessageBox::warn2( this, tr( "Warning" ), aWarning,
691                               tr( "&OK" ), tr( "&Cancel" ), 0, 1, 1 ) == 0;
692
693     if( toApply )
694     {
695       ApplyInsideGaussPoints();
696       ApplyOutsideGaussPoints();
697     }
698   }
699 }
700
701 void VVTK_SegmentationCursorDlg::ApplySegmentationCursor()
702 {
703   if( myIsPlaneSegmentation )
704   {
705     VISU_PlanesWidget *pPlanesWidget=myWidgetCtrl->GetPlanesWidget();
706     vtkFloatingPointType origin[3];
707     origin[0] = myXOriginSpinBox->value();
708     origin[1] = myYOriginSpinBox->value();
709     origin[2] = myZOriginSpinBox->value();
710     pPlanesWidget->SetOrigin( origin );
711
712     vtkFloatingPointType normal[3];
713     normal[0] = myDXDirectionSpinBox->value();
714     normal[1] = myDYDirectionSpinBox->value();
715     normal[2] = myDZDirectionSpinBox->value();
716     
717     if( normal[0] == 0.0 && normal[1] == 0.0 && normal[2] == 0.0 )
718     {
719       normal[2] = 1.0;
720       myDZDirectionSpinBox->setValue( 1.0 );
721     }
722     pPlanesWidget->SetNormal( normal );
723
724     pPlanesWidget->SetDistance( myDepthSpinBox->value() );
725     
726     myWidgetCtrl->InvokeEvent(vtkCommand::EndInteractionEvent,NULL);
727     myWidgetCtrl->GetInteractor()->Render();
728   }
729   else
730   {
731     VISU_SphereWidget *pSphereWidget=myWidgetCtrl->GetSphereWidget();
732     vtkFloatingPointType origin[3], aRadius;
733     origin[0] = myXOriginSpinBox->value();
734     origin[1] = myYOriginSpinBox->value();
735     origin[2] = myZOriginSpinBox->value();
736     pSphereWidget->SetCenter(origin);
737     
738     aRadius=myRadiusSpinBox->value();
739     pSphereWidget->SetRadius(aRadius);
740
741     pSphereWidget->SetRatio(myRatioSpinBox->value());
742
743     myWidgetCtrl->InvokeEvent(vtkCommand::EndInteractionEvent,NULL);
744     myWidgetCtrl->GetInteractor()->Render();
745   }
746 }
747
748 void VVTK_SegmentationCursorDlg::ApplyInsideGaussPoints()
749 {
750   QString anInsideMainTexture = myInsidePrimitiveBox->getMainTexture();
751   QString anInsideAlphaTexture = myInsidePrimitiveBox->getAlphaTexture();
752   VISU::TTextureValue aTexture = MakeImageData( true, anInsideMainTexture, anInsideAlphaTexture );
753
754   if( aTexture.GetPointer() )
755     myInsideCursorSettings->SetTexture( aTexture.GetPointer() );
756
757   myInsideCursorSettings->SetInitial( false );
758   myInsideCursorSettings->SetPrimitiveType( myInsidePrimitiveBox->getPrimitiveType() );
759   myInsideCursorSettings->SetClamp( myInsidePrimitiveBox->getClamp() );
760   myInsideCursorSettings->SetAlphaThreshold( myInsidePrimitiveBox->getAlphaThreshold() );
761   myInsideCursorSettings->SetResolution( myInsidePrimitiveBox->getResolution() );
762
763   myInsideCursorSettings->SetMinSize( myInsideSizeBox->getMinSize() );
764   myInsideCursorSettings->SetMaxSize( myInsideSizeBox->getMaxSize() );
765
766   myInsideCursorSettings->SetMagnification( this->getMagnification() );
767   myInsideCursorSettings->SetIncrement( this->getIncrement() );
768
769   myInsideCursorSettings->InvokeEvent(VISU::UpdateInsideSettingsEvent,NULL);
770 }
771
772 void VVTK_SegmentationCursorDlg::ApplyOutsideGaussPoints()
773 {
774   QString anOutsideMainTexture = myOutsidePrimitiveBox->getMainTexture();
775   QString anOutsideAlphaTexture = myOutsidePrimitiveBox->getAlphaTexture();
776   VISU::TTextureValue aTexture = MakeImageData( false, anOutsideMainTexture, anOutsideAlphaTexture );
777
778   if( aTexture.GetPointer() )
779     myOutsideCursorSettings->SetTexture( aTexture.GetPointer() );
780
781   myOutsideCursorSettings->SetInitial( false );
782   myOutsideCursorSettings->SetPrimitiveType( myOutsidePrimitiveBox->getPrimitiveType() );
783   myOutsideCursorSettings->SetClamp( myOutsidePrimitiveBox->getClamp() );
784   myOutsideCursorSettings->SetAlphaThreshold( myOutsidePrimitiveBox->getAlphaThreshold() );
785   myOutsideCursorSettings->SetResolution( myOutsidePrimitiveBox->getResolution() );
786
787   myOutsideCursorSettings->SetSize( myOutsideSizeBox->getOutsideSize() );
788   myOutsideCursorSettings->SetUniform( myOutsideSizeBox->getUniform() );
789
790   QColor aButtonColor = myOutsideSizeBox->getColor();
791   vtkFloatingPointType aColor[3];
792   aColor[0] = aButtonColor.red() / 255.0;
793   aColor[1] = aButtonColor.green() / 255.0;
794   aColor[2] = aButtonColor.blue() / 255.0;
795   myOutsideCursorSettings->SetColor( aColor );
796
797   myOutsideCursorSettings->SetMagnification( this->getMagnification() );
798   myOutsideCursorSettings->SetIncrement( this->getIncrement() );
799
800   myOutsideCursorSettings->InvokeEvent(VISU::UpdateOutsideSettingsEvent,NULL);
801 }
802
803 void VVTK_SegmentationCursorDlg::onClickClose()
804 {
805   myPlaneAction->setOn( false );
806   mySphereAction->setOn( false );
807
808   emit scgClose();
809
810   reject();
811 }
812
813 void VVTK_SegmentationCursorDlg::onClickHelp()
814 {
815   QString aHelpFileName = "segmentation.htm";
816   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
817   if (app)
818     app->onHelpContextModule(app->activeModule() ? app->moduleName(app->activeModule()->moduleName()) : QString(""), aHelpFileName);
819   else {
820     SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
821                            QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
822                            arg(app->resourceMgr()->stringValue("ExternalBrowser", "application")).arg(aHelpFileName),
823                            QObject::tr("BUT_OK"));
824   }
825 }
826
827 void VVTK_SegmentationCursorDlg::done( int r )
828 {
829   myPlaneAction->setOn( false );
830   mySphereAction->setOn( false );
831
832   emit scgClose();
833
834   QDialog::done( r );
835 }
836
837 void VVTK_SegmentationCursorDlg::onParentShow()
838 {
839   if(myPlaneAction->isOn() || mySphereAction->isOn())
840     show();
841   else
842     hide();
843 }
844
845 void VVTK_SegmentationCursorDlg::onParentHide()
846 {
847   hide();
848 }
849
850 bool VVTK_SegmentationCursorDlg::CheckNumberOfFaces()
851 {
852   if( !myInteractor )
853     return false;
854
855   vtkRenderer* aRenderer = myInteractor->getRenderer();
856
857   if( !aRenderer )
858     return false;
859
860   int aNumberOfPoints = 0;
861
862   vtkActor* anActor;
863   vtkActorCollection* anActColl = aRenderer->GetActors();
864   for( anActColl->InitTraversal(); ( anActor = anActColl->GetNextActor() ) != NULL; )
865   {
866     if( VISU_GaussPtsAct1* aGaussActor = VISU_GaussPtsAct1::SafeDownCast( anActor ) )
867       aNumberOfPoints += aGaussActor->GetInput()->GetNumberOfCells();
868   }
869   return !( myInsidePrimitiveBox->getPrimitiveType() == VISU_OpenGLPointSpriteMapper::GeomSphere &&
870             aNumberOfPoints * myInsidePrimitiveBox->getFaceNumber() * 2 > myInsidePrimitiveBox->getFaceLimit() ||
871             myOutsidePrimitiveBox->getPrimitiveType() == VISU_OpenGLPointSpriteMapper::GeomSphere &&
872             aNumberOfPoints * myOutsidePrimitiveBox->getFaceNumber() > myOutsidePrimitiveBox->getFaceLimit() );
873 }