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