Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "qxtbasespinbox.h"
00026
00027 class QxtBaseSpinBoxPrivate : public QxtPrivate<QxtBaseSpinBox>
00028 {
00029 public:
00030 QXT_DECLARE_PUBLIC(QxtBaseSpinBox);
00031 QxtBaseSpinBoxPrivate();
00032
00033 int base;
00034 bool upper;
00035 };
00036
00037 QxtBaseSpinBoxPrivate::QxtBaseSpinBoxPrivate() : base(10), upper(false)
00038 {
00039 }
00040
00068 QxtBaseSpinBox::QxtBaseSpinBox(QWidget* parent) : QSpinBox(parent)
00069 {
00070 QXT_INIT_PRIVATE(QxtBaseSpinBox);
00071 }
00072
00076 QxtBaseSpinBox::QxtBaseSpinBox(int base, QWidget* parent) : QSpinBox(parent)
00077 {
00078 QXT_INIT_PRIVATE(QxtBaseSpinBox);
00079 qxt_d().base = base;
00080 }
00081
00085 QxtBaseSpinBox::~QxtBaseSpinBox()
00086 {
00087 }
00088
00092 void QxtBaseSpinBox::fixup(QString& input) const
00093 {
00094 QString inputWithoutPrefix = input.mid(prefix().length());
00095 inputWithoutPrefix = qxt_d().upper ? inputWithoutPrefix.toUpper() : inputWithoutPrefix.toLower();
00096 input = prefix() + inputWithoutPrefix;
00097 }
00098
00102 QValidator::State QxtBaseSpinBox::validate(QString& input, int& pos) const
00103 {
00104
00105 const QString prefix = QSpinBox::prefix();
00106 const QString inputWithoutPrefix = input.mid(prefix.length());
00107 if (pos < prefix.length())
00108 {
00109
00110 return QValidator::Invalid;
00111 }
00112 else if (inputWithoutPrefix.isEmpty())
00113 {
00114
00115 return QValidator::Intermediate;
00116 }
00117
00118
00119
00120
00121 Q_UNUSED(pos);
00122
00123 bool ok = false;
00124 const int min = minimum();
00125 const int max = maximum();
00126 const int number = inputWithoutPrefix.toInt(&ok, qxt_d().base);
00127
00128 QValidator::State state = QValidator::Invalid;
00129 if (!ok)
00130 {
00131
00132 state = QValidator::Invalid;
00133 }
00134 else if (number >= min && number <= max)
00135 {
00136
00137 if (qxt_d().upper)
00138 return (input == prefix + inputWithoutPrefix.toUpper() ? QValidator::Acceptable : QValidator::Intermediate);
00139 else
00140 return (input == prefix + inputWithoutPrefix.toLower() ? QValidator::Acceptable : QValidator::Intermediate);
00141 }
00142 else
00143 {
00144
00145 state = QValidator::Intermediate;
00146 }
00147 return state;
00148 }
00149
00158 int QxtBaseSpinBox::base() const
00159 {
00160 return qxt_d().base;
00161 }
00162
00163 void QxtBaseSpinBox::setBase(int base)
00164 {
00165 if (base < 2 || base > 36)
00166 qWarning("QxtBaseSpinBox: base must be between 2 and 36");
00167
00168 base = qBound(2, base, 36);
00169 if (qxt_d().base != base)
00170 {
00171 qxt_d().base = base;
00172 emit baseChanged(base);
00173 setValue(value());
00174 }
00175 }
00176
00185 bool QxtBaseSpinBox::isUpperCase() const
00186 {
00187 return qxt_d().upper;
00188 }
00189
00190 void QxtBaseSpinBox::setUpperCase(bool upperCase)
00191 {
00192 if (qxt_d().upper != upperCase)
00193 {
00194 qxt_d().upper = upperCase;
00195 setValue(value());
00196 }
00197 }
00198
00202 QString QxtBaseSpinBox::textFromValue(int value) const
00203 {
00204 QString text = QString::number(value, qxt_d().base);
00205 if (qxt_d().upper)
00206 return text.toUpper();
00207 return text;
00208 }
00209
00213 int QxtBaseSpinBox::valueFromText(const QString& text) const
00214 {
00215 return text.toInt(0, qxt_d().base);
00216 }