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_IntSpinBox.cxx
20 // Author: Oleg UVAROV
22 #include "SalomeApp_IntSpinBox.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_IntSpinBox
41 Constructs a spin box with 0 as minimum value and 99 as maximum value,
42 a step value of 1. The value is initially set to 0.
44 \param parent parent object
46 SalomeApp_IntSpinBox::SalomeApp_IntSpinBox( QWidget* parent )
47 : QtxIntSpinBox( parent ),
50 connectSignalsAndSlots();
56 Constructs a spin box with specified minimum, maximum and step value.
57 The value is initially set to the minimum value.
59 \param min spin box minimum possible value
60 \param max spin box maximum possible value
61 \param step spin box increment/decrement value
62 \param parent parent object
64 SalomeApp_IntSpinBox::SalomeApp_IntSpinBox( int min, int max, int step, QWidget* parent )
65 : QtxIntSpinBox( min, max, step, parent ),
68 connectSignalsAndSlots();
74 SalomeApp_IntSpinBox::~SalomeApp_IntSpinBox()
79 \brief Connect signals and slots.
81 void SalomeApp_IntSpinBox::connectSignalsAndSlots()
83 connect( this, SIGNAL( editingFinished() ),
84 this, SLOT( onEditingFinished() ) );
86 connect( this, SIGNAL( valueChanged( const QString& ) ),
87 this, SLOT( onTextChanged( const QString& ) ) );
89 connect( lineEdit(), SIGNAL( textChanged( const QString& ) ),
90 this, SLOT( onTextChanged( const QString& ) ) );
92 connect( lineEdit(), SIGNAL( textChanged( const QString& )),
93 this, SIGNAL( textChanged( const QString& ) ) );
97 \brief This function is called when editing is finished.
99 void SalomeApp_IntSpinBox::onEditingFinished()
101 if( myTextValue.isNull() )
102 myTextValue = text();
104 setText( myTextValue );
108 \brief This function is called when value is changed.
110 void SalomeApp_IntSpinBox::onTextChanged( const QString& text )
115 if( isValid( text, value ) == Acceptable )
116 myCorrectValue = text;
120 \brief Interpret text entered by the user as a value.
121 \param text text entered by the user
125 int SalomeApp_IntSpinBox::valueFromText( const QString& text ) const
128 if( isValid( text, value ) == Acceptable )
131 return defaultValue();
135 \brief This function is used by the spin box whenever it needs to display
138 \param val spin box value
139 \return text representation of the value
142 QString SalomeApp_IntSpinBox::textFromValue( int val ) const
144 return QtxIntSpinBox::textFromValue( val );
148 \brief This function is used to determine whether input is valid.
149 \param str currently entered value
150 \param pos cursor position in the string
151 \return validating operation result
153 QValidator::State SalomeApp_IntSpinBox::validate( QString& str, int& pos ) const
155 return QValidator::Acceptable;
159 \brief This function is used to determine whether input is valid.
160 \return validating operation result
162 bool SalomeApp_IntSpinBox::isValid( QString& msg, bool toCorrect )
165 State aState = isValid( text(), value );
167 if( aState != Acceptable )
171 if( aState == Incompatible )
172 msg += tr( "ERR_INCOMPATIBLE_TYPE" ).arg( text() ) + "\n";
173 else if( aState == NoVariable )
174 msg += tr( "ERR_NO_VARIABLE" ).arg( text() ) + "\n";
175 else if( aState == Invalid )
176 msg += tr( "ERR_INVALID_VALUE" ) + "\n";
178 setText( myCorrectValue );
187 \brief This function is used to set a default value for this spinbox.
188 \param value default value
190 void SalomeApp_IntSpinBox::setDefaultValue( const int value )
192 myDefaultValue = value;
196 \brief This function is used to set a current value for this spinbox.
197 \param value current value
199 void SalomeApp_IntSpinBox::setValue( const int value )
201 QtxIntSpinBox::setValue( value );
203 myCorrectValue = QString::number( value );
204 myTextValue = myCorrectValue;
208 \brief This function is used to set a text for this spinbox.
209 \param value current value
211 void SalomeApp_IntSpinBox::setText( const QString& value )
213 lineEdit()->setText(value);
217 \brief This function is used to determine whether input is valid.
218 \return validating operation result
220 SalomeApp_IntSpinBox::State SalomeApp_IntSpinBox::isValid( const QString& text, int& value ) const
222 SearchState aSearchState = findVariable( text, value );
223 if( aSearchState == NotFound )
226 value = text.toInt( &ok );
229 text.toDouble( &ok );
235 else if( aSearchState == IncorrectType )
238 if( !checkRange( value ) )
245 \brief This function return a default acceptable value (commonly, 0).
246 \return default acceptable value
248 int SalomeApp_IntSpinBox::defaultValue() const
250 if( minimum() > myDefaultValue || maximum() < myDefaultValue )
253 return myDefaultValue;
257 \brief This function is used to check that string value lies within predefined range.
260 bool SalomeApp_IntSpinBox::checkRange( const int value ) const
262 return value >= minimum() && value <= maximum();
266 \brief This function is used to determine whether input is a variable name and to get its value.
267 \return status of search operation
269 SalomeApp_IntSpinBox::SearchState SalomeApp_IntSpinBox::findVariable( const QString& name, int& value ) const
272 if( SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() ) )
274 if( SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() ) )
276 _PTR(Study) studyDS = study->studyDS();
278 std::string aName = name.toStdString();
279 if( studyDS->IsVariable( aName ) )
281 if( studyDS->IsInteger( aName ) )
283 value = studyDS->GetInteger( aName );
286 return IncorrectType;
294 \brief This function is called when the spinbox recieves key press event.
296 void SalomeApp_IntSpinBox::keyPressEvent( QKeyEvent* e )
298 if ( e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter )
299 QWidget::keyPressEvent( e );
301 QtxIntSpinBox::keyPressEvent( e );
305 \brief This function is called when the spinbox recieves show event.
307 void SalomeApp_IntSpinBox::showEvent( QShowEvent* )
309 setText( myTextValue );