Commit dcda50c4 authored by SZPYRKA Jean-Luc's avatar SZPYRKA Jean-Luc

merged code for Double and Scientific SpinBox

parent 71138a62
......@@ -40,9 +40,18 @@ int main(int argc, char **argv)
real_parameter.connect([=] (QVariant v) {
double value = v.value<dtk::d_real>().value();
qInfo() << "real_parameter =" << value;
qInfo() << "real_parameter (dbl)=" << value;
});
dtk::d_real scientific_parameter = 1.23456;
scientific_parameter.setDocumentation("A real parameter.");
scientific_parameter.setDecimals(6);
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;
});
QList<QString> available_strings;
available_strings.append("FirstString");
......@@ -71,8 +80,17 @@ int main(int argc, char **argv)
});
QVBoxLayout *central_layout = new QVBoxLayout;
central_layout->addWidget(first_real_parameter_widget);
central_layout->addWidget(second_real_parameter_widget);
QFrame *frame = new QFrame();
frame->setFrameStyle(QFrame::Box | QFrame::Raised);
central_layout->addWidget(frame);
QVBoxLayout *real_layout = new QVBoxLayout;
frame->setLayout(real_layout);
real_layout->addWidget(first_real_parameter_widget);
real_layout->addWidget(second_real_parameter_widget);
central_layout->addWidget(new QSplitter);
central_layout->addWidget(scientific_parameter_widget);
central_layout->addWidget(new QSplitter);
central_layout->addWidget(inliststring_parameter_widget);
central_layout->addWidget(new QSplitter);
......
......@@ -20,7 +20,7 @@ project(dtkWidgets VERSION ${dtkWidgets_VERSION})
set(${PROJECT_NAME}_HEADERS
dtkApplication.h
dtkScientificSpinBox.h
dtkDoubleSpinBox.h
dtkWidgets
dtkWidgets.h
dtkWidgetsColorButton
......@@ -103,7 +103,7 @@ set(${PROJECT_NAME}_HEADERS
set(${PROJECT_NAME}_SOURCES
dtkApplication.cpp
dtkScientificSpinBox.cpp
dtkDoubleSpinBox.cpp
dtkWidgets.cpp
dtkWidgetsColorButton.cpp
dtkWidgetsController.cpp
......
// Version: $Id$
//
//
// Commentary:
//
//
// Change Log:
//
//
// Code:
#include "dtkDoubleSpinBox.h"
class dtkDoubleSpinBoxPrivate
{
public:
int decimals;
QDoubleValidator *validator;
bool scientific;
};
dtkDoubleSpinBox::dtkDoubleSpinBox(QDoubleValidator::Notation notation, QWidget *parent) : QDoubleSpinBox(parent)
{
d = new dtkDoubleSpinBoxPrivate;
d->decimals = 9;
d->validator = new QDoubleValidator();
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
this->setMaximum(std::numeric_limits<double>::lowest());
this->setDecimals(d->decimals);
}
dtkDoubleSpinBox::~dtkDoubleSpinBox(void)
{
}
int dtkDoubleSpinBox::decimals() const
{
return d->decimals;
}
void dtkDoubleSpinBox::setDecimals(int value)
{
qDebug() << Q_FUNC_INFO << "decimal = " << value;
d->decimals = value;
}
QString dtkDoubleSpinBox::textFromValue ( double value ) const
{
QString strValue;
if (d->scientific)
strValue = QString::number(value, 'e', d->decimals);
else
strValue = QString::number(value, 'f', d->decimals);
qDebug() << Q_FUNC_INFO << "value =" << value << "text =" << strValue;
return strValue;
}
double dtkDoubleSpinBox::valueFromText ( const QString & text ) const
{
double dblValue = text.toDouble();
qDebug() << Q_FUNC_INFO << "text =" << text << "value =" << dblValue;
return dblValue;
}
QValidator::State dtkDoubleSpinBox::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.h ends here
// Version: $Id$
//
//
// Commentary:
//
//
// Change Log:
//
//
// Code:
#include <QtCore>
#include <QtWidgets>
// ///////////////////////////////////////////////////////////////////
// dtkDoubleSpinbox
// ///////////////////////////////////////////////////////////////////
//
// holds both standard and scienti
// . is the separator and does not depend on locale()
//
// ///////////////////////////////////////////////////////////////////
class dtkDoubleSpinBox : public QDoubleSpinBox
{
Q_OBJECT
public:
dtkDoubleSpinBox(QDoubleValidator::Notation, QWidget * parent = 0);
~dtkDoubleSpinBox(void);
public:
int decimals() const;
void setDecimals(int);
QString textFromValue ( double ) const;
double valueFromText ( const QString& ) const;
QValidator::State validate(QString&, int&) const;
private:
class dtkDoubleSpinBoxPrivate *d;
};
//
// dtkDoubleSpinBox.h ends here
......@@ -14,6 +14,7 @@
#include "dtkWidgetsParameterDoubleSpinBox.h"
#include "dtkDoubleSpinBox.h"
#include <dtkLog>
// ///////////////////////////////////////////////////////////////////
......@@ -23,7 +24,7 @@
class dtkWidgetsParameterDoubleSpinBoxPrivate
{
public:
QDoubleSpinBox *spin_box = nullptr;
dtkDoubleSpinBox *spin_box = nullptr;
};
// ///////////////////////////////////////////////////////////////////
......@@ -32,7 +33,7 @@ public:
dtkWidgetsParameterDoubleSpinBox::dtkWidgetsParameterDoubleSpinBox(QWidget* parent) : dtkWidgetsParameterBase<dtk::d_real>(parent), d(new dtkWidgetsParameterDoubleSpinBoxPrivate)
{
d->spin_box = new QDoubleSpinBox;
d->spin_box = new dtkDoubleSpinBox(QDoubleValidator::StandardNotation);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(d->spin_box);
......@@ -74,7 +75,7 @@ bool dtkWidgetsParameterDoubleSpinBox::connect(dtkCoreParameter *p)
d->spin_box->blockSignals(false);
});
QObject::connect(d->spin_box, QOverload<double>::of(&QDoubleSpinBox::valueChanged), [=] (double v)
QObject::connect(d->spin_box, QOverload<double>::of(&dtkDoubleSpinBox::valueChanged), [=] (double v)
{
m_parameter->setValue(v);
});
......
#include "dtkWidgetsParameterScientificSpinBox.h"
// Version: $Id$
//
//
// Commentary:
//
//
// Change Log:
//
//
// Code:
#include "dtkWidgetsParameterScientificSpinBox.h"
#include "dtkDoubleSpinBox.h"
#include <dtkLog>
// ///////////////////////////////////////////////////////////////////
// dtkWidgetsParameterScientificSpinBoxPrivate declaration
// ///////////////////////////////////////////////////////////////////
class dtkWidgetsParameterScientificSpinBoxPrivate
{
public:
dtkDoubleSpinBox *spin_box = nullptr;
};
// ///////////////////////////////////////////////////////////////////
// dtkWidgetsParameterScientificSpinBox implementation
// ///////////////////////////////////////////////////////////////////
dtkWidgetsParameterScientificSpinBox::dtkWidgetsParameterScientificSpinBox(QWidget* parent) : dtkWidgetsParameterBase<dtk::d_real>(parent), d(new dtkWidgetsParameterScientificSpinBoxPrivate)
{
d->spin_box = new dtkDoubleSpinBox(QDoubleValidator::ScientificNotation);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(d->spin_box);
this->setLayout(layout);
}
dtkWidgetsParameterScientificSpinBox::~dtkWidgetsParameterScientificSpinBox(void)
{
delete d;
}
bool dtkWidgetsParameterScientificSpinBox::connect(dtkCoreParameter *p)
{
if (!p) {
qWarning() << Q_FUNC_INFO << "The input parameter is null. Nothing is done.";
return false;
}
m_parameter = dynamic_cast<dtk::d_real *>(p);
if(!m_parameter) {
qWarning() << Q_FUNC_INFO << "The type of the parameter is not compatible with the widget dtkWidgetsParameterIntSpinBox.";
return false;
}
d->spin_box->setMinimum(m_parameter->min());
d->spin_box->setMaximum(m_parameter->max());
d->spin_box->setToolTip(m_parameter->documentation());
d->spin_box->setValue(m_parameter->value());
d->spin_box->setDecimals(m_parameter->decimals());
m_parameter->connect([=] (QVariant v)
{
double value = v.value<dtk::d_real>().value();
d->spin_box->blockSignals(true);
d->spin_box->setValue(value);
d->spin_box->blockSignals(false);
});
QObject::connect(d->spin_box, QOverload<double>::of(&dtkDoubleSpinBox::valueChanged), [=] (double v)
{
m_parameter->setValue(v);
});
return true;
}
//
// dtkWidgetsParameterScientificSpinBoxWidget.cpp ends here
// Version: $Id$
//
//
// Commentary:
//
//
// Change Log:
//
//
// Code:
#pragma once
#include <dtkWidgetsExport>
#include <QtWidgets>
#include "dtkWidgetsParameter.h"
#include <dtkCore/dtkCoreParameter>
class DTKWIDGETS_EXPORT dtkWidgetsParameterScientificSpinBox : public dtkWidgetsParameterBase<dtk::d_real>
{
Q_OBJECT
public:
dtkWidgetsParameterScientificSpinBox(QWidget* parent = 0);
~dtkWidgetsParameterScientificSpinBox(void);
public:
bool connect(dtkCoreParameter *) override;
private:
using dtkWidgetsParameterBase<dtk::d_real>::m_parameter;
class dtkWidgetsParameterScientificSpinBoxPrivate *d;
};
// ///////////////////////////////////////////////////////////////////
inline dtkWidgetsParameter *dtkWidgetsParameterScientificSpinBoxCreator(void)
{
return new dtkWidgetsParameterScientificSpinBox();
}
//
// dtkWidgetsParameterScientificSpinBox.h ends here
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