| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <private/qabstractspinbox_p.h>
|
|---|
| 43 | #include <qspinbox.h>
|
|---|
| 44 |
|
|---|
| 45 | #ifndef QT_NO_SPINBOX
|
|---|
| 46 |
|
|---|
| 47 | #include <qlineedit.h>
|
|---|
| 48 | #include <qlocale.h>
|
|---|
| 49 | #include <qvalidator.h>
|
|---|
| 50 | #include <qdebug.h>
|
|---|
| 51 |
|
|---|
| 52 | #include <math.h>
|
|---|
| 53 |
|
|---|
| 54 | QT_BEGIN_NAMESPACE
|
|---|
| 55 |
|
|---|
| 56 | //#define QSPINBOX_QSBDEBUG
|
|---|
| 57 | #ifdef QSPINBOX_QSBDEBUG
|
|---|
| 58 | # define QSBDEBUG qDebug
|
|---|
| 59 | #else
|
|---|
| 60 | # define QSBDEBUG if (false) qDebug
|
|---|
| 61 | #endif
|
|---|
| 62 |
|
|---|
| 63 | static bool isIntermediateValueHelper(qint64 num, qint64 minimum, qint64 maximum, qint64 *match = 0);
|
|---|
| 64 |
|
|---|
| 65 | class QSpinBoxPrivate : public QAbstractSpinBoxPrivate
|
|---|
| 66 | {
|
|---|
| 67 | Q_DECLARE_PUBLIC(QSpinBox)
|
|---|
| 68 | public:
|
|---|
| 69 | QSpinBoxPrivate(QWidget *parent = 0);
|
|---|
| 70 | void emitSignals(EmitPolicy ep, const QVariant &);
|
|---|
| 71 |
|
|---|
| 72 | virtual QVariant valueFromText(const QString &n) const;
|
|---|
| 73 | virtual QString textFromValue(const QVariant &n) const;
|
|---|
| 74 | QVariant validateAndInterpret(QString &input, int &pos,
|
|---|
| 75 | QValidator::State &state) const;
|
|---|
| 76 | bool isIntermediateValue(const QString &str) const;
|
|---|
| 77 | QChar thousand;
|
|---|
| 78 |
|
|---|
| 79 | inline void init() {
|
|---|
| 80 | setLayoutItemMargins(QStyle::SE_SpinBoxLayoutItem);
|
|---|
| 81 | }
|
|---|
| 82 | };
|
|---|
| 83 |
|
|---|
| 84 | class QDoubleSpinBoxPrivate : public QAbstractSpinBoxPrivate
|
|---|
| 85 | {
|
|---|
| 86 | Q_DECLARE_PUBLIC(QDoubleSpinBox)
|
|---|
| 87 | public:
|
|---|
| 88 | QDoubleSpinBoxPrivate(QWidget *parent = 0);
|
|---|
| 89 | void emitSignals(EmitPolicy ep, const QVariant &);
|
|---|
| 90 | bool isIntermediateValue(const QString &str) const;
|
|---|
| 91 |
|
|---|
| 92 | virtual QVariant valueFromText(const QString &n) const;
|
|---|
| 93 | virtual QString textFromValue(const QVariant &n) const;
|
|---|
| 94 | QVariant validateAndInterpret(QString &input, int &pos,
|
|---|
| 95 | QValidator::State &state) const;
|
|---|
| 96 | double round(double input) const;
|
|---|
| 97 | // variables
|
|---|
| 98 | int decimals;
|
|---|
| 99 | QChar delimiter, thousand;
|
|---|
| 100 | };
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 | /*!
|
|---|
| 104 | \class QSpinBox
|
|---|
| 105 | \brief The QSpinBox class provides a spin box widget.
|
|---|
| 106 |
|
|---|
| 107 | \ingroup basicwidgets
|
|---|
| 108 | \mainclass
|
|---|
| 109 |
|
|---|
| 110 | QSpinBox is designed to handle integers and discrete sets of
|
|---|
| 111 | values (e.g., month names); use QDoubleSpinBox for floating point
|
|---|
| 112 | values.
|
|---|
| 113 |
|
|---|
| 114 | QSpinBox allows the user to choose a value by clicking the up/down
|
|---|
| 115 | buttons or pressing up/down on the keyboard to increase/decrease
|
|---|
| 116 | the value currently displayed. The user can also type the value in
|
|---|
| 117 | manually. The spin box supports integer values but can be extended to
|
|---|
| 118 | use different strings with validate(), textFromValue() and valueFromText().
|
|---|
| 119 |
|
|---|
| 120 | Every time the value changes QSpinBox emits the valueChanged()
|
|---|
| 121 | signals. The current value can be fetched with value() and set
|
|---|
| 122 | with setValue().
|
|---|
| 123 |
|
|---|
| 124 | Clicking the up/down buttons or using the keyboard accelerator's
|
|---|
| 125 | up and down arrows will increase or decrease the current value in
|
|---|
| 126 | steps of size singleStep(). If you want to change this behaviour you
|
|---|
| 127 | can reimplement the virtual function stepBy(). The minimum and
|
|---|
| 128 | maximum value and the step size can be set using one of the
|
|---|
| 129 | constructors, and can be changed later with setMinimum(),
|
|---|
| 130 | setMaximum() and setSingleStep().
|
|---|
| 131 |
|
|---|
| 132 | Most spin boxes are directional, but QSpinBox can also operate as
|
|---|
| 133 | a circular spin box, i.e. if the range is 0-99 and the current
|
|---|
| 134 | value is 99, clicking "up" will give 0 if wrapping() is set to
|
|---|
| 135 | true. Use setWrapping() if you want circular behavior.
|
|---|
| 136 |
|
|---|
| 137 | The displayed value can be prepended and appended with arbitrary
|
|---|
| 138 | strings indicating, for example, currency or the unit of
|
|---|
| 139 | measurement. See setPrefix() and setSuffix(). The text in the spin
|
|---|
| 140 | box is retrieved with text() (which includes any prefix() and
|
|---|
| 141 | suffix()), or with cleanText() (which has no prefix(), no suffix()
|
|---|
| 142 | and no leading or trailing whitespace).
|
|---|
| 143 |
|
|---|
| 144 | It is often desirable to give the user a special (often default)
|
|---|
| 145 | choice in addition to the range of numeric values. See
|
|---|
| 146 | setSpecialValueText() for how to do this with QSpinBox.
|
|---|
| 147 |
|
|---|
| 148 | \table 100%
|
|---|
| 149 | \row \o \inlineimage windowsxp-spinbox.png Screenshot of a Windows XP spin box
|
|---|
| 150 | \o A spin box shown in the \l{Windows XP Style Widget Gallery}{Windows XP widget style}.
|
|---|
| 151 | \row \o \inlineimage plastique-spinbox.png Screenshot of a Plastique spin box
|
|---|
| 152 | \o A spin box shown in the \l{Plastique Style Widget Gallery}{Plastique widget style}.
|
|---|
| 153 | \row \o \inlineimage macintosh-spinbox.png Screenshot of a Macintosh spin box
|
|---|
| 154 | \o A spin box shown in the \l{Macintosh Style Widget Gallery}{Macintosh widget style}.
|
|---|
| 155 | \endtable
|
|---|
| 156 |
|
|---|
| 157 | \section1 Subclassing QSpinBox
|
|---|
| 158 |
|
|---|
| 159 | If using prefix(), suffix(), and specialValueText() don't provide
|
|---|
| 160 | enough control, you subclass QSpinBox and reimplement
|
|---|
| 161 | valueFromText() and textFromValue(). For example, here's the code
|
|---|
| 162 | for a custom spin box that allows the user to enter icon sizes
|
|---|
| 163 | (e.g., "32 x 32"):
|
|---|
| 164 |
|
|---|
| 165 | \snippet examples/widgets/icons/iconsizespinbox.cpp 1
|
|---|
| 166 | \codeline
|
|---|
| 167 | \snippet examples/widgets/icons/iconsizespinbox.cpp 2
|
|---|
| 168 |
|
|---|
| 169 | See the \l{widgets/icons}{Icons} example for the full source
|
|---|
| 170 | code.
|
|---|
| 171 |
|
|---|
| 172 | \sa QDoubleSpinBox, QDateTimeEdit, QSlider, {Spin Boxes Example}
|
|---|
| 173 | */
|
|---|
| 174 |
|
|---|
| 175 | /*!
|
|---|
| 176 | \fn void QSpinBox::valueChanged(int i)
|
|---|
| 177 |
|
|---|
| 178 | This signal is emitted whenever the spin box's value is changed.
|
|---|
| 179 | The new value's integer value is passed in \a i.
|
|---|
| 180 | */
|
|---|
| 181 |
|
|---|
| 182 | /*!
|
|---|
| 183 | \fn void QSpinBox::valueChanged(const QString &text)
|
|---|
| 184 |
|
|---|
| 185 | \overload
|
|---|
| 186 |
|
|---|
| 187 | The new value is passed literally in \a text with no prefix() or
|
|---|
| 188 | suffix().
|
|---|
| 189 | */
|
|---|
| 190 |
|
|---|
| 191 | /*!
|
|---|
| 192 | Constructs a spin box with 0 as minimum value and 99 as maximum value, a
|
|---|
| 193 | step value of 1. The value is initially set to 0. It is parented to \a
|
|---|
| 194 | parent.
|
|---|
| 195 |
|
|---|
| 196 | \sa setMinimum(), setMaximum(), setSingleStep()
|
|---|
| 197 | */
|
|---|
| 198 |
|
|---|
| 199 | QSpinBox::QSpinBox(QWidget *parent)
|
|---|
| 200 | : QAbstractSpinBox(*new QSpinBoxPrivate(parent), parent)
|
|---|
| 201 | {
|
|---|
| 202 | Q_D(QSpinBox);
|
|---|
| 203 | d->init();
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | #ifdef QT3_SUPPORT
|
|---|
| 207 | /*!
|
|---|
| 208 | Use one of the constructors that doesn't take the \a name
|
|---|
| 209 | argument and then use setObjectName() instead.
|
|---|
| 210 | */
|
|---|
| 211 | QSpinBox::QSpinBox(QWidget *parent, const char *name)
|
|---|
| 212 | : QAbstractSpinBox(*new QSpinBoxPrivate(parent), parent)
|
|---|
| 213 | {
|
|---|
| 214 | Q_D(QSpinBox);
|
|---|
| 215 | setObjectName(QString::fromAscii(name));
|
|---|
| 216 | d->init();
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | /*!
|
|---|
| 220 | Use one of the constructors that doesn't take the \a name
|
|---|
| 221 | argument and then use setObjectName() instead.
|
|---|
| 222 | */
|
|---|
| 223 | QSpinBox::QSpinBox(int minimum, int maximum, int step, QWidget *parent, const char *name)
|
|---|
| 224 | : QAbstractSpinBox(*new QSpinBoxPrivate(parent), parent)
|
|---|
| 225 | {
|
|---|
| 226 | Q_D(QSpinBox);
|
|---|
| 227 | d->minimum = QVariant(qMin<int>(minimum, maximum));
|
|---|
| 228 | d->maximum = QVariant(qMax<int>(minimum, maximum));
|
|---|
| 229 | d->singleStep = QVariant(step);
|
|---|
| 230 | setObjectName(QString::fromAscii(name));
|
|---|
| 231 | d->init();
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | #endif
|
|---|
| 235 |
|
|---|
| 236 | /*!
|
|---|
| 237 | \property QSpinBox::value
|
|---|
| 238 | \brief the value of the spin box
|
|---|
| 239 |
|
|---|
| 240 | setValue() will emit valueChanged() if the new value is different
|
|---|
| 241 | from the old one.
|
|---|
| 242 | */
|
|---|
| 243 |
|
|---|
| 244 | int QSpinBox::value() const
|
|---|
| 245 | {
|
|---|
| 246 | Q_D(const QSpinBox);
|
|---|
| 247 | return d->value.toInt();
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | void QSpinBox::setValue(int value)
|
|---|
| 251 | {
|
|---|
| 252 | Q_D(QSpinBox);
|
|---|
| 253 | d->setValue(QVariant(value), EmitIfChanged);
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | /*!
|
|---|
| 257 | \property QSpinBox::prefix
|
|---|
| 258 | \brief the spin box's prefix
|
|---|
| 259 |
|
|---|
| 260 | The prefix is prepended to the start of the displayed value.
|
|---|
| 261 | Typical use is to display a unit of measurement or a currency
|
|---|
| 262 | symbol. For example:
|
|---|
| 263 |
|
|---|
| 264 | \snippet doc/src/snippets/code/src_gui_widgets_qspinbox.cpp 0
|
|---|
| 265 |
|
|---|
| 266 | To turn off the prefix display, set this property to an empty
|
|---|
| 267 | string. The default is no prefix. The prefix is not displayed when
|
|---|
| 268 | value() == minimum() and specialValueText() is set.
|
|---|
| 269 |
|
|---|
| 270 | If no prefix is set, prefix() returns an empty string.
|
|---|
| 271 |
|
|---|
| 272 | \sa suffix(), setSuffix(), specialValueText(), setSpecialValueText()
|
|---|
| 273 | */
|
|---|
| 274 |
|
|---|
| 275 | QString QSpinBox::prefix() const
|
|---|
| 276 | {
|
|---|
| 277 | Q_D(const QSpinBox);
|
|---|
| 278 | return d->prefix;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | void QSpinBox::setPrefix(const QString &prefix)
|
|---|
| 282 | {
|
|---|
| 283 | Q_D(QSpinBox);
|
|---|
| 284 |
|
|---|
| 285 | d->prefix = prefix;
|
|---|
| 286 | d->updateEdit();
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | /*!
|
|---|
| 290 | \property QSpinBox::suffix
|
|---|
| 291 | \brief the suffix of the spin box
|
|---|
| 292 |
|
|---|
| 293 | The suffix is appended to the end of the displayed value. Typical
|
|---|
| 294 | use is to display a unit of measurement or a currency symbol. For
|
|---|
| 295 | example:
|
|---|
| 296 |
|
|---|
| 297 | \snippet doc/src/snippets/code/src_gui_widgets_qspinbox.cpp 1
|
|---|
| 298 |
|
|---|
| 299 | To turn off the suffix display, set this property to an empty
|
|---|
| 300 | string. The default is no suffix. The suffix is not displayed for
|
|---|
| 301 | the minimum() if specialValueText() is set.
|
|---|
| 302 |
|
|---|
| 303 | If no suffix is set, suffix() returns an empty string.
|
|---|
| 304 |
|
|---|
| 305 | \sa prefix(), setPrefix(), specialValueText(), setSpecialValueText()
|
|---|
| 306 | */
|
|---|
| 307 |
|
|---|
| 308 | QString QSpinBox::suffix() const
|
|---|
| 309 | {
|
|---|
| 310 | Q_D(const QSpinBox);
|
|---|
| 311 |
|
|---|
| 312 | return d->suffix;
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | void QSpinBox::setSuffix(const QString &suffix)
|
|---|
| 316 | {
|
|---|
| 317 | Q_D(QSpinBox);
|
|---|
| 318 |
|
|---|
| 319 | d->suffix = suffix;
|
|---|
| 320 | d->updateEdit();
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | /*!
|
|---|
| 324 | \property QSpinBox::cleanText
|
|---|
| 325 |
|
|---|
| 326 | \brief the text of the spin box excluding any prefix, suffix,
|
|---|
| 327 | or leading or trailing whitespace.
|
|---|
| 328 |
|
|---|
| 329 | \sa text, QSpinBox::prefix, QSpinBox::suffix
|
|---|
| 330 | */
|
|---|
| 331 |
|
|---|
| 332 | QString QSpinBox::cleanText() const
|
|---|
| 333 | {
|
|---|
| 334 | Q_D(const QSpinBox);
|
|---|
| 335 |
|
|---|
| 336 | return d->stripped(d->edit->displayText());
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 |
|
|---|
| 340 | /*!
|
|---|
| 341 | \property QSpinBox::singleStep
|
|---|
| 342 | \brief the step value
|
|---|
| 343 |
|
|---|
| 344 | When the user uses the arrows to change the spin box's value the
|
|---|
| 345 | value will be incremented/decremented by the amount of the
|
|---|
| 346 | singleStep. The default value is 1. Setting a singleStep value of
|
|---|
| 347 | less than 0 does nothing.
|
|---|
| 348 | */
|
|---|
| 349 |
|
|---|
| 350 | int QSpinBox::singleStep() const
|
|---|
| 351 | {
|
|---|
| 352 | Q_D(const QSpinBox);
|
|---|
| 353 |
|
|---|
| 354 | return d->singleStep.toInt();
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | void QSpinBox::setSingleStep(int value)
|
|---|
| 358 | {
|
|---|
| 359 | Q_D(QSpinBox);
|
|---|
| 360 | if (value >= 0) {
|
|---|
| 361 | d->singleStep = QVariant(value);
|
|---|
| 362 | d->updateEdit();
|
|---|
| 363 | }
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| 366 | /*!
|
|---|
| 367 | \property QSpinBox::minimum
|
|---|
| 368 |
|
|---|
| 369 | \brief the minimum value of the spin box
|
|---|
| 370 |
|
|---|
| 371 | When setting this property the \l maximum is adjusted
|
|---|
| 372 | if necessary to ensure that the range remains valid.
|
|---|
| 373 |
|
|---|
| 374 | The default minimum value is 0.
|
|---|
| 375 |
|
|---|
| 376 | \sa setRange() specialValueText
|
|---|
| 377 | */
|
|---|
| 378 |
|
|---|
| 379 | int QSpinBox::minimum() const
|
|---|
| 380 | {
|
|---|
| 381 | Q_D(const QSpinBox);
|
|---|
| 382 |
|
|---|
| 383 | return d->minimum.toInt();
|
|---|
| 384 | }
|
|---|
| 385 |
|
|---|
| 386 | void QSpinBox::setMinimum(int minimum)
|
|---|
| 387 | {
|
|---|
| 388 | Q_D(QSpinBox);
|
|---|
| 389 | const QVariant m(minimum);
|
|---|
| 390 | d->setRange(m, (d->variantCompare(d->maximum, m) > 0 ? d->maximum : m));
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | /*!
|
|---|
| 394 | \property QSpinBox::maximum
|
|---|
| 395 |
|
|---|
| 396 | \brief the maximum value of the spin box
|
|---|
| 397 |
|
|---|
| 398 | When setting this property the \l minimum is adjusted
|
|---|
| 399 | if necessary, to ensure that the range remains valid.
|
|---|
| 400 |
|
|---|
| 401 | The default maximum value is 99.
|
|---|
| 402 |
|
|---|
| 403 | \sa setRange() specialValueText
|
|---|
| 404 |
|
|---|
| 405 | */
|
|---|
| 406 |
|
|---|
| 407 | int QSpinBox::maximum() const
|
|---|
| 408 | {
|
|---|
| 409 | Q_D(const QSpinBox);
|
|---|
| 410 |
|
|---|
| 411 | return d->maximum.toInt();
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | void QSpinBox::setMaximum(int maximum)
|
|---|
| 415 | {
|
|---|
| 416 | Q_D(QSpinBox);
|
|---|
| 417 | const QVariant m(maximum);
|
|---|
| 418 | d->setRange((d->variantCompare(d->minimum, m) < 0 ? d->minimum : m), m);
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | /*!
|
|---|
| 422 | Convenience function to set the \a minimum, and \a maximum values
|
|---|
| 423 | with a single function call.
|
|---|
| 424 |
|
|---|
| 425 | \snippet doc/src/snippets/code/src_gui_widgets_qspinbox.cpp 2
|
|---|
| 426 | is equivalent to:
|
|---|
| 427 | \snippet doc/src/snippets/code/src_gui_widgets_qspinbox.cpp 3
|
|---|
| 428 |
|
|---|
| 429 | \sa minimum maximum
|
|---|
| 430 | */
|
|---|
| 431 |
|
|---|
| 432 | void QSpinBox::setRange(int minimum, int maximum)
|
|---|
| 433 | {
|
|---|
| 434 | Q_D(QSpinBox);
|
|---|
| 435 | d->setRange(QVariant(minimum), QVariant(maximum));
|
|---|
| 436 | }
|
|---|
| 437 |
|
|---|
| 438 | /*!
|
|---|
| 439 | This virtual function is used by the spin box whenever it needs
|
|---|
| 440 | to display the given \a value. The default implementation returns
|
|---|
| 441 | a string containing \a value printed in the standard way using
|
|---|
| 442 | QWidget::locale().toString(). Reimplementations may return anything. (See
|
|---|
| 443 | the example in the detailed description.)
|
|---|
| 444 |
|
|---|
| 445 | Note: QSpinBox does not call this function for specialValueText()
|
|---|
| 446 | and that neither prefix() nor suffix() should be included in the
|
|---|
| 447 | return value.
|
|---|
| 448 |
|
|---|
| 449 | If you reimplement this, you may also need to reimplement
|
|---|
| 450 | valueFromText() and validate()
|
|---|
| 451 |
|
|---|
| 452 | \sa valueFromText(), validate()
|
|---|
| 453 | */
|
|---|
| 454 |
|
|---|
| 455 | QString QSpinBox::textFromValue(int value) const
|
|---|
| 456 | {
|
|---|
| 457 | Q_D(const QSpinBox);
|
|---|
| 458 | QString str = locale().toString(value);
|
|---|
| 459 | if (qAbs(value) >= 1000 || value == INT_MIN) {
|
|---|
| 460 | str.remove(d->thousand);
|
|---|
| 461 | }
|
|---|
| 462 |
|
|---|
| 463 | return str;
|
|---|
| 464 | }
|
|---|
| 465 |
|
|---|
| 466 | /*!
|
|---|
| 467 | \fn int QSpinBox::valueFromText(const QString &text) const
|
|---|
| 468 |
|
|---|
| 469 | This virtual function is used by the spin box whenever it needs to
|
|---|
| 470 | interpret \a text entered by the user as a value.
|
|---|
| 471 |
|
|---|
| 472 | Subclasses that need to display spin box values in a non-numeric
|
|---|
| 473 | way need to reimplement this function.
|
|---|
| 474 |
|
|---|
| 475 | Note: QSpinBox handles specialValueText() separately; this
|
|---|
| 476 | function is only concerned with the other values.
|
|---|
| 477 |
|
|---|
| 478 | \sa textFromValue(), validate()
|
|---|
| 479 | */
|
|---|
| 480 |
|
|---|
| 481 | int QSpinBox::valueFromText(const QString &text) const
|
|---|
| 482 | {
|
|---|
| 483 | Q_D(const QSpinBox);
|
|---|
| 484 |
|
|---|
| 485 | QString copy = text;
|
|---|
| 486 | int pos = d->edit->cursorPosition();
|
|---|
| 487 | QValidator::State state = QValidator::Acceptable;
|
|---|
| 488 | return d->validateAndInterpret(copy, pos, state).toInt();
|
|---|
| 489 | }
|
|---|
| 490 |
|
|---|
| 491 | /*!
|
|---|
| 492 | \reimp
|
|---|
| 493 | */
|
|---|
| 494 | QValidator::State QSpinBox::validate(QString &text, int &pos) const
|
|---|
| 495 | {
|
|---|
| 496 | Q_D(const QSpinBox);
|
|---|
| 497 |
|
|---|
| 498 | QValidator::State state;
|
|---|
| 499 | d->validateAndInterpret(text, pos, state);
|
|---|
| 500 | return state;
|
|---|
| 501 | }
|
|---|
| 502 |
|
|---|
| 503 |
|
|---|
| 504 | /*!
|
|---|
| 505 | \reimp
|
|---|
| 506 | */
|
|---|
| 507 | void QSpinBox::fixup(QString &input) const
|
|---|
| 508 | {
|
|---|
| 509 | Q_D(const QSpinBox);
|
|---|
| 510 |
|
|---|
| 511 | input.remove(d->thousand);
|
|---|
| 512 | }
|
|---|
| 513 |
|
|---|
| 514 |
|
|---|
| 515 | // --- QDoubleSpinBox ---
|
|---|
| 516 |
|
|---|
| 517 | /*!
|
|---|
| 518 | \class QDoubleSpinBox
|
|---|
| 519 | \brief The QDoubleSpinBox class provides a spin box widget that
|
|---|
| 520 | takes doubles.
|
|---|
| 521 |
|
|---|
| 522 | \ingroup basicwidgets
|
|---|
| 523 | \mainclass
|
|---|
| 524 |
|
|---|
| 525 | QDoubleSpinBox allows the user to choose a value by clicking the
|
|---|
| 526 | up and down buttons or by pressing Up or Down on the keyboard to
|
|---|
| 527 | increase or decrease the value currently displayed. The user can
|
|---|
| 528 | also type the value in manually. The spin box supports double
|
|---|
| 529 | values but can be extended to use different strings with
|
|---|
| 530 | validate(), textFromValue() and valueFromText().
|
|---|
| 531 |
|
|---|
| 532 | Every time the value changes QDoubleSpinBox emits the
|
|---|
| 533 | valueChanged() signal. The current value can be fetched with
|
|---|
| 534 | value() and set with setValue().
|
|---|
| 535 |
|
|---|
| 536 | Note: QDoubleSpinBox will round numbers so they can be displayed
|
|---|
| 537 | with the current precision. In a QDoubleSpinBox with decimals set
|
|---|
| 538 | to 2, calling setValue(2.555) will cause value() to return 2.56.
|
|---|
| 539 |
|
|---|
| 540 | Clicking the up and down buttons or using the keyboard accelerator's
|
|---|
| 541 | Up and Down arrows will increase or decrease the current value in
|
|---|
| 542 | steps of size singleStep(). If you want to change this behavior you
|
|---|
| 543 | can reimplement the virtual function stepBy(). The minimum and
|
|---|
| 544 | maximum value and the step size can be set using one of the
|
|---|
| 545 | constructors, and can be changed later with setMinimum(),
|
|---|
| 546 | setMaximum() and setSingleStep(). The spinbox has a default
|
|---|
| 547 | precision of 2 decimal places but this can be changed using
|
|---|
| 548 | setDecimals().
|
|---|
| 549 |
|
|---|
| 550 | Most spin boxes are directional, but QDoubleSpinBox can also
|
|---|
| 551 | operate as a circular spin box, i.e. if the range is 0.0-99.9 and
|
|---|
| 552 | the current value is 99.9, clicking "up" will give 0 if wrapping()
|
|---|
| 553 | is set to true. Use setWrapping() if you want circular behavior.
|
|---|
| 554 |
|
|---|
| 555 | The displayed value can be prepended and appended with arbitrary
|
|---|
| 556 | strings indicating, for example, currency or the unit of
|
|---|
| 557 | measurement. See setPrefix() and setSuffix(). The text in the spin
|
|---|
| 558 | box is retrieved with text() (which includes any prefix() and
|
|---|
| 559 | suffix()), or with cleanText() (which has no prefix(), no suffix()
|
|---|
| 560 | and no leading or trailing whitespace).
|
|---|
| 561 |
|
|---|
| 562 | It is often desirable to give the user a special (often default)
|
|---|
| 563 | choice in addition to the range of numeric values. See
|
|---|
| 564 | setSpecialValueText() for how to do this with QDoubleSpinBox.
|
|---|
| 565 |
|
|---|
| 566 | \sa QSpinBox, QDateTimeEdit, QSlider, {Spin Boxes Example}
|
|---|
| 567 | */
|
|---|
| 568 |
|
|---|
| 569 | /*!
|
|---|
| 570 | \fn void QDoubleSpinBox::valueChanged(double d);
|
|---|
| 571 |
|
|---|
| 572 | This signal is emitted whenever the spin box's value is changed.
|
|---|
| 573 | The new value is passed in \a d.
|
|---|
| 574 | */
|
|---|
| 575 |
|
|---|
| 576 | /*!
|
|---|
| 577 | \fn void QDoubleSpinBox::valueChanged(const QString &text);
|
|---|
| 578 |
|
|---|
| 579 | \overload
|
|---|
| 580 |
|
|---|
| 581 | The new value is passed literally in \a text with no prefix() or
|
|---|
| 582 | suffix().
|
|---|
| 583 | */
|
|---|
| 584 |
|
|---|
| 585 | /*!
|
|---|
| 586 | Constructs a spin box with 0.0 as minimum value and 99.99 as maximum value,
|
|---|
| 587 | a step value of 1.0 and a precision of 2 decimal places. The value is
|
|---|
| 588 | initially set to 0.00. The spin box has the given \a parent.
|
|---|
| 589 |
|
|---|
| 590 | \sa setMinimum(), setMaximum(), setSingleStep()
|
|---|
| 591 | */
|
|---|
| 592 | QDoubleSpinBox::QDoubleSpinBox(QWidget *parent)
|
|---|
| 593 | : QAbstractSpinBox(*new QDoubleSpinBoxPrivate(parent), parent)
|
|---|
| 594 | {
|
|---|
| 595 | }
|
|---|
| 596 |
|
|---|
| 597 | /*!
|
|---|
| 598 | \property QDoubleSpinBox::value
|
|---|
| 599 | \brief the value of the spin box
|
|---|
| 600 |
|
|---|
| 601 | setValue() will emit valueChanged() if the new value is different
|
|---|
| 602 | from the old one.
|
|---|
| 603 |
|
|---|
| 604 | Note: The value will be rounded so it can be displayed with the
|
|---|
| 605 | current setting of decimals.
|
|---|
| 606 |
|
|---|
| 607 | \sa decimals
|
|---|
| 608 | */
|
|---|
| 609 | double QDoubleSpinBox::value() const
|
|---|
| 610 | {
|
|---|
| 611 | Q_D(const QDoubleSpinBox);
|
|---|
| 612 |
|
|---|
| 613 | return d->value.toDouble();
|
|---|
| 614 | }
|
|---|
| 615 |
|
|---|
| 616 | void QDoubleSpinBox::setValue(double value)
|
|---|
| 617 | {
|
|---|
| 618 | Q_D(QDoubleSpinBox);
|
|---|
| 619 | QVariant v(d->round(value));
|
|---|
| 620 | d->setValue(v, EmitIfChanged);
|
|---|
| 621 | }
|
|---|
| 622 | /*!
|
|---|
| 623 | \property QDoubleSpinBox::prefix
|
|---|
| 624 | \brief the spin box's prefix
|
|---|
| 625 |
|
|---|
| 626 | The prefix is prepended to the start of the displayed value.
|
|---|
| 627 | Typical use is to display a unit of measurement or a currency
|
|---|
| 628 | symbol. For example:
|
|---|
| 629 |
|
|---|
| 630 | \snippet doc/src/snippets/code/src_gui_widgets_qspinbox.cpp 4
|
|---|
| 631 |
|
|---|
| 632 | To turn off the prefix display, set this property to an empty
|
|---|
| 633 | string. The default is no prefix. The prefix is not displayed when
|
|---|
| 634 | value() == minimum() and specialValueText() is set.
|
|---|
| 635 |
|
|---|
| 636 | If no prefix is set, prefix() returns an empty string.
|
|---|
| 637 |
|
|---|
| 638 | \sa suffix(), setSuffix(), specialValueText(), setSpecialValueText()
|
|---|
| 639 | */
|
|---|
| 640 |
|
|---|
| 641 | QString QDoubleSpinBox::prefix() const
|
|---|
| 642 | {
|
|---|
| 643 | Q_D(const QDoubleSpinBox);
|
|---|
| 644 |
|
|---|
| 645 | return d->prefix;
|
|---|
| 646 | }
|
|---|
| 647 |
|
|---|
| 648 | void QDoubleSpinBox::setPrefix(const QString &prefix)
|
|---|
| 649 | {
|
|---|
| 650 | Q_D(QDoubleSpinBox);
|
|---|
| 651 |
|
|---|
| 652 | d->prefix = prefix;
|
|---|
| 653 | d->updateEdit();
|
|---|
| 654 | }
|
|---|
| 655 |
|
|---|
| 656 | /*!
|
|---|
| 657 | \property QDoubleSpinBox::suffix
|
|---|
| 658 | \brief the suffix of the spin box
|
|---|
| 659 |
|
|---|
| 660 | The suffix is appended to the end of the displayed value. Typical
|
|---|
| 661 | use is to display a unit of measurement or a currency symbol. For
|
|---|
| 662 | example:
|
|---|
| 663 |
|
|---|
| 664 | \snippet doc/src/snippets/code/src_gui_widgets_qspinbox.cpp 5
|
|---|
| 665 |
|
|---|
| 666 | To turn off the suffix display, set this property to an empty
|
|---|
| 667 | string. The default is no suffix. The suffix is not displayed for
|
|---|
| 668 | the minimum() if specialValueText() is set.
|
|---|
| 669 |
|
|---|
| 670 | If no suffix is set, suffix() returns an empty string.
|
|---|
| 671 |
|
|---|
| 672 | \sa prefix(), setPrefix(), specialValueText(), setSpecialValueText()
|
|---|
| 673 | */
|
|---|
| 674 |
|
|---|
| 675 | QString QDoubleSpinBox::suffix() const
|
|---|
| 676 | {
|
|---|
| 677 | Q_D(const QDoubleSpinBox);
|
|---|
| 678 |
|
|---|
| 679 | return d->suffix;
|
|---|
| 680 | }
|
|---|
| 681 |
|
|---|
| 682 | void QDoubleSpinBox::setSuffix(const QString &suffix)
|
|---|
| 683 | {
|
|---|
| 684 | Q_D(QDoubleSpinBox);
|
|---|
| 685 |
|
|---|
| 686 | d->suffix = suffix;
|
|---|
| 687 | d->updateEdit();
|
|---|
| 688 | }
|
|---|
| 689 |
|
|---|
| 690 | /*!
|
|---|
| 691 | \property QDoubleSpinBox::cleanText
|
|---|
| 692 |
|
|---|
| 693 | \brief the text of the spin box excluding any prefix, suffix,
|
|---|
| 694 | or leading or trailing whitespace.
|
|---|
| 695 |
|
|---|
| 696 | \sa text, QDoubleSpinBox::prefix, QDoubleSpinBox::suffix
|
|---|
| 697 | */
|
|---|
| 698 |
|
|---|
| 699 | QString QDoubleSpinBox::cleanText() const
|
|---|
| 700 | {
|
|---|
| 701 | Q_D(const QDoubleSpinBox);
|
|---|
| 702 |
|
|---|
| 703 | return d->stripped(d->edit->displayText());
|
|---|
| 704 | }
|
|---|
| 705 |
|
|---|
| 706 | /*!
|
|---|
| 707 | \property QDoubleSpinBox::singleStep
|
|---|
| 708 | \brief the step value
|
|---|
| 709 |
|
|---|
| 710 | When the user uses the arrows to change the spin box's value the
|
|---|
| 711 | value will be incremented/decremented by the amount of the
|
|---|
| 712 | singleStep. The default value is 1.0. Setting a singleStep value
|
|---|
| 713 | of less than 0 does nothing.
|
|---|
| 714 | */
|
|---|
| 715 | double QDoubleSpinBox::singleStep() const
|
|---|
| 716 | {
|
|---|
| 717 | Q_D(const QDoubleSpinBox);
|
|---|
| 718 |
|
|---|
| 719 | return d->singleStep.toDouble();
|
|---|
| 720 | }
|
|---|
| 721 |
|
|---|
| 722 | void QDoubleSpinBox::setSingleStep(double value)
|
|---|
| 723 | {
|
|---|
| 724 | Q_D(QDoubleSpinBox);
|
|---|
| 725 |
|
|---|
| 726 | if (value >= 0) {
|
|---|
| 727 | d->singleStep = value;
|
|---|
| 728 | d->updateEdit();
|
|---|
| 729 | }
|
|---|
| 730 | }
|
|---|
| 731 |
|
|---|
| 732 | /*!
|
|---|
| 733 | \property QDoubleSpinBox::minimum
|
|---|
| 734 |
|
|---|
| 735 | \brief the minimum value of the spin box
|
|---|
| 736 |
|
|---|
| 737 | When setting this property the \l maximum is adjusted
|
|---|
| 738 | if necessary to ensure that the range remains valid.
|
|---|
| 739 |
|
|---|
| 740 | The default minimum value is 0.0.
|
|---|
| 741 |
|
|---|
| 742 | Note: The minimum value will be rounded to match the decimals
|
|---|
| 743 | property.
|
|---|
| 744 |
|
|---|
| 745 | \sa decimals, setRange() specialValueText
|
|---|
| 746 | */
|
|---|
| 747 |
|
|---|
| 748 | double QDoubleSpinBox::minimum() const
|
|---|
| 749 | {
|
|---|
| 750 | Q_D(const QDoubleSpinBox);
|
|---|
| 751 |
|
|---|
| 752 | return d->minimum.toDouble();
|
|---|
| 753 | }
|
|---|
| 754 |
|
|---|
| 755 | void QDoubleSpinBox::setMinimum(double minimum)
|
|---|
| 756 | {
|
|---|
| 757 | Q_D(QDoubleSpinBox);
|
|---|
| 758 | const QVariant m(d->round(minimum));
|
|---|
| 759 | d->setRange(m, (d->variantCompare(d->maximum, m) > 0 ? d->maximum : m));
|
|---|
| 760 | }
|
|---|
| 761 |
|
|---|
| 762 | /*!
|
|---|
| 763 | \property QDoubleSpinBox::maximum
|
|---|
| 764 |
|
|---|
| 765 | \brief the maximum value of the spin box
|
|---|
| 766 |
|
|---|
| 767 | When setting this property the \l minimum is adjusted
|
|---|
| 768 | if necessary, to ensure that the range remains valid.
|
|---|
| 769 |
|
|---|
| 770 | The default maximum value is 99.99.
|
|---|
| 771 |
|
|---|
| 772 | Note: The maximum value will be rounded to match the decimals
|
|---|
| 773 | property.
|
|---|
| 774 |
|
|---|
| 775 | \sa decimals, setRange()
|
|---|
| 776 | */
|
|---|
| 777 |
|
|---|
| 778 | double QDoubleSpinBox::maximum() const
|
|---|
| 779 | {
|
|---|
| 780 | Q_D(const QDoubleSpinBox);
|
|---|
| 781 |
|
|---|
| 782 | return d->maximum.toDouble();
|
|---|
| 783 | }
|
|---|
| 784 |
|
|---|
| 785 | void QDoubleSpinBox::setMaximum(double maximum)
|
|---|
| 786 | {
|
|---|
| 787 | Q_D(QDoubleSpinBox);
|
|---|
| 788 | const QVariant m(d->round(maximum));
|
|---|
| 789 | d->setRange((d->variantCompare(d->minimum, m) < 0 ? d->minimum : m), m);
|
|---|
| 790 | }
|
|---|
| 791 |
|
|---|
| 792 | /*!
|
|---|
| 793 | Convenience function to set the \a minimum and \a maximum values
|
|---|
| 794 | with a single function call.
|
|---|
| 795 |
|
|---|
| 796 | Note: The maximum and minimum values will be rounded to match the
|
|---|
| 797 | decimals property.
|
|---|
| 798 |
|
|---|
| 799 | \snippet doc/src/snippets/code/src_gui_widgets_qspinbox.cpp 6
|
|---|
| 800 | is equivalent to:
|
|---|
| 801 | \snippet doc/src/snippets/code/src_gui_widgets_qspinbox.cpp 7
|
|---|
| 802 |
|
|---|
| 803 | \sa minimum maximum
|
|---|
| 804 | */
|
|---|
| 805 |
|
|---|
| 806 | void QDoubleSpinBox::setRange(double minimum, double maximum)
|
|---|
| 807 | {
|
|---|
| 808 | Q_D(QDoubleSpinBox);
|
|---|
| 809 | d->setRange(QVariant(d->round(minimum)), QVariant(d->round(maximum)));
|
|---|
| 810 | }
|
|---|
| 811 |
|
|---|
| 812 | /*!
|
|---|
| 813 | \property QDoubleSpinBox::decimals
|
|---|
| 814 |
|
|---|
| 815 | \brief the precision of the spin box, in decimals
|
|---|
| 816 |
|
|---|
| 817 | Sets how many decimals the spinbox will use for displaying and
|
|---|
| 818 | interpreting doubles.
|
|---|
| 819 |
|
|---|
| 820 | \warning The results might not be reliable with very high values
|
|---|
| 821 | for \a decimals.
|
|---|
| 822 |
|
|---|
| 823 | Note: The maximum, minimum and value might change as a result of
|
|---|
| 824 | changing this property.
|
|---|
| 825 | */
|
|---|
| 826 |
|
|---|
| 827 | int QDoubleSpinBox::decimals() const
|
|---|
| 828 | {
|
|---|
| 829 | Q_D(const QDoubleSpinBox);
|
|---|
| 830 |
|
|---|
| 831 | return d->decimals;
|
|---|
| 832 | }
|
|---|
| 833 |
|
|---|
| 834 | void QDoubleSpinBox::setDecimals(int decimals)
|
|---|
| 835 | {
|
|---|
| 836 | Q_D(QDoubleSpinBox);
|
|---|
| 837 | d->decimals = qMax(0, decimals);
|
|---|
| 838 |
|
|---|
| 839 | setRange(minimum(), maximum()); // make sure values are rounded
|
|---|
| 840 | setValue(value());
|
|---|
| 841 | }
|
|---|
| 842 |
|
|---|
| 843 | /*!
|
|---|
| 844 | This virtual function is used by the spin box whenever it needs to
|
|---|
| 845 | display the given \a value. The default implementation returns a string
|
|---|
| 846 | containing \a value printed using QWidget::locale().toString(\a value,
|
|---|
| 847 | QLatin1Char('f'), decimals()) and will remove the thousand
|
|---|
| 848 | separator. Reimplementations may return anything.
|
|---|
| 849 |
|
|---|
| 850 | Note: QDoubleSpinBox does not call this function for
|
|---|
| 851 | specialValueText() and that neither prefix() nor suffix() should
|
|---|
| 852 | be included in the return value.
|
|---|
| 853 |
|
|---|
| 854 | If you reimplement this, you may also need to reimplement
|
|---|
| 855 | valueFromText().
|
|---|
| 856 |
|
|---|
| 857 | \sa valueFromText()
|
|---|
| 858 | */
|
|---|
| 859 |
|
|---|
| 860 |
|
|---|
| 861 | QString QDoubleSpinBox::textFromValue(double value) const
|
|---|
| 862 | {
|
|---|
| 863 | Q_D(const QDoubleSpinBox);
|
|---|
| 864 | QString str = locale().toString(value, 'f', d->decimals);
|
|---|
| 865 | if (qAbs(value) >= 1000.0) {
|
|---|
| 866 | str.remove(d->thousand);
|
|---|
| 867 | }
|
|---|
| 868 | return str;
|
|---|
| 869 | }
|
|---|
| 870 |
|
|---|
| 871 | /*!
|
|---|
| 872 | This virtual function is used by the spin box whenever it needs to
|
|---|
| 873 | interpret \a text entered by the user as a value.
|
|---|
| 874 |
|
|---|
| 875 | Subclasses that need to display spin box values in a non-numeric
|
|---|
| 876 | way need to reimplement this function.
|
|---|
| 877 |
|
|---|
| 878 | Note: QDoubleSpinBox handles specialValueText() separately; this
|
|---|
| 879 | function is only concerned with the other values.
|
|---|
| 880 |
|
|---|
| 881 | \sa textFromValue(), validate()
|
|---|
| 882 | */
|
|---|
| 883 | double QDoubleSpinBox::valueFromText(const QString &text) const
|
|---|
| 884 | {
|
|---|
| 885 | Q_D(const QDoubleSpinBox);
|
|---|
| 886 |
|
|---|
| 887 | QString copy = text;
|
|---|
| 888 | int pos = d->edit->cursorPosition();
|
|---|
| 889 | QValidator::State state = QValidator::Acceptable;
|
|---|
| 890 | return d->validateAndInterpret(copy, pos, state).toDouble();
|
|---|
| 891 | }
|
|---|
| 892 |
|
|---|
| 893 | /*!
|
|---|
| 894 | \reimp
|
|---|
| 895 | */
|
|---|
| 896 | QValidator::State QDoubleSpinBox::validate(QString &text, int &pos) const
|
|---|
| 897 | {
|
|---|
| 898 | Q_D(const QDoubleSpinBox);
|
|---|
| 899 |
|
|---|
| 900 | QValidator::State state;
|
|---|
| 901 | d->validateAndInterpret(text, pos, state);
|
|---|
| 902 | return state;
|
|---|
| 903 | }
|
|---|
| 904 |
|
|---|
| 905 |
|
|---|
| 906 | /*!
|
|---|
| 907 | \reimp
|
|---|
| 908 | */
|
|---|
| 909 | void QDoubleSpinBox::fixup(QString &input) const
|
|---|
| 910 | {
|
|---|
| 911 | Q_D(const QDoubleSpinBox);
|
|---|
| 912 |
|
|---|
| 913 | input.remove(d->thousand);
|
|---|
| 914 | }
|
|---|
| 915 |
|
|---|
| 916 | // --- QSpinBoxPrivate ---
|
|---|
| 917 |
|
|---|
| 918 | /*!
|
|---|
| 919 | \internal
|
|---|
| 920 | Constructs a QSpinBoxPrivate object
|
|---|
| 921 | */
|
|---|
| 922 |
|
|---|
| 923 | QSpinBoxPrivate::QSpinBoxPrivate(QWidget *parent)
|
|---|
| 924 | {
|
|---|
| 925 | minimum = QVariant((int)0);
|
|---|
| 926 | maximum = QVariant((int)99);
|
|---|
| 927 | value = minimum;
|
|---|
| 928 | singleStep = QVariant((int)1);
|
|---|
| 929 | type = QVariant::Int;
|
|---|
| 930 | const QString str = (parent ? parent->locale() : QLocale()).toString(4567);
|
|---|
| 931 | if (str.size() == 5) {
|
|---|
| 932 | thousand = QChar(str.at(1));
|
|---|
| 933 | }
|
|---|
| 934 |
|
|---|
| 935 | }
|
|---|
| 936 |
|
|---|
| 937 | /*!
|
|---|
| 938 | \internal
|
|---|
| 939 | \reimp
|
|---|
| 940 | */
|
|---|
| 941 |
|
|---|
| 942 | void QSpinBoxPrivate::emitSignals(EmitPolicy ep, const QVariant &old)
|
|---|
| 943 | {
|
|---|
| 944 | Q_Q(QSpinBox);
|
|---|
| 945 | if (ep != NeverEmit) {
|
|---|
| 946 | pendingEmit = false;
|
|---|
| 947 | if (ep == AlwaysEmit || value != old) {
|
|---|
| 948 | emit q->valueChanged(edit->displayText());
|
|---|
| 949 | emit q->valueChanged(value.toInt());
|
|---|
| 950 | }
|
|---|
| 951 | }
|
|---|
| 952 | }
|
|---|
| 953 |
|
|---|
| 954 | /*!
|
|---|
| 955 | \internal
|
|---|
| 956 | \reimp
|
|---|
| 957 | */
|
|---|
| 958 |
|
|---|
| 959 | QString QSpinBoxPrivate::textFromValue(const QVariant &value) const
|
|---|
| 960 | {
|
|---|
| 961 | Q_Q(const QSpinBox);
|
|---|
| 962 | return q->textFromValue(value.toInt());
|
|---|
| 963 | }
|
|---|
| 964 | /*!
|
|---|
| 965 | \internal
|
|---|
| 966 | \reimp
|
|---|
| 967 | */
|
|---|
| 968 |
|
|---|
| 969 | QVariant QSpinBoxPrivate::valueFromText(const QString &text) const
|
|---|
| 970 | {
|
|---|
| 971 | Q_Q(const QSpinBox);
|
|---|
| 972 |
|
|---|
| 973 | return QVariant(q->valueFromText(text));
|
|---|
| 974 | }
|
|---|
| 975 |
|
|---|
| 976 |
|
|---|
| 977 | /*!
|
|---|
| 978 | \internal
|
|---|
| 979 |
|
|---|
| 980 | Return true if str can become a number which is between minimum and
|
|---|
| 981 | maximum or false if this is not possible.
|
|---|
| 982 | */
|
|---|
| 983 |
|
|---|
| 984 | bool QSpinBoxPrivate::isIntermediateValue(const QString &str) const
|
|---|
| 985 | {
|
|---|
| 986 | const int num = q_func()->locale().toInt(str, 0, 10);
|
|---|
| 987 | const int min = minimum.toInt();
|
|---|
| 988 | const int max = maximum.toInt();
|
|---|
| 989 |
|
|---|
| 990 | int numDigits = 0;
|
|---|
| 991 | int digits[10];
|
|---|
| 992 | int tmp = num;
|
|---|
| 993 | if (tmp == 0) {
|
|---|
| 994 | numDigits = 1;
|
|---|
| 995 | digits[0] = 0;
|
|---|
| 996 | } else {
|
|---|
| 997 | tmp = num;
|
|---|
| 998 | for (int i=0; tmp != 0; ++i) {
|
|---|
| 999 | digits[numDigits++] = qAbs(tmp % 10);
|
|---|
| 1000 | tmp /= 10;
|
|---|
| 1001 | }
|
|---|
| 1002 | }
|
|---|
| 1003 |
|
|---|
| 1004 | int failures = 0;
|
|---|
| 1005 | for (int number=min; /*number<=max*/; ++number) {
|
|---|
| 1006 | tmp = number;
|
|---|
| 1007 | for (int i=0; tmp != 0;) {
|
|---|
| 1008 | if (digits[i] == qAbs(tmp % 10)) {
|
|---|
| 1009 | if (++i == numDigits)
|
|---|
| 1010 | return true;
|
|---|
| 1011 | }
|
|---|
| 1012 | tmp /= 10;
|
|---|
| 1013 | }
|
|---|
| 1014 | if (failures++ == 500000) //upper bound
|
|---|
| 1015 | return true;
|
|---|
| 1016 | if (number == max) // needed for INT_MAX
|
|---|
| 1017 | break;
|
|---|
| 1018 | }
|
|---|
| 1019 | return false;
|
|---|
| 1020 | }
|
|---|
| 1021 |
|
|---|
| 1022 | /*!
|
|---|
| 1023 | \internal Multi purpose function that parses input, sets state to
|
|---|
| 1024 | the appropriate state and returns the value it will be interpreted
|
|---|
| 1025 | as.
|
|---|
| 1026 | */
|
|---|
| 1027 |
|
|---|
| 1028 | QVariant QSpinBoxPrivate::validateAndInterpret(QString &input, int &pos,
|
|---|
| 1029 | QValidator::State &state) const
|
|---|
| 1030 | {
|
|---|
| 1031 | if (cachedText == input && !input.isEmpty()) {
|
|---|
| 1032 | state = cachedState;
|
|---|
| 1033 | QSBDEBUG() << "cachedText was" << "'" << cachedText << "'" << "state was "
|
|---|
| 1034 | << state << " and value was " << cachedValue;
|
|---|
| 1035 |
|
|---|
| 1036 | return cachedValue;
|
|---|
| 1037 | }
|
|---|
| 1038 | const int max = maximum.toInt();
|
|---|
| 1039 | const int min = minimum.toInt();
|
|---|
| 1040 |
|
|---|
| 1041 | QString copy = stripped(input, &pos);
|
|---|
| 1042 | QSBDEBUG() << "input" << input << "copy" << copy;
|
|---|
| 1043 | state = QValidator::Acceptable;
|
|---|
| 1044 | int num = min;
|
|---|
| 1045 |
|
|---|
| 1046 | if (max != min && (copy.isEmpty()
|
|---|
| 1047 | || (min < 0 && copy == QLatin1String("-"))
|
|---|
| 1048 | || (min >= 0 && copy == QLatin1String("+")))) {
|
|---|
| 1049 | state = QValidator::Intermediate;
|
|---|
| 1050 | QSBDEBUG() << __FILE__ << __LINE__<< "num is set to" << num;
|
|---|
| 1051 | } else if (copy.startsWith(QLatin1String("-")) && min >= 0) {
|
|---|
| 1052 | state = QValidator::Invalid; // special-case -0 will be interpreted as 0 and thus not be invalid with a range from 0-100
|
|---|
| 1053 | } else {
|
|---|
| 1054 | bool ok = false;
|
|---|
| 1055 | bool removedThousand = false;
|
|---|
| 1056 | num = q_func()->locale().toInt(copy, &ok, 10);
|
|---|
| 1057 | if (!ok && copy.contains(thousand) && (max >= 1000 || min <= -1000)) {
|
|---|
| 1058 | const int s = copy.size();
|
|---|
| 1059 | copy.remove(thousand);
|
|---|
| 1060 | pos = qMax(0, pos - (s - copy.size()));
|
|---|
| 1061 | removedThousand = true;
|
|---|
| 1062 | num = q_func()->locale().toInt(copy, &ok, 10);
|
|---|
| 1063 | }
|
|---|
| 1064 | QSBDEBUG() << __FILE__ << __LINE__<< "num is set to" << num;
|
|---|
| 1065 | if (!ok) {
|
|---|
| 1066 | state = QValidator::Invalid;
|
|---|
| 1067 | } else if (num >= min && num <= max) {
|
|---|
| 1068 | state = removedThousand ? QValidator::Intermediate : QValidator::Acceptable;
|
|---|
| 1069 | } else if (max == min) {
|
|---|
| 1070 | state = QValidator::Invalid;
|
|---|
| 1071 | } else {
|
|---|
| 1072 | if ((num >= 0 && num > max) || (num < 0 && num < min)) {
|
|---|
| 1073 | state = QValidator::Invalid;
|
|---|
| 1074 | QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid";
|
|---|
| 1075 | } else {
|
|---|
| 1076 | state = isIntermediateValue(copy) ? QValidator::Intermediate : QValidator::Invalid;
|
|---|
| 1077 | QSBDEBUG() << __FILE__ << __LINE__<< "state is set to "
|
|---|
| 1078 | << (state == QValidator::Intermediate ? "Intermediate" : "Acceptable");
|
|---|
| 1079 | }
|
|---|
| 1080 | }
|
|---|
| 1081 | }
|
|---|
| 1082 | if (state != QValidator::Acceptable)
|
|---|
| 1083 | num = max > 0 ? min : max;
|
|---|
| 1084 | input = prefix + copy + suffix;
|
|---|
| 1085 | cachedText = input;
|
|---|
| 1086 | cachedState = state;
|
|---|
| 1087 | cachedValue = QVariant((int)num);
|
|---|
| 1088 |
|
|---|
| 1089 | QSBDEBUG() << "cachedText is set to '" << cachedText << "' state is set to "
|
|---|
| 1090 | << state << " and value is set to " << cachedValue;
|
|---|
| 1091 | return cachedValue;
|
|---|
| 1092 | }
|
|---|
| 1093 |
|
|---|
| 1094 | // --- QDoubleSpinBoxPrivate ---
|
|---|
| 1095 |
|
|---|
| 1096 | /*!
|
|---|
| 1097 | \internal
|
|---|
| 1098 | Constructs a QSpinBoxPrivate object
|
|---|
| 1099 | */
|
|---|
| 1100 |
|
|---|
| 1101 | QDoubleSpinBoxPrivate::QDoubleSpinBoxPrivate(QWidget *parent)
|
|---|
| 1102 | {
|
|---|
| 1103 | minimum = QVariant(0.0);
|
|---|
| 1104 | maximum = QVariant(99.99);
|
|---|
| 1105 | value = minimum;
|
|---|
| 1106 | singleStep = QVariant(1.0);
|
|---|
| 1107 | decimals = 2;
|
|---|
| 1108 | type = QVariant::Double;
|
|---|
| 1109 | const QString str = (parent ? parent->locale() : QLocale()).toString(4567.1);
|
|---|
| 1110 | if (str.size() == 6) {
|
|---|
| 1111 | delimiter = str.at(4);
|
|---|
| 1112 | thousand = QChar((ushort)0);
|
|---|
| 1113 | } else if (str.size() == 7) {
|
|---|
| 1114 | thousand = str.at(1);
|
|---|
| 1115 | delimiter = str.at(5);
|
|---|
| 1116 | }
|
|---|
| 1117 | Q_ASSERT(!delimiter.isNull());
|
|---|
| 1118 | }
|
|---|
| 1119 |
|
|---|
| 1120 | /*!
|
|---|
| 1121 | \internal
|
|---|
| 1122 | \reimp
|
|---|
| 1123 | */
|
|---|
| 1124 |
|
|---|
| 1125 | void QDoubleSpinBoxPrivate::emitSignals(EmitPolicy ep, const QVariant &old)
|
|---|
| 1126 | {
|
|---|
| 1127 | Q_Q(QDoubleSpinBox);
|
|---|
| 1128 | if (ep != NeverEmit) {
|
|---|
| 1129 | pendingEmit = false;
|
|---|
| 1130 | if (ep == AlwaysEmit || value != old) {
|
|---|
| 1131 | emit q->valueChanged(edit->displayText());
|
|---|
| 1132 | emit q->valueChanged(value.toDouble());
|
|---|
| 1133 | }
|
|---|
| 1134 | }
|
|---|
| 1135 | }
|
|---|
| 1136 |
|
|---|
| 1137 |
|
|---|
| 1138 | bool QDoubleSpinBoxPrivate::isIntermediateValue(const QString &str) const
|
|---|
| 1139 | {
|
|---|
| 1140 | QSBDEBUG() << "input is" << str << minimum << maximum;
|
|---|
| 1141 | qint64 dec = 1;
|
|---|
| 1142 | for (int i=0; i<decimals; ++i)
|
|---|
| 1143 | dec *= 10;
|
|---|
| 1144 |
|
|---|
| 1145 | const QLatin1Char dot('.');
|
|---|
| 1146 |
|
|---|
| 1147 | // I know QString::number() uses CLocale so I use dot
|
|---|
| 1148 | const QString minstr = QString::number(minimum.toDouble(), 'f', decimals);
|
|---|
| 1149 | bool ok;
|
|---|
| 1150 | qint64 min_left = minstr.left(minstr.indexOf(dot)).toLongLong(&ok);
|
|---|
| 1151 | if (!ok)
|
|---|
| 1152 | return false;
|
|---|
| 1153 | qint64 min_right = minstr.mid(minstr.indexOf(dot) + 1).toLongLong();
|
|---|
| 1154 |
|
|---|
| 1155 | const QString maxstr = QString::number(maximum.toDouble(), 'f', decimals);
|
|---|
| 1156 | qint64 max_left = maxstr.left(maxstr.indexOf(dot)).toLongLong(&ok);
|
|---|
| 1157 | if (!ok)
|
|---|
| 1158 | return true;
|
|---|
| 1159 | qint64 max_right = maxstr.mid(maxstr.indexOf(dot) + 1).toLongLong();
|
|---|
| 1160 |
|
|---|
| 1161 | const int dotindex = str.indexOf(delimiter);
|
|---|
| 1162 | const bool negative = maximum.toDouble() < 0;
|
|---|
| 1163 | qint64 left = 0, right = 0;
|
|---|
| 1164 | bool doleft = true;
|
|---|
| 1165 | bool doright = true;
|
|---|
| 1166 | if (dotindex == -1) {
|
|---|
| 1167 | left = str.toLongLong();
|
|---|
| 1168 | doright = false;
|
|---|
| 1169 | } else if (dotindex == 0 || (dotindex == 1 && str.at(0) == QLatin1Char('+'))) {
|
|---|
| 1170 | if (negative) {
|
|---|
| 1171 | QSBDEBUG() << __FILE__ << __LINE__ << "returns false";
|
|---|
| 1172 | return false;
|
|---|
| 1173 | }
|
|---|
| 1174 | doleft = false;
|
|---|
| 1175 | right = str.mid(dotindex + 1).toLongLong();
|
|---|
| 1176 | } else if (dotindex == 1 && str.at(0) == QLatin1Char('-')) {
|
|---|
| 1177 | if (!negative) {
|
|---|
| 1178 | QSBDEBUG() << __FILE__ << __LINE__ << "returns false";
|
|---|
| 1179 | return false;
|
|---|
| 1180 | }
|
|---|
| 1181 | doleft = false;
|
|---|
| 1182 | right = str.mid(dotindex + 1).toLongLong();
|
|---|
| 1183 | } else {
|
|---|
| 1184 | left = str.left(dotindex).toLongLong();
|
|---|
| 1185 | if (dotindex == str.size() - 1) {
|
|---|
| 1186 | doright = false;
|
|---|
| 1187 | } else {
|
|---|
| 1188 | right = str.mid(dotindex + 1).toLongLong();
|
|---|
| 1189 | }
|
|---|
| 1190 | }
|
|---|
| 1191 | if ((left >= 0 && max_left < 0 && !str.startsWith(QLatin1Char('-'))) || (left < 0 && min_left >= 0)) {
|
|---|
| 1192 | QSBDEBUG("returns false 0");
|
|---|
| 1193 | return false;
|
|---|
| 1194 | }
|
|---|
| 1195 |
|
|---|
| 1196 | qint64 match = min_left;
|
|---|
| 1197 | if (doleft && !isIntermediateValueHelper(left, min_left, max_left, &match)) {
|
|---|
| 1198 | QSBDEBUG() << __FILE__ << __LINE__ << "returns false";
|
|---|
| 1199 | return false;
|
|---|
| 1200 | }
|
|---|
| 1201 | if (doright) {
|
|---|
| 1202 | QSBDEBUG("match %lld min_left %lld max_left %lld", match, min_left, max_left);
|
|---|
| 1203 | if (!doleft) {
|
|---|
| 1204 | if (min_left == max_left) {
|
|---|
| 1205 | const bool ret = isIntermediateValueHelper(qAbs(left),
|
|---|
| 1206 | negative ? max_right : min_right,
|
|---|
| 1207 | negative ? min_right : max_right);
|
|---|
| 1208 | QSBDEBUG() << __FILE__ << __LINE__ << "returns" << ret;
|
|---|
| 1209 | return ret;
|
|---|
| 1210 | } else if (qAbs(max_left - min_left) == 1) {
|
|---|
| 1211 | const bool ret = isIntermediateValueHelper(qAbs(left), min_right, negative ? 0 : dec)
|
|---|
| 1212 | || isIntermediateValueHelper(qAbs(left), negative ? dec : 0, max_right);
|
|---|
| 1213 | QSBDEBUG() << __FILE__ << __LINE__ << "returns" << ret;
|
|---|
| 1214 | return ret;
|
|---|
| 1215 | } else {
|
|---|
| 1216 | const bool ret = isIntermediateValueHelper(qAbs(left), 0, dec);
|
|---|
| 1217 | QSBDEBUG() << __FILE__ << __LINE__ << "returns" << ret;
|
|---|
| 1218 | return ret;
|
|---|
| 1219 | }
|
|---|
| 1220 | }
|
|---|
| 1221 | if (match != min_left) {
|
|---|
| 1222 | min_right = negative ? dec : 0;
|
|---|
| 1223 | }
|
|---|
| 1224 | if (match != max_left) {
|
|---|
| 1225 | max_right = negative ? 0 : dec;
|
|---|
| 1226 | }
|
|---|
| 1227 | qint64 tmpl = negative ? max_right : min_right;
|
|---|
| 1228 | qint64 tmpr = negative ? min_right : max_right;
|
|---|
| 1229 | const bool ret = isIntermediateValueHelper(right, tmpl, tmpr);
|
|---|
| 1230 | QSBDEBUG() << __FILE__ << __LINE__ << "returns" << ret;
|
|---|
| 1231 | return ret;
|
|---|
| 1232 | }
|
|---|
| 1233 | QSBDEBUG() << __FILE__ << __LINE__ << "returns true";
|
|---|
| 1234 | return true;
|
|---|
| 1235 | }
|
|---|
| 1236 |
|
|---|
| 1237 | /*!
|
|---|
| 1238 | \internal
|
|---|
| 1239 | \reimp
|
|---|
| 1240 | */
|
|---|
| 1241 | QVariant QDoubleSpinBoxPrivate::valueFromText(const QString &f) const
|
|---|
| 1242 | {
|
|---|
| 1243 | Q_Q(const QDoubleSpinBox);
|
|---|
| 1244 | return QVariant(q->valueFromText(f));
|
|---|
| 1245 | }
|
|---|
| 1246 |
|
|---|
| 1247 | /*!
|
|---|
| 1248 | \internal
|
|---|
| 1249 | Rounds to a double value that is restricted to decimals.
|
|---|
| 1250 | E.g. // decimals = 2
|
|---|
| 1251 |
|
|---|
| 1252 | round(5.555) => 5.56
|
|---|
| 1253 | */
|
|---|
| 1254 |
|
|---|
| 1255 | double QDoubleSpinBoxPrivate::round(double value) const
|
|---|
| 1256 | {
|
|---|
| 1257 | Q_Q(const QDoubleSpinBox);
|
|---|
| 1258 | const QString strDbl = q->locale().toString(value, 'f', decimals);
|
|---|
| 1259 | return q->locale().toDouble(strDbl);
|
|---|
| 1260 | }
|
|---|
| 1261 |
|
|---|
| 1262 |
|
|---|
| 1263 | /*!
|
|---|
| 1264 | \internal Multi purpose function that parses input, sets state to
|
|---|
| 1265 | the appropriate state and returns the value it will be interpreted
|
|---|
| 1266 | as.
|
|---|
| 1267 | */
|
|---|
| 1268 |
|
|---|
| 1269 | QVariant QDoubleSpinBoxPrivate::validateAndInterpret(QString &input, int &pos,
|
|---|
| 1270 | QValidator::State &state) const
|
|---|
| 1271 | {
|
|---|
| 1272 | if (cachedText == input && !input.isEmpty()) {
|
|---|
| 1273 | state = cachedState;
|
|---|
| 1274 | QSBDEBUG() << "cachedText was" << "'" << cachedText << "'" << "state was "
|
|---|
| 1275 | << state << " and value was " << cachedValue;
|
|---|
| 1276 | return cachedValue;
|
|---|
| 1277 | }
|
|---|
| 1278 | const double max = maximum.toDouble();
|
|---|
| 1279 | const double min = minimum.toDouble();
|
|---|
| 1280 |
|
|---|
| 1281 | QString copy = stripped(input, &pos);
|
|---|
| 1282 | QSBDEBUG() << "input" << input << "copy" << copy;
|
|---|
| 1283 | int len = copy.size();
|
|---|
| 1284 | double num = min;
|
|---|
| 1285 | const bool plus = max >= 0;
|
|---|
| 1286 | const bool minus = min <= 0;
|
|---|
| 1287 |
|
|---|
| 1288 | switch (len) {
|
|---|
| 1289 | case 0:
|
|---|
| 1290 | state = max != min ? QValidator::Intermediate : QValidator::Invalid;
|
|---|
| 1291 | goto end;
|
|---|
| 1292 | case 1:
|
|---|
| 1293 | if (copy.at(0) == delimiter
|
|---|
| 1294 | || (plus && copy.at(0) == QLatin1Char('+'))
|
|---|
| 1295 | || (minus && copy.at(0) == QLatin1Char('-'))) {
|
|---|
| 1296 | state = QValidator::Intermediate;
|
|---|
| 1297 | goto end;
|
|---|
| 1298 | }
|
|---|
| 1299 | break;
|
|---|
| 1300 | case 2:
|
|---|
| 1301 | if (copy.at(1) == delimiter
|
|---|
| 1302 | && ((plus && copy.at(0) == QLatin1Char('+')) || (minus && copy.at(0) == QLatin1Char('-')))) {
|
|---|
| 1303 | state = QValidator::Intermediate;
|
|---|
| 1304 | goto end;
|
|---|
| 1305 | }
|
|---|
| 1306 | break;
|
|---|
| 1307 | default: break;
|
|---|
| 1308 | }
|
|---|
| 1309 |
|
|---|
| 1310 | if (copy.at(0) == thousand) {
|
|---|
| 1311 | QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid";
|
|---|
| 1312 | state = QValidator::Invalid;
|
|---|
| 1313 | goto end;
|
|---|
| 1314 | } else if (len > 1) {
|
|---|
| 1315 | const int dec = copy.indexOf(delimiter);
|
|---|
| 1316 | if (dec != -1) {
|
|---|
| 1317 | if (dec + 1 < copy.size() && copy.at(dec + 1) == delimiter && pos == dec + 1) {
|
|---|
| 1318 | copy.remove(dec + 1, 1); // typing a delimiter when you are on the delimiter
|
|---|
| 1319 | } // should be treated as typing right arrow
|
|---|
| 1320 |
|
|---|
| 1321 | if (copy.size() - dec > decimals + 1) {
|
|---|
| 1322 | QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid";
|
|---|
| 1323 | state = QValidator::Invalid;
|
|---|
| 1324 | goto end;
|
|---|
| 1325 | }
|
|---|
| 1326 | for (int i=dec + 1; i<copy.size(); ++i) {
|
|---|
| 1327 | if (copy.at(i).isSpace() || copy.at(i) == thousand) {
|
|---|
| 1328 | QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid";
|
|---|
| 1329 | state = QValidator::Invalid;
|
|---|
| 1330 | goto end;
|
|---|
| 1331 | }
|
|---|
| 1332 | }
|
|---|
| 1333 | } else {
|
|---|
| 1334 | const QChar &last = copy.at(len - 1);
|
|---|
| 1335 | const QChar &secondLast = copy.at(len - 2);
|
|---|
| 1336 | if ((last == thousand || last.isSpace())
|
|---|
| 1337 | && (secondLast == thousand || secondLast.isSpace())) {
|
|---|
| 1338 | state = QValidator::Invalid;
|
|---|
| 1339 | QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid";
|
|---|
| 1340 | goto end;
|
|---|
| 1341 | } else if (last.isSpace() && (!thousand.isSpace() || secondLast.isSpace())) {
|
|---|
| 1342 | state = QValidator::Invalid;
|
|---|
| 1343 | QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid";
|
|---|
| 1344 | goto end;
|
|---|
| 1345 | }
|
|---|
| 1346 | }
|
|---|
| 1347 | }
|
|---|
| 1348 |
|
|---|
| 1349 | {
|
|---|
| 1350 | bool ok = false;
|
|---|
| 1351 | QLocale loc(q_func()->locale());
|
|---|
| 1352 | num = loc.toDouble(copy, &ok);
|
|---|
| 1353 | QSBDEBUG() << __FILE__ << __LINE__ << loc << copy << num << ok;
|
|---|
| 1354 | bool notAcceptable = false;
|
|---|
| 1355 |
|
|---|
| 1356 | if (!ok) {
|
|---|
| 1357 | if (thousand.isPrint()) {
|
|---|
| 1358 | if (max < 1000 && min > -1000 && copy.contains(thousand)) {
|
|---|
| 1359 | state = QValidator::Invalid;
|
|---|
| 1360 | QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid";
|
|---|
| 1361 | goto end;
|
|---|
| 1362 | }
|
|---|
| 1363 |
|
|---|
| 1364 | const int len = copy.size();
|
|---|
| 1365 | for (int i=0; i<len- 1; ++i) {
|
|---|
| 1366 | if (copy.at(i) == thousand && copy.at(i + 1) == thousand) {
|
|---|
| 1367 | QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid";
|
|---|
| 1368 | state = QValidator::Invalid;
|
|---|
| 1369 | goto end;
|
|---|
| 1370 | }
|
|---|
| 1371 | }
|
|---|
| 1372 |
|
|---|
| 1373 | const int s = copy.size();
|
|---|
| 1374 | copy.remove(thousand);
|
|---|
| 1375 | pos = qMax(0, pos - (s - copy.size()));
|
|---|
| 1376 |
|
|---|
| 1377 |
|
|---|
| 1378 | num = loc.toDouble(copy, &ok);
|
|---|
| 1379 | QSBDEBUG() << thousand << num << copy << ok;
|
|---|
| 1380 |
|
|---|
| 1381 | if (!ok) {
|
|---|
| 1382 | state = QValidator::Invalid;
|
|---|
| 1383 | QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid";
|
|---|
| 1384 | goto end;
|
|---|
| 1385 | }
|
|---|
| 1386 | notAcceptable = true;
|
|---|
| 1387 | }
|
|---|
| 1388 | }
|
|---|
| 1389 |
|
|---|
| 1390 | if (!ok) {
|
|---|
| 1391 | state = QValidator::Invalid;
|
|---|
| 1392 | QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid";
|
|---|
| 1393 | } else if (num >= min && num <= max) {
|
|---|
| 1394 | state = notAcceptable ? QValidator::Intermediate : QValidator::Acceptable;
|
|---|
| 1395 | QSBDEBUG() << __FILE__ << __LINE__<< "state is set to "
|
|---|
| 1396 | << (state == QValidator::Intermediate ? "Intermediate" : "Acceptable");
|
|---|
| 1397 | } else if (max == min) { // when max and min is the same the only non-Invalid input is max (or min)
|
|---|
| 1398 | state = QValidator::Invalid;
|
|---|
| 1399 | QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid";
|
|---|
| 1400 | } else {
|
|---|
| 1401 | if ((num >= 0 && num > max) || (num < 0 && num < min)) {
|
|---|
| 1402 | state = QValidator::Invalid;
|
|---|
| 1403 | QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid";
|
|---|
| 1404 | } else {
|
|---|
| 1405 | state = isIntermediateValue(copy) ? QValidator::Intermediate : QValidator::Invalid;
|
|---|
| 1406 | QSBDEBUG() << __FILE__ << __LINE__<< "state is set to "
|
|---|
| 1407 | << (state == QValidator::Intermediate ? "Intermediate" : "Acceptable");
|
|---|
| 1408 | }
|
|---|
| 1409 | }
|
|---|
| 1410 | }
|
|---|
| 1411 |
|
|---|
| 1412 | end:
|
|---|
| 1413 | if (state != QValidator::Acceptable) {
|
|---|
| 1414 | num = max > 0 ? min : max;
|
|---|
| 1415 | }
|
|---|
| 1416 |
|
|---|
| 1417 | input = prefix + copy + suffix;
|
|---|
| 1418 | cachedText = input;
|
|---|
| 1419 | cachedState = state;
|
|---|
| 1420 | cachedValue = QVariant(num);
|
|---|
| 1421 | return QVariant(num);
|
|---|
| 1422 | }
|
|---|
| 1423 |
|
|---|
| 1424 | /*
|
|---|
| 1425 | \internal
|
|---|
| 1426 | \reimp
|
|---|
| 1427 | */
|
|---|
| 1428 |
|
|---|
| 1429 | QString QDoubleSpinBoxPrivate::textFromValue(const QVariant &f) const
|
|---|
| 1430 | {
|
|---|
| 1431 | Q_Q(const QDoubleSpinBox);
|
|---|
| 1432 | return q->textFromValue(f.toDouble());
|
|---|
| 1433 | }
|
|---|
| 1434 |
|
|---|
| 1435 | /*!
|
|---|
| 1436 | \fn void QSpinBox::setLineStep(int step)
|
|---|
| 1437 |
|
|---|
| 1438 | Use setSingleStep() instead.
|
|---|
| 1439 | */
|
|---|
| 1440 |
|
|---|
| 1441 | /*!
|
|---|
| 1442 | \fn void QSpinBox::setMaxValue(int value)
|
|---|
| 1443 |
|
|---|
| 1444 | Use setMaximum() instead.
|
|---|
| 1445 | */
|
|---|
| 1446 |
|
|---|
| 1447 | /*!
|
|---|
| 1448 | \fn void QSpinBox::setMinValue(int value)
|
|---|
| 1449 |
|
|---|
| 1450 | Use setMinimum() instead.
|
|---|
| 1451 | */
|
|---|
| 1452 |
|
|---|
| 1453 | /*!
|
|---|
| 1454 | \fn int QSpinBox::maxValue() const
|
|---|
| 1455 |
|
|---|
| 1456 | Use maximum() instead.
|
|---|
| 1457 | */
|
|---|
| 1458 |
|
|---|
| 1459 | /*!
|
|---|
| 1460 | \fn int QSpinBox::minValue() const
|
|---|
| 1461 |
|
|---|
| 1462 | Use minimum() instead.
|
|---|
| 1463 | */
|
|---|
| 1464 |
|
|---|
| 1465 | /*!
|
|---|
| 1466 | \internal Returns whether \a str is a string which value cannot be
|
|---|
| 1467 | parsed but still might turn into something valid.
|
|---|
| 1468 | */
|
|---|
| 1469 |
|
|---|
| 1470 | static bool isIntermediateValueHelper(qint64 num, qint64 min, qint64 max, qint64 *match)
|
|---|
| 1471 | {
|
|---|
| 1472 | QSBDEBUG("%lld %lld %lld", num, min, max);
|
|---|
| 1473 |
|
|---|
| 1474 | if (num >= min && num <= max) {
|
|---|
| 1475 | if (match)
|
|---|
| 1476 | *match = num;
|
|---|
| 1477 | QSBDEBUG("returns true 0");
|
|---|
| 1478 | return true;
|
|---|
| 1479 | }
|
|---|
| 1480 | qint64 tmp = num;
|
|---|
| 1481 |
|
|---|
| 1482 | int numDigits = 0;
|
|---|
| 1483 | int digits[10];
|
|---|
| 1484 | if (tmp == 0) {
|
|---|
| 1485 | numDigits = 1;
|
|---|
| 1486 | digits[0] = 0;
|
|---|
| 1487 | } else {
|
|---|
| 1488 | tmp = qAbs(num);
|
|---|
| 1489 | for (int i=0; tmp > 0; ++i) {
|
|---|
| 1490 | digits[numDigits++] = tmp % 10;
|
|---|
| 1491 | tmp /= 10;
|
|---|
| 1492 | }
|
|---|
| 1493 | }
|
|---|
| 1494 |
|
|---|
| 1495 | int failures = 0;
|
|---|
| 1496 | qint64 number;
|
|---|
| 1497 | for (number=max; number>=min; --number) {
|
|---|
| 1498 | tmp = qAbs(number);
|
|---|
| 1499 | for (int i=0; tmp > 0;) {
|
|---|
| 1500 | if (digits[i] == (tmp % 10)) {
|
|---|
| 1501 | if (++i == numDigits) {
|
|---|
| 1502 | if (match)
|
|---|
| 1503 | *match = number;
|
|---|
| 1504 | QSBDEBUG("returns true 1");
|
|---|
| 1505 | return true;
|
|---|
| 1506 | }
|
|---|
| 1507 | }
|
|---|
| 1508 | tmp /= 10;
|
|---|
| 1509 | }
|
|---|
| 1510 | if (failures++ == 500000) { //upper bound
|
|---|
| 1511 | if (match)
|
|---|
| 1512 | *match = num;
|
|---|
| 1513 | QSBDEBUG("returns true 2");
|
|---|
| 1514 | return true;
|
|---|
| 1515 | }
|
|---|
| 1516 | }
|
|---|
| 1517 | QSBDEBUG("returns false");
|
|---|
| 1518 | return false;
|
|---|
| 1519 | }
|
|---|
| 1520 |
|
|---|
| 1521 | /*! \reimp */
|
|---|
| 1522 | bool QSpinBox::event(QEvent *event)
|
|---|
| 1523 | {
|
|---|
| 1524 | Q_D(QSpinBox);
|
|---|
| 1525 | if (event->type() == QEvent::StyleChange
|
|---|
| 1526 | #ifdef Q_WS_MAC
|
|---|
| 1527 | || event->type() == QEvent::MacSizeChange
|
|---|
| 1528 | #endif
|
|---|
| 1529 | )
|
|---|
| 1530 | d->setLayoutItemMargins(QStyle::SE_SpinBoxLayoutItem);
|
|---|
| 1531 | return QAbstractSpinBox::event(event);
|
|---|
| 1532 | }
|
|---|
| 1533 |
|
|---|
| 1534 | QT_END_NAMESPACE
|
|---|
| 1535 |
|
|---|
| 1536 | #endif // QT_NO_SPINBOX
|
|---|