Salome HOME
sources v1.2c
[modules/visu.git] / src / VISUGUI / VisuGUI_CutPlanesDlg.cxx
1 //  VISU VISUGUI : GUI of VISU component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : VisuGUI_CutPlanesDlg.cxx
25 //  Author : Laurent CORNABE & Hubert ROLLAND 
26 //  Module : VISU
27 //  $Header$
28
29 #include "VisuGUI_CutPlanesDlg.h"
30 #include "VISU_CutPlanes_i.hh"
31
32 #include "QAD_Application.h"
33 #include "QAD_Desktop.h"
34
35 #include <qlayout.h>
36 #include <qcheckbox.h>
37 #include <qstyle.h>
38 #include <qstring.h>
39 #include <qvalidator.h>
40 #include <qlineedit.h>
41
42 using namespace std;
43
44 /*!
45   Constructor
46 */
47 VisuGUI_CutPlanesDlg::VisuGUI_CutPlanesDlg()
48     : QDialog(  QAD_Application::getDesktop(), "VisuGUI_CutPlanesDlg", true, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
49 {
50   setCaption( tr( "Cut Planes Definition" ) );
51   setSizeGripEnabled( TRUE );
52
53   QGridLayout* TopLayout = new QGridLayout( this ); 
54   TopLayout->setSpacing( 6 );
55   TopLayout->setMargin( 11 );
56  
57   QButtonGroup* SelPlane = new QButtonGroup( tr( "Orientation" ), this, "SelPlane" );
58   SelPlane->setTitle( tr( "Orientation" ) );
59   SelPlane->setColumnLayout(0, Qt::Vertical );
60   SelPlane->layout()->setSpacing( 0 );
61   SelPlane->layout()->setMargin( 0 );
62   QGridLayout* SelPlaneLayout = new QGridLayout( SelPlane->layout() );
63   SelPlaneLayout->setAlignment( Qt::AlignTop );
64   SelPlaneLayout->setSpacing( 6 );
65   SelPlaneLayout->setMargin( 11 );
66
67   RBzx = new QRadioButton( tr( "// Z-X" ), SelPlane, "RBzx" );
68   RByz = new QRadioButton( tr( "// Y-Z" ), SelPlane, "RByz" );
69   RBxy = new QRadioButton( tr( "// X-Y" ), SelPlane, "RBxy" );
70   SelPlaneLayout->addWidget( RBxy, 0, 0 );
71   SelPlaneLayout->addWidget( RByz, 0, 1 );
72   SelPlaneLayout->addWidget( RBzx, 0, 2 );
73
74   QLabel* LabelPosi_3 = new QLabel( tr( "Number of planes:" ), this, "LabelPosi_3" );
75
76   nbPlan = new QSpinBox( 1, 100, 1, this, "nbPlan" );
77   nbPlan->setValue( 1 );
78   nbPlan->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
79
80   myPosTable = new QTable(this, "Positions of planes" );
81   myPosTable->setMaximumHeight( 227 );
82   myPosTable->setMinimumWidth( 294 );
83   //myPosTable->setGeometry( QRect( 11, 98, 289, 325 ) );
84   myPosTable->setNumCols(2);
85   myPosTable->setNumRows(0);
86   
87   QHeader *th = myPosTable->horizontalHeader();
88   th->setLabel( 0, "Position" );
89   th->setLabel( 1, "Set default" );
90     
91   QGroupBox* GBrot = new QGroupBox( tr( "Rotations" ), this, "GBrot" );
92   GBrot->setColumnLayout(0, Qt::Vertical );
93   GBrot->layout()->setSpacing( 0 );
94   GBrot->layout()->setMargin( 0 );
95   QGridLayout* GBrotLayout = new QGridLayout( GBrot->layout() );
96   GBrotLayout->setAlignment( Qt::AlignTop );
97   GBrotLayout->setSpacing( 6 );
98   GBrotLayout->setMargin( 11 );
99
100   LabelRot1 = new QLabel( tr( "Rotation around X (Y to Z):" ), GBrot, "LabelRot1" );
101   GBrotLayout->addWidget( LabelRot1, 0, 0 );
102
103   Rot1 = new QAD_SpinBoxDbl( GBrot, -180, 180, 10 );
104   Rot1->setValue( 0 );
105   Rot1->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
106   GBrotLayout->addWidget( Rot1, 0, 1 );
107
108   LabelRot2 = new QLabel( tr( "Rotation around Y (Z to X):" ), GBrot, "LabelRot2" );
109   GBrotLayout->addWidget( LabelRot2, 1, 0 );
110   
111   Rot2 = new QAD_SpinBoxDbl( GBrot, -180, 180, 10 );
112   Rot2->setValue( 0 );
113   Rot2->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
114   GBrotLayout->addWidget( Rot2, 1, 1 );
115
116   QGroupBox* GroupButtons = new QGroupBox( this, "GroupButtons" );
117   GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) ); 
118   GroupButtons->setTitle( tr( ""  ) );
119   GroupButtons->setColumnLayout(0, Qt::Vertical );
120   GroupButtons->layout()->setSpacing( 0 );
121   GroupButtons->layout()->setMargin( 0 );
122   QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
123   GroupButtonsLayout->setAlignment( Qt::AlignTop );
124   GroupButtonsLayout->setSpacing( 6 );
125   GroupButtonsLayout->setMargin( 11 );
126
127   QPushButton* buttonOk = new QPushButton( tr( "&OK" ), GroupButtons, "buttonOk" );
128   buttonOk->setAutoDefault( TRUE );
129   buttonOk->setDefault( TRUE );
130   GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
131   GroupButtonsLayout->addItem( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 1 );
132
133   QPushButton* buttonCancel = new QPushButton( tr( "&Cancel" ), GroupButtons, "buttonCancel" );
134   buttonCancel->setAutoDefault( TRUE );
135   GroupButtonsLayout->addWidget( buttonCancel, 0, 2 );
136
137   QLabel* aPosLbl = new QLabel(tr( "LBL_POS" ), this);
138   myPosSpn = new QAD_SpinBoxDbl( this, 0, 1, 0.1 );
139
140   th->setStyle("QStyle::CE_PushButton");
141
142   // layouting
143   TopLayout->addMultiCellWidget( SelPlane, 0, 0, 0, 1 );
144   TopLayout->addMultiCellWidget( GBrot, 1, 1, 0, 1 );
145   TopLayout->addWidget( LabelPosi_3,   2, 0 );
146   TopLayout->addWidget( nbPlan,        2, 1 );
147   TopLayout->addMultiCellWidget( myPosTable, 3, 3, 0, 1 );
148   TopLayout->setRowStretch ( 3, 3 );
149   TopLayout->addWidget(aPosLbl, 4, 0 );
150   TopLayout->addWidget(myPosSpn, 4, 1 );
151   TopLayout->addMultiCellWidget( GroupButtons, 5, 5, 0, 1 );
152
153   // signals and slots connections
154   connect( SelPlane, SIGNAL( clicked( int )), this, SLOT( orientationChanged( int ) ) );
155   connect( nbPlan, SIGNAL( valueChanged( int )), this, SLOT( DrawTable( ) ) );
156   connect( myPosSpn, SIGNAL( valueChanged( double )), this, SLOT( DrawTable( ) ) );
157   connect( RBzx, SIGNAL( toggled(bool)), this, SLOT( DrawTable( ) ) );
158   connect( RByz, SIGNAL( toggled(bool)), this, SLOT( DrawTable( ) ) );
159   connect( RBxy, SIGNAL( toggled(bool)), this, SLOT( DrawTable( ) ) );
160   connect( th, SIGNAL( clicked(int)), this, SLOT( setDefault(int) ) );
161
162   connect( buttonOk,     SIGNAL( clicked() ), this, SLOT( accept() ) );
163   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
164
165   myCutPlanes = NULL;
166
167   // default values
168   hasInit = false;
169   X1 = 0; X2 = 0;
170   Y1 = 0; Y2 = 0;
171   Z1 = 0; Z2 = 0;
172   RByz->setChecked( true );
173   orientationChanged( 0 );
174 }
175
176 /*!
177   Destructor
178 */
179 VisuGUI_CutPlanesDlg::~VisuGUI_CutPlanesDlg()
180 {
181   if(myCutPlanes) delete myCutPlanes; 
182 }
183
184
185 void VisuGUI_CutPlanesDlg::initFromPrsObject(VISU::CutPlanes_i* thePrs) {
186   setNbPlanes(thePrs->GetNbPlanes());
187   setRotation(thePrs->GetRotateX()*180./PI, thePrs->GetRotateY()*180./PI);
188   setPlanePos(thePrs->GetOrientationType());
189   myPosSpn->setValue(thePrs->GetDisplacement());
190   hasInit = true;
191   // init table
192   myCutPlanes = new VISU::CutPlanes_i(thePrs->GetResult(),false);
193   myCutPlanes->SameAs(thePrs);
194   DrawTable();
195 }
196
197 void VisuGUI_CutPlanesDlg::storeToPrsObject(VISU::CutPlanes_i* thePrs) {
198   thePrs->SetNbPlanes(getNbPlanes());
199   thePrs->SetRotateX(getRotation1()*PI/180.);
200   thePrs->SetRotateY(getRotation2()*PI/180.);
201   thePrs->SetOrientationType(getOrientaion());
202   thePrs->SetDisplacement(myPosSpn->value());
203   for (int i = 0; i < getNbPlanes(); ++i) 
204     {
205       if (!((QCheckBox*)myPosTable->cellWidget( i, 1 ))->isChecked())
206         thePrs->SetPlanePosition( i, ((QLineEdit*)myPosTable->cellWidget( i, 0 ))->text().toDouble() );
207       else thePrs->SetDefault(i);
208     }
209 }
210
211 /*!
212   Called when orientation is changed
213 */
214 void VisuGUI_CutPlanesDlg::orientationChanged( int )
215 {
216   if ( RBxy->isChecked() ) {
217     LabelRot1->setText( tr( "Rotation around X (Y to Z):" ) );
218     LabelRot2->setText( tr( "Rotation around Y (Z to X):" ) );
219   } else if ( RByz->isChecked() ) {
220     LabelRot1->setText( tr( "Rotation around Y (Z to X):" ) );
221     LabelRot2->setText( tr( "Rotation around Z (X to Y):" ) );
222   } else { 
223     LabelRot1->setText( tr( "Rotation around Z (X to Y):" ) );
224     LabelRot2->setText( tr( "Rotation around X (Y to Z):" ) );
225   }
226 }
227
228
229
230 /*!
231   Sets bounds ( must be called first )
232 */
233 void VisuGUI_CutPlanesDlg::setBounds( const double x1, const double x2, 
234                                       const double y1, const double y2, 
235                                       const double z1, const double z2 )
236 {
237   X1 = min( x1, x2 ); X2 = max( x1, x2 );
238   Y1 = min( y1, y2 ); Y2 = max( y1, y2 );
239   Z1 = min( z1, z2 ); Z2 = max( z1, z2 );
240   orientationChanged( 0 );
241 }
242
243 /*!
244   Sets nb of planes
245 */
246 void VisuGUI_CutPlanesDlg::setNbPlanes( const int nbp )
247 {
248   nbPlan->setValue( nbp );
249 }
250
251 /*!
252   Gets nb of planes
253 */
254 int VisuGUI_CutPlanesDlg::getNbPlanes()
255 {
256   return nbPlan->value();
257 }
258
259 /*!
260   Sets planes orientation and position
261 */
262 void VisuGUI_CutPlanesDlg::setPlanePos( const VISU::CutPlanes::Orientation  orient/*, const double pos1, const double pos2 */)
263 {
264   if ( orient == VISU::CutPlanes::XY )           // xy
265     RBxy->setChecked( true );
266   else if ( orient == VISU::CutPlanes::YZ )       // yz
267     RByz->setChecked( true );
268   else                         // zx
269     RBzx->setChecked( true );
270   orientationChanged( 0 );
271 }
272
273
274 /*!
275   Gets planes orientation
276 */
277 VISU::CutPlanes::Orientation VisuGUI_CutPlanesDlg::getOrientaion()
278 {
279   VISU::CutPlanes::Orientation orient;
280   if ( RBxy->isChecked() )     // xy
281     orient = VISU::CutPlanes::XY;
282   if ( RByz->isChecked() )     // yz
283     orient = VISU::CutPlanes::YZ;
284   if ( RBzx->isChecked() )     // zx
285     orient = VISU::CutPlanes::ZX;
286   return orient;
287 }
288
289
290 /*!
291   Sets planes rotation
292 */
293 void VisuGUI_CutPlanesDlg::setRotation( const double r1, const double r2 )
294 {
295   Rot1->setValue( r1 );
296   Rot2->setValue( r2 );
297 }
298
299 /*!
300   Gets plane rotation 1
301 */
302 double VisuGUI_CutPlanesDlg::getRotation1()
303 {
304   return Rot1->value();
305 }
306
307 /*!
308   Gets plane rotation 2
309 */
310 double VisuGUI_CutPlanesDlg::getRotation2()
311 {
312   return Rot2->value();
313 }
314
315 /*!
316   Draw the table of planes positions
317 */
318
319 void VisuGUI_CutPlanesDlg::DrawTable()
320 {
321   if (!hasInit) return;
322
323   int aNbRows = myPosTable->numRows();
324   int aNbPlanes = getNbPlanes();  
325      
326   if (aNbRows>0)
327     for (int i = 0; i < aNbRows; ++i) 
328       {
329         if (!((QCheckBox*)myPosTable->cellWidget( i, 1 ))->isChecked())
330           myCutPlanes->SetPlanePosition( i, ((QLineEdit*)myPosTable->cellWidget( i, 0 ))->text().toDouble() );
331       }
332   
333   myCutPlanes->SetNbPlanes(aNbPlanes);
334   myCutPlanes->SetDisplacement(myPosSpn->value());
335   myCutPlanes->SetOrientationType(getOrientaion());
336
337   if (aNbRows < aNbPlanes)
338     {
339       if (aNbRows == 0) myPosTable->setNumRows(aNbPlanes);
340       else myPosTable->insertRows(aNbRows, aNbPlanes - aNbRows );
341       
342       QHeader *vh = myPosTable->verticalHeader();
343       QString str;
344
345       QDoubleValidator *dvalidator = new QDoubleValidator(myPosTable);
346       dvalidator->setDecimals(32);
347
348       for (int i = aNbRows; i < myPosTable->numRows(); ++i) 
349         {
350           QLineEdit *editline = new QLineEdit( myPosTable );
351           editline->setValidator(dvalidator);
352           myPosTable->setCellWidget( i, 0, editline );
353                   
354           QCheckBox *checkbox = new QCheckBox( myPosTable );
355           myPosTable->setCellWidget(i, 1, checkbox );
356           connect(checkbox , SIGNAL( clicked() ), this , SLOT( setDefault() ) );
357           
358           str = str.setNum(i+1);
359           str.prepend ("Plane# ");
360           vh->setLabel( i, str );
361         }
362     }
363   else if (aNbRows > aNbPlanes)
364     for (int i = aNbRows-1; myPosTable->numRows() != aNbPlanes; --i )
365       myPosTable->removeRow(i);
366   
367   for (int i = 0; i < myPosTable->numRows(); ++i) 
368     {
369       ((QLineEdit*)myPosTable->cellWidget( i, 0 ))->setText( QString::number(myCutPlanes->GetPlanePosition(i)) );
370       
371       if (myCutPlanes->IsDefault(i)) 
372         {
373           ((QCheckBox*)myPosTable->cellWidget( i, 1 ))->setChecked(true);
374           ((QLineEdit*)myPosTable->cellWidget( i, 0 ))->setEnabled(false);
375         }
376     }
377 }
378
379 void VisuGUI_CutPlanesDlg::setDefault(int all)
380 {
381   if (all == 0) return;
382
383   if (all == 1)
384     for (int i = 0; i < getNbPlanes(); ++i) 
385       ((QCheckBox*)myPosTable->cellWidget( i, 1 ))->setChecked(true);
386         
387   for (int i = 0; i < getNbPlanes(); ++i) 
388     {
389       if ( ((QCheckBox*)myPosTable->cellWidget( i, 1 ))->isChecked() )
390         {
391           myCutPlanes->SetDefault(i);
392           ((QLineEdit*)myPosTable->cellWidget( i, 0 ))->setText( QString::number(myCutPlanes->GetPlanePosition(i)) );
393           ((QLineEdit*)myPosTable->cellWidget( i, 0 ))->setEnabled(false);
394         }
395       else ((QLineEdit*)myPosTable->cellWidget( i, 0 ))->setEnabled(true); 
396     }
397 }