Salome HOME
SALOME PAL V1_4_1
[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 using namespace std;
30 #include "SMESHGUI_Preferences_SelectionDlg.h"
31 #include "SMESHGUI.h"
32
33 #include <qgroupbox.h>
34 #include <qlayout.h>
35 #include <qlabel.h>
36 #include <qlineedit.h>
37 #include <qvalidator.h>
38 #include <qspinbox.h>
39 #include <qpushbutton.h>
40 #include <qpalette.h>
41 #include <qcolordialog.h>
42
43 //=================================================================================
44 // class    : SMESHGUI_LineEdit
45 // purpose  : 
46 //=================================================================================
47 SMESHGUI_LineEdit::SMESHGUI_LineEdit(QWidget* parent, const char *name)
48   : QLineEdit(parent, name)
49 {
50 }
51
52 SMESHGUI_LineEdit::SMESHGUI_LineEdit(const QString& text, QWidget* parent, const char *name)
53   : QLineEdit(text, parent, name)
54 {
55 }
56
57 void SMESHGUI_LineEdit::focusOutEvent(QFocusEvent* e)
58 {
59   const QValidator* aVal = validator();
60   QString aText = text();
61   int aCurPos = cursorPosition();
62   if (aVal && aVal->validate(aText, aCurPos) != QValidator::Acceptable) {
63     QString aValid = aText;
64     aVal->fixup(aValid);
65     if (aText != aValid) {
66       setText(aValid);
67       update();
68       return;
69     }
70   }
71   QLineEdit::focusOutEvent(e);
72 }
73
74
75 //=================================================================================
76 // class    : SMESHGUI_DoubleValidator
77 // purpose  : 
78 //=================================================================================
79 SMESHGUI_DoubleValidator::SMESHGUI_DoubleValidator(QObject * parent, const char *name)
80   : QDoubleValidator(parent, name)
81 {
82 }
83
84 SMESHGUI_DoubleValidator::SMESHGUI_DoubleValidator(double bottom, double top, int decimals,
85                                                  QObject * parent, const char *name)
86   : QDoubleValidator(bottom, top, decimals, parent, name)
87 {
88 }
89
90 void SMESHGUI_DoubleValidator::fixup(QString& theText) const
91 {
92   bool ok;
93   double aValue = theText.toDouble(&ok);
94   if (ok) {
95     if (aValue < bottom())
96       theText = QString::number(bottom(), 'g', decimals());
97     if (aValue > top())
98       theText = QString::number(top(), 'g', decimals());
99   }
100 }
101
102
103 //=================================================================================
104 // class    : SMESHGUI_Preferences_SelectionDlg()
105 // purpose  : 
106 //=================================================================================
107 SMESHGUI_Preferences_SelectionDlg::SMESHGUI_Preferences_SelectionDlg( QWidget* parent, const char* name )
108   : QDialog( parent, name, true, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
109 {
110   if ( !name ) setName( "SMESHGUI_Preferences_SelectionDlg" );
111   setCaption( tr( "SMESH_PREF_SELECTION"  ) );
112
113   QVBoxLayout* aMainLayout = new QVBoxLayout(this, 11, 6);
114   QLabel* aLabel;
115   
116   /***************************************************************/
117   QGroupBox* aSelectBox = new QGroupBox(4, Qt::Horizontal, this, "selection");
118   aSelectBox->setTitle(tr("SMESH_SELECTION"));
119
120   aLabel = new QLabel(aSelectBox, "selection color label");
121   aLabel->setText(tr("SMESH_OUTLINE_COLOR"));
122   myColor[2] = new QPushButton(aSelectBox, "outline color");
123   myColor[2]->setFixedSize(QSize(25, 25));
124
125   aSelectBox->addSpace(0);
126   aSelectBox->addSpace(0);
127
128   aLabel = new QLabel(aSelectBox, "selection color label");
129   aLabel->setText(tr("SMESH_ELEMENTS_COLOR"));
130   myColor[1] = new QPushButton(aSelectBox, "elements color");
131   myColor[1]->setFixedSize(QSize(25, 25));
132
133   aLabel = new QLabel(aSelectBox, "selection width label");
134   aLabel->setText(tr("SMESH_WIDTH"));
135   myWidth[1] = new QSpinBox(0, 5, 1, aSelectBox, "selection width");
136   myWidth[1]->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
137   myWidth[1]->setButtonSymbols(QSpinBox::PlusMinus);
138   myWidth[1]->setMinimumWidth(50);
139   
140   /***************************************************************/
141   QGroupBox* aPreSelectBox = new QGroupBox(1, Qt::Vertical, this, "preselection");
142   aPreSelectBox->setTitle(tr("SMESH_PRESELECTION"));
143
144   aLabel = new QLabel(aPreSelectBox, "preselection color label");
145   aLabel->setText(tr("SMESH_HILIGHT_COLOR"));
146   myColor[0] = new QPushButton(aPreSelectBox, "preselection color");
147   myColor[0]->setFixedSize(QSize(25, 25));
148
149   aLabel = new QLabel(aPreSelectBox, "preselection width label");
150   aLabel->setText(tr("SMESH_WIDTH"));
151   myWidth[0] = new QSpinBox(0, 5, 1, aPreSelectBox, "preselection width");
152   myWidth[0]->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
153   myWidth[0]->setButtonSymbols(QSpinBox::PlusMinus);
154   myWidth[0]->setMinimumWidth(50);
155   
156   /***************************************************************/
157   QGroupBox* aPrecisionBox = new QGroupBox(1, Qt::Vertical, this, "preselection");
158   aPrecisionBox->setTitle(tr("SMESH_PRECISION"));
159   QDoubleValidator* aValidator = new SMESHGUI_DoubleValidator(aPrecisionBox);
160   aValidator->setBottom(0.001);
161   aValidator->setDecimals(6);
162
163   aLabel = new QLabel(aPrecisionBox, "node tol label");
164   aLabel->setText(tr("SMESH_NODES"));
165   myPrecision[0] = new SMESHGUI_LineEdit(aPrecisionBox, "node precision");
166   myPrecision[0]->setValidator(aValidator);
167
168   aLabel = new QLabel(aPrecisionBox, "item tol label");
169   aLabel->setText(tr("SMESH_ELEMENTS"));
170   myPrecision[1] = new SMESHGUI_LineEdit(aPrecisionBox, "item precision");
171   myPrecision[1]->setValidator(aValidator);
172
173   /***************************************************************/
174   QFrame* aButtons = new QFrame(this, "button box");
175   aButtons->setFrameStyle(QFrame::Box | QFrame::Sunken);
176   QHBoxLayout* aBtnLayout = new QHBoxLayout(aButtons, 11, 6);
177   aBtnLayout->setAutoAdd(false);
178
179   QPushButton* aOKBtn = new QPushButton(aButtons, "ok");
180   aOKBtn->setText(tr("SMESH_BUT_OK"));
181   aOKBtn->setAutoDefault(true);
182   aOKBtn->setDefault(true);
183   QPushButton* aCloseBtn = new QPushButton(aButtons, "close");
184   aCloseBtn->setText(tr("SMESH_BUT_CLOSE"));
185   aCloseBtn->setAutoDefault(true);
186
187   aBtnLayout->addWidget(aOKBtn);
188   aBtnLayout->addStretch();
189   aBtnLayout->addWidget(aCloseBtn);
190
191   /***************************************************************/
192   aMainLayout->addWidget(aSelectBox);
193   aMainLayout->addWidget(aPreSelectBox);
194   aMainLayout->addWidget(aPrecisionBox);
195   aMainLayout->addWidget(aButtons);
196
197   for (int i = 0; i < 3; i++)
198     connect(myColor[i], SIGNAL(clicked()), this, SLOT(onSelectColor()));
199
200   connect(aOKBtn, SIGNAL(clicked()), this, SLOT(accept()));
201   connect(aCloseBtn, SIGNAL(clicked()), this, SLOT(reject()));
202
203   /* Move widget on the botton right corner of main widget */
204   int x, y ;
205   SMESHGUI::GetSMESHGUI()->DefineDlgPosition(this, x, y);
206   this->move(x, y);
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 }