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