Salome HOME
Copyright update: 2016
[modules/gui.git] / src / Qtx / QtxIntSpinBox.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, 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   // VSR 01/07/2010: Disable thousands separator for spin box
65   // (to avoid incosistency of double-2-string and string-2-double conversion)
66   QLocale loc;
67   loc.setNumberOptions(loc.numberOptions() | QLocale::OmitGroupSeparator | QLocale::RejectGroupSeparator);
68   setLocale(loc);
69
70   setCorrectionMode( QSpinBox::CorrectToNearestValue );
71   connect( lineEdit(), SIGNAL( textChanged( const QString& ) ), 
72            this, SLOT( onTextChanged( const QString& ) ) );
73 }
74
75 /*!
76   \brief Constructor.
77
78   Constructs a spin box with specified minimum, maximum and step value.
79   The value is initially set to the minimum value.
80
81   \param min spin box minimum possible value
82   \param max spin box maximum possible value
83   \param step spin box increment/decrement value
84   \param parent parent object
85 */
86 QtxIntSpinBox::QtxIntSpinBox( int min, int max, int step, QWidget* parent )
87 : QSpinBox( parent ),
88   myCleared( false )
89 {
90   // VSR 01/07/2010: Disable thousands separator for spin box
91   // (to avoid incosistency of double-2-string and string-2-double conversion)
92   QLocale loc;
93   loc.setNumberOptions(loc.numberOptions() | QLocale::OmitGroupSeparator | QLocale::RejectGroupSeparator);
94   setLocale(loc);
95
96   setMinimum( min );
97   setMaximum( max );
98   setSingleStep( step );
99   setCorrectionMode( QSpinBox::CorrectToNearestValue );
100
101   connect( lineEdit(), SIGNAL( textChanged( const QString& ) ), 
102            this, SLOT( onTextChanged( const QString& ) ) );
103 }
104
105 /*!
106   \brief Destructor.
107 */
108 QtxIntSpinBox::~QtxIntSpinBox()
109 {
110 }
111
112 /*!
113   \brief Check if spin box is in the "cleared" state.
114   \return \c true if spin box is cleared
115 */
116 bool QtxIntSpinBox::isCleared() const
117 {
118   return myCleared;
119 }
120
121 /*!
122   \brief Change "cleared" status of the spin box.
123   \param on new "cleared" status
124 */
125 void QtxIntSpinBox::setCleared( const bool on )
126 {
127   if ( myCleared == on )
128     return;
129   
130   myCleared = on;
131   setSpecialValueText( specialValueText() );
132 }
133
134 /*!
135   \brief Convert value to the text.
136   \param val value being converted
137   \return string containing the converted value
138 */
139 QString QtxIntSpinBox::textFromValue( int val ) const
140 {
141   return myCleared ? QString() : QSpinBox::textFromValue( val );
142 }
143
144 /*!
145   \brief Perform \a steps increment/decrement steps.
146   
147   The \a steps value can be any integer number. If it is > 0,
148   the value incrementing is done, otherwise value is decremented
149   \a steps times.  
150
151   \param steps number of increment/decrement steps
152 */
153 void QtxIntSpinBox::stepBy( int steps )
154 {
155   myCleared = false;
156
157   QSpinBox::stepBy( steps );
158 }
159
160 /*!
161   \brief Called when user enters the text in the spin box.
162   \param txt current spin box text (not used)
163 */
164 void QtxIntSpinBox::onTextChanged( const QString& )
165 {
166   myCleared = false;
167 }