• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • kdeui
  • util
knumvalidator.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 **
3 **
4 ** KIntValidator, KFloatValidator:
5 ** Copyright (C) 1999 Glen Parker <glenebob@nwlink.com>
6 ** KDoubleValidator:
7 ** Copyright (c) 2002 Marc Mutz <mutz@kde.org>
8 **
9 ** This library is free software; you can redistribute it and/or
10 ** modify it under the terms of the GNU Library General Public
11 ** License as published by the Free Software Foundation; either
12 ** version 2 of the License, or (at your option) any later version.
13 **
14 ** This library is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ** Library General Public License for more details.
18 **
19 ** You should have received a copy of the GNU Library General Public
20 ** License along with this library; if not, write to the Free
21 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 **
23 *****************************************************************************/
24 
25 #include "knumvalidator.h"
26 
27 #include <QtGui/QWidget>
28 #include <QtCore/QCharRef>
29 
30 #include <klocale.h>
31 #include <kglobal.h>
32 #include <kdebug.h>
33 
35 // Implementation of KIntValidator
36 //
37 class KIntValidator::KIntValidatorPrivate
38 {
39 public:
40  KIntValidatorPrivate()
41  : _base(0), _min(0), _max(0)
42  {}
43  int _base;
44  int _min;
45  int _max;
46 };
47 
48 KIntValidator::KIntValidator ( QWidget * parent, int base )
49  : QValidator(parent), d(new KIntValidatorPrivate)
50 {
51  setBase(base);
52 }
53 
54 KIntValidator::KIntValidator ( int bottom, int top, QWidget * parent, int base )
55  : QValidator(parent), d(new KIntValidatorPrivate)
56 {
57  setBase(base);
58  setRange(bottom, top);
59 }
60 
61 KIntValidator::~KIntValidator ()
62 {
63  delete d;
64 }
65 
66 QValidator::State KIntValidator::validate ( QString &str, int & ) const
67 {
68  bool ok;
69  int val = 0;
70  QString newStr;
71 
72  newStr = str.trimmed();
73  if (d->_base > 10)
74  newStr = newStr.toUpper();
75 
76  if (newStr == QLatin1String("-")) {// a special case
77  if ((d->_min || d->_max) && d->_min >= 0)
78  ok = false;
79  else
80  return QValidator::Acceptable;
81  }
82  else if (!newStr.isEmpty())
83  val = newStr.toInt(&ok, d->_base);
84  else {
85  val = 0;
86  ok = true;
87  }
88 
89  if (! ok)
90  return QValidator::Invalid;
91 
92  if ((! d->_min && ! d->_max) || (val >= d->_min && val <= d->_max))
93  return QValidator::Acceptable;
94 
95  if (d->_max && d->_min >= 0 && val < 0)
96  return QValidator::Invalid;
97 
98  return QValidator::Intermediate;
99 }
100 
101 void KIntValidator::fixup ( QString &str ) const
102 {
103  int dummy;
104  int val;
105  QValidator::State state;
106 
107  state = validate(str, dummy);
108 
109  if (state == QValidator::Invalid || state == QValidator::Acceptable)
110  return;
111 
112  if (! d->_min && ! d->_max)
113  return;
114 
115  val = str.toInt(0, d->_base);
116 
117  if (val < d->_min) val = d->_min;
118  if (val > d->_max) val = d->_max;
119 
120  str.setNum(val, d->_base);
121 }
122 
123 void KIntValidator::setRange ( int bottom, int top )
124 {
125  d->_min = bottom;
126  d->_max = top;
127 
128  if (d->_max < d->_min)
129  d->_max = d->_min;
130 }
131 
132 void KIntValidator::setBase ( int base )
133 {
134  d->_base = base;
135  if (d->_base < 2) d->_base = 2;
136  if (d->_base > 36) d->_base = 36;
137 }
138 
139 int KIntValidator::bottom () const
140 {
141  return d->_min;
142 }
143 
144 int KIntValidator::top () const
145 {
146  return d->_max;
147 }
148 
149 int KIntValidator::base () const
150 {
151  return d->_base;
152 }
153 
154 
156 // Implementation of KFloatValidator
157 //
158 
159 class KFloatValidator::KFloatValidatorPrivate
160 {
161 public:
162  KFloatValidatorPrivate()
163  : acceptLocalizedNumbers(false), _min(0), _max(0)
164  {}
165  bool acceptLocalizedNumbers;
166  double _min;
167  double _max;
168 };
169 
170 
171 KFloatValidator::KFloatValidator ( QWidget * parent )
172  : QValidator(parent), d(new KFloatValidatorPrivate)
173 {
174  d->acceptLocalizedNumbers=false;
175 }
176 
177 KFloatValidator::KFloatValidator ( double bottom, double top, QWidget * parent )
178  : QValidator(parent), d(new KFloatValidatorPrivate)
179 {
180  d->acceptLocalizedNumbers=false;
181  setRange(bottom, top);
182 }
183 
184 KFloatValidator::KFloatValidator ( double bottom, double top, bool localeAware, QWidget * parent )
185  : QValidator(parent), d(new KFloatValidatorPrivate)
186 {
187  d->acceptLocalizedNumbers = localeAware;
188  setRange(bottom, top);
189 }
190 
191 KFloatValidator::~KFloatValidator ()
192 {
193  delete d;
194 }
195 
196 void KFloatValidator::setAcceptLocalizedNumbers(bool _b)
197 {
198  d->acceptLocalizedNumbers=_b;
199 }
200 
201 bool KFloatValidator::acceptLocalizedNumbers() const
202 {
203  return d->acceptLocalizedNumbers;
204 }
205 
206 QValidator::State KFloatValidator::validate ( QString &str, int & ) const
207 {
208  bool ok;
209  double val = 0;
210  QString newStr;
211  newStr = str.trimmed();
212 
213  if (newStr == QLatin1String("-")) {// a special case
214  if ((d->_min || d->_max) && d->_min >= 0)
215  ok = false;
216  else
217  return QValidator::Acceptable;
218  }
219  else if (newStr == QLatin1String(".") || (d->acceptLocalizedNumbers && newStr==KGlobal::locale()->decimalSymbol())) // another special case
220  return QValidator::Acceptable;
221  else if (!newStr.isEmpty())
222  {
223  val = newStr.toDouble(&ok);
224  if(!ok && d->acceptLocalizedNumbers)
225  val= KGlobal::locale()->readNumber(newStr,&ok);
226  }
227  else {
228  val = 0;
229  ok = true;
230  }
231 
232  if (! ok)
233  return QValidator::Invalid;
234 
235  if (( !d->_min && !d->_max) || (val >= d->_min && val <= d->_max))
236  return QValidator::Acceptable;
237 
238  if (d->_max && d->_min >= 0 && val < 0)
239  return QValidator::Invalid;
240 
241  if ( (d->_min || d->_max) && (val < d->_min || val > d->_max))
242  return QValidator::Invalid;
243 
244  return QValidator::Intermediate;
245 }
246 
247 void KFloatValidator::fixup ( QString &str ) const
248 {
249  int dummy;
250  double val;
251  QValidator::State state;
252 
253  state = validate(str, dummy);
254 
255  if (state == QValidator::Invalid || state == QValidator::Acceptable)
256  return;
257 
258  if (! d->_min && ! d->_max)
259  return;
260 
261  val = str.toDouble();
262 
263  if (val < d->_min) val = d->_min;
264  if (val > d->_max) val = d->_max;
265 
266  str.setNum(val);
267 }
268 
269 void KFloatValidator::setRange ( double bottom, double top )
270 {
271  d->_min = bottom;
272  d->_max = top;
273 
274  if (d->_max < d->_min)
275  d->_max = d->_min;
276 }
277 
278 double KFloatValidator::bottom () const
279 {
280  return d->_min;
281 }
282 
283 double KFloatValidator::top () const
284 {
285  return d->_max;
286 }
287 
288 
289 
290 
292 // Implementation of KDoubleValidator
293 //
294 
295 class KDoubleValidator::KDoubleValidatorPrivate {
296 public:
297  KDoubleValidatorPrivate( bool accept=true ) : acceptLocalizedNumbers( accept ) {}
298 
299  bool acceptLocalizedNumbers;
300 };
301 
302 KDoubleValidator::KDoubleValidator( QObject * parent )
303  : QDoubleValidator( parent ), d( new KDoubleValidatorPrivate() )
304 {
305 }
306 
307 KDoubleValidator::KDoubleValidator( double bottom, double top, int decimals,
308  QObject * parent )
309  : QDoubleValidator( bottom, top, decimals, parent ), d( new KDoubleValidatorPrivate() )
310 {
311 }
312 
313 KDoubleValidator::~KDoubleValidator()
314 {
315  delete d;
316 }
317 
318 bool KDoubleValidator::acceptLocalizedNumbers() const {
319  return d->acceptLocalizedNumbers;
320 }
321 
322 void KDoubleValidator::setAcceptLocalizedNumbers( bool accept ) {
323  d->acceptLocalizedNumbers = accept;
324 }
325 
326 QValidator::State KDoubleValidator::validate( QString & input, int & p ) const {
327  QString s = input;
328  if ( acceptLocalizedNumbers() ) {
329  KLocale * l = KGlobal::locale();
330  // ok, we have to re-format the number to have:
331  // 1. decimalSymbol == '.'
332  // 2. negativeSign == '-'
333  // 3. positiveSign == <empty>
334  // 4. thousandsSeparator() == <empty> (we don't check that there
335  // are exactly three decimals between each separator):
336  QString d = l->decimalSymbol(),
337  n = l->negativeSign(),
338  p = l->positiveSign(),
339  t = l->thousandsSeparator();
340  // first, delete p's and t's:
341  if ( !p.isEmpty() )
342  for ( int idx = s.indexOf( p ) ; idx >= 0 ; idx = s.indexOf( p, idx ) )
343  s.remove( idx, p.length() );
344 
345 
346  if ( !t.isEmpty() )
347  for ( int idx = s.indexOf( t ) ; idx >= 0 ; idx = s.indexOf( t, idx ) )
348  s.remove( idx, t.length() );
349 
350  // then, replace the d's and n's
351  if ( ( !n.isEmpty() && n.indexOf('.') != -1 ) ||
352  ( !d.isEmpty() && d.indexOf('-') != -1 ) ) {
353  // make sure we don't replace something twice:
354  kWarning() << "KDoubleValidator: decimal symbol contains '-' or "
355  "negative sign contains '.' -> improve algorithm" << endl;
356  return Invalid;
357  }
358 
359  if ( !d.isEmpty() && d != "." )
360  for ( int idx = s.indexOf( d ) ; idx >= 0 ; idx = s.indexOf( d, idx + 1 ) )
361  s.replace( idx, d.length(), '.');
362 
363  if ( !n.isEmpty() && n != "-" )
364  for ( int idx = s.indexOf( n ) ; idx >= 0 ; idx = s.indexOf( n, idx + 1 ) )
365  s.replace( idx, n.length(), '-' );
366  }
367 
368  return base::validate( s, p );
369 }
370 
371 #include "knumvalidator.moc"
KDoubleValidator::acceptLocalizedNumbers
bool acceptLocalizedNumbers() const
KFloatValidator::~KFloatValidator
virtual ~KFloatValidator()
Destructs the validator.
Definition: knumvalidator.cpp:191
KFloatValidator::bottom
virtual double bottom() const
Returns the current minimum value allowed.
Definition: knumvalidator.cpp:278
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QWidget
QString::toUpper
QString toUpper() const
KFloatValidator::setAcceptLocalizedNumbers
void setAcceptLocalizedNumbers(bool b)
Sets the validator to be locale aware if is true.
Definition: knumvalidator.cpp:196
kdebug.h
KIntValidator::top
virtual int top() const
Returns the current maximum value allowed.
Definition: knumvalidator.cpp:144
QDoubleValidator::validate
virtual QValidator::State validate(QString &input, int &pos) const
KFloatValidator::acceptLocalizedNumbers
bool acceptLocalizedNumbers() const
Returns true if the validator is locale aware.
Definition: knumvalidator.cpp:201
KIntValidator::bottom
virtual int bottom() const
Returns the current minimum value allowed.
Definition: knumvalidator.cpp:139
KFloatValidator::KFloatValidator
KFloatValidator(QWidget *parent)
Constructor.
Definition: knumvalidator.cpp:171
QString::remove
QString & remove(int position, int n)
KIntValidator::KIntValidator
KIntValidator(QWidget *parent, int base=10)
Constructor.
Definition: knumvalidator.cpp:48
QString::toDouble
double toDouble(bool *ok) const
klocale.h
KLocale::decimalSymbol
QString decimalSymbol() const
KIntValidator::setRange
virtual void setRange(int bottom, int top)
Sets the minimum and maximum values allowed.
Definition: knumvalidator.cpp:123
KIntValidator::~KIntValidator
virtual ~KIntValidator()
Destructs the validator.
Definition: knumvalidator.cpp:61
kglobal.h
KIntValidator::base
virtual int base() const
Returns the current numeric base.
Definition: knumvalidator.cpp:149
KDoubleValidator::~KDoubleValidator
virtual ~KDoubleValidator()
Destructs the validator.
Definition: knumvalidator.cpp:313
KLocale::readNumber
double readNumber(const QString &numStr, bool *ok=0) const
knumvalidator.h
KFloatValidator::setRange
virtual void setRange(double bottom, double top)
Sets the minimum and maximum value allowed.
Definition: knumvalidator.cpp:269
QObject
QString::toInt
int toInt(bool *ok, int base) const
QString::isEmpty
bool isEmpty() const
QString::trimmed
QString trimmed() const
KDoubleValidator::KDoubleValidator
KDoubleValidator(QObject *parent)
Constuct a locale-aware KDoubleValidator with default range (whatever QDoubleValidator uses for that)...
Definition: knumvalidator.cpp:302
KIntValidator::validate
virtual State validate(QString &, int &) const
Validates the text, and return the result.
Definition: knumvalidator.cpp:66
KFloatValidator::validate
virtual State validate(QString &, int &) const
Validates the text, and return the result.
Definition: knumvalidator.cpp:206
QString
KFloatValidator::fixup
virtual void fixup(QString &) const
Fixes the text if possible, providing a valid string.
Definition: knumvalidator.cpp:247
KIntValidator::setBase
virtual void setBase(int base)
Sets the numeric base value.
Definition: knumvalidator.cpp:132
KIntValidator::fixup
virtual void fixup(QString &) const
Fixes the text if possible, providing a valid string.
Definition: knumvalidator.cpp:101
KStandardGuiItem::ok
KGuiItem ok()
Returns the 'Ok' gui item.
Definition: kstandardguiitem.cpp:107
KGlobal::locale
KLocale * locale()
KDoubleValidator::validate
virtual QValidator::State validate(QString &input, int &pos) const
Overloaded for internal reasons.
Definition: knumvalidator.cpp:326
QString::replace
QString & replace(int position, int n, QChar after)
KDoubleValidator::setAcceptLocalizedNumbers
void setAcceptLocalizedNumbers(bool accept)
Sets whether to accept localized numbers (default: true)
Definition: knumvalidator.cpp:322
KLocale
QLatin1String
QString::setNum
QString & setNum(short n, int base)
KLocale::positiveSign
QString positiveSign() const
KLocale::negativeSign
QString negativeSign() const
QString::length
int length() const
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
QDoubleValidator
KFloatValidator::top
virtual double top() const
Returns the current maximum value allowed.
Definition: knumvalidator.cpp:283
QValidator
KLocale::thousandsSeparator
QString thousandsSeparator() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:23:59 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal