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