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