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) ...@@ -50,7 +50,7 @@ int main(int argc, char **argv)
dtkWidgetsParameter *scientific_parameter_widget = dtk::widgets::parameters::pluginFactory().create(&scientific_parameter, "dtkWidgetsParameterScientificSpinBox"); dtkWidgetsParameter *scientific_parameter_widget = dtk::widgets::parameters::pluginFactory().create(&scientific_parameter, "dtkWidgetsParameterScientificSpinBox");
scientific_parameter.connect([=] (QVariant v) { scientific_parameter.connect([=] (QVariant v) {
double value = v.value<dtk::d_real>().value(); double value = v.value<dtk::d_real>().value();
qInfo() << "scientific_parameter(dbl) =" << value; printf("scientific_parameter(XXX) = %.12f\n", value);
}); });
QList<QString> available_strings; QList<QString> available_strings;
......
...@@ -14,65 +14,147 @@ ...@@ -14,65 +14,147 @@
#include "dtkDoubleSpinBox.h" #include "dtkDoubleSpinBox.h"
class dtkDoubleSpinBoxPrivate class dtkDoubleSpinBoxPrivate : public QObject
{ {
Q_OBJECT
public slots:
void update();
void touch();
public: public:
double value;
QDoubleValidator *validator; QDoubleValidator *validator;
bool scientific; bool scientific;
}; int decimals;
double minimum;
double maximum;
dtkDoubleSpinBox::dtkDoubleSpinBox(QDoubleValidator::Notation notation, QWidget *parent) : QDoubleSpinBox(parent) public:
{ QString textFromValue ( double ) const;
d = new dtkDoubleSpinBoxPrivate; double valueFromText ( const QString& ) const;
d->validator = new QDoubleValidator(); QValidator::State validate(QString&, int&) const;
d->validator->setNotation(notation);
this->lineEdit()->setValidator(d->validator);
d->scientific = ( notation == QDoubleValidator::ScientificNotation);
this->setMaximum(std::numeric_limits<double>::max()); // instead of default 99.99 public:
this->setMaximum(std::numeric_limits<double>::lowest()); dtkDoubleSpinBox *q;
this->setDecimals(32); };
void dtkDoubleSpinBoxPrivate::update(void)
{
this->value = valueFromText( q->text());
this->touch();
} }
dtkDoubleSpinBox::~dtkDoubleSpinBox(void) void dtkDoubleSpinBoxPrivate::touch()
{ {
delete d->validator; q->emit valueChanged(this->value);
delete d;
} }
QString dtkDoubleSpinBox::textFromValue ( double value ) const QString dtkDoubleSpinBoxPrivate::textFromValue ( double value ) const
{ {
QString strValue; QString strValue;
if (d->scientific) if (this->scientific)
strValue = QString::number(value, 'e', this->decimals()); strValue = QString::number(value, 'e', this->decimals);
else 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; return strValue;
} }
#include <cstdlib>
double dtkDoubleSpinBox::valueFromText ( const QString & text ) const 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; return dblValue;
} }
QValidator::State dtkDoubleSpinBox::validate(QString &text, int &pos) const QValidator::State dtkDoubleSpinBoxPrivate::validate(QString &text, int &pos) const
{ {
// check user input // check user input
bool input_check; bool input_check;
text.toDouble(&input_check); text.toDouble(&input_check);
//qDebug() << Q_FUNC_INFO << "text =" << text << "pos =" << pos << "validate =" << input_check;
return (input_check ? QValidator::Acceptable : QValidator::Invalid); 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) void dtkDoubleSpinBox::stepBy(int step)
{ {
if (step == 0 ) { if (step == 0 ) {
...@@ -97,17 +179,55 @@ void dtkDoubleSpinBox::stepBy(int step) ...@@ -97,17 +179,55 @@ void dtkDoubleSpinBox::stepBy(int step)
void dtkDoubleSpinBox::stepDown(void) void dtkDoubleSpinBox::stepDown(void)
{ {
if (d->scientific) if (d->scientific)
setValue(value()/10.0); d->value /= 10.0;
else else
setValue(value()-1.0); d->value -= 1.0;
d->touch();
} }
void dtkDoubleSpinBox::stepUp(void) void dtkDoubleSpinBox::stepUp(void)
{ {
if (d->scientific) if (d->scientific)
setValue(value()*10.0); d->value *= 10.0;
else 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 // dtkDoubleSpinBox.h ends here
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
// //
// /////////////////////////////////////////////////////////////////// // ///////////////////////////////////////////////////////////////////
class dtkDoubleSpinBox : public QDoubleSpinBox class dtkDoubleSpinBox : public QAbstractSpinBox
{ {
Q_OBJECT Q_OBJECT
...@@ -32,16 +32,21 @@ public: ...@@ -32,16 +32,21 @@ public:
dtkDoubleSpinBox(QDoubleValidator::Notation, QWidget * parent = 0); dtkDoubleSpinBox(QDoubleValidator::Notation, QWidget * parent = 0);
~dtkDoubleSpinBox(void); ~dtkDoubleSpinBox(void);
public: signals:
QString textFromValue ( double ) const; void valueChanged(double);
double valueFromText ( const QString& ) const;
QValidator::State validate(QString&, int&) const;
public:
void stepBy(int); void stepBy(int);
void stepDown(void); void stepDown(void);
void stepUp(void); void stepUp(void);
void setMinimum(double);
void setMaximum(double);
void setValue(double);
void setDecimals(int);
protected:
QAbstractSpinBox::StepEnabled stepEnabled(void) const;
private: private:
class dtkDoubleSpinBoxPrivate *d; 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