]> SALOME platform Git repositories - modules/gui.git/blob - src/SalomeApp/SalomeApp_IntSpinBox.cxx
Salome HOME
4d3460a3bd320294afeb4e6a8580a05ec121558e
[modules/gui.git] / src / SalomeApp / SalomeApp_IntSpinBox.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_IntSpinBox.cxx
20 // Author:    Oleg UVAROV
21
22 #include "SalomeApp_IntSpinBox.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_IntSpinBox
36 */
37
38 /*!
39   \brief Constructor.
40
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.
43
44   \param parent parent object
45 */
46 SalomeApp_IntSpinBox::SalomeApp_IntSpinBox( QWidget* parent )
47 : QtxIntSpinBox( parent ),
48   myDefaultValue( 0 )
49 {
50   connectSignalsAndSlots();
51 }
52
53 /*!
54   \brief Constructor.
55
56   Constructs a spin box with specified minimum, maximum and step value.
57   The value is initially set to the minimum value.
58
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
63 */
64 SalomeApp_IntSpinBox::SalomeApp_IntSpinBox( int min, int max, int step, QWidget* parent )
65 : QtxIntSpinBox( min, max, step, parent ),
66   myDefaultValue( 0 )
67 {
68   connectSignalsAndSlots();
69 }
70
71 /*!
72   \brief Destructor.
73 */
74 SalomeApp_IntSpinBox::~SalomeApp_IntSpinBox()
75 {
76 }
77
78 /*!
79   \brief Connect signals and slots.
80 */
81 void SalomeApp_IntSpinBox::connectSignalsAndSlots()
82 {
83   connect( this, SIGNAL( editingFinished() ),
84            this, SLOT( onEditingFinished() ) );
85
86   connect( this, SIGNAL( valueChanged( const QString& ) ),
87            this, SLOT( onTextChanged( const QString& ) ) );
88
89   connect( lineEdit(), SIGNAL( textChanged( const QString& ) ),
90            this, SLOT( onTextChanged( const QString& ) ) );
91
92   connect( lineEdit(), SIGNAL( textChanged( const QString& )),
93            this, SIGNAL( textChanged( const QString& ) ) );
94 }
95
96 /*!
97   \brief This function is called when editing is finished.
98 */
99 void SalomeApp_IntSpinBox::onEditingFinished()
100 {
101   if( myTextValue.isNull() )
102     myTextValue = text();
103
104   setText( myTextValue );
105 }
106
107 /*!
108   \brief This function is called when value is changed.
109 */
110 void SalomeApp_IntSpinBox::onTextChanged( const QString& text )
111 {
112   myTextValue = text;
113
114   int value = 0;
115   if( isValid( text, value ) == Acceptable )
116     myCorrectValue = text;
117 }
118
119 /*!
120   \brief Interpret text entered by the user as a value.
121   \param text text entered by the user
122   \return mapped value
123   \sa textFromValue()
124 */
125 int SalomeApp_IntSpinBox::valueFromText( const QString& text ) const
126 {
127   int value = 0;
128   if( isValid( text, value ) == Acceptable )
129     return value;
130
131   return defaultValue();
132 }
133
134 /*!
135   \brief This function is used by the spin box whenever it needs to display
136   the given value.
137
138   \param val spin box value
139   \return text representation of the value
140   \sa valueFromText()
141 */
142 QString SalomeApp_IntSpinBox::textFromValue( int val ) const
143 {
144   return QtxIntSpinBox::textFromValue( val );
145 }
146
147 /*!
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
152 */
153 QValidator::State SalomeApp_IntSpinBox::validate( QString& str, int& pos ) const
154 {
155   return QValidator::Acceptable;
156 }
157
158 /*!
159   \brief This function is used to determine whether input is valid.
160   \return validating operation result
161 */
162 bool SalomeApp_IntSpinBox::isValid( QString& msg, bool toCorrect )
163 {
164   int value;
165   State aState = isValid( text(), value );
166
167   if( aState != Acceptable )
168   {
169     if( toCorrect )
170     {
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";
177
178       setText( myCorrectValue );
179     }
180     return false;
181   }
182
183   return true;
184 }
185
186 /*!
187   \brief This function is used to set a default value for this spinbox.
188   \param value default value
189 */
190 void SalomeApp_IntSpinBox::setDefaultValue( const int value )
191 {
192   myDefaultValue = value;
193 }
194
195 /*!
196   \brief This function is used to set a current value for this spinbox.
197   \param value current value
198 */
199 void SalomeApp_IntSpinBox::setValue( const int value )
200 {
201   QtxIntSpinBox::setValue( value );
202
203   myCorrectValue = QString::number( value );
204   myTextValue = myCorrectValue;
205 }
206
207 /*!
208   \brief This function is used to set a text for this spinbox.
209   \param value current value
210 */
211 void SalomeApp_IntSpinBox::setText( const QString& value )
212 {
213   lineEdit()->setText(value);
214 }
215
216 /*!
217   \brief This function is used to determine whether input is valid.
218   \return validating operation result
219 */
220 SalomeApp_IntSpinBox::State SalomeApp_IntSpinBox::isValid( const QString& text, int& value ) const
221 {
222   SearchState aSearchState = findVariable( text, value );
223   if( aSearchState == NotFound )
224   {
225     bool ok = false;
226     value = text.toInt( &ok );
227     if( !ok )
228     {
229       text.toDouble( &ok );
230       if( ok )
231         return Invalid;
232       return NoVariable;
233     }
234   }
235   else if( aSearchState == IncorrectType )
236     return Incompatible;
237
238   if( !checkRange( value ) )
239     return Invalid;
240
241   return Acceptable;
242 }
243
244 /*!
245   \brief This function return a default acceptable value (commonly, 0).
246   \return default acceptable value
247 */
248 int SalomeApp_IntSpinBox::defaultValue() const
249 {
250   if( minimum() > myDefaultValue || maximum() < myDefaultValue )
251     return minimum();
252
253   return myDefaultValue;
254 }
255
256 /*!
257   \brief This function is used to check that string value lies within predefined range.
258   \return check status
259 */
260 bool SalomeApp_IntSpinBox::checkRange( const int value ) const
261 {
262   return value >= minimum() && value <= maximum();
263 }
264
265 /*!
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
268 */
269 SalomeApp_IntSpinBox::SearchState SalomeApp_IntSpinBox::findVariable( const QString& name, int& value ) const
270 {
271   value = 0;
272   if( SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() ) )
273   {
274     if( SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() ) )
275     {
276       _PTR(Study) studyDS = study->studyDS();
277
278       std::string aName = name.toStdString();
279       if( studyDS->IsVariable( aName ) )
280       {
281         if( studyDS->IsInteger( aName ) )
282         {
283           value = studyDS->GetInteger( aName );
284           return Found;
285         }
286         return IncorrectType;
287       }
288     }
289   }
290   return NotFound;
291 }
292
293 /*!
294   \brief This function is called when the spinbox recieves key press event.
295 */
296 void SalomeApp_IntSpinBox::keyPressEvent( QKeyEvent* e )
297 {
298   if ( e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter )
299     QWidget::keyPressEvent( e );
300   else
301     QtxIntSpinBox::keyPressEvent( e );
302 }
303
304 /*!
305   \brief This function is called when the spinbox recieves show event.
306 */
307 void SalomeApp_IntSpinBox::showEvent( QShowEvent* )
308 {
309   setText( myTextValue );
310 }