1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // File: SalomeApp_DoubleSpinBox.cxx
20 // Author: Oleg UVAROV
22 #include "SalomeApp_DoubleSpinBox.h"
23 #include "SalomeApp_Application.h"
24 #include "SalomeApp_Study.h"
26 #include <SUIT_Session.h>
28 #include "SALOMEDSClient_ClientFactory.hxx"
29 #include CORBA_SERVER_HEADER(SALOMEDS)
35 \class SalomeApp_DoubleSpinBox
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.
45 \param parent parent object
47 SalomeApp_DoubleSpinBox::SalomeApp_DoubleSpinBox( QWidget* parent )
48 : QtxDoubleSpinBox( parent ),
49 myDefaultValue( 0.0 ),
50 myIsRangeSet( false ),
54 connectSignalsAndSlots();
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.
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
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 ),
76 connectSignalsAndSlots();
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.
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
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 ),
98 connectSignalsAndSlots();
104 SalomeApp_DoubleSpinBox::~SalomeApp_DoubleSpinBox()
109 \brief Connect signals and slots.
111 void SalomeApp_DoubleSpinBox::connectSignalsAndSlots()
113 connect( this, SIGNAL( editingFinished() ),
114 this, SLOT( onEditingFinished() ) );
116 connect( this, SIGNAL( valueChanged( const QString& ) ),
117 this, SLOT( onTextChanged( const QString& ) ) );
119 connect( lineEdit(), SIGNAL( textChanged( const QString& ) ),
120 this, SLOT( onTextChanged( const QString& ) ) );
122 connect( lineEdit(), SIGNAL( textChanged( const QString& )),
123 this, SIGNAL( textChanged( const QString& ) ) );
127 \brief This function is called when editing is finished.
129 void SalomeApp_DoubleSpinBox::onEditingFinished()
131 if( myTextValue.isNull() )
132 myTextValue = text();
134 setText( myTextValue );
138 \brief This function is called when value is changed.
140 void SalomeApp_DoubleSpinBox::onTextChanged( const QString& text )
145 if( isValid( text, value ) == Acceptable )
146 myCorrectValue = text;
150 \brief Interpret text entered by the user as a value.
151 \param text text entered by the user
155 double SalomeApp_DoubleSpinBox::valueFromText( const QString& text ) const
158 if( isValid( text, value ) == Acceptable )
161 return defaultValue();
165 \brief This function is used by the spin box whenever it needs to display
168 \param val spin box value
169 \return text representation of the value
172 QString SalomeApp_DoubleSpinBox::textFromValue( double val ) const
174 return QtxDoubleSpinBox::textFromValue( val );
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
183 QValidator::State SalomeApp_DoubleSpinBox::validate( QString& str, int& pos ) const
185 return QValidator::Acceptable;
189 \brief This function is used to determine whether input is valid.
190 \return validating operation result
192 bool SalomeApp_DoubleSpinBox::isValid( QString& msg, bool toCorrect )
195 State aState = isValid( text(), value );
197 if( aState != Acceptable )
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";
208 setText( myCorrectValue );
217 \brief This function is used to set a default value for this spinbox.
218 \param value default value
220 void SalomeApp_DoubleSpinBox::setDefaultValue( const double value )
222 myDefaultValue = value;
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
230 void SalomeApp_DoubleSpinBox::setRange( const double min, const double max )
232 QtxDoubleSpinBox::setRange( min, max );
240 \brief This function is used to set a current value for this spinbox.
241 \param value current value
243 void SalomeApp_DoubleSpinBox::setValue( const double value )
245 QtxDoubleSpinBox::setValue( value );
247 myCorrectValue = QString::number( value );
248 myTextValue = myCorrectValue;
252 \brief This function is used to set a text for this spinbox.
253 \param value current value
255 void SalomeApp_DoubleSpinBox::setText( const QString& value )
257 lineEdit()->setText(value);
261 \brief This function is used to determine whether input is valid.
262 \return validating operation result
264 SalomeApp_DoubleSpinBox::State SalomeApp_DoubleSpinBox::isValid( const QString& text, double& value ) const
266 SearchState aSearchState = findVariable( text, value );
267 if( aSearchState == NotFound )
270 value = text.toDouble( &ok );
274 else if( aSearchState == IncorrectType )
277 if( !checkRange( value ) )
284 \brief This function return a default acceptable value (commonly, 0.0).
285 \return default acceptable value
287 double SalomeApp_DoubleSpinBox::defaultValue() const
289 if( myMinimum > myDefaultValue || myMaximum < myDefaultValue )
292 return myDefaultValue;
296 \brief This function is used to check that string value lies within predefined range.
299 bool SalomeApp_DoubleSpinBox::checkRange( const double value ) const
304 return value >= myMinimum && value <= myMaximum;
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
311 SalomeApp_DoubleSpinBox::SearchState SalomeApp_DoubleSpinBox::findVariable( const QString& name, double& value ) const
314 if( SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() ) )
316 if( SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() ) )
318 _PTR(Study) studyDS = study->studyDS();
320 std::string aName = name.toStdString();
321 if( studyDS->IsVariable( aName ) )
323 if( studyDS->IsReal( aName ) || studyDS->IsInteger( aName ) )
325 value = studyDS->GetReal( aName );
328 return IncorrectType;
336 \brief This function is called when the spinbox recieves key press event.
338 void SalomeApp_DoubleSpinBox::keyPressEvent( QKeyEvent* e )
340 if ( e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter )
341 QWidget::keyPressEvent( e );
343 QtxDoubleSpinBox::keyPressEvent( e );
347 \brief This function is called when the spinbox recieves show event.
349 void SalomeApp_DoubleSpinBox::showEvent( QShowEvent* )
351 setText( myTextValue );