Salome HOME
Updated copyright comment
[modules/gui.git] / src / Qtx / QtxIntSpinBox.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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, or (at your option) any later version.
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
23 // File:      QtxIntSpinBox.cxx
24 // Author:    Sergey TELKOV
25 //
26 #include "QtxIntSpinBox.h"
27
28 #include <QLineEdit>
29 /*!
30   \class QtxIntSpinBox
31   \brief Enhanced version of the Qt's spin box.
32
33   The QtxIntSpinBox class represents the widget for entering the
34   integer values. In addition to the functionality provided by
35   QSpinBox, this class supports "cleared" state - this is the
36   state corresponding to "None" (or empty) entered value.
37
38   To set "cleared" state use setCleared() method. To check if the spin
39   box stores "cleared" state, use isCleared() method.
40   For example:
41   \code
42   if (mySpinBox->isCleared()) {
43     ... // process "None" state
44   }
45   else {
46     int value = mySpinBox->value();
47     ... // process entered value
48   }
49   \endcode
50 */
51
52 /*!
53   \brief Constructor.
54
55   Constructs a spin box with 0 as minimum value and 99 as maximum value, 
56   a step value of 1. The value is initially set to 0.
57
58   \param parent parent object
59 */
60 QtxIntSpinBox::QtxIntSpinBox( QWidget* parent )
61 : QSpinBox( parent ),
62   myCleared( false )
63 {
64 #if !defined(GLOBAL_DOUBLE_CONVERSION)
65   // VSR 01/07/2010: Disable thousands separator for spin box
66   // (to avoid incosistency of double-2-string and string-2-double conversion)
67   // see issue 14540 (old id 21219)
68   QLocale loc;
69   loc.setNumberOptions(loc.numberOptions() | QLocale::OmitGroupSeparator | QLocale::RejectGroupSeparator);
70   setLocale(loc);
71 #endif
72
73   setCorrectionMode( QSpinBox::CorrectToNearestValue );
74   connect( lineEdit(), SIGNAL( textChanged( const QString& ) ), 
75            this, SLOT( onTextChanged( const QString& ) ) );
76 }
77
78 /*!
79   \brief Constructor.
80
81   Constructs a spin box with specified minimum, maximum and step value.
82   The value is initially set to the minimum value.
83
84   \param min spin box minimum possible value
85   \param max spin box maximum possible value
86   \param step spin box increment/decrement value
87   \param parent parent object
88 */
89 QtxIntSpinBox::QtxIntSpinBox( int min, int max, int step, QWidget* parent )
90 : QSpinBox( parent ),
91   myCleared( false )
92 {
93 #if !defined(GLOBAL_DOUBLE_CONVERSION)
94   // VSR 01/07/2010: Disable thousands separator for spin box
95   // (to avoid incosistency of double-2-string and string-2-double conversion)
96   // see issue 14540 (old id 21219)
97   QLocale loc;
98   loc.setNumberOptions(loc.numberOptions() | QLocale::OmitGroupSeparator | QLocale::RejectGroupSeparator);
99   setLocale(loc);
100 #endif
101
102   setMinimum( min );
103   setMaximum( max );
104   setSingleStep( step );
105   setCorrectionMode( QSpinBox::CorrectToNearestValue );
106
107   connect( lineEdit(), SIGNAL( textChanged( const QString& ) ), 
108            this, SLOT( onTextChanged( const QString& ) ) );
109 }
110
111 /*!
112   \brief Destructor.
113 */
114 QtxIntSpinBox::~QtxIntSpinBox()
115 {
116 }
117
118 /*!
119   \brief Check if spin box is in the "cleared" state.
120   \return \c true if spin box is cleared
121 */
122 bool QtxIntSpinBox::isCleared() const
123 {
124   return myCleared;
125 }
126
127 /*!
128   \brief Change "cleared" status of the spin box.
129   \param on new "cleared" status
130 */
131 void QtxIntSpinBox::setCleared( const bool on )
132 {
133   if ( myCleared == on )
134     return;
135   
136   myCleared = on;
137   setSpecialValueText( specialValueText() );
138 }
139
140 /*!
141   \brief Convert value to the text.
142   \param val value being converted
143   \return string containing the converted value
144 */
145 QString QtxIntSpinBox::textFromValue( int val ) const
146 {
147   return myCleared ? QString() : QSpinBox::textFromValue( val );
148 }
149
150 /*!
151   \brief Perform \a steps increment/decrement steps.
152   
153   The \a steps value can be any integer number. If it is > 0,
154   the value incrementing is done, otherwise value is decremented
155   \a steps times.  
156
157   \param steps number of increment/decrement steps
158 */
159 void QtxIntSpinBox::stepBy( int steps )
160 {
161   myCleared = false;
162
163   QSpinBox::stepBy( steps );
164 }
165
166 /*!
167   \brief Called when user enters the text in the spin box.
168   \param txt current spin box text (not used)
169 */
170 void QtxIntSpinBox::onTextChanged( const QString& )
171 {
172   myCleared = false;
173 }