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