Salome HOME
Update version number: 3.1.0a2
[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.org 
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   /* Move widget on the botton right corner of main widget */
210   int x, y ;
211   mySMESHGUI->DefineDlgPosition(this, x, y);
212   this->move(x, y);
213 }
214
215 //=================================================================================
216 // function : ~SMESHGUI_Preferences_SelectionDlg()
217 // purpose  : Destroys the object and frees any allocated resources
218 //=================================================================================
219 SMESHGUI_Preferences_SelectionDlg::~SMESHGUI_Preferences_SelectionDlg()
220 {
221     // no need to delete child widgets, Qt does it all for us
222 }
223
224 //=================================================================================
225 // function : closeEvent()
226 // purpose  :
227 //=================================================================================
228 void SMESHGUI_Preferences_SelectionDlg::closeEvent( QCloseEvent* e )
229 {
230   reject();
231 }
232
233 //=================================================================================
234 // function : onSelectColor()
235 // purpose  :
236 //=================================================================================
237 void SMESHGUI_Preferences_SelectionDlg::onSelectColor()
238 {
239   QPushButton* aSender = (QPushButton*)sender();
240   QColor aColor = aSender->palette().active().button();
241   aColor = QColorDialog::getColor(aColor, this);
242   if (aColor.isValid()) {
243     QPalette aPal = aSender->palette();
244     aPal.setColor(QColorGroup::Button, aColor);
245     aSender->setPalette(aPal);
246   }
247 }
248
249 //=================================================================================
250 // function : SetColor()
251 // purpose  :
252 //=================================================================================
253 void SMESHGUI_Preferences_SelectionDlg::SetColor(int type, QColor color)
254 {
255   if (type > 0 && type <= 3) {
256     QPalette aPal = myColor[type-1]->palette();
257     aPal.setColor(QColorGroup::Button, color);
258     myColor[type-1]->setPalette(aPal);
259  }
260 }
261
262 //=================================================================================
263 // function : GetColor()
264 // purpose  :
265 //=================================================================================
266 QColor SMESHGUI_Preferences_SelectionDlg::GetColor(int type)
267 {
268   QColor aColor;
269   if (type > 0 && type <= 3)
270     aColor = myColor[type-1]->palette().active().button();
271   return aColor;
272 }
273
274 //=================================================================================
275 // function : SetWidth()
276 // purpose  :
277 //=================================================================================
278 void SMESHGUI_Preferences_SelectionDlg::SetWidth(int type, int value)
279 {
280   if (type > 0 && type <= 2)
281     myWidth[type-1]->setValue(value);
282 }
283
284 //=================================================================================
285 // function : GetWidth()
286 // purpose  :
287 //=================================================================================
288 int SMESHGUI_Preferences_SelectionDlg::GetWidth(int type)
289 {
290   if (type > 0 && type <= 2)
291     return myWidth[type-1]->value();
292   return 0;
293 }
294
295 //=================================================================================
296 // function : SetPrecision()
297 // purpose  :
298 //=================================================================================
299 void SMESHGUI_Preferences_SelectionDlg::SetPrecision(int type, double value)
300 {
301   if (type > 0 && type <= 2)
302     myPrecision[type-1]->setText(QString::number(value));
303 }
304
305 //=================================================================================
306 // function : GetPrecision()
307 // purpose  :
308 //=================================================================================
309 double SMESHGUI_Preferences_SelectionDlg::GetPrecision(int type)
310 {
311   if (type > 0 && type <= 2)
312     return myPrecision[type-1]->text().toDouble();
313   return 0;
314 }