KDELibs4Support

knumvalidator.cpp
1 /**********************************************************************
2 **
3 **
4 ** KIntValidator:
5 ** Copyright (C) 1999 Glen Parker <[email protected]>
6 ** KDoubleValidator:
7 ** Copyright (c) 2002 Marc Mutz <[email protected]>
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 <QWidget>
28 #include <QCharRef>
29 
30 #include <klocale.h>
31 #include <kdebug.h>
32 
34 // Implementation of KIntValidator
35 //
36 class Q_DECL_HIDDEN KIntValidator::KIntValidatorPrivate
37 {
38 public:
39  KIntValidatorPrivate()
40  : _base(0), _min(0), _max(0)
41  {}
42  int _base;
43  int _min;
44  int _max;
45 };
46 
48  : QValidator(parent), d(new KIntValidatorPrivate)
49 {
50  setBase(base);
51 }
52 
54  : QValidator(parent), d(new KIntValidatorPrivate)
55 {
56  setBase(base);
57  setRange(bottom, top);
58 }
59 
61 {
62  delete d;
63 }
64 
66 {
67  bool ok;
68  int val = 0;
69  QString newStr;
70 
71  newStr = str.trimmed();
72  if (d->_base > 10) {
73  newStr = newStr.toUpper();
74  }
75 
76  if (newStr == QLatin1String("-")) {// a special case
77  if ((d->_min || d->_max) && d->_min >= 0) {
78  ok = false;
79  } else {
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 
93  if ((! d->_min && ! d->_max) || (val >= d->_min && val <= d->_max)) {
95  }
96 
97  if (d->_max && d->_min >= 0 && val < 0) {
98  return QValidator::Invalid;
99  }
100 
102 }
103 
105 {
106  int dummy;
107  int val;
108  QValidator::State state;
109 
110  state = validate(str, dummy);
111 
112  if (state == QValidator::Invalid || state == QValidator::Acceptable) {
113  return;
114  }
115 
116  if (! d->_min && ! d->_max) {
117  return;
118  }
119 
120  val = str.toInt(nullptr, d->_base);
121 
122  if (val < d->_min) {
123  val = d->_min;
124  }
125  if (val > d->_max) {
126  val = d->_max;
127  }
128 
129  str.setNum(val, d->_base);
130 }
131 
133 {
134  d->_min = bottom;
135  d->_max = top;
136 
137  if (d->_max < d->_min) {
138  d->_max = d->_min;
139  }
140 }
141 
143 {
144  d->_base = base;
145  if (d->_base < 2) {
146  d->_base = 2;
147  }
148  if (d->_base > 36) {
149  d->_base = 36;
150  }
151 }
152 
154 {
155  return d->_min;
156 }
157 
159 {
160  return d->_max;
161 }
162 
164 {
165  return d->_base;
166 }
167 
169 // Implementation of KDoubleValidator
170 //
171 
172 class Q_DECL_HIDDEN KDoubleValidator::KDoubleValidatorPrivate
173 {
174 public:
175  KDoubleValidatorPrivate(bool accept = true) : acceptLocalizedNumbers(accept) {}
176 
177  bool acceptLocalizedNumbers;
178 };
179 
181  : QDoubleValidator(parent), d(new KDoubleValidatorPrivate())
182 {
183 }
184 
186  QObject *parent)
187  : QDoubleValidator(bottom, top, decimals, parent), d(new KDoubleValidatorPrivate())
188 {
189 }
190 
192 {
193  delete d;
194 }
195 
197 {
198  return d->acceptLocalizedNumbers;
199 }
200 
202 {
203  d->acceptLocalizedNumbers = accept;
204 }
205 
207 {
208  QString s = input;
209  if (acceptLocalizedNumbers()) {
210  KLocale *l = KLocale::global();
211  // ok, we have to re-format the number to have:
212  // 1. decimalSymbol == '.'
213  // 2. negativeSign == '-'
214  // 3. positiveSign == <empty>
215  // 4. thousandsSeparator() == <empty> (we don't check that there
216  // are exactly three decimals between each separator):
217  QString d = l->decimalSymbol(),
218  n = l->negativeSign(),
219  p = l->positiveSign(),
220  t = l->thousandsSeparator();
221  // first, delete p's and t's:
222  if (!p.isEmpty())
223  for (int idx = s.indexOf(p); idx >= 0; idx = s.indexOf(p, idx)) {
224  s.remove(idx, p.length());
225  }
226 
227  if (!t.isEmpty())
228  for (int idx = s.indexOf(t); idx >= 0; idx = s.indexOf(t, idx)) {
229  s.remove(idx, t.length());
230  }
231 
232  // then, replace the d's and n's
233  if ((!n.isEmpty() && n.indexOf('.') != -1) ||
234  (!d.isEmpty() && d.indexOf('-') != -1)) {
235  // make sure we don't replace something twice:
236  kWarning() << "KDoubleValidator: decimal symbol contains '-' or "
237  "negative sign contains '.' -> improve algorithm" << endl;
238  return Invalid;
239  }
240 
241  if (!d.isEmpty() && d != ".")
242  for (int idx = s.indexOf(d); idx >= 0; idx = s.indexOf(d, idx + 1)) {
243  s.replace(idx, d.length(), '.');
244  }
245 
246  if (!n.isEmpty() && n != "-")
247  for (int idx = s.indexOf(n); idx >= 0; idx = s.indexOf(n, idx + 1)) {
248  s.replace(idx, n.length(), '-');
249  }
250  }
251 
252  return base::validate(s, p);
253 }
254 
bool acceptLocalizedNumbers() const
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QString toUpper() const const
virtual int top() const
Returns the current maximum value allowed.
virtual QValidator::State validate(QString &input, int &pos) const const override
State validate(QString &, int &) const override
Validates the text, and return the result.
virtual int bottom() const
Returns the current minimum value allowed.
QString & remove(int position, int n)
KIntValidator(QWidget *parent, int base=10)
Constructor.
QString decimalSymbol() const
Returns what a decimal point should look like ("." or "," etc.) according to the current locale or us...
Definition: klocale.cpp:175
double top() const const
virtual void setRange(int bottom, int top)
Sets the minimum and maximum values allowed.
virtual ~KIntValidator()
Destructs the validator.
virtual int base() const
Returns the current numeric base.
virtual ~KDoubleValidator()
Destructs the validator.
double bottom() const const
QValidator::State validate(QString &input, int &pos) const override
Overloaded for internal reasons.
int toInt(bool *ok, int base) const const
bool isEmpty() const const
QString trimmed() const const
KDoubleValidator(QObject *parent)
Constuct a locale-aware KDoubleValidator with default range (whatever QDoubleValidator uses for that)...
int decimals() const const
virtual void setBase(int base)
Sets the numeric base value.
QValidator for integers.
Definition: knumvalidator.h:42
static KLocale * global()
Return the global KLocale instance.
Definition: klocale.cpp:309
QString & replace(int position, int n, QChar after)
void setAcceptLocalizedNumbers(bool accept)
Sets whether to accept localized numbers (default: true)
KLocale provides support for language and country specific stuff.
Definition: klocale.h:75
void fixup(QString &) const override
Fixes the text if possible, providing a valid string.
QString & setNum(short n, int base)
QString positiveSign() const
Returns what a positive sign should look like ("+", " ", etc.) according to the current locale or use...
Definition: klocale.cpp:200
QString negativeSign() const
Returns what a negative sign should look like ("-", etc.) according to the current locale or user set...
Definition: klocale.cpp:205
int length() const const
QObject * parent() const const
QString thousandsSeparator() const
Returns what the thousands separator should look like ("," or "." etc.) according to the current loca...
Definition: klocale.cpp:180
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jul 13 2020 22:58:31 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.