src/gui/widgets/qspinbox.cpp
changeset 7 f7bc934e204c
parent 3 41300fa6a67c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
     1 /****************************************************************************
     1 /****************************************************************************
     2 **
     2 **
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the QtGui module of the Qt Toolkit.
     7 ** This file is part of the QtGui module of the Qt Toolkit.
     8 **
     8 **
    97 
    97 
    98     inline void init() {
    98     inline void init() {
    99         Q_Q(QDoubleSpinBox);
    99         Q_Q(QDoubleSpinBox);
   100         q->setInputMethodHints(Qt::ImhFormattedNumbersOnly);
   100         q->setInputMethodHints(Qt::ImhFormattedNumbersOnly);
   101     }
   101     }
       
   102 
       
   103     // When fiddling with the decimals property, we may lose precision in these properties.
       
   104     double actualMin;
       
   105     double actualMax;
   102 };
   106 };
   103 
   107 
   104 
   108 
   105 /*!
   109 /*!
   106     \class QSpinBox
   110     \class QSpinBox
   442     Q_D(QSpinBox);
   446     Q_D(QSpinBox);
   443     d->setRange(QVariant(minimum), QVariant(maximum));
   447     d->setRange(QVariant(minimum), QVariant(maximum));
   444 }
   448 }
   445 
   449 
   446 /*!
   450 /*!
   447     This virtual function is used by the spin box whenever it needs
   451     This virtual function is used by the spin box whenever it needs to
   448     to display the given \a value. The default implementation returns
   452     display the given \a value. The default implementation returns a
   449     a string containing \a value printed in the standard way using
   453     string containing \a value printed in the standard way using
   450     QWidget::locale().toString(). Reimplementations may return anything. (See
   454     QWidget::locale().toString(), but with the thousand separator
   451     the example in the detailed description.)
   455     removed. Reimplementations may return anything. (See the example
       
   456     in the detailed description.)
   452 
   457 
   453     Note: QSpinBox does not call this function for specialValueText()
   458     Note: QSpinBox does not call this function for specialValueText()
   454     and that neither prefix() nor suffix() should be included in the
   459     and that neither prefix() nor suffix() should be included in the
   455     return value.
   460     return value.
   456 
   461 
   457     If you reimplement this, you may also need to reimplement
   462     If you reimplement this, you may also need to reimplement
   458     valueFromText() and validate()
   463     valueFromText() and validate()
   459 
   464 
   460     \sa valueFromText(), validate()
   465     \sa valueFromText(), validate(), QLocale::groupSeparator()
   461 */
   466 */
   462 
   467 
   463 QString QSpinBox::textFromValue(int value) const
   468 QString QSpinBox::textFromValue(int value) const
   464 {
   469 {
   465     QString str = locale().toString(value);
   470     QString str = locale().toString(value);
   760 }
   765 }
   761 
   766 
   762 void QDoubleSpinBox::setMinimum(double minimum)
   767 void QDoubleSpinBox::setMinimum(double minimum)
   763 {
   768 {
   764     Q_D(QDoubleSpinBox);
   769     Q_D(QDoubleSpinBox);
       
   770     d->actualMin = minimum;
   765     const QVariant m(d->round(minimum));
   771     const QVariant m(d->round(minimum));
   766     d->setRange(m, (d->variantCompare(d->maximum, m) > 0 ? d->maximum : m));
   772     d->setRange(m, (d->variantCompare(d->maximum, m) > 0 ? d->maximum : m));
   767 }
   773 }
   768 
   774 
   769 /*!
   775 /*!
   790 }
   796 }
   791 
   797 
   792 void QDoubleSpinBox::setMaximum(double maximum)
   798 void QDoubleSpinBox::setMaximum(double maximum)
   793 {
   799 {
   794     Q_D(QDoubleSpinBox);
   800     Q_D(QDoubleSpinBox);
       
   801     d->actualMax = maximum;
   795     const QVariant m(d->round(maximum));
   802     const QVariant m(d->round(maximum));
   796     d->setRange((d->variantCompare(d->minimum, m) < 0 ? d->minimum : m), m);
   803     d->setRange((d->variantCompare(d->minimum, m) < 0 ? d->minimum : m), m);
   797 }
   804 }
   798 
   805 
   799 /*!
   806 /*!
   811 */
   818 */
   812 
   819 
   813 void QDoubleSpinBox::setRange(double minimum, double maximum)
   820 void QDoubleSpinBox::setRange(double minimum, double maximum)
   814 {
   821 {
   815     Q_D(QDoubleSpinBox);
   822     Q_D(QDoubleSpinBox);
       
   823     d->actualMin = minimum;
       
   824     d->actualMax = maximum;
   816     d->setRange(QVariant(d->round(minimum)), QVariant(d->round(maximum)));
   825     d->setRange(QVariant(d->round(minimum)), QVariant(d->round(maximum)));
   817 }
   826 }
   818 
   827 
   819 /*!
   828 /*!
   820      \property QDoubleSpinBox::decimals
   829      \property QDoubleSpinBox::decimals
   841 void QDoubleSpinBox::setDecimals(int decimals)
   850 void QDoubleSpinBox::setDecimals(int decimals)
   842 {
   851 {
   843     Q_D(QDoubleSpinBox);
   852     Q_D(QDoubleSpinBox);
   844     d->decimals = qBound(0, decimals, DBL_MAX_10_EXP + DBL_DIG);
   853     d->decimals = qBound(0, decimals, DBL_MAX_10_EXP + DBL_DIG);
   845 
   854 
   846     setRange(minimum(), maximum()); // make sure values are rounded
   855     setRange(d->actualMin, d->actualMax); // make sure values are rounded
   847     setValue(value());
   856     setValue(value());
   848 }
   857 }
   849 
   858 
   850 /*!
   859 /*!
   851     This virtual function is used by the spin box whenever it needs to
   860     This virtual function is used by the spin box whenever it needs to
   859     be included in the return value.
   868     be included in the return value.
   860 
   869 
   861     If you reimplement this, you may also need to reimplement
   870     If you reimplement this, you may also need to reimplement
   862     valueFromText().
   871     valueFromText().
   863 
   872 
   864     \sa valueFromText()
   873     \sa valueFromText(), QLocale::groupSeparator()
   865 */
   874 */
   866 
   875 
   867 
   876 
   868 QString QDoubleSpinBox::textFromValue(double value) const
   877 QString QDoubleSpinBox::textFromValue(double value) const
   869 {
   878 {
  1049     Constructs a QSpinBoxPrivate object
  1058     Constructs a QSpinBoxPrivate object
  1050 */
  1059 */
  1051 
  1060 
  1052 QDoubleSpinBoxPrivate::QDoubleSpinBoxPrivate()
  1061 QDoubleSpinBoxPrivate::QDoubleSpinBoxPrivate()
  1053 {
  1062 {
  1054     minimum = QVariant(0.0);
  1063     actualMin = 0.0;
  1055     maximum = QVariant(99.99);
  1064     actualMax = 99.99;
       
  1065     minimum = QVariant(actualMin);
       
  1066     maximum = QVariant(actualMax);
  1056     value = minimum;
  1067     value = minimum;
  1057     singleStep = QVariant(1.0);
  1068     singleStep = QVariant(1.0);
  1058     decimals = 2;
  1069     decimals = 2;
  1059     type = QVariant::Double;
  1070     type = QVariant::Double;
  1060 }
  1071 }