Salome HOME
updated copyright message
[modules/gui.git] / src / Qtx / QtxValidator.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
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, or (at your option) any later version.
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
20 // File:      QtxValidator.cxx
21 // Author:    Alexandre SOLOVYOV
22 //
23 #include "QtxValidator.h"
24
25 /*!
26   \class QtxIntValidator
27   \brief Validator for integer numbers with possibility to fix up the invalid value.
28 */
29
30 /*!
31   \brief Constructor.
32   \param parent parent object
33 */
34 QtxIntValidator::QtxIntValidator( QObject* parent )
35 : QIntValidator( parent )
36 {
37 }
38
39 /*!
40   \brief Constructor.
41   \param bot minimum possible value
42   \param top maximum possible value
43   \param parent parent object
44 */
45 QtxIntValidator::QtxIntValidator( const int bot, const int top, QObject* parent )
46 : QIntValidator( bot, top, parent )
47 {
48 }
49
50 /*!
51   \brief Destructor.
52 */
53 QtxIntValidator::~QtxIntValidator()
54 {
55 }
56
57 /*!
58   \brief Validate the input and fixup it if necessary.
59
60   If the string represents integer value less then minimum value, it becomes equal to the minimum.
61   if the string represents integer value more then mazimum value, it becomes equal to the maximum.
62   If the string is not evaluated as integer it becomes equal to \c 0.
63
64   \param str the string to be validated
65 */
66 void QtxIntValidator::fixup( QString& str ) const
67 {
68   bool ok = false;
69   int i = str.toInt( &ok );
70   if ( ok )
71   {
72     if ( i < bottom() )
73       str = QString::number( bottom() );
74     else if( i > top() )
75       str = QString::number( top() );
76   }
77   else
78     str = QString ( "0" );
79 }
80
81 /*!
82   \class QtxDoubleValidator
83   \brief Validator for double numbers with possibility to fix up the invalid value.
84 */
85
86 /*!
87   \brief Constructor
88   \param parent parent object
89 */
90 QtxDoubleValidator::QtxDoubleValidator( QObject* parent )
91 : QDoubleValidator( parent )
92 {
93 }
94
95 /*!
96   \brief Constructor.
97   \param bot minimum possible value
98   \param top maximum possible value
99   \param dec precision (number of digits after dot)
100   \param parent parent object
101 */
102 QtxDoubleValidator::QtxDoubleValidator( const double bot, const double top,
103                                         const int dec, QObject* parent )
104 : QDoubleValidator( bot, top, dec, parent )
105 {
106 }
107
108 /*!
109   \brief Destructor.
110 */
111 QtxDoubleValidator::~QtxDoubleValidator()
112 {
113 }
114
115 /*!
116   \brief Validate the input and fixup it if necessary.
117
118   If the string represents double value less then minimum value, it becomes equal to the minimum.
119   if the string represents double value more then mazimum value, it becomes equal to the maximum.
120   If the string is not evaluated as double it becomes equal to \c 0.
121
122   \param str the string to be validated
123 */
124 void QtxDoubleValidator::fixup( QString& str ) const
125 {
126   bool ok = false;
127   double d = str.toDouble( &ok );
128   if ( ok )
129   {
130     if ( d < bottom() )
131       str = QString::number( bottom() );
132     else if ( d > top() )
133       str = QString::number( top() );
134   }
135   else
136     str = QString( "0" );
137 }