Salome HOME
09a7266fac8ba453c700c0c1890ccf606abed56e
[modules/visu.git] / src / VISUGUI / VisuGUI_CutPlanesDlg.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21
22 //  VISU VISUGUI : GUI of VISU component
23 //  File   : VisuGUI_CutPlanesDlg.cxx
24 //  Author : Laurent CORNABE & Hubert ROLLAND
25 //  Module : VISU
26
27 #include "VisuGUI_CutPlanesDlg.h"
28
29 #include "VISU_ColoredPrs3dFactory.hh"
30
31 #include "VisuGUI.h"
32 #include "VisuGUI_Tools.h"
33 #include "VisuGUI_ViewTools.h"
34 #include "VisuGUI_InputPane.h"
35
36 #include "VISU_ColoredPrs3dFactory.hh"
37 #include "VISU_CutPlanes_i.hh"
38 #include "VISU_Result_i.hh"
39 #include "VISU_CutPlanesPL.hxx"
40
41 //#include "OB_Browser.h"
42
43 #include "VISU_TableDlg.h"
44
45 #include "SVTK_ViewWindow.h"
46
47 #include "LightApp_Application.h"
48
49 #include <SalomeApp_IntSpinBox.h>
50 #include <SalomeApp_DoubleSpinBox.h>
51
52 #include "SUIT_Application.h"
53 #include "SUIT_Desktop.h"
54 #include "SUIT_ViewWindow.h"
55 #include "SUIT_ViewManager.h"
56 #include "SUIT_Session.h"
57 #include "SUIT_MessageBox.h"
58 #include "SUIT_ResourceMgr.h"
59
60 #include <QLayout>
61 #include <QLabel>
62 #include <QButtonGroup>
63 #include <QCheckBox>
64 #include <QStyle>
65 #include <QValidator>
66 #include <QLineEdit>
67 #include <QTabWidget>
68 #include <QKeyEvent>
69 #include <QRadioButton>
70 #include <QTableWidget>
71 #include <QGroupBox>
72 #include <QPushButton>
73 #include <QHeaderView>
74 #include <QComboBox>
75
76 #include <vtkRenderer.h>
77 #include <vtkPolyData.h>
78 #include <vtkAppendPolyData.h>
79 #include <vtkDataSetMapper.h>
80 #include <vtkDataSet.h>
81
82 #include <limits>
83
84 using namespace std;
85
86 //extern VisuGUI *visuGUI;
87
88 VisuGUI_CutPlanesPane::VisuGUI_CutPlanesPane (QWidget* theParent)
89      : QFrame(theParent), myPreviewActor(0)
90 {
91   QGridLayout* TopLayout = new QGridLayout( this );
92   TopLayout->setSpacing( 6 );
93   TopLayout->setMargin( 10 );
94
95   QButtonGroup* SelPlane = new QButtonGroup ( this );
96   QGroupBox* aGB = new QGroupBox( tr("TXT_ORIENTATION"), this );
97   //SelPlane->setTitle(tr("TXT_ORIENTATION"));
98   //SelPlane->setColumnLayout(0, Qt::Vertical);
99   //SelPlane->layout()->setSpacing( 0 );
100   //SelPlane->layout()->setMargin( 0 );
101   QGridLayout* SelPlaneLayout = new QGridLayout( aGB );
102   SelPlaneLayout->setAlignment( Qt::AlignTop );
103   SelPlaneLayout->setSpacing( 6 );
104   SelPlaneLayout->setMargin( 11 );
105
106   RBxy = new QRadioButton (tr("PARALLEL_XOY"), aGB);
107   RByz = new QRadioButton (tr("PARALLEL_YOZ"), aGB);
108   RBzx = new QRadioButton (tr("PARALLEL_ZOX"), aGB);
109   SelPlaneLayout->addWidget( RBxy, 0, 0 );
110   SelPlaneLayout->addWidget( RByz, 0, 1 );
111   SelPlaneLayout->addWidget( RBzx, 0, 2 );
112   SelPlane->addButton( RBxy );
113   SelPlane->addButton( RByz );
114   SelPlane->addButton( RBzx );
115
116   QLabel* LabelPosi_3 = new QLabel (tr("LBL_NB_PLANS"), this);
117
118   nbPlan = new SalomeApp_IntSpinBox( this );
119   nbPlan->setAcceptNames( false );
120   nbPlan->setMinimum( 1 );
121   nbPlan->setMaximum(100);
122   nbPlan->setSingleStep( 1 );
123   nbPlan->setValue( 1 );
124   nbPlan->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
125
126   myPosTable = new QTableWidget (this);
127   myPosTable->setMaximumHeight( 227 );
128   myPosTable->setMinimumWidth( 294 );
129   myPosTable->setColumnCount(2);
130   myPosTable->setRowCount(0);
131   connect( myPosTable, SIGNAL(cellChanged(int, int)), this, SLOT(onValueChanged(int, int)) );
132
133   myPosTable->setItemDelegateForColumn( 0,
134          new NumDelegateItem( myPosTable, NumDelegateItem::NV_Real) );
135
136   myPosTable->setEditTriggers(
137          (QAbstractItemView::EditTriggers)(QAbstractItemView::AllEditTriggers &(~QAbstractItemView::CurrentChanged)));
138
139   QStringList aLabels;
140   aLabels.append( "Position" );
141   aLabels.append( "Set default" );
142   
143   myPosTable->setHorizontalHeaderLabels( aLabels );
144
145   QGroupBox* GBrot = new QGroupBox (tr("LBL_ROTATION"), this);
146   //GBrot->setColumnLayout(0, Qt::Vertical);
147   //GBrot->layout()->setSpacing( 0 );
148   //GBrot->layout()->setMargin( 0 );
149   QGridLayout* GBrotLayout = new QGridLayout( GBrot );
150   GBrotLayout->setAlignment( Qt::AlignTop );
151   GBrotLayout->setSpacing( 6 );
152   GBrotLayout->setMargin( 11 );
153
154   LabelRot1 = new QLabel (tr("LBL_ROT_X"), GBrot );
155   GBrotLayout->addWidget( LabelRot1, 0, 0 );
156
157   Rot1 = new SalomeApp_DoubleSpinBox( GBrot );
158   VISU::initSpinBox( Rot1, -180., 180., 10., "angle_precision" );  
159   Rot1->setValue( 0 );
160   Rot1->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
161   connect( Rot1, SIGNAL( valueChanged( double )), this, SLOT( onRotation( double ) ) );
162   GBrotLayout->addWidget( Rot1, 0, 1 );
163
164   LabelRot2 = new QLabel (tr("LBL_ROT_Y"), GBrot );
165   GBrotLayout->addWidget( LabelRot2, 1, 0 );
166
167   Rot2 = new SalomeApp_DoubleSpinBox( GBrot );
168   VISU::initSpinBox( Rot2, -180., 180., 10., "angle_precision" );
169   Rot2->setValue( 0 );
170   Rot2->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
171   connect( Rot2, SIGNAL( valueChanged( double )), this, SLOT( onRotation( double ) ) );
172   GBrotLayout->addWidget( Rot2, 1, 1 );
173
174   QLabel* aPosLbl = new QLabel(tr( "LBL_POS" ), this);
175   myPosSpn = new SalomeApp_DoubleSpinBox( this );
176   VISU::initSpinBox( myPosSpn, 0., 1., .1, "parametric_precision" );
177
178   GDeformation = new QGroupBox(tr("LBL_DEFORMATION"), this);
179   GDeformation->setCheckable(true);
180   GDeformation->setChecked(false);
181   QGridLayout* GDeformationLayout = new QGridLayout( GDeformation );
182   GDeformationLayout->setSpacing( 6 );
183   GDeformationLayout->setMargin( 11 );
184   connect(GDeformation, SIGNAL(toggled(bool)), this, SLOT(onDeformationCheck(bool)));
185
186   QLabel* LabelDeformation1 = new QLabel (tr("LBL_DEFROMATION_VECT"), GDeformation);
187   GDeformationLayout->addWidget( LabelDeformation1, 0, 0 );
188
189   myVectorialFieldCombo = new QComboBox (GDeformation);
190   GDeformationLayout->addWidget( myVectorialFieldCombo, 0, 1 );
191
192   QLabel* LabelDeformation2 = new QLabel (tr("LBL_DEFROMATION_SCALE"), GDeformation);
193   GDeformationLayout->addWidget( LabelDeformation2, 1, 0 );
194   myScaleSpn = new SalomeApp_DoubleSpinBox (GDeformation);
195   VISU::initSpinBox( myScaleSpn, 0., 1.0E+38, 0.1, "visual_data_precision" );
196   myScaleSpn->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
197   GDeformationLayout->addWidget( myScaleSpn, 1, 1 );
198   connect(myVectorialFieldCombo,     SIGNAL(activated(int)), this, SLOT(onVectorialFieldChanged(int)));
199   connect(myScaleSpn, SIGNAL(valueChanged(double)), this, SLOT(onScaleFactorChanged(double)));
200   myScaleSpn->setValue(0.1);
201
202   myPreviewCheck = new QCheckBox(tr("LBL_SHOW_PREVIEW"), this);
203   myPreviewCheck->setChecked(false);
204   connect( myPreviewCheck, SIGNAL( toggled( bool )), this, SLOT( onPreviewCheck( bool ) ) );
205
206   TopLayout->addWidget( aGB, 0, 0, 1, 2 );
207   TopLayout->addWidget( GBrot, 1, 0, 1, 2 );
208   TopLayout->addWidget( LabelPosi_3,   2, 0 );
209   TopLayout->addWidget( nbPlan,        2, 1 );
210   TopLayout->addWidget( myPosTable, 3, 0, 1, 2 );
211   TopLayout->setRowStretch ( 3, 3 );
212   TopLayout->addWidget(aPosLbl, 4, 0 );
213   TopLayout->addWidget(myPosSpn, 4, 1 );
214   TopLayout->addWidget( GDeformation, 5, 0, 1, 2 );
215   TopLayout->addWidget( myPreviewCheck, 6, 0, 1, 2 );
216
217   // signals and slots connections
218   connect( SelPlane, SIGNAL( buttonClicked( int )), this, SLOT( orientationChanged( int ) ) );
219   connect( nbPlan, SIGNAL( valueChanged( int )), this, SLOT( DrawTable( ) ) );
220   connect( myPosSpn, SIGNAL( valueChanged( double )), this, SLOT( DrawTable( ) ) );
221   connect( myPosTable->horizontalHeader(), SIGNAL( sectionClicked(int)), this, SLOT( setDefault(int) ) );
222
223   // default values
224   hasInit = false;
225   X1 = 0; X2 = 0;
226   Y1 = 0; Y2 = 0;
227   Z1 = 0; Z2 = 0;
228   RByz->setChecked( true );
229
230 } // End of Constructor
231
232 VisuGUI_CutPlanesPane::~VisuGUI_CutPlanesPane()
233 {
234   deletePlanes();
235   if (SVTK_ViewWindow* vf = VISU::GetActiveViewWindow<SVTK_ViewWindow>())
236     vf->Repaint();
237 }
238
239 void VisuGUI_CutPlanesPane::initFromPrsObject (VISU::CutPlanes_i* thePrs)
240 {
241   setNbPlanes(thePrs->GetNbPlanes());
242   setRotation(thePrs->GetRotateX()*180./M_PI, thePrs->GetRotateY()*180./M_PI);
243   setPlanePos(thePrs->GetOrientationType());
244   myPosSpn->setValue(thePrs->GetDisplacement());
245   InitEntity2VectorialFieldsMap(thePrs);
246   InsertAllVectorialFields();
247   setScaleFactor(thePrs->GetScale());
248   if(myEntity2VectorialFields.size() < 1)
249     GDeformation->setDisabled(true);
250   else{
251     if(thePrs->IsDeformed()){
252       GDeformation->setChecked(true);
253       int idx = myVectorialFieldCombo->findText(thePrs->GetVectorialFieldName());
254       if (idx >= 0)
255         myVectorialFieldCombo->setCurrentIndex(idx);
256       else {
257         myVectorialFieldCombo->addItem(thePrs->GetVectorialFieldName());
258         myVectorialFieldCombo->setCurrentIndex(myVectorialFieldCombo->count()-1);
259       }
260     }
261   }
262     
263   hasInit = true;
264   // init table
265   myCutPlanes = VISU::TSameAsFactory<VISU::TCUTPLANES>().Create(thePrs, VISU::ColoredPrs3d_i::EDoNotPublish);
266   myCutPlanes->SameAs(thePrs);
267   DrawTable();
268
269   // Draw Preview
270   if (myPreviewCheck->isChecked()) {
271     createPlanes();
272   }
273 }
274
275 void VisuGUI_CutPlanesPane::InitEntity2VectorialFieldsMap(VISU::ColoredPrs3d_i* thePrs){
276
277   VISU::Result_var theResult = thePrs->GetResultObject();
278   VISU::Result_i* aResult = dynamic_cast<VISU::Result_i*>(GetServant(theResult).in());
279   VISU::Result::Entities_var aEntities = aResult->GetEntities(thePrs->GetMeshName());
280   VISU::Result::TimeStampNumbers_var aScalarTimeStamp = aResult->GetTimeStampNumbers(thePrs->GetMeshName(),
281                                                                                      thePrs->GetEntity(),
282                                                                                      thePrs->GetFieldName());
283   CORBA::Long aTimeStampNumber = aScalarTimeStamp->length();
284   
285   for(size_t iEntity = 0; iEntity < aEntities->length(); iEntity++){
286     VISU::Entity aEntity = aEntities[iEntity];
287     VISU::Result::EntityNames_var aFields = aResult->GetFields(thePrs->GetMeshName(),aEntity);
288     for(size_t iField = 0; iField < aFields->length(); iField++){
289
290       TVectorialFieldsList aVectorialFields;
291       VISU::Result::TimeStampNumbers_var aTimeStamps =  aResult->GetTimeStampNumbers(thePrs->GetMeshName(),aEntity,aFields[iField] );
292       
293       if((aResult->GetNumberOfComponents(thePrs->GetMeshName(),aEntity,aFields[iField] ) > 1) &&
294          (aTimeStamps->length() >= aTimeStampNumber)){
295         aVectorialFields.push_back(QString(aFields[iField].in()));
296       }
297       if(aVectorialFields.size() > 0){
298         myEntity2VectorialFields.insert(TEntVectPair(aEntity,aVectorialFields));
299       }
300     }
301   }
302 }
303
304 void VisuGUI_CutPlanesPane::InsertAllVectorialFields(){
305   TEntity2VectorialFields::const_iterator aFieldIter = myEntity2VectorialFields.begin();
306   for( ;aFieldIter != myEntity2VectorialFields.end();aFieldIter++ ){
307     TVectorialFieldsList aVectorialFields = aFieldIter->second;
308     for(size_t iField = 0;iField < aVectorialFields.size(); iField++)
309       myVectorialFieldCombo->addItem(aVectorialFields[iField]);
310   }
311 }
312
313 double VisuGUI_CutPlanesPane::getScaleFactor(){
314   return myScaleSpn->value();
315 }
316
317 void VisuGUI_CutPlanesPane::setScaleFactor(double theFactor)
318 {
319   double step = 0.1;
320   if (fabs(theFactor) > std::numeric_limits<double>::epsilon()) {
321     int degree = int(log10(fabs(theFactor))) - 1;
322     if (fabs(theFactor) < 1) {
323       // as logarithm value is negative in this case
324       // and it is truncated to the bigger integer
325       degree -= 1;
326     }
327     step = pow(10., double(degree));
328   }
329
330   myScaleSpn->setSingleStep(step);
331   myScaleSpn->setValue(theFactor);
332 }
333
334 void VisuGUI_CutPlanesPane::createPlanes()
335 {
336   if (VISU::GetActiveViewWindow<SVTK_ViewWindow>() == NULL) return;
337   if (!(bool)myCutPlanes) return;
338   if (myPreviewActor != 0) return;
339
340   vtkDataSet* aDataSet = myCutPlanes->GetSpecificPL()->GetWarpVectorOutput();
341   if (!aDataSet->GetNumberOfCells()) {
342     onPreviewCheck(false);
343     return;
344   }
345   vtkDataSetMapper* aPlaneMapper = vtkDataSetMapper::New();
346   aPlaneMapper->SetInput(aDataSet);
347   aPlaneMapper->ScalarVisibilityOff();
348
349   myPreviewActor = SALOME_Actor::New();
350   myPreviewActor->PickableOff();
351   myPreviewActor->SetMapper(aPlaneMapper);
352   aPlaneMapper->Delete();
353   VISU::GetActiveViewWindow<SVTK_ViewWindow>()->AddActor(myPreviewActor);
354 }
355
356 int VisuGUI_CutPlanesPane::storeToPrsObject (VISU::CutPlanes_i* thePrs)
357 {
358   thePrs->SetOrientation(getOrientaion(), getRotation1()*M_PI/180., getRotation2()*M_PI/180.);
359   thePrs->SetNbPlanes(getNbPlanes());
360   thePrs->SetDisplacement(myPosSpn->value());
361   for (int i = 0; i < getNbPlanes(); ++i) {
362     QTableWidgetItem* anItem = myPosTable->item(i, 1);
363     if (!anItem)
364       break;
365     if (anItem->checkState() != Qt::Checked )
366       thePrs->SetPlanePosition(i, myPosTable->item(i, 0)->text().toDouble());
367     else
368       thePrs->SetDefault(i);
369   }
370
371   thePrs->UseDeformation(GDeformation->isChecked());
372   
373   if(GDeformation->isChecked()){
374     TEntity2VectorialFields::const_iterator anIter = myEntity2VectorialFields.begin();
375     VISU::Entity anEntity;
376     QString aFieldName;
377     bool isInited = false;
378     for(;anIter != myEntity2VectorialFields.end();anIter++){
379       TVectorialFieldsList aFields = anIter->second;
380       for(int it = 0;it < aFields.size();it++)
381         if(!QString::compare(aFields[it], myVectorialFieldCombo->currentText ())){
382           anEntity = anIter->first;
383           aFieldName = aFields[it];
384           isInited = true;
385           break;
386         }
387       if(isInited)
388         break;
389     }
390     if(isInited)
391       thePrs->SetVectorialField(anEntity,aFieldName.toLatin1().constData());
392     
393     thePrs->SetScale(myScaleSpn->value());
394   }
395   return 1;
396 }
397
398 /*!
399   Called when orientation is changed
400 */
401 void VisuGUI_CutPlanesPane::orientationChanged (int Id)
402 {
403   if ( RBxy->isChecked() ) { // RBxy->isChecked()
404     LabelRot1->setText(tr("LBL_ROT_X"));
405     LabelRot2->setText(tr("LBL_ROT_Y"));
406   } else if ( RByz->isChecked() ) { // RByz->isChecked()
407     LabelRot1->setText(tr("LBL_ROT_Y"));
408     LabelRot2->setText(tr("LBL_ROT_Z"));
409   } else if ( RBzx->isChecked() ) {
410     LabelRot1->setText(tr("LBL_ROT_Z"));
411     LabelRot2->setText(tr("LBL_ROT_X"));
412   }
413   DrawTable();
414 }
415
416 /*!
417   Sets planes orientation and position
418 */
419 void VisuGUI_CutPlanesPane::setPlanePos (const VISU::CutPlanes::Orientation orient)
420 {
421   if (orient == VISU::CutPlanes::XY)           // xy
422     {
423       RBxy->setChecked( true );
424       orientationChanged( 0 );
425     }
426   else if (orient == VISU::CutPlanes::YZ)       // yz
427     {
428       RByz->setChecked( true );
429       orientationChanged( 1 );
430     }
431   else                         // zx
432    {
433      RBzx->setChecked( true );
434      orientationChanged( 2 );
435    }
436
437 }
438
439 /*!
440   Gets planes orientation
441 */
442 VISU::CutPlanes::Orientation VisuGUI_CutPlanesPane::getOrientaion()
443 {
444   VISU::CutPlanes::Orientation orient;
445   if ( RBxy->isChecked() )     // xy
446     orient = VISU::CutPlanes::XY;
447   else if ( RByz->isChecked() )     // yz
448     orient = VISU::CutPlanes::YZ;
449   else if ( RBzx->isChecked() )     // zx
450     orient = VISU::CutPlanes::ZX;
451   return orient;
452 }
453
454 /*!
455   Sets planes rotation
456 */
457 void VisuGUI_CutPlanesPane::setRotation (const double r1, const double r2)
458 {
459   Rot1->setValue( r1 );
460   Rot2->setValue( r2 );
461 }
462
463 void VisuGUI_CutPlanesPane::onScaleFactorChanged(double theFactor){
464   DrawTable();
465 }
466
467 void VisuGUI_CutPlanesPane::onVectorialFieldChanged(int pos){
468   InitVectorialField();
469   DrawTable();
470 }
471
472 void VisuGUI_CutPlanesPane::onDeformationCheck(bool Flag){
473   if(!(bool)myCutPlanes || !hasInit) return;
474   myCutPlanes->UseDeformation(Flag);
475   InitVectorialField();
476   DrawTable();
477 }
478
479 void VisuGUI_CutPlanesPane::InitVectorialField(){
480   if(myCutPlanes->IsDeformed()){
481     TEntity2VectorialFields::const_iterator anIter = myEntity2VectorialFields.begin();
482     VISU::Entity anEntity;
483     QString aFieldName;
484     bool isInited = false;
485     for(;anIter != myEntity2VectorialFields.end();anIter++){
486       TVectorialFieldsList aFields = anIter->second;
487       for(int it = 0;it < aFields.size();it++)
488         if(!QString::compare(aFields[it], myVectorialFieldCombo->currentText ())){
489           anEntity = anIter->first;
490           aFieldName = aFields[it];
491           isInited = true;
492           break;
493         }
494       if(isInited)
495         break;
496     }
497     if(isInited)
498       myCutPlanes->SetVectorialField(anEntity,aFieldName.toLatin1().constData());
499   }
500 }
501
502 /*!
503   Draw the table of planes positions
504 */
505 void VisuGUI_CutPlanesPane::DrawTable()
506 {
507   if (!(bool)myCutPlanes) return;
508   if (!hasInit) return;
509
510   int aNbRows = myPosTable->rowCount();
511   int aNbPlanes = getNbPlanes();
512
513   myCutPlanes->SetOrientation(getOrientaion(),getRotation1()*M_PI/180.,getRotation2()*M_PI/180.);
514   myCutPlanes->SetNbPlanes(aNbPlanes);
515   myCutPlanes->SetDisplacement(myPosSpn->value());
516   myCutPlanes->SetScale(getScaleFactor());
517   if (aNbRows>0)
518     for (int i = 0; i < aNbRows; ++i) {
519       QTableWidgetItem* aItem = myPosTable->item( i, 1 );
520       if (aItem == 0) break;
521       if (aItem->checkState() != Qt::Checked )
522         myCutPlanes->SetPlanePosition(i, myPosTable->item(i, 0)->text().toDouble());
523     }
524
525   if (aNbPlanes > aNbRows)
526     while( aNbPlanes > myPosTable->rowCount() )
527       myPosTable->insertRow( myPosTable->rowCount() );
528   else if (aNbPlanes < aNbRows)
529     myPosTable->setRowCount(aNbPlanes);
530
531   QStringList aLabels;
532   QString str("Plane# %1");
533   int aMin = (aNbRows < aNbPlanes)? aNbRows : aNbPlanes;
534   for (int i = 0; i<aMin; i++) {
535     myPosTable->item( i, 0 )->setText( QString::number(myCutPlanes->GetPlanePosition(i)));
536     myPosTable->item( i, 1 )->setCheckState( myCutPlanes->IsDefault(i) ? Qt::Checked : Qt::Unchecked);
537     aLabels.append( str.arg(i+1) );
538   }
539
540   //QHeader *vh = myPosTable->verticalHeader();
541   for (int i=aMin; i<aNbPlanes; i++) {
542     QTableWidgetItem* aEditItem = new QTableWidgetItem();
543     aEditItem->setText( QString::number(myCutPlanes->GetPlanePosition(i)));
544     //aEditItem->setReplaceable(false);
545     //aEditItem->setEnabled(!myCutPlanes->IsDefault(i));
546     Qt::ItemFlags aFlags = aEditItem->flags();
547     if ( myCutPlanes->IsDefault(i) )
548       aFlags |= Qt::ItemIsEnabled;
549     else
550       aFlags &= (~Qt::ItemIsEnabled);
551     myPosTable->setItem(i, 0, aEditItem);
552
553     /*QCheckTableItem* aCheck = new QCheckTableItem(myPosTable, 0);
554     aCheck->setChecked(myCutPlanes->IsDefault(i));
555     aCheck->setReplaceable(false);*/
556     QTableWidgetItem* aCheck = new QTableWidgetItem();
557     aFlags = aCheck->flags();
558     //aFlags |=  Qt::ItemIsUserCheckable;
559     aFlags &=  (~Qt::ItemIsEditable);
560     aCheck->setFlags( aFlags );
561     aCheck->setCheckState( myCutPlanes->IsDefault(i) ?
562                            Qt::Checked : Qt::Unchecked );
563     myPosTable->setItem(i, 1, aCheck);
564
565     //vh->setLabel( i, str.arg(i+1) );
566     aLabels.append( str.arg(i+1) );
567   }
568   myPosTable->setVerticalHeaderLabels( aLabels );
569
570   // Update preview
571   if (myPreviewCheck->isChecked()) {
572     if (SVTK_ViewWindow* vf = VISU::GetActiveViewWindow<SVTK_ViewWindow>()) {
573       myCutPlanes->GetPipeLine()->Update();
574       deletePlanes();
575       createPlanes();
576       vf->Repaint();
577     }
578   }
579 }
580
581 void VisuGUI_CutPlanesPane::setDefault (int all)
582 {
583   myPosTable->setCurrentCell(-1, 1);
584   myPosTable->clearSelection();
585   if (all == 0) return;
586   if (all == 1)
587     for (int i = 0; i < getNbPlanes(); ++i)
588       myPosTable->item( i, 1 )->setCheckState(Qt::Checked);
589   //((QCheckTableItem*)myPosTable->item( i, 1 ))->setChecked(true);
590
591   for (int i = 0; i < getNbPlanes(); ++i) {
592     if ( myPosTable->item( i, 1 )->checkState() == Qt::Checked )
593     {
594       myCutPlanes->SetDefault(i);
595       myPosTable->item( i, 0)->setText( QString::number(myCutPlanes->GetPlanePosition(i)));
596       myPosTable->item( i, 0 )->setFlags( myPosTable->item( i, 0 )->flags() &(~Qt::ItemIsEnabled ) );
597     }
598     else
599       myPosTable->item( i, 0 )->setFlags( myPosTable->item( i, 0 )->flags() | Qt::ItemIsEnabled );
600   }
601 }
602
603 void VisuGUI_CutPlanesPane::onValueChanged (int theRow, int theCol)
604 {
605   QAbstractTableModel* aModel = qobject_cast<QAbstractTableModel*>( myPosTable->model() );
606   if ( !aModel )
607     return;
608   
609   if ( !myPosTable->item(theRow, 1) || !myPosTable->item(theRow, 0) )
610     return;
611
612   QModelIndex anIndex;
613
614   QTableWidgetItem* anItem = myPosTable->item(theRow, theCol);
615   if (!anItem )
616     return;
617
618   bool isDefault = myPosTable->item(theRow, 1)->checkState() == Qt::Checked;
619   if (theCol == 0 )
620   {
621     anIndex = aModel->index( theRow, 0 );
622     if ( !isDefault)
623       myCutPlanes->SetPlanePosition( theRow, anIndex.data().toDouble());
624           
625             
626     if ( myPreviewCheck->isChecked() )
627     {
628       //Update Preview
629       if (SVTK_ViewWindow* vf = VISU::GetActiveViewWindow<SVTK_ViewWindow>())
630       {
631         myCutPlanes->GetPipeLine()->Update();
632         deletePlanes();
633         createPlanes();
634         vf->Repaint();
635       }
636     }
637   }
638   else
639   {
640     anItem = myPosTable->item(theRow, 0);
641     Qt::ItemFlags flags = anItem->flags();
642     if ( isDefault )
643     {
644       myCutPlanes->SetDefault(theRow);
645       anIndex = aModel->index( theRow, 0 );
646       aModel->setData( anIndex, myCutPlanes->GetPlanePosition(theRow), Qt::DisplayRole );
647
648       /*if ( myPreviewCheck->isChecked() )
649         {
650         //not needed update presentation, because item->setFlags generates signal cellChanged() again
651         }*/
652       flags &= (~Qt::ItemIsEnabled );
653     }
654     else
655       flags |=  Qt::ItemIsEnabled;
656    
657     anItem->setFlags( flags );
658   }
659   
660   /*if (theCol == 0) {
661     bool isChanged = !anIndex.data().isNull();
662     QTableWidgetItem* anItem = myPosTable->item(theRow, 1);
663     if ( !anItem  )
664     {
665       anItem = new QTableWidgetItem();
666       myPosTable->setItem( theRow, 1, anItem );
667       Qt::ItemFlags flags = anItem->flags();
668       //flags |=  Qt::ItemIsUserCheckable;
669       flags &=  (~Qt::ItemIsEditable);
670       anItem->setFlags( flags );
671     }
672     anItem->setCheckState(!isChanged ? Qt::Checked : Qt::Unchecked);
673
674     if (myPreviewCheck->isChecked()) {
675       //Update Preview
676       if (SVTK_ViewWindow* vf = VISU::GetActiveViewWindow<SVTK_ViewWindow>()) {
677         myCutPlanes->SetPlanePosition( theRow, anIndex.data().toDouble());
678         myCutPlanes->GetPipeLine()->Update();
679         deletePlanes();
680         createPlanes();
681         vf->Repaint();
682       }
683     }
684   } else {
685     bool isDefault = myPosTable->item(theRow, 1)->checkState() == Qt::Checked;
686     QTableWidgetItem* anItem = myPosTable->item(theRow, 0);
687     if ( !anItem  )
688     {
689       anItem = new QTableWidgetItem();
690       myPosTable->setItem( theRow, 0, anItem );
691     }
692       
693     if (isDefault) {
694       myCutPlanes->SetDefault(theRow);
695       //myPosTable->setText(theRow, 0, QString::number(myCutPlanes->GetPlanePosition(theRow)));
696       anIndex = aModel->index( theRow, 0 );
697       aModel->setData( anIndex, myCutPlanes->GetPlanePosition(theRow), Qt::DisplayRole );
698       anItem->setFlags( anItem->flags() | Qt::ItemIsEnabled );
699       if (myPreviewCheck->isChecked()) {
700         if (SVTK_ViewWindow* vf = VISU::GetActiveViewWindow<SVTK_ViewWindow>()) {
701           //Update Preview
702           myCutPlanes->GetPipeLine()->Update();
703           deletePlanes();
704           createPlanes();
705           vf->Repaint();
706         }
707       }
708     }
709     //myPosTable->item(theRow, 0)->setEnabled(!isDefault);
710     anItem->setFlags( anItem->flags() &(~Qt::ItemIsEnabled ) );
711     }*/
712 }
713
714 void VisuGUI_CutPlanesPane::deletePlanes()
715 {
716   if (myPreviewActor == 0) return;
717   vtkRenderer* aRend       = myPreviewActor->GetRenderer();
718   vtkRenderWindow* aWnd = aRend->GetRenderWindow();
719
720   if(aRend && aWnd)
721     myPreviewActor->RemoveFromRender(aRend);
722 //  Bug IPAL11962: preview is still in the viewer, if viewer not active.
723 //   if (SVTK_ViewWindow* vf = VISU::GetActiveViewWindow<SVTK_ViewWindow>())
724 //     vf->RemoveActor(myPreviewActor);
725   myPreviewActor->Delete();
726   myPreviewActor = 0;
727 }
728
729 void VisuGUI_CutPlanesPane::onRotation (double theValue)
730 {
731   if (!(bool)myCutPlanes) return;
732   if (myPreviewCheck->isChecked()) {
733     if (SVTK_ViewWindow* vf = VISU::GetActiveViewWindow<SVTK_ViewWindow>()) {
734       myCutPlanes->SetOrientation(getOrientaion(),getRotation1()*M_PI/180.,getRotation2()*M_PI/180.);
735       myCutPlanes->GetPipeLine()->Update();
736       deletePlanes();
737       createPlanes();
738       vf->Repaint();
739     }
740   }
741 }
742
743 void VisuGUI_CutPlanesPane::onPreviewCheck (bool thePreview)
744 {
745   if (SVTK_ViewWindow* vf = VISU::GetActiveViewWindow<SVTK_ViewWindow>()) {
746     if (thePreview) {
747       storeToPrsObject(myCutPlanes);
748       myCutPlanes->GetPipeLine()->Update();
749       createPlanes();
750       if(VISU::GetResourceMgr()->booleanValue("VISU","automatic_fit_all",false))
751         vf->onFitAll(); // Repaint() is called inside
752       else
753         vf->Repaint();
754     } else {
755       deletePlanes();
756       vf->Repaint();
757     }
758   }
759 }
760
761 //###################################################################
762
763
764 /*!
765   Constructor
766 */
767 VisuGUI_CutPlanesDlg::VisuGUI_CutPlanesDlg (SalomeApp_Module* theModule)
768   : VisuGUI_ScalarBarBaseDlg(theModule)
769 {
770   setWindowTitle("Cut Planes Definition");
771   setSizeGripEnabled(true);
772
773   QVBoxLayout* TopLayout = new QVBoxLayout (this);
774   TopLayout->setSpacing(6);
775   TopLayout->setMargin(11);
776
777   // Tab pane
778   myTabBox = new QTabWidget(this);
779   myCutPane = new VisuGUI_CutPlanesPane(this/*, myMgr*/);
780   myTabBox->addTab(myCutPane, "Cut Planes");
781   myInputPane = new VisuGUI_InputPane(VISU::TCUTPLANES, theModule, this);
782   myTabBox->addTab(GetScalarPane(), "Scalar Bar");
783   myTabBox->addTab(myInputPane, "Input");
784
785   TopLayout->addWidget(myTabBox);
786
787   QGroupBox* GroupButtons = new QGroupBox(this);
788   GroupButtons->setGeometry(QRect(10, 10, 281, 48));
789   GroupButtons->setTitle("");
790   //GroupButtons->setColumnLayout(0, Qt::Vertical);
791   //GroupButtons->layout()->setSpacing(0);
792   //GroupButtons->layout()->setMargin(0);
793   QGridLayout* GroupButtonsLayout = new QGridLayout(GroupButtons);
794   GroupButtonsLayout->setAlignment(Qt::AlignTop);
795   GroupButtonsLayout->setSpacing(6);
796   GroupButtonsLayout->setMargin(11);
797
798   QPushButton* buttonOk = new QPushButton(tr("BUT_OK"), GroupButtons);
799   buttonOk->setAutoDefault(TRUE);
800   buttonOk->setDefault(TRUE);
801   GroupButtonsLayout->addWidget(buttonOk, 0, 0);
802   GroupButtonsLayout->addItem(new QSpacerItem(5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 1);
803
804   QPushButton* buttonCancel = new QPushButton(tr("BUT_CANCEL"), GroupButtons);
805   buttonCancel->setAutoDefault(TRUE);
806   GroupButtonsLayout->addWidget(buttonCancel, 0, 2);
807
808   QPushButton* buttonHelp = new QPushButton(tr("BUT_HELP"), GroupButtons);
809   buttonHelp->setAutoDefault(TRUE);
810   GroupButtonsLayout->addWidget(buttonHelp, 0, 3);
811
812   TopLayout->addWidget(GroupButtons);
813
814   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(accept()));
815   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
816   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(onHelp()));
817 }
818
819 VisuGUI_CutPlanesDlg::~VisuGUI_CutPlanesDlg()
820 {}
821
822 void VisuGUI_CutPlanesDlg::initFromPrsObject ( VISU::ColoredPrs3d_i* thePrs,
823                                                bool theInit )
824 {
825   if( theInit )
826     myPrsCopy = VISU::TSameAsFactory<VISU::TCUTPLANES>().Create(thePrs, VISU::ColoredPrs3d_i::EDoNotPublish);
827
828   VisuGUI_ScalarBarBaseDlg::initFromPrsObject(myPrsCopy, theInit);
829
830   myCutPane->initFromPrsObject(myPrsCopy);
831
832   if( !theInit )
833     return;
834
835   myInputPane->initFromPrsObject( myPrsCopy );
836   myTabBox->setCurrentIndex( 0 );
837 }
838
839 int VisuGUI_CutPlanesDlg::storeToPrsObject (VISU::ColoredPrs3d_i* thePrs)
840 {
841   if(!myInputPane->check() || !GetScalarPane()->check())
842     return 0;
843
844   int anIsOk = myInputPane->storeToPrsObject(myPrsCopy);
845   anIsOk &= GetScalarPane()->storeToPrsObject(myPrsCopy);
846   anIsOk &= myCutPane->storeToPrsObject(myPrsCopy);
847
848   VISU::TSameAsFactory<VISU::TCUTPLANES>().Copy(myPrsCopy, thePrs);
849
850   return anIsOk;
851 }
852
853 void VisuGUI_CutPlanesDlg::accept()
854 {
855   VisuGUI_ScalarBarBaseDlg::accept();
856 }
857
858 void VisuGUI_CutPlanesDlg::reject() {
859   VisuGUI_ScalarBarBaseDlg::reject();
860 }
861
862 QString VisuGUI_CutPlanesDlg::GetContextHelpFilePath()
863 {
864   return "cut_planes_page.html";
865 }
866
867 //###################################################################
868
869 /*QWidget* VisuGUI_NumEditItem::createEditor() const
870 {
871   QLineEdit *editline = new QLineEdit(text(), table()->viewport());
872   QDoubleValidator *dvalidator = new QDoubleValidator(table()->viewport());
873   dvalidator->setDecimals(32);
874   editline->setValidator(dvalidator);
875   return editline;
876 }*/