Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/gui.git] / src / SVTK / SVTK_SetRotationPointDlg.cxx
1 //  SALOME VTKViewer : build VTK viewer into Salome desktop
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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : 
25 //  Author : 
26 //  Module : SALOME
27 //  $Header$
28
29 #include "SVTK_SetRotationPointDlg.h"
30 #include "SVTK_MainWindow.h"
31 #include "SVTK_RenderWindowInteractor.h"
32 #include "SVTK_Event.h"
33 #include "SVTK_InteractorStyle.h"
34
35 #include "VTKViewer_Utilities.h"
36
37 #include "QtxAction.h"
38
39 #include <qlineedit.h>
40 #include <qgroupbox.h>
41 #include <qlabel.h>
42 #include <qpushbutton.h>
43 #include <qlayout.h>
44 #include <qvalidator.h>
45 #include <qbuttongroup.h>
46 #include <qobjectlist.h>
47 #include <qcheckbox.h>
48 #include <qhbox.h>
49
50 #include <vtkCallbackCommand.h>
51
52 using namespace std;
53
54 /*!
55   Constructor
56 */
57 SVTK_SetRotationPointDlg
58 ::SVTK_SetRotationPointDlg(QtxAction* theAction,
59                            SVTK_MainWindow* theParent,
60                            const char* theName):
61   SVTK_DialogBase(theAction,
62                   theParent, 
63                   theName),
64   myMainWindow(theParent),
65   myPriority(0.0),
66   myEventCallbackCommand(vtkCallbackCommand::New()),
67   myRWInteractor(theParent->GetInteractor())
68 {
69   setCaption(tr("DLG_TITLE"));
70   setSizeGripEnabled(TRUE);
71
72   // Create layout for this dialog
73   QGridLayout* layoutDlg = new QGridLayout (this);
74   layoutDlg->setSpacing(6);
75   layoutDlg->setMargin(11);
76
77   // Create check box "Use Bounding Box Center"
78   QHBox* aCheckBox = new QHBox(this);
79
80   myIsBBCenter = new QCheckBox(tr("USE_BBCENTER"), aCheckBox);
81   myIsBBCenter->setChecked(true);
82   connect(myIsBBCenter, SIGNAL(stateChanged(int)), SLOT(onBBCenterChecked()));
83
84   // Create croup button with radio buttons
85   myGroupSelButton = new QButtonGroup(2,Qt::Vertical,"",this);
86   myGroupSelButton->setMargin(11);
87   
88   // Create "Set to Origin" button
89   myToOrigin = new QPushButton(myGroupSelButton);
90   myToOrigin->setText(tr("LBL_TOORIGIN"));
91   connect(myToOrigin, SIGNAL(clicked()), this, SLOT(onToOrigin()));
92
93   // Create "Select Point from View" button
94   mySelectPoint = new QPushButton(myGroupSelButton);
95   mySelectPoint->setText(tr("LBL_SELECTPOINT"));
96   mySelectPoint->setToggleButton(true);
97   connect(mySelectPoint, SIGNAL(clicked()), this, SLOT(onSelectPoint()));
98
99   // Create croup box with grid layout
100   myGroupBoxCoord = new QGroupBox(this, "GroupBox");
101   QHBoxLayout* aHBoxLayout = new QHBoxLayout(myGroupBoxCoord);
102   aHBoxLayout->setMargin(11);
103   aHBoxLayout->setSpacing(6);
104
105   // "X" coordinate
106   QLabel* TextLabelX = new QLabel (tr("LBL_X"), myGroupBoxCoord, "TextLabelX");
107   TextLabelX->setFixedWidth(15);
108   myX = new QLineEdit(myGroupBoxCoord);
109   myX->setValidator(new QDoubleValidator(myX));
110   myX->setText(QString::number(0.0));
111   connect(myX, SIGNAL(textChanged(const QString&)), this, SLOT(onCoordChanged()));
112
113   // "Y" coordinate
114   QLabel* TextLabelY = new QLabel (tr("LBL_Y"), myGroupBoxCoord, "TextLabelY");
115   TextLabelY->setFixedWidth(15);
116   myY = new QLineEdit(myGroupBoxCoord);
117   myY->setValidator(new QDoubleValidator(myY));
118   myY->setText(QString::number(0.0));
119   connect(myY, SIGNAL(textChanged(const QString&)), this, SLOT(onCoordChanged()));
120
121   // "Z" coordinate
122   QLabel* TextLabelZ = new QLabel (tr("LBL_Z"), myGroupBoxCoord, "TextLabelZ");
123   TextLabelZ->setFixedWidth(15);
124   myZ = new QLineEdit(myGroupBoxCoord);
125   myZ->setValidator(new QDoubleValidator(myZ));
126   myZ->setText(QString::number(0.0));
127   connect(myZ, SIGNAL(textChanged(const QString&)), this, SLOT(onCoordChanged()));
128
129   // Layout widgets in the horizontal group box
130   aHBoxLayout->addWidget(TextLabelX);
131   aHBoxLayout->addWidget(myX);
132   aHBoxLayout->addWidget(TextLabelY);
133   aHBoxLayout->addWidget(myY);
134   aHBoxLayout->addWidget(TextLabelZ);
135   aHBoxLayout->addWidget(myZ);
136
137   // "Close" button
138   QGroupBox* aGroupBox = new QGroupBox(this);
139   QHBoxLayout* aHBoxLayout2 = new QHBoxLayout(aGroupBox);
140   aHBoxLayout2->setMargin(11);
141   aHBoxLayout2->setSpacing(6);
142
143   QPushButton* m_bClose = new QPushButton(tr("&Close"), aGroupBox, "m_bClose");
144   m_bClose->setAutoDefault(TRUE);
145   m_bClose->setFixedSize(m_bClose->sizeHint());
146   connect(m_bClose, SIGNAL(clicked()), this, SLOT(onClickClose()));
147
148   // Layout buttons
149   aHBoxLayout2->addWidget(m_bClose);
150
151   // Layout top level widgets
152   layoutDlg->addWidget(aCheckBox,0,0);
153   layoutDlg->addWidget(myGroupSelButton,1,0);
154   layoutDlg->addWidget(myGroupBoxCoord,2,0);
155   layoutDlg->addWidget(aGroupBox,3,0);
156   
157   setEnabled(myGroupSelButton,!myIsBBCenter->isChecked());
158   setEnabled(myGroupBoxCoord,!myIsBBCenter->isChecked());
159
160   this->resize(400, this->sizeHint().height());
161
162   myEventCallbackCommand->Delete();
163   myEventCallbackCommand->SetClientData(this);
164   myEventCallbackCommand->SetCallback(SVTK_SetRotationPointDlg::ProcessEvents);
165   myIsObserverAdded = false;
166 }
167
168 /*
169  *  Destroys the object and frees any allocated resources
170  */
171 SVTK_SetRotationPointDlg
172 ::~SVTK_SetRotationPointDlg()
173 {
174   // no need to delete child widgets, Qt does it all for us
175 }
176
177 void
178 SVTK_SetRotationPointDlg
179 ::addObserver()
180 {
181   if ( !myIsObserverAdded ) {
182     vtkInteractorStyle* aIStyle = myRWInteractor->GetInteractorStyle();
183     aIStyle->AddObserver(SVTK::BBCenterChanged, myEventCallbackCommand.GetPointer(), myPriority);
184     aIStyle->AddObserver(SVTK::RotationPointChanged, myEventCallbackCommand.GetPointer(), myPriority);
185     myIsObserverAdded = true;
186   }
187 }
188
189 /*!
190   Return true if it is the first show for this dialog
191 */
192 bool
193 SVTK_SetRotationPointDlg
194 ::IsFirstShown()
195 {
196   return myIsBBCenter->isChecked() && myX->text().toDouble() == 0.
197     && myY->text().toDouble() == 0. && myZ->text().toDouble() == 0.;
198 }
199
200 /*!
201   Processes events
202 */
203 void 
204 SVTK_SetRotationPointDlg
205 ::ProcessEvents(vtkObject* vtkNotUsed(theObject), 
206                 unsigned long theEvent,
207                 void* theClientData, 
208                 void* theCallData)
209 {
210   SVTK_SetRotationPointDlg* self = reinterpret_cast<SVTK_SetRotationPointDlg*>(theClientData);
211   vtkFloatingPointType* aCoord = (vtkFloatingPointType*)theCallData;
212   
213   switch ( theEvent ) {
214   case SVTK::BBCenterChanged:
215     if ( self->myIsBBCenter->isChecked()
216          ||
217          IsBBEmpty(self->myMainWindow->getRenderer()) )
218     {
219       if ( aCoord )
220       {
221         self->myX->setText( QString::number(aCoord[0]) );
222         self->myY->setText( QString::number(aCoord[1]) );
223         self->myZ->setText( QString::number(aCoord[2]) );
224       }
225     }
226     break;
227   case SVTK::RotationPointChanged:
228     if ( aCoord )
229     {
230       self->myX->setText( QString::number(aCoord[0]) );
231       self->myY->setText( QString::number(aCoord[1]) );
232       self->myZ->setText( QString::number(aCoord[2]) );
233     }
234     if ( !self->myIsBBCenter->isChecked() )
235       self->mySelectPoint->toggle();
236   }
237 }
238
239 void 
240 SVTK_SetRotationPointDlg
241 ::setEnabled(QGroupBox* theGrp, const bool theState)
242 {
243   QObjectList aChildren(*theGrp->children());
244   QObject* anObj;
245   for(anObj = aChildren.first(); anObj !=0; anObj = aChildren.next())
246   {
247     if (anObj !=0 && anObj->inherits("QLineEdit"))
248       ((QLineEdit*)anObj)->setReadOnly(!theState);
249     if (anObj !=0 && anObj->inherits("QPushButton"))
250       ((QLineEdit*)anObj)->setEnabled(theState);
251   }
252   
253 }
254
255 void 
256 SVTK_SetRotationPointDlg
257 ::onBBCenterChecked()
258 {
259   setEnabled(myGroupSelButton,!myIsBBCenter->isChecked());
260   setEnabled(myGroupBoxCoord,!myIsBBCenter->isChecked());
261   
262   if ( myIsBBCenter->isChecked() )
263   { 
264     if ( mySelectPoint->state() == QButton::On )
265       mySelectPoint->toggle();
266     // activate mode : the rotation point is the center of the bounding box
267     // send the data to the SVTK_InteractorStyle: set the type of the rotation point
268     //                                            calculate coordinates of the rotation point
269     myMainWindow->activateSetRotationGravity();
270   }
271   else
272   {
273     QString aX = myX->text();
274     myX->setText(QString::number(aX.toDouble()+1.));
275     myX->setText(aX);
276   }
277 }
278
279 void
280 SVTK_SetRotationPointDlg
281 ::onToOrigin()
282 {
283   if ( mySelectPoint->state() == QButton::On )
284     mySelectPoint->toggle();
285   myX->setText(QString::number(0.0));
286   myY->setText(QString::number(0.0));
287   myZ->setText(QString::number(0.0));
288 }
289
290 void
291 SVTK_SetRotationPointDlg
292 ::onSelectPoint()
293 {
294   if ( mySelectPoint->state() == QButton::On )
295     myMainWindow->activateStartPointSelection();
296   else
297     mySelectPoint->toggle();
298 }
299
300 void
301 SVTK_SetRotationPointDlg
302 ::onCoordChanged()
303 {
304   if ( !myIsBBCenter->isChecked() ) {
305     if ( mySelectPoint->state() == QButton::On
306          &&
307          ( myX->hasFocus() || myY->hasFocus() || myZ->hasFocus() ) )
308       mySelectPoint->toggle();
309     vtkFloatingPointType aCenter[3] = {myX->text().toDouble(), 
310                                        myY->text().toDouble(), 
311                                        myZ->text().toDouble()};
312     myMainWindow->activateSetRotationSelected((void*)aCenter);
313   }
314   else
315     myMainWindow->activateSetRotationGravity();
316 }
317
318 void
319 SVTK_SetRotationPointDlg
320 ::onClickClose()
321 {
322   reject();
323 }
324
325