Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / SalomeApp / SalomeApp_DoubleSpinBox.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // File:      SalomeApp_DoubleSpinBox.cxx
20 // Author:    Oleg UVAROV
21
22 #include "SalomeApp_DoubleSpinBox.h"
23 #include "SalomeApp_Application.h"
24 #include "SalomeApp_Study.h"
25
26 #include <SUIT_Session.h>
27
28 #include "SALOMEDSClient_ClientFactory.hxx" 
29 #include CORBA_SERVER_HEADER(SALOMEDS)
30
31 #include <QKeyEvent>
32 #include <QLineEdit>
33
34 /*!
35   \class SalomeApp_DoubleSpinBox
36 */
37
38 /*!
39   \brief Constructor.
40
41   Constructs a spin box with 0.0 as minimum value and 99.99 as maximum value,
42   a step value of 1.0 and a precision of 2 decimal places. 
43   The value is initially set to 0.00.
44
45   \param parent parent object
46 */
47 SalomeApp_DoubleSpinBox::SalomeApp_DoubleSpinBox( QWidget* parent )
48 : QtxDoubleSpinBox( parent ),
49   myDefaultValue( 0.0 ),
50   myIsRangeSet( false ),
51   myMinimum( 0.0 ),
52   myMaximum( 99.99 )
53 {
54   connectSignalsAndSlots();
55 }
56
57 /*!
58   \brief Constructor.
59
60   Constructs a spin box with specified minimum, maximum and step value.
61   The precision is set to 2 decimal places. 
62   The value is initially set to the minimum value.
63
64   \param min spin box minimum possible value
65   \param max spin box maximum possible value
66   \param step spin box increment/decrement value
67   \param parent parent object
68 */
69 SalomeApp_DoubleSpinBox::SalomeApp_DoubleSpinBox( double min, double max, double step, QWidget* parent )
70 : QtxDoubleSpinBox( min, max, step, parent ),
71   myDefaultValue( 0.0 ),
72   myIsRangeSet( false ),
73   myMinimum( min ),
74   myMaximum( max )
75 {
76   connectSignalsAndSlots();
77 }
78
79 /*!
80   \brief Constructor.
81
82   Constructs a spin box with specified minimum, maximum and step value.
83   The precision is set to 2 decimal places. 
84   The value is initially set to the minimum value.
85
86   \param min spin box minimum possible value
87   \param max spin box maximum possible value
88   \param step spin box increment/decrement value
89   \param parent parent object
90 */
91 SalomeApp_DoubleSpinBox::SalomeApp_DoubleSpinBox( double min, double max, double step, int prec, int dec, QWidget* parent )
92 : QtxDoubleSpinBox( min, max, step, prec, dec, parent ),
93   myDefaultValue( 0.0 ),
94   myIsRangeSet( false ),
95   myMinimum( min ),
96   myMaximum( max )
97 {
98   connectSignalsAndSlots();
99 }
100
101 /*!
102   \brief Destructor.
103 */
104 SalomeApp_DoubleSpinBox::~SalomeApp_DoubleSpinBox()
105 {
106 }
107
108 /*!
109   \brief Connect signals and slots.
110 */
111 void SalomeApp_DoubleSpinBox::connectSignalsAndSlots()
112 {
113   connect( this, SIGNAL( editingFinished() ),
114            this, SLOT( onEditingFinished() ) );
115
116   connect( this, SIGNAL( valueChanged( const QString& ) ),
117            this, SLOT( onTextChanged( const QString& ) ) );
118
119   connect( lineEdit(), SIGNAL( textChanged( const QString& ) ),
120            this, SLOT( onTextChanged( const QString& ) ) );
121
122   connect( lineEdit(), SIGNAL( textChanged( const QString& )),
123            this, SIGNAL( textChanged( const QString& ) ) );
124 }
125
126 /*!
127   \brief This function is called when editing is finished.
128 */
129 void SalomeApp_DoubleSpinBox::onEditingFinished()
130 {
131   if( myTextValue.isNull() )
132     myTextValue = text();
133
134   setText( myTextValue );
135 }
136
137 /*!
138   \brief This function is called when value is changed.
139 */
140 void SalomeApp_DoubleSpinBox::onTextChanged( const QString& text )
141 {
142   myTextValue = text;
143
144   double value = 0;
145   if( isValid( text, value ) == Acceptable )
146     myCorrectValue = text;
147 }
148
149 /*!
150   \brief Interpret text entered by the user as a value.
151   \param text text entered by the user
152   \return mapped value
153   \sa textFromValue()
154 */
155 double SalomeApp_DoubleSpinBox::valueFromText( const QString& text ) const
156 {
157   double value = 0;
158   if( isValid( text, value ) == Acceptable )
159     return value;
160
161   return defaultValue();
162 }
163
164 /*!
165   \brief This function is used by the spin box whenever it needs to display
166   the given value.
167
168   \param val spin box value
169   \return text representation of the value
170   \sa valueFromText()
171 */
172 QString SalomeApp_DoubleSpinBox::textFromValue( double val ) const
173 {
174   return QtxDoubleSpinBox::textFromValue( val );
175 }
176
177 /*!
178   \brief This function is used to determine whether input is valid.
179   \param str currently entered value
180   \param pos cursor position in the string
181   \return validating operation result
182 */
183 QValidator::State SalomeApp_DoubleSpinBox::validate( QString& str, int& pos ) const
184 {
185   return QValidator::Acceptable;
186 }
187
188 /*!
189   \brief This function is used to determine whether input is valid.
190   \return validating operation result
191 */
192 bool SalomeApp_DoubleSpinBox::isValid( QString& msg, bool toCorrect )
193 {
194   double value;
195   State aState = isValid( text(), value );
196
197   if( aState != Acceptable )
198   {
199     if( toCorrect )
200     {
201       if( aState == Incompatible )
202         msg += tr( "ERR_INCOMPATIBLE_TYPE" ).arg( text() ) + "\n";
203       else if( aState == NoVariable )
204         msg += tr( "ERR_NO_VARIABLE" ).arg( text() ) + "\n";
205       else if( aState == Invalid )
206         msg += tr( "ERR_INVALID_VALUE" ) + "\n";
207
208       setText( myCorrectValue );
209     }
210     return false;
211   }
212
213   return true;
214 }
215
216 /*!
217   \brief This function is used to set a default value for this spinbox.
218   \param value default value
219 */
220 void SalomeApp_DoubleSpinBox::setDefaultValue( const double value )
221 {
222   myDefaultValue = value;
223 }
224
225 /*!
226   \brief This function is used to set minimum and maximum values for this spinbox.
227   \param min minimum value
228   \param max maximum value
229 */
230 void SalomeApp_DoubleSpinBox::setRange( const double min, const double max )
231 {
232   QtxDoubleSpinBox::setRange( min, max );
233
234   myIsRangeSet = true;
235   myMinimum = min;
236   myMaximum = max;
237 }
238
239 /*!
240   \brief This function is used to set a current value for this spinbox.
241   \param value current value
242 */
243 void SalomeApp_DoubleSpinBox::setValue( const double value )
244 {
245   QtxDoubleSpinBox::setValue( value );
246
247   myCorrectValue = QString::number( value );
248   myTextValue = myCorrectValue;
249 }
250
251 /*!
252   \brief This function is used to set a text for this spinbox.
253   \param value current value
254 */
255 void SalomeApp_DoubleSpinBox::setText( const QString& value )
256 {
257   lineEdit()->setText(value);
258 }
259
260 /*!
261   \brief This function is used to determine whether input is valid.
262   \return validating operation result
263 */
264 SalomeApp_DoubleSpinBox::State SalomeApp_DoubleSpinBox::isValid( const QString& text, double& value ) const
265 {
266   SearchState aSearchState = findVariable( text, value );
267   if( aSearchState == NotFound )
268   {
269     bool ok = false;
270     value = text.toDouble( &ok );
271     if( !ok )
272       return NoVariable;
273   }
274   else if( aSearchState == IncorrectType )
275     return Incompatible;
276
277   if( !checkRange( value ) )
278     return Invalid;
279
280   return Acceptable;
281 }
282
283 /*!
284   \brief This function return a default acceptable value (commonly, 0.0).
285   \return default acceptable value
286 */
287 double SalomeApp_DoubleSpinBox::defaultValue() const
288 {
289   if( myMinimum > myDefaultValue || myMaximum < myDefaultValue )
290     return myMinimum;
291
292   return myDefaultValue;
293 }
294
295 /*!
296   \brief This function is used to check that string value lies within predefined range.
297   \return check status
298 */
299 bool SalomeApp_DoubleSpinBox::checkRange( const double value ) const
300 {
301   if( !myIsRangeSet )
302     return true;
303
304   return value >= myMinimum && value <= myMaximum;
305 }
306
307 /*!
308   \brief This function is used to determine whether input is a variable name and to get its value.
309   \return status of search operation
310 */
311 SalomeApp_DoubleSpinBox::SearchState SalomeApp_DoubleSpinBox::findVariable( const QString& name, double& value ) const
312 {
313   value = 0;
314   if( SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() ) )
315   {
316     if( SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() ) )
317     {
318       _PTR(Study) studyDS = study->studyDS();
319
320       std::string aName = name.toStdString();
321       if( studyDS->IsVariable( aName ) )
322       {
323         if( studyDS->IsReal( aName ) || studyDS->IsInteger( aName ) )
324         {
325           value = studyDS->GetReal( aName );
326           return Found;
327         }
328         return IncorrectType;
329       }
330     }
331   }
332   return NotFound;
333 }
334
335 /*!
336   \brief This function is called when the spinbox recieves key press event.
337 */
338 void SalomeApp_DoubleSpinBox::keyPressEvent( QKeyEvent* e )
339 {
340   if ( e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter )
341     QWidget::keyPressEvent( e );
342   else
343     QtxDoubleSpinBox::keyPressEvent( e );
344 }
345
346 /*!
347   \brief This function is called when the spinbox recieves show event.
348 */
349 void SalomeApp_DoubleSpinBox::showEvent( QShowEvent* )
350 {
351   setText( myTextValue );
352 }