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