dtkDoubleSpinBox.cpp 4.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Version: $Id$
//
//

// Commentary:
//
//

// Change Log:
//
//

// Code:

#include "dtkDoubleSpinBox.h"

SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
17
class dtkDoubleSpinBoxPrivate : public QObject
18
{
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
19 20 21 22 23 24
    Q_OBJECT

public slots:
    void update();
    void touch();

25
public:
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
26 27
    double            value;

28 29
    QDoubleValidator *validator;
    bool              scientific;
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
30 31 32
    int               decimals;
    double            minimum;
    double            maximum;
33

SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
34 35 36
public:
    QString textFromValue ( double ) const;
    double  valueFromText ( const QString& ) const;
37

SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
38
    QValidator::State validate(QString&, int&) const;
39 40


SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
41 42 43 44 45 46 47 48
public:
    dtkDoubleSpinBox *q;
};

void dtkDoubleSpinBoxPrivate::update(void)
{
    this->value = valueFromText( q->text());
    this->touch();
49 50
}

SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
51
void dtkDoubleSpinBoxPrivate::touch()
52
{
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
53
    q->emit valueChanged(this->value);
54 55
}

SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
56
QString dtkDoubleSpinBoxPrivate::textFromValue ( double value ) const
57 58
{
    QString strValue;
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
59 60
    if (this->scientific)
        strValue = QString::number(value, 'e', this->decimals);
61
    else
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
62
        strValue = QString::number(value, 'f', this->decimals);
63 64 65 66

    return strValue;
}

SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
67 68
#include <cstdlib>
double dtkDoubleSpinBoxPrivate::valueFromText ( const QString & text ) const
69
{
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    // truncate the input text to the right decimal
    QString work_on = text;

    QStringList mant_exp;
    if ( this->scientific ) { // split mantisse + exp
        mant_exp = text.split(QRegExp("e"));
        work_on = mant_exp[0];
    }

    QStringList int_dec;
    int_dec = work_on.split(QRegExp("\\."));

    // truncate decimals
    if (int_dec.size() > 1) {
        if ( int_dec[1].length() > this->decimals) {
            int_dec[1] = int_dec[1].left(this->decimals);
        }
    }

    QString result = int_dec[0] + "." + int_dec[1];
    if ( this->scientific && mant_exp.size() > 1) {
        result += "e" + mant_exp[1];
    }
    else {
        result += "e+0" ;
    }
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
96

SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
97 98 99 100 101
    double dblValue = result.toDouble();
    //q->lineEdit()->blockSignals(true);  // protected !!
    //q->lineEdit()->setText(result);
    //q->lineEdit()->blockSignals(false);
    q->setValue(dblValue);
102 103 104
    return dblValue;
}

SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
105
QValidator::State dtkDoubleSpinBoxPrivate::validate(QString &text, int &pos) const
106 107 108 109 110 111 112
{
    // check user input
    bool input_check;
    text.toDouble(&input_check);

    return (input_check ? QValidator::Acceptable : QValidator::Invalid);
}
113

SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

///

dtkDoubleSpinBox::dtkDoubleSpinBox(QDoubleValidator::Notation notation, QWidget *parent) : QAbstractSpinBox(parent)
{
    d = new dtkDoubleSpinBoxPrivate;
    d->q = this;

    this->setLocale(QLocale::C);

    d->value = 0.0;

    d->validator = new QDoubleValidator();
    d->validator->setNotation(notation);

    d->scientific = ( notation == QDoubleValidator::ScientificNotation);

    //this->setMaximum(std::numeric_limits<double>::max());    // instead of default 99.99
    //this->setMaximum(std::numeric_limits<double>::lowest());
    //this->setDecimals(32);
    d->maximum = std::numeric_limits<double>::max();    // instead of default 99.99
    d->minimum = std::numeric_limits<double>::lowest();
    d->decimals = 32;

    this->setReadOnly(false);
    this->setButtonSymbols(QAbstractSpinBox::PlusMinus);
    this->setCorrectionMode(QAbstractSpinBox::CorrectToPreviousValue);
    this->setKeyboardTracking(true);
    this->lineEdit()->setValidator(d->validator);
    this->lineEdit()->setReadOnly(false);

    connect(this,
            SIGNAL(editingFinished()),
            d,
            SLOT(update()));

}

dtkDoubleSpinBox::~dtkDoubleSpinBox(void)
{
    delete d->validator;
    delete d;
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
void dtkDoubleSpinBox::stepBy(int step)
{
    if (step == 0 ) {
        return;
    }
    if (step < 0) {
        int i = step;
        while ( i < 0 ) {
            stepDown();
            i++;
        }
    }
    else  {
        int i = step;
        while ( i > 0) {
            stepUp();
            i--;
        }
    }
}

void dtkDoubleSpinBox::stepDown(void)
{
    if (d->scientific)
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
182
        d->value /= 10.0;
183
    else
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
184 185 186
        d->value -= 1.0;

    d->touch();
187 188 189 190 191
}

void dtkDoubleSpinBox::stepUp(void)
{
    if (d->scientific)
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
192
        d->value *= 10.0;
193
    else
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
        d->value += 1.0;

    d->touch();
}


void dtkDoubleSpinBox::setMinimum(double min)
{
    d->minimum = min;
}

void dtkDoubleSpinBox::setMaximum(double max)
{
    d->maximum = max;
}

void dtkDoubleSpinBox::setValue(double val)
{
    d->value = val;
    this->lineEdit()->blockSignals(true);
    this->lineEdit()->setText(d->textFromValue(val));
    this->lineEdit()->blockSignals(false);
216
}
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

void dtkDoubleSpinBox::setDecimals(int dec)
{
    d->decimals = dec;
    this->setValue(d->value);  // muste change the text in the widget accordingly
}

QAbstractSpinBox::StepEnabled dtkDoubleSpinBox::stepEnabled(void) const
{
    return ( QAbstractSpinBox::StepUpEnabled | QAbstractSpinBox::StepDownEnabled ) ;
}


#include "dtkDoubleSpinBox.moc"

232 233
//
// dtkDoubleSpinBox.h ends here