Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / OCCViewer / OCCViewer_SetRotationPointDlg.cxx
1 //  Copyright (C) 2007-2008  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 #include "OCCViewer_SetRotationPointDlg.h"
23
24 #include <QtxAction.h>
25
26 #include "OCCViewer_ViewWindow.h"
27
28 #include <QLineEdit>
29 #include <QGroupBox>
30 #include <QLabel>
31 #include <QPushButton>
32 #include <QGridLayout>
33 #include <QDoubleValidator>
34 #include <QCheckBox>
35 #include <QHBoxLayout>
36
37 /*!
38   Constructor
39   \param view - view window
40   \param parent - parent widget
41   \param name - dialog name
42   \param modal - is this dialog modal
43   \param fl - flags
44 */
45 OCCViewer_SetRotationPointDlg::OCCViewer_SetRotationPointDlg( OCCViewer_ViewWindow* view, QWidget* parent, const char* name, bool modal, Qt::WindowFlags fl )
46 : QDialog( parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint ),
47   myView( view )
48 {
49   setObjectName( "OCCViewer_SetRotationPointDlg" );
50   setModal( modal );
51
52   setWindowTitle(tr("CAPTION"));
53   setSizeGripEnabled(TRUE);
54
55   // Create layout for this dialog
56   QGridLayout* layoutDlg = new QGridLayout (this);
57   layoutDlg->setSpacing(6);
58   layoutDlg->setMargin(11);
59
60   // Create check box "Use Bounding Box Center"
61   QHBoxLayout* aCheckBox = new QHBoxLayout;
62
63   myIsBBCenter = new QCheckBox(tr("USE_BBCENTER"));
64   myIsBBCenter->setChecked(true);
65   aCheckBox->addWidget(myIsBBCenter);
66   connect(myIsBBCenter, SIGNAL(stateChanged(int)), SLOT(onBBCenterChecked()));
67
68   // Create croup button with radio buttons
69   myGroupBoxSel = new QGroupBox( "", this );
70   QVBoxLayout *vbox = new QVBoxLayout;
71   vbox->setMargin(11);
72   vbox->addStretch(1);
73   
74   // Create "Set to Origin" button
75   myToOrigin = new QPushButton(tr("LBL_TOORIGIN"));
76   vbox->addWidget(myToOrigin);
77   connect(myToOrigin, SIGNAL(clicked()), this, SLOT(onToOrigin()));
78
79   // Create "Select Point from View" button
80   mySelectPoint = new QPushButton(tr("LBL_SELECTPOINT"));
81   mySelectPoint->setCheckable(true);
82   vbox->addWidget(mySelectPoint);
83   connect(mySelectPoint, SIGNAL(clicked()), this, SLOT(onSelectPoint()));
84
85   myGroupBoxSel->setLayout(vbox);
86
87   // Create croup box with grid layout
88   myGroupBoxCoord = new QGroupBox(this);
89   myGroupBoxCoord->setObjectName("GroupBox");
90   QHBoxLayout* aHBoxLayout = new QHBoxLayout(myGroupBoxCoord);
91   aHBoxLayout->setMargin(11);
92   aHBoxLayout->setSpacing(6);
93
94   // "X" coordinate
95   QLabel* TextLabelX = new QLabel (tr("LBL_X"), myGroupBoxCoord );
96   TextLabelX->setObjectName("TextLabelX");
97   TextLabelX->setFixedWidth(15);
98   myX = new QLineEdit(myGroupBoxCoord);
99   myX->setValidator(new QDoubleValidator(myX));
100   myX->setText(QString::number(0.0));
101   connect(myX, SIGNAL(textChanged(const QString&)), this, SLOT(onCoordChanged()));
102
103   // "Y" coordinate
104   QLabel* TextLabelY = new QLabel (tr("LBL_Y"), myGroupBoxCoord );
105   TextLabelY->setObjectName("TextLabelY");
106   TextLabelY->setFixedWidth(15);
107   myY = new QLineEdit(myGroupBoxCoord);
108   myY->setValidator(new QDoubleValidator(myY));
109   myY->setText(QString::number(0.0));
110   connect(myY, SIGNAL(textChanged(const QString&)), this, SLOT(onCoordChanged()));
111
112   // "Z" coordinate
113   QLabel* TextLabelZ = new QLabel (tr("LBL_Z"), myGroupBoxCoord );
114   TextLabelZ->setObjectName("TextLabelZ");
115   TextLabelZ->setFixedWidth(15);
116   myZ = new QLineEdit(myGroupBoxCoord);
117   myZ->setValidator(new QDoubleValidator(myZ));
118   myZ->setText(QString::number(0.0));
119   connect(myZ, SIGNAL(textChanged(const QString&)), this, SLOT(onCoordChanged()));
120
121   // Layout widgets in the horizontal group box
122   aHBoxLayout->addWidget(TextLabelX);
123   aHBoxLayout->addWidget(myX);
124   aHBoxLayout->addWidget(TextLabelY);
125   aHBoxLayout->addWidget(myY);
126   aHBoxLayout->addWidget(TextLabelZ);
127   aHBoxLayout->addWidget(myZ);
128
129   // "Close" button
130   QGroupBox* aGroupBox = new QGroupBox(this);
131   QHBoxLayout* aHBoxLayout2 = new QHBoxLayout(aGroupBox);
132   aHBoxLayout2->setMargin(11);
133   aHBoxLayout2->setSpacing(6);
134
135   QPushButton* m_bClose = new QPushButton(tr("&Close"), aGroupBox );
136   m_bClose->setObjectName("m_bClose");
137   m_bClose->setAutoDefault(TRUE);
138   m_bClose->setFixedSize(m_bClose->sizeHint());
139   connect(m_bClose, SIGNAL(clicked()), this, SLOT(onClickClose()));
140
141   // Layout buttons
142   aHBoxLayout2->addWidget(m_bClose);
143
144   // Layout top level widgets
145   layoutDlg->addLayout(aCheckBox,0,0);
146   layoutDlg->addWidget(myGroupBoxSel,1,0);
147   layoutDlg->addWidget(myGroupBoxCoord,2,0);
148   layoutDlg->addWidget(aGroupBox,3,0);
149   
150   setEnabled(myGroupBoxSel,!myIsBBCenter->isChecked());
151   setEnabled(myGroupBoxCoord,!myIsBBCenter->isChecked());
152
153   this->resize(400, this->sizeHint().height());
154
155   connect(view, SIGNAL(Show( QShowEvent * )), this, SLOT(onViewShow()));
156   connect(view, SIGNAL(Hide( QHideEvent * )), this, SLOT(onViewHide()));
157 }
158
159 /*
160  *  Destroys the object and frees any allocated resources
161  */
162 OCCViewer_SetRotationPointDlg
163 ::~OCCViewer_SetRotationPointDlg()
164 {
165   // no need to delete child widgets, Qt does it all for us
166 }
167
168 /*!
169   Return true if it is the first show for this dialog
170 */
171 bool
172 OCCViewer_SetRotationPointDlg
173 ::IsFirstShown()
174 {
175   return myIsBBCenter->isChecked() && myX->text().toDouble() == 0.
176     && myY->text().toDouble() == 0. && myZ->text().toDouble() == 0.;
177 }
178
179 void 
180 OCCViewer_SetRotationPointDlg
181 ::setEnabled(QGroupBox* theGrp, const bool theState)
182 {
183   QObjectList aChildren(theGrp->children());
184   QObject* anObj;
185   for(int i = 0; i < aChildren.size(); i++)
186   {
187     anObj = aChildren.at(i);
188     if (anObj !=0 && anObj->inherits("QLineEdit"))
189       ((QLineEdit*)anObj)->setReadOnly(!theState);
190     if (anObj !=0 && anObj->inherits("QPushButton"))
191       ((QLineEdit*)anObj)->setEnabled(theState);
192   }
193   
194 }
195
196 void 
197 OCCViewer_SetRotationPointDlg
198 ::onBBCenterChecked()
199 {
200   setEnabled(myGroupBoxSel,!myIsBBCenter->isChecked());
201   setEnabled(myGroupBoxCoord,!myIsBBCenter->isChecked());
202   
203   if ( myIsBBCenter->isChecked() )
204   {
205     if ( mySelectPoint->isChecked() )
206       mySelectPoint->toggle();
207     myView->activateSetRotationGravity();
208   }
209   else
210     myView->activateSetRotationSelected(myX->text().toDouble(), 
211                                         myY->text().toDouble(), 
212                                         myZ->text().toDouble());
213 }
214
215 void
216 OCCViewer_SetRotationPointDlg
217 ::onToOrigin()
218 {
219   if ( mySelectPoint->isChecked() )
220     mySelectPoint->toggle();
221   setCoords();
222   myView->activateSetRotationSelected(myX->text().toDouble(), 
223                                       myY->text().toDouble(), 
224                                       myZ->text().toDouble());
225 }
226
227 void
228 OCCViewer_SetRotationPointDlg
229 ::onSelectPoint()
230 {
231   if ( mySelectPoint->isChecked() )
232     myView->activateStartPointSelection();
233   else
234     mySelectPoint->toggle();
235 }
236
237 void
238 OCCViewer_SetRotationPointDlg
239 ::onCoordChanged()
240 {
241   if ( !myIsBBCenter->isChecked() )
242   {
243     if ( mySelectPoint->isChecked()
244          &&
245          ( myX->hasFocus() || myY->hasFocus() || myZ->hasFocus() ) )
246       mySelectPoint->toggle();
247     myView->activateSetRotationSelected(myX->text().toDouble(), 
248                                         myY->text().toDouble(), 
249                                         myZ->text().toDouble());
250   }
251 }
252
253 void
254 OCCViewer_SetRotationPointDlg
255 ::setCoords(double theX, double theY, double theZ)
256 {
257   myX->setText(QString::number(theX));
258   myY->setText(QString::number(theY));
259   myZ->setText(QString::number(theZ));
260 }
261
262 void
263 OCCViewer_SetRotationPointDlg
264 ::toggleChange()
265 {
266   if ( !myIsBBCenter->isChecked() )
267     mySelectPoint->toggle();
268 }
269
270 void
271 OCCViewer_SetRotationPointDlg
272 ::onClickClose()
273 {
274   myAction->setChecked( false );
275   reject();
276 }
277
278 void 
279 OCCViewer_SetRotationPointDlg
280 ::onViewShow()
281 {
282   if(myAction->isChecked())
283     show();
284   else
285     hide();
286 }
287
288 void 
289 OCCViewer_SetRotationPointDlg
290 ::onViewHide()
291 {
292   hide();
293 }
294
295 /*!
296   Custom handling of close event: toggle action
297 */
298 void
299 OCCViewer_SetRotationPointDlg
300 ::closeEvent( QCloseEvent* e )
301 {
302   myAction->setChecked( false );
303   QDialog::closeEvent( e );
304 }