Salome HOME
Issue 0020580: improved validation in integer and double spin boxes, possibility...
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_SpinBox.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_SpinBox.cxx
24 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
25 // SMESH includes
26 //
27 #include "SMESHGUI_SpinBox.h"
28
29 #include <SUIT_Session.h>
30 #include <SUIT_ResourceMgr.h>
31
32 // Qt includes
33 #include <QLineEdit>
34 #include <QVariant>
35
36 //=================================================================================
37 // class    : SMESHGUI_SpinBox()
38 // purpose  : constructor of specific widget accepting floats in double precision.
39 //=================================================================================
40 SMESHGUI_SpinBox::SMESHGUI_SpinBox( QWidget* parent )
41   : SalomeApp_DoubleSpinBox( parent )
42 {
43 }
44
45 //=================================================================================
46 // function : ~SMESHGUI_SpinBox()
47 // purpose  : destructor
48 //=================================================================================
49 SMESHGUI_SpinBox::~SMESHGUI_SpinBox()
50 {
51 }
52
53 //=================================================================================
54 // function : SetStep()  [SLOT]
55 // purpose  :
56 //=================================================================================
57 void SMESHGUI_SpinBox::SetStep( double newStep )
58 {
59   setSingleStep( newStep );
60 }
61
62 //=================================================================================
63 // function : SetValue()
64 // purpose  :
65 //=================================================================================
66 void SMESHGUI_SpinBox::SetValue( double v )
67 {
68   setValue(v);
69   editor()->setCursorPosition( 0 );
70 }
71
72 //=================================================================================
73 // function : GetValue()
74 // purpose  : returns a double
75 //=================================================================================
76 double SMESHGUI_SpinBox::GetValue() const
77 {
78   return value();
79 }
80
81 //=================================================================================
82 // function : GetString()
83 // purpose  : returns a QString
84 //=================================================================================
85 QString SMESHGUI_SpinBox::GetString() const
86 {
87   return cleanText();
88 }
89
90 //=================================================================================
91 // function : editor()
92 // purpose  : returns editor
93 //=================================================================================
94 QLineEdit* SMESHGUI_SpinBox::editor() const
95 {
96   return SalomeApp_DoubleSpinBox::lineEdit();
97
98
99 //=================================================================================
100 // function : RangeStepAndValidator()
101 // purpose  :
102 //=================================================================================
103 void SMESHGUI_SpinBox::RangeStepAndValidator( double min,
104                                               double max,
105                                               double step,
106                                               const char* quantity )
107 {
108   // Obtain precision from preferences
109   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
110   int precision = resMgr->integerValue( "SMESH", quantity, -3 );
111   
112   setPrecision(precision); // PAL8769. Minus is for using 'g' double->string conversion specifier,
113   //                          see QtxDoubleSpinBox::mapValueToText( double v )
114   //                          san: this can be achieved using preferences
115   setDecimals(qAbs(precision));
116   setRange(min, max);
117   setSingleStep( step );
118   setDefaultValue( min );
119   
120   // Add a hint for the user saying how to tune precision
121   QString userPropName = QObject::tr( QString( "SMESH_PREF_%1" ).arg( quantity ).toLatin1().constData() );
122   setProperty( "validity_tune_hint", 
123                QVariant( QObject::tr( "SMESH_PRECISION_HINT" ).arg( userPropName ) ) );  
124 }