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

// Commentary:
//
//

// Change Log:
//
//

// Code:

15
#include "dtkWidgetsSpinBoxDouble.h"
16

17 18 19 20 21 22
#include <QtWidgets>

// ///////////////////////////////////////////////////////////////////
//
// ///////////////////////////////////////////////////////////////////

23
class dtkWidgetsSpinBoxDoublePrivate : public QObject
24
{
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
25 26 27 28 29 30
    Q_OBJECT

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

31
public:
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
32 33
    double            value;

34 35
    QDoubleValidator *validator;
    bool              scientific;
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
36 37 38
    int               decimals;
    double            minimum;
    double            maximum;
39

SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
40 41 42
public:
    QString textFromValue ( double ) const;
    double  valueFromText ( const QString& ) const;
43

SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
44
    QValidator::State validate(QString&, int&) const;
45 46


SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
47
public:
48
    dtkWidgetsSpinBoxDouble *q;
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
49 50
};

51
void dtkWidgetsSpinBoxDoublePrivate::update(void)
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
52 53 54
{
    this->value = valueFromText( q->text());
    this->touch();
55 56
}

57
void dtkWidgetsSpinBoxDoublePrivate::touch()
58
{
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
59
    q->emit valueChanged(this->value);
60 61
}

62
QString dtkWidgetsSpinBoxDoublePrivate::textFromValue ( double value ) const
63 64
{
    QString strValue;
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
65 66 67 68 69 70 71

    if (value < this->minimum)
        value = this->minimum;

    if (value > this->maximum)
        value = this->maximum;

SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
72 73
    if (this->scientific)
        strValue = QString::number(value, 'e', this->decimals);
74
    else
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
75
        strValue = QString::number(value, 'f', this->decimals);
76 77 78 79

    return strValue;
}

80
double dtkWidgetsSpinBoxDoublePrivate::valueFromText ( const QString & text ) const
81
{
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    // 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];
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
102 103 104 105 106 107 108
    if ( this->scientific ) {
        if (mant_exp.size() > 1) {
            result += "e" + mant_exp[1];
        }
        else {
            result += "e+0" ;
        }
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
109
    }
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
110

SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
111
    double dblValue = result.toDouble();
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
112 113 114 115 116 117 118

    // check min/max ? What if decimals or min and max are inadequate ?
    if (dblValue < this->minimum)
        dblValue = this->minimum;
    if (dblValue > this->maximum)
        dblValue = this->maximum;

SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
119 120 121 122
    //q->lineEdit()->blockSignals(true);  // protected !!
    //q->lineEdit()->setText(result);
    //q->lineEdit()->blockSignals(false);
    q->setValue(dblValue);
123 124 125
    return dblValue;
}

126
QValidator::State dtkWidgetsSpinBoxDoublePrivate::validate(QString &text, int &pos) const
127 128 129 130 131 132 133
{
    // check user input
    bool input_check;
    text.toDouble(&input_check);

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

135 136 137
// ///////////////////////////////////////////////////////////////////
//
// ///////////////////////////////////////////////////////////////////
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
138

139
dtkWidgetsSpinBoxDouble::dtkWidgetsSpinBoxDouble(QDoubleValidator::Notation notation, QWidget *parent) : QAbstractSpinBox(parent), d(new dtkWidgetsSpinBoxDoublePrivate)
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
{
    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()));

}

173
dtkWidgetsSpinBoxDouble::~dtkWidgetsSpinBoxDouble(void)
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
174 175 176 177 178
{
    delete d->validator;
    delete d;
}

179
void dtkWidgetsSpinBoxDouble::stepBy(int step)
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
{
    if (step == 0 ) {
        return;
    }
    if (step < 0) {
        int i = step;
        while ( i < 0 ) {
            stepDown();
            i++;
        }
    }
    else  {
        int i = step;
        while ( i > 0) {
            stepUp();
            i--;
        }
    }
}

200
void dtkWidgetsSpinBoxDouble::stepDown(void)
201 202
{
    if (d->scientific)
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
203
        d->value /= 10.0;
204
    else
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
205 206
        d->value -= 1.0;

CABEL Tristan's avatar
CABEL Tristan committed
207 208 209
    if(d->value < d->minimum)
        d->value = d->minimum;

SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
210
    d->touch();
211 212
}

213
void dtkWidgetsSpinBoxDouble::stepUp(void)
214 215
{
    if (d->scientific)
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
216
        d->value *= 10.0;
217
    else
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
218 219
        d->value += 1.0;

CABEL Tristan's avatar
CABEL Tristan committed
220 221 222
    if(d->value > d->maximum)
        d->value = d->maximum;

SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
223 224 225 226
    d->touch();
}


227
void dtkWidgetsSpinBoxDouble::setMinimum(double min)
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
228 229 230 231
{
    d->minimum = min;
}

232
void dtkWidgetsSpinBoxDouble::setMaximum(double max)
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
233 234 235 236
{
    d->maximum = max;
}

237
void dtkWidgetsSpinBoxDouble::setValue(double val)
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
238 239 240 241 242
{
    d->value = val;
    this->lineEdit()->blockSignals(true);
    this->lineEdit()->setText(d->textFromValue(val));
    this->lineEdit()->blockSignals(false);
243
}
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
244

245
void dtkWidgetsSpinBoxDouble::setDecimals(int dec)
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
246 247 248 249 250
{
    d->decimals = dec;
    this->setValue(d->value);  // muste change the text in the widget accordingly
}

251
QAbstractSpinBox::StepEnabled dtkWidgetsSpinBoxDouble::stepEnabled(void) const
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
252 253 254 255 256
{
    return ( QAbstractSpinBox::StepUpEnabled | QAbstractSpinBox::StepDownEnabled ) ;
}


257
#include "dtkWidgetsSpinBoxDouble.moc"
SZPYRKA Jean-Luc's avatar
SZPYRKA Jean-Luc committed
258

259
//
260
// dtkWidgetsSpinBoxDouble.h ends here