Commit 51b42199 authored by SZPYRKA Jean-Luc's avatar SZPYRKA Jean-Luc

fixed dtkDoubleSpinBox

parent 9d8b6f8a
......@@ -50,7 +50,7 @@ int main(int argc, char **argv)
dtkWidgetsParameter *scientific_parameter_widget = dtk::widgets::parameters::pluginFactory().create(&scientific_parameter, "dtkWidgetsParameterScientificSpinBox");
scientific_parameter.connect([=] (QVariant v) {
double value = v.value<dtk::d_real>().value();
qInfo() << "scientific_parameter(dbl) =" << value;
printf("scientific_parameter(XXX) = %.12f\n", value);
});
QList<QString> available_strings;
......
......@@ -14,65 +14,147 @@
#include "dtkDoubleSpinBox.h"
class dtkDoubleSpinBoxPrivate
class dtkDoubleSpinBoxPrivate : public QObject
{
Q_OBJECT
public slots:
void update();
void touch();
public:
double value;
QDoubleValidator *validator;
bool scientific;
};
int decimals;
double minimum;
double maximum;
dtkDoubleSpinBox::dtkDoubleSpinBox(QDoubleValidator::Notation notation, QWidget *parent) : QDoubleSpinBox(parent)
{
d = new dtkDoubleSpinBoxPrivate;
public:
QString textFromValue ( double ) const;
double valueFromText ( const QString& ) const;
d->validator = new QDoubleValidator();
d->validator->setNotation(notation);
this->lineEdit()->setValidator(d->validator);
QValidator::State validate(QString&, int&) const;
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);
public:
dtkDoubleSpinBox *q;
};
void dtkDoubleSpinBoxPrivate::update(void)
{
this->value = valueFromText( q->text());
this->touch();
}
dtkDoubleSpinBox::~dtkDoubleSpinBox(void)
void dtkDoubleSpinBoxPrivate::touch()
{
delete d->validator;
delete d;
q->emit valueChanged(this->value);
}
QString dtkDoubleSpinBox::textFromValue ( double value ) const
QString dtkDoubleSpinBoxPrivate::textFromValue ( double value ) const
{
QString strValue;
if (d->scientific)
strValue = QString::number(value, 'e', this->decimals());
if (this->scientific)
strValue = QString::number(value, 'e', this->decimals);
else
strValue = QString::number(value, 'f', this->decimals());
strValue = QString::number(value, 'f', this->decimals);
//qDebug() << Q_FUNC_INFO << "value =" << value << "text =" << strValue;
return strValue;
}
double dtkDoubleSpinBox::valueFromText ( const QString & text ) const
#include <cstdlib>
double dtkDoubleSpinBoxPrivate::valueFromText ( const QString & text ) const
{
double dblValue = text.toDouble();
// 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" ;
}
//qDebug() << Q_FUNC_INFO << "text =" << text << "value =" << dblValue;
double dblValue = result.toDouble();
//q->lineEdit()->blockSignals(true); // protected !!
//q->lineEdit()->setText(result);
//q->lineEdit()->blockSignals(false);
q->setValue(dblValue);
return dblValue;
}
QValidator::State dtkDoubleSpinBox::validate(QString &text, int &pos) const
QValidator::State dtkDoubleSpinBoxPrivate::validate(QString &text, int &pos) const
{
// check user input
bool input_check;
text.toDouble(&input_check);
//qDebug() << Q_FUNC_INFO << "text =" << text << "pos =" << pos << "validate =" << input_check;
return (input_check ? QValidator::Acceptable : QValidator::Invalid);
}
///
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;
}
void dtkDoubleSpinBox::stepBy(int step)
{
if (step == 0 ) {
......@@ -97,17 +179,55 @@ void dtkDoubleSpinBox::stepBy(int step)
void dtkDoubleSpinBox::stepDown(void)
{
if (d->scientific)
setValue(value()/10.0);
d->value /= 10.0;
else
setValue(value()-1.0);
d->value -= 1.0;
d->touch();
}
void dtkDoubleSpinBox::stepUp(void)
{
if (d->scientific)
setValue(value()*10.0);
d->value *= 10.0;
else
setValue(value()+1.0);
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);
}
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"
//
// dtkDoubleSpinBox.h ends here
......@@ -24,7 +24,7 @@
//
// ///////////////////////////////////////////////////////////////////
class dtkDoubleSpinBox : public QDoubleSpinBox
class dtkDoubleSpinBox : public QAbstractSpinBox
{
Q_OBJECT
......@@ -32,16 +32,21 @@ public:
dtkDoubleSpinBox(QDoubleValidator::Notation, QWidget * parent = 0);
~dtkDoubleSpinBox(void);
public:
QString textFromValue ( double ) const;
double valueFromText ( const QString& ) const;
QValidator::State validate(QString&, int&) const;
signals:
void valueChanged(double);
public:
void stepBy(int);
void stepDown(void);
void stepUp(void);
void setMinimum(double);
void setMaximum(double);
void setValue(double);
void setDecimals(int);
protected:
QAbstractSpinBox::StepEnabled stepEnabled(void) const;
private:
class dtkDoubleSpinBoxPrivate *d;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment