Salome HOME
PAL15429 Computation of the mesh, based on "014.brep" via Tetrahedron(NETGEN), is...
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Preferences_SelectionDlg.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 //  SMESH SMESHGUI : GUI for SMESH component
23 //  File   : SMESHGUI_Preferences_SelectionDlg.cxx
24 //  Author : Natalia KOPNOVA
25 //  Module : SMESH
26 //  $Header$
27 //
28 #include "SMESHGUI_Preferences_SelectionDlg.h"
29 #include "SMESHGUI.h"
30
31 #include "SMESHGUI_Utils.h"
32
33 #include "SUIT_Desktop.h"
34
35 #include <qgroupbox.h>
36 #include <qlayout.h>
37 #include <qlabel.h>
38 #include <qlineedit.h>
39 #include <qvalidator.h>
40 #include <qspinbox.h>
41 #include <qpushbutton.h>
42 #include <qpalette.h>
43 #include <qcolordialog.h>
44
45 using namespace std;
46
47 //=================================================================================
48 // class    : SMESHGUI_LineEdit
49 // purpose  : 
50 //=================================================================================
51 SMESHGUI_LineEdit::SMESHGUI_LineEdit(QWidget* parent, const char *name)
52   : QLineEdit(parent, name)
53 {
54 }
55
56 SMESHGUI_LineEdit::SMESHGUI_LineEdit(const QString& text, QWidget* parent, const char *name)
57   : QLineEdit(text, parent, name)
58 {
59 }
60
61 void SMESHGUI_LineEdit::focusOutEvent(QFocusEvent* e)
62 {
63   const QValidator* aVal = validator();
64   QString aText = text();
65   int aCurPos = cursorPosition();
66   if (aVal && aVal->validate(aText, aCurPos) != QValidator::Acceptable) {
67     QString aValid = aText;
68     aVal->fixup(aValid);
69     if (aText != aValid) {
70       setText(aValid);
71       update();
72       return;
73     }
74   }
75   QLineEdit::focusOutEvent(e);
76 }
77
78
79 //=================================================================================
80 // class    : SMESHGUI_DoubleValidator
81 // purpose  : 
82 //=================================================================================
83 SMESHGUI_DoubleValidator::SMESHGUI_DoubleValidator(QObject * parent, const char *name)
84   : QDoubleValidator(parent, name)
85 {
86 }
87
88 SMESHGUI_DoubleValidator::SMESHGUI_DoubleValidator(double bottom, double top, int decimals,
89                                                  QObject * parent, const char *name)
90   : QDoubleValidator(bottom, top, decimals, parent, name)
91 {
92 }
93
94 void SMESHGUI_DoubleValidator::fixup(QString& theText) const
95 {
96   bool ok;
97   double aValue = theText.toDouble(&ok);
98   if (ok) {
99     if (aValue < bottom())
100       theText = QString::number(bottom(), 'g', decimals());
101     if (aValue > top())
102       theText = QString::number(top(), 'g', decimals());
103   }
104 }
105
106
107 //=================================================================================
108 // class    : SMESHGUI_Preferences_SelectionDlg()
109 // purpose  : 
110 //=================================================================================
111 SMESHGUI_Preferences_SelectionDlg::SMESHGUI_Preferences_SelectionDlg( SMESHGUI* theModule, const char* name )
112   : QDialog( SMESH::GetDesktop( theModule ), name, true, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
113     mySMESHGUI( theModule )
114 {
115   if ( !name ) setName( "SMESHGUI_Preferences_SelectionDlg" );
116   setCaption( tr( "SMESH_PREF_SELECTION"  ) );
117
118   QVBoxLayout* aMainLayout = new QVBoxLayout(this, 11, 6);
119   QLabel* aLabel;
120   
121   /***************************************************************/
122   QGroupBox* aSelectBox = new QGroupBox(4, Qt::Horizontal, this, "selection");
123   aSelectBox->setTitle(tr("SMESH_SELECTION"));
124
125   aLabel = new QLabel(aSelectBox, "selection color label");
126   aLabel->setText(tr("SMESH_OUTLINE_COLOR"));
127   myColor[2] = new QPushButton(aSelectBox, "outline color");
128   myColor[2]->setFixedSize(QSize(25, 25));
129
130   aSelectBox->addSpace(0);
131   aSelectBox->addSpace(0);
132
133   aLabel = new QLabel(aSelectBox, "selection color label");
134   aLabel->setText(tr("SMESH_ELEMENTS_COLOR"));
135   myColor[1] = new QPushButton(aSelectBox, "elements color");
136   myColor[1]->setFixedSize(QSize(25, 25));
137
138   aLabel = new QLabel(aSelectBox, "selection width label");
139   aLabel->setText(tr("SMESH_WIDTH"));
140   myWidth[1] = new QSpinBox(0, 5, 1, aSelectBox, "selection width");
141   myWidth[1]->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
142   myWidth[1]->setButtonSymbols(QSpinBox::PlusMinus);
143   myWidth[1]->setMinimumWidth(50);
144   
145   /***************************************************************/
146   QGroupBox* aPreSelectBox = new QGroupBox(1, Qt::Vertical, this, "preselection");
147   aPreSelectBox->setTitle(tr("SMESH_PRESELECTION"));
148
149   aLabel = new QLabel(aPreSelectBox, "preselection color label");
150   aLabel->setText(tr("SMESH_HILIGHT_COLOR"));
151   myColor[0] = new QPushButton(aPreSelectBox, "preselection color");
152   myColor[0]->setFixedSize(QSize(25, 25));
153
154   aLabel = new QLabel(aPreSelectBox, "preselection width label");
155   aLabel->setText(tr("SMESH_WIDTH"));
156   myWidth[0] = new QSpinBox(0, 5, 1, aPreSelectBox, "preselection width");
157   myWidth[0]->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
158   myWidth[0]->setButtonSymbols(QSpinBox::PlusMinus);
159   myWidth[0]->setMinimumWidth(50);
160   
161   /***************************************************************/
162   QGroupBox* aPrecisionBox = new QGroupBox(1, Qt::Vertical, this, "preselection");
163   aPrecisionBox->setTitle(tr("SMESH_PRECISION"));
164   QDoubleValidator* aValidator = new SMESHGUI_DoubleValidator(aPrecisionBox);
165   aValidator->setBottom(0.001);
166   aValidator->setDecimals(6);
167
168   aLabel = new QLabel(aPrecisionBox, "node tol label");
169   aLabel->setText(tr("SMESH_NODES"));
170   myPrecision[0] = new SMESHGUI_LineEdit(aPrecisionBox, "node precision");
171   myPrecision[0]->setValidator(aValidator);
172
173   aLabel = new QLabel(aPrecisionBox, "item tol label");
174   aLabel->setText(tr("SMESH_ELEMENTS"));
175   myPrecision[1] = new SMESHGUI_LineEdit(aPrecisionBox, "item precision");
176   myPrecision[1]->setValidator(aValidator);
177
178   /***************************************************************/
179   QFrame* aButtons = new QFrame(this, "button box");
180   aButtons->setFrameStyle(QFrame::Box | QFrame::Sunken);
181   QHBoxLayout* aBtnLayout = new QHBoxLayout(aButtons, 11, 6);
182   aBtnLayout->setAutoAdd(false);
183
184   QPushButton* aOKBtn = new QPushButton(aButtons, "ok");
185   aOKBtn->setText(tr("SMESH_BUT_OK"));
186   aOKBtn->setAutoDefault(true);
187   aOKBtn->setDefault(true);
188   QPushButton* aCloseBtn = new QPushButton(aButtons, "close");
189   aCloseBtn->setText(tr("SMESH_BUT_CLOSE"));
190   aCloseBtn->setAutoDefault(true);
191
192   aBtnLayout->addWidget(aOKBtn);
193   aBtnLayout->addStretch();
194   aBtnLayout->addWidget(aCloseBtn);
195
196   /***************************************************************/
197   aMainLayout->addWidget(aSelectBox);
198   aMainLayout->addWidget(aPreSelectBox);
199   aMainLayout->addWidget(aPrecisionBox);
200   aMainLayout->addWidget(aButtons);
201
202   for (int i = 0; i < 3; i++)
203     connect(myColor[i], SIGNAL(clicked()), this, SLOT(onSelectColor()));
204
205   connect(aOKBtn, SIGNAL(clicked()), this, SLOT(accept()));
206   connect(aCloseBtn, SIGNAL(clicked()), this, SLOT(reject()));
207 }
208
209 //=================================================================================
210 // function : ~SMESHGUI_Preferences_SelectionDlg()
211 // purpose  : Destroys the object and frees any allocated resources
212 //=================================================================================
213 SMESHGUI_Preferences_SelectionDlg::~SMESHGUI_Preferences_SelectionDlg()
214 {
215     // no need to delete child widgets, Qt does it all for us
216 }
217
218 //=================================================================================
219 // function : closeEvent()
220 // purpose  :
221 //=================================================================================
222 void SMESHGUI_Preferences_SelectionDlg::closeEvent( QCloseEvent* e )
223 {
224   reject();
225 }
226
227 //=================================================================================
228 // function : onSelectColor()
229 // purpose  :
230 //=================================================================================
231 void SMESHGUI_Preferences_SelectionDlg::onSelectColor()
232 {
233   QPushButton* aSender = (QPushButton*)sender();
234   QColor aColor = aSender->palette().active().button();
235   aColor = QColorDialog::getColor(aColor, this);
236   if (aColor.isValid()) {
237     QPalette aPal = aSender->palette();
238     aPal.setColor(QColorGroup::Button, aColor);
239     aSender->setPalette(aPal);
240   }
241 }
242
243 //=================================================================================
244 // function : SetColor()
245 // purpose  :
246 //=================================================================================
247 void SMESHGUI_Preferences_SelectionDlg::SetColor(int type, QColor color)
248 {
249   if (type > 0 && type <= 3) {
250     QPalette aPal = myColor[type-1]->palette();
251     aPal.setColor(QColorGroup::Button, color);
252     myColor[type-1]->setPalette(aPal);
253  }
254 }
255
256 //=================================================================================
257 // function : GetColor()
258 // purpose  :
259 //=================================================================================
260 QColor SMESHGUI_Preferences_SelectionDlg::GetColor(int type)
261 {
262   QColor aColor;
263   if (type > 0 && type <= 3)
264     aColor = myColor[type-1]->palette().active().button();
265   return aColor;
266 }
267
268 //=================================================================================
269 // function : SetWidth()
270 // purpose  :
271 //=================================================================================
272 void SMESHGUI_Preferences_SelectionDlg::SetWidth(int type, int value)
273 {
274   if (type > 0 && type <= 2)
275     myWidth[type-1]->setValue(value);
276 }
277
278 //=================================================================================
279 // function : GetWidth()
280 // purpose  :
281 //=================================================================================
282 int SMESHGUI_Preferences_SelectionDlg::GetWidth(int type)
283 {
284   if (type > 0 && type <= 2)
285     return myWidth[type-1]->value();
286   return 0;
287 }
288
289 //=================================================================================
290 // function : SetPrecision()
291 // purpose  :
292 //=================================================================================
293 void SMESHGUI_Preferences_SelectionDlg::SetPrecision(int type, double value)
294 {
295   if (type > 0 && type <= 2)
296     myPrecision[type-1]->setText(QString::number(value));
297 }
298
299 //=================================================================================
300 // function : GetPrecision()
301 // purpose  :
302 //=================================================================================
303 double SMESHGUI_Preferences_SelectionDlg::GetPrecision(int type)
304 {
305   if (type > 0 && type <= 2)
306     return myPrecision[type-1]->text().toDouble();
307   return 0;
308 }