Salome HOME
0aed2c230fd9a64bd963ae236085d65edb686f6b
[modules/gui.git] / src / OCCViewer / OCCViewer_SetRotationPointDlg.cxx
1 // Copyright (C) 2005  CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "OCCViewer_SetRotationPointDlg.h"
20
21 #include <QtxAction.h>
22
23 #include "OCCViewer_ViewWindow.h"
24
25 #include <qlineedit.h>
26 #include <qgroupbox.h>
27 #include <qlabel.h>
28 #include <qpushbutton.h>
29 #include <qlayout.h>
30 #include <qvalidator.h>
31 #include <qbuttongroup.h>
32 #include <qobjectlist.h>
33 #include <qcheckbox.h>
34 #include <qhbox.h>
35
36 /*!
37   Constructor
38   \param view - view window
39   \param parent - parent widget
40   \param name - dialog name
41   \param modal - is this dialog modal
42   \param fl - flags
43 */
44 OCCViewer_SetRotationPointDlg::OCCViewer_SetRotationPointDlg( OCCViewer_ViewWindow* view, QWidget* parent, const char* name, bool modal, WFlags fl )
45 : QDialog( parent, "OCCViewer_SetRotationPointDlg", modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
46   myView( view )
47 {
48   setCaption(tr("CAPTION"));
49   setSizeGripEnabled(TRUE);
50
51   // Create layout for this dialog
52   QGridLayout* layoutDlg = new QGridLayout (this);
53   layoutDlg->setSpacing(6);
54   layoutDlg->setMargin(11);
55
56   // Create check box "Use Bounding Box Center"
57   QHBox* aCheckBox = new QHBox(this);
58
59   myIsBBCenter = new QCheckBox(tr("USE_BBCENTER"), aCheckBox);
60   myIsBBCenter->setChecked(true);
61   connect(myIsBBCenter, SIGNAL(stateChanged(int)), SLOT(onBBCenterChecked()));
62
63   // Create croup button with radio buttons
64   myGroupSelButton = new QButtonGroup(2,Qt::Vertical,"",this);
65   myGroupSelButton->setMargin(11);
66   
67   // Create "Set to Origin" button
68   myToOrigin = new QPushButton(myGroupSelButton);
69   myToOrigin->setText(tr("LBL_TOORIGIN"));
70   connect(myToOrigin, SIGNAL(clicked()), this, SLOT(onToOrigin()));
71
72   // Create "Select Point from View" button
73   mySelectPoint = new QPushButton(myGroupSelButton);
74   mySelectPoint->setText(tr("LBL_SELECTPOINT"));
75   mySelectPoint->setToggleButton(true);
76   connect(mySelectPoint, SIGNAL(clicked()), this, SLOT(onSelectPoint()));
77
78   // Create croup box with grid layout
79   myGroupBoxCoord = new QGroupBox(this, "GroupBox");
80   QHBoxLayout* aHBoxLayout = new QHBoxLayout(myGroupBoxCoord);
81   aHBoxLayout->setMargin(11);
82   aHBoxLayout->setSpacing(6);
83
84   // "X" coordinate
85   QLabel* TextLabelX = new QLabel (tr("LBL_X"), myGroupBoxCoord, "TextLabelX");
86   TextLabelX->setFixedWidth(15);
87   myX = new QLineEdit(myGroupBoxCoord);
88   myX->setValidator(new QDoubleValidator(myX));
89   myX->setText(QString::number(0.0));
90   connect(myX, SIGNAL(textChanged(const QString&)), this, SLOT(onCoordChanged()));
91
92   // "Y" coordinate
93   QLabel* TextLabelY = new QLabel (tr("LBL_Y"), myGroupBoxCoord, "TextLabelY");
94   TextLabelY->setFixedWidth(15);
95   myY = new QLineEdit(myGroupBoxCoord);
96   myY->setValidator(new QDoubleValidator(myY));
97   myY->setText(QString::number(0.0));
98   connect(myY, SIGNAL(textChanged(const QString&)), this, SLOT(onCoordChanged()));
99
100   // "Z" coordinate
101   QLabel* TextLabelZ = new QLabel (tr("LBL_Z"), myGroupBoxCoord, "TextLabelZ");
102   TextLabelZ->setFixedWidth(15);
103   myZ = new QLineEdit(myGroupBoxCoord);
104   myZ->setValidator(new QDoubleValidator(myZ));
105   myZ->setText(QString::number(0.0));
106   connect(myZ, SIGNAL(textChanged(const QString&)), this, SLOT(onCoordChanged()));
107
108   // Layout widgets in the horizontal group box
109   aHBoxLayout->addWidget(TextLabelX);
110   aHBoxLayout->addWidget(myX);
111   aHBoxLayout->addWidget(TextLabelY);
112   aHBoxLayout->addWidget(myY);
113   aHBoxLayout->addWidget(TextLabelZ);
114   aHBoxLayout->addWidget(myZ);
115
116   // "Close" button
117   QGroupBox* aGroupBox = new QGroupBox(this);
118   QHBoxLayout* aHBoxLayout2 = new QHBoxLayout(aGroupBox);
119   aHBoxLayout2->setMargin(11);
120   aHBoxLayout2->setSpacing(6);
121
122   QPushButton* m_bClose = new QPushButton(tr("&Close"), aGroupBox, "m_bClose");
123   m_bClose->setAutoDefault(TRUE);
124   m_bClose->setFixedSize(m_bClose->sizeHint());
125   connect(m_bClose, SIGNAL(clicked()), this, SLOT(onClickClose()));
126
127   // Layout buttons
128   aHBoxLayout2->addWidget(m_bClose);
129
130   // Layout top level widgets
131   layoutDlg->addWidget(aCheckBox,0,0);
132   layoutDlg->addWidget(myGroupSelButton,1,0);
133   layoutDlg->addWidget(myGroupBoxCoord,2,0);
134   layoutDlg->addWidget(aGroupBox,3,0);
135   
136   setEnabled(myGroupSelButton,!myIsBBCenter->isChecked());
137   setEnabled(myGroupBoxCoord,!myIsBBCenter->isChecked());
138
139   this->resize(400, this->sizeHint().height());
140
141   connect(view, SIGNAL(Show( QShowEvent * )), this, SLOT(onViewShow()));
142   connect(view, SIGNAL(Hide( QHideEvent * )), this, SLOT(onViewHide()));
143 }
144
145 /*
146  *  Destroys the object and frees any allocated resources
147  */
148 OCCViewer_SetRotationPointDlg
149 ::~OCCViewer_SetRotationPointDlg()
150 {
151   // no need to delete child widgets, Qt does it all for us
152 }
153
154 /*!
155   Return true if it is the first show for this dialog
156 */
157 bool
158 OCCViewer_SetRotationPointDlg
159 ::IsFirstShown()
160 {
161   return myIsBBCenter->isChecked() && myX->text().toDouble() == 0.
162     && myY->text().toDouble() == 0. && myZ->text().toDouble() == 0.;
163 }
164
165 void 
166 OCCViewer_SetRotationPointDlg
167 ::setEnabled(QGroupBox* theGrp, const bool theState)
168 {
169   QObjectList aChildren(*theGrp->children());
170   QObject* anObj;
171   for(anObj = aChildren.first(); anObj !=0; anObj = aChildren.next())
172   {
173     if (anObj !=0 && anObj->inherits("QLineEdit"))
174       ((QLineEdit*)anObj)->setReadOnly(!theState);
175     if (anObj !=0 && anObj->inherits("QPushButton"))
176       ((QLineEdit*)anObj)->setEnabled(theState);
177   }
178   
179 }
180
181 void 
182 OCCViewer_SetRotationPointDlg
183 ::onBBCenterChecked()
184 {
185   setEnabled(myGroupSelButton,!myIsBBCenter->isChecked());
186   setEnabled(myGroupBoxCoord,!myIsBBCenter->isChecked());
187   
188   if ( myIsBBCenter->isChecked() )
189   {
190     if ( mySelectPoint->state() == QButton::On )
191       mySelectPoint->toggle();
192     myView->activateSetRotationGravity();
193   }
194   else
195     myView->activateSetRotationSelected(myX->text().toDouble(), 
196                                         myY->text().toDouble(), 
197                                         myZ->text().toDouble());
198 }
199
200 void
201 OCCViewer_SetRotationPointDlg
202 ::onToOrigin()
203 {
204   if ( mySelectPoint->state() == QButton::On )
205     mySelectPoint->toggle();
206   setCoords();
207   myView->activateSetRotationSelected(myX->text().toDouble(), 
208                                       myY->text().toDouble(), 
209                                       myZ->text().toDouble());
210 }
211
212 void
213 OCCViewer_SetRotationPointDlg
214 ::onSelectPoint()
215 {
216   if ( mySelectPoint->state() == QButton::On )
217     myView->activateStartPointSelection();
218   else
219     mySelectPoint->toggle();
220 }
221
222 void
223 OCCViewer_SetRotationPointDlg
224 ::onCoordChanged()
225 {
226   if ( !myIsBBCenter->isChecked() )
227   {
228     if ( mySelectPoint->state() == QButton::On
229          &&
230          ( myX->hasFocus() || myY->hasFocus() || myZ->hasFocus() ) )
231       mySelectPoint->toggle();
232     myView->activateSetRotationSelected(myX->text().toDouble(), 
233                                         myY->text().toDouble(), 
234                                         myZ->text().toDouble());
235   }
236 }
237
238 void
239 OCCViewer_SetRotationPointDlg
240 ::setCoords(double theX, double theY, double theZ)
241 {
242   myX->setText(QString::number(theX));
243   myY->setText(QString::number(theY));
244   myZ->setText(QString::number(theZ));
245 }
246
247 void
248 OCCViewer_SetRotationPointDlg
249 ::toggleChange()
250 {
251   if ( !myIsBBCenter->isChecked() )
252     mySelectPoint->toggle();
253 }
254
255 void
256 OCCViewer_SetRotationPointDlg
257 ::onClickClose()
258 {
259   myAction->setOn( false );
260   reject();
261 }
262
263 void 
264 OCCViewer_SetRotationPointDlg
265 ::onViewShow()
266 {
267   if(myAction->isOn())
268     show();
269   else
270     hide();
271 }
272
273 void 
274 OCCViewer_SetRotationPointDlg
275 ::onViewHide()
276 {
277   hide();
278 }