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