summaryrefslogtreecommitdiffstats
path: root/src/widget/hexspinbox.cpp
blob: e3f25c4c533d17893d6627ae54d271f3dcf7caf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <qvalidator.h>

#include "hexspinbox.h"

HexSpinBox::HexSpinBox(QWidget *parent)
    : QSpinBox(parent)
{

    setRange(0, 255);
}

QString HexSpinBox::textFromValue(int value) const 
{
    //Construct a hex string formatted like 0x0f. 
    return QString("0x") + QString("%1").arg(value, 
                                2,   //Field width (makes "F" become "0F")
                                16, 
                                QLatin1Char('0')).toUpper();
}

int HexSpinBox::valueFromText(const QString& text) const
{
    bool ok;
    return text.toInt(&ok, 16);
}

QValidator::State HexSpinBox::validate ( QString & input, int & pos ) const
{
    const QRegExp regExp("^0(x|X)[0-9A-Fa-f]+");
    QRegExpValidator validator(regExp, NULL); 
    return validator.validate(input, pos);
}