Public Slots | Signals | Public Member Functions | Protected Member Functions | Properties | Friends

QxtBaseSpinBox Class Reference
[QxtGui]

A spin box with support for numbers in base between 2 and 36. More...

#include <qxtbasespinbox.h>

Collaboration diagram for QxtBaseSpinBox:
Collaboration graph
[legend]

List of all members.

Public Slots

void setBase (int base)

Signals

void baseChanged (int base)

Public Member Functions

 QxtBaseSpinBox (QWidget *parent=0)
 QxtBaseSpinBox (int base, QWidget *parent=0)
virtual ~QxtBaseSpinBox ()
virtual void fixup (QString &input) const
virtual QValidator::State validate (QString &input, int &pos) const
bool isUpperCase () const
void setUpperCase (bool upperCase)
int base () const

Protected Member Functions

virtual QString textFromValue (int value) const
virtual int valueFromText (const QString &text) const

Properties

int base
 This property holds the number base.
bool upperCase
 This property holds whether letters are shown in upper case.

Friends

class QxtBaseSpinBoxPrivate

Detailed Description

A spin box with support for numbers in base between 2 and 36.

Example spinbox for hexadecimal input:

    QxtBaseSpinBox* spinBox = new QxtBaseSpinBox(16, this);
    spinBox->setPrefix("0x");
    spinBox->setValue(0xbabe);
    spinBox->setUpperCase(true);
qxtbasespinbox.png

QxtBaseSpinBox in action.

Note:
Notice that QxtBaseSpinBox is not locale-aware.

Definition at line 34 of file qxtbasespinbox.h.


Constructor & Destructor Documentation

QxtBaseSpinBox::QxtBaseSpinBox ( QWidget *  parent = 0 ) [explicit]

Constructs a new QxtBaseSpinBox with parent. Base defaults to 10.

Definition at line 68 of file qxtbasespinbox.cpp.

References QXT_INIT_PRIVATE.

                                              : QSpinBox(parent)
{
    QXT_INIT_PRIVATE(QxtBaseSpinBox);
}
QxtBaseSpinBox::QxtBaseSpinBox ( int  base,
QWidget *  parent = 0 
) [explicit]

Constructs a new QxtBaseSpinBox with base and parent.

Definition at line 76 of file qxtbasespinbox.cpp.

References base(), and QXT_INIT_PRIVATE.

                                                        : QSpinBox(parent)
{
    QXT_INIT_PRIVATE(QxtBaseSpinBox);
    qxt_d().base = base;
}
QxtBaseSpinBox::~QxtBaseSpinBox (  ) [virtual]

Destructs the spin box.

Definition at line 85 of file qxtbasespinbox.cpp.

{
}

Member Function Documentation

int QxtBaseSpinBox::base (  ) const
QxtBaseSpinBox::baseChanged ( int  base ) [signal]

This signal is emitted whenever the number base has changed.

Referenced by setBase().

void QxtBaseSpinBox::fixup ( QString &  input ) const [virtual]

Definition at line 92 of file qxtbasespinbox.cpp.

{
    QString inputWithoutPrefix = input.mid(prefix().length());
    inputWithoutPrefix = qxt_d().upper ? inputWithoutPrefix.toUpper() : inputWithoutPrefix.toLower();
    input = prefix() + inputWithoutPrefix;
}
bool QxtBaseSpinBox::isUpperCase (  ) const

Definition at line 185 of file qxtbasespinbox.cpp.

{
    return qxt_d().upper;
}
void QxtBaseSpinBox::setBase ( int  base ) [slot]

Definition at line 163 of file qxtbasespinbox.cpp.

References base(), and baseChanged().

{
    if (base < 2 || base > 36)
        qWarning("QxtBaseSpinBox: base must be between 2 and 36");

    base = qBound(2, base, 36);
    if (qxt_d().base != base)
    {
        qxt_d().base = base;
        emit baseChanged(base);
        setValue(value());
    }
}
void QxtBaseSpinBox::setUpperCase ( bool  upperCase )

Definition at line 190 of file qxtbasespinbox.cpp.

References upperCase.

{
    if (qxt_d().upper != upperCase)
    {
        qxt_d().upper = upperCase;
        setValue(value());
    }
}
QString QxtBaseSpinBox::textFromValue ( int  value ) const [protected, virtual]

Definition at line 202 of file qxtbasespinbox.cpp.

References base().

{
    QString text = QString::number(value, qxt_d().base);
    if (qxt_d().upper)
        return text.toUpper();
    return text;
}
QValidator::State QxtBaseSpinBox::validate ( QString &  input,
int &  pos 
) const [virtual]

Definition at line 102 of file qxtbasespinbox.cpp.

References base(), QuadProgPP::max(), and min.

{
    // quick rejects
    const QString prefix = QSpinBox::prefix();
    const QString inputWithoutPrefix = input.mid(prefix.length());
    if (pos < prefix.length())
    {
        // do not let modify prefix
        return QValidator::Invalid;
    }
    else if (inputWithoutPrefix.isEmpty())
    {
        // allow empty input => intermediate
        return QValidator::Intermediate;
    }

    // Invalid:      input is invalid according to the string list
    // Intermediate: it is likely that a little more editing will make the input acceptable
    // Acceptable:   the input is valid.
    Q_UNUSED(pos);

    bool ok = false;
    const int min = minimum();
    const int max = maximum();
    const int number = inputWithoutPrefix.toInt(&ok, qxt_d().base);

    QValidator::State state = QValidator::Invalid;
    if (!ok)
    {
        // cannot convert => invalid
        state = QValidator::Invalid;
    }
    else if (number >= min && number <= max)
    {
        // converts ok, between boundaries => acceptable if case matches
        if (qxt_d().upper)
            return (input == prefix + inputWithoutPrefix.toUpper() ? QValidator::Acceptable : QValidator::Intermediate);
        else
            return (input == prefix + inputWithoutPrefix.toLower() ? QValidator::Acceptable : QValidator::Intermediate);
    }
    else
    {
        // converts ok, outside boundaries => intermediate
        state = QValidator::Intermediate;
    }
    return state;
}
int QxtBaseSpinBox::valueFromText ( const QString &  text ) const [protected, virtual]

Definition at line 213 of file qxtbasespinbox.cpp.

References base().

{
    return text.toInt(0, qxt_d().base);
}

Friends And Related Function Documentation

friend class QxtBaseSpinBoxPrivate [friend]

Definition at line 37 of file qxtbasespinbox.h.


Property Documentation

int QxtBaseSpinBox::base [read, write]

This property holds the number base.

The base must be between 2 and 36.

The default value is 10.

Definition at line 37 of file qxtbasespinbox.h.

QxtBaseSpinBox::upperCase [read, write]

This property holds whether letters are shown in upper case.

Naturally, this applies to only bases which can contain letters.

The default value is false.

Definition at line 39 of file qxtbasespinbox.h.

Referenced by setUpperCase().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines