Commit 67fc90e4 authored by Alexander Stippich's avatar Alexander Stippich
Browse files

rework the float option to a double option

parent 32ff8dd8
......@@ -38,7 +38,7 @@ set(ksane_SRCS
options/ksanestringoption.cpp
options/ksanegammaoption.cpp
options/ksaneintegeroption.cpp
options/ksanefloatoption.cpp
options/ksanedoubleoption.cpp
options/ksanelistoption.cpp
options/ksaneinvertoption.cpp
)
......
......@@ -39,7 +39,7 @@
#include "ksanebooloption.h"
#include "ksanelistoption.h"
#include "ksanestringoption.h"
#include "ksanefloatoption.h"
#include "ksanedoubleoption.h"
#include "ksanegammaoption.h"
#include "ksaneintegeroption.h"
#include "ksanedevicedialog.h"
......@@ -444,8 +444,8 @@ bool KSaneWidget::openDevice(const QString &deviceName)
case KSaneOption::TypeInteger:
d->m_optList.append(new KSaneIntegerOption(d->m_saneHandle, i));
break;
case KSaneOption::TypeFloat:
d->m_optList.append(new KSaneFloatOption(d->m_saneHandle, i));
case KSaneOption::TypeDouble:
d->m_optList.append(new KSaneDoubleOption(d->m_saneHandle, i));
break;
case KSaneOption::TypeValueList:
d->m_optList.append(new KSaneListOption(d->m_saneHandle, i));
......
......@@ -378,7 +378,7 @@ KSaneOptionWidget *KSaneWidgetPrivate::createOptionWidget(QWidget *parent, KSane
case KSaneOption::TypeInteger:
widget = new LabeledSlider(parent, option);
break;
case KSaneOption::TypeFloat:
case KSaneOption::TypeDouble:
widget = new LabeledFSlider(parent, option);
break;
case KSaneOption::TypeValueList:
......
......@@ -7,12 +7,13 @@
*
* SPDX-FileCopyrightText: 2009 Kare Sars <kare dot sars at iki dot fi>
* SPDX-FileCopyrightText: 2014 Gregor Mitsch : port to KDE5 frameworks
* SPDX-FileCopyrightText: 2021 Alexander Stippich <a.stippich@gmx.net>
*
* SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*
* ============================================================ */
// Local includes
#include "ksanefloatoption.h"
#include "ksanedoubleoption.h"
#include <QVarLengthArray>
......@@ -24,17 +25,17 @@ static const float MIN_FIXED_STEP = 0.0001;
namespace KSaneIface
{
KSaneFloatOption::KSaneFloatOption(const SANE_Handle handle, const int index)
: KSaneOption(handle, index), m_fVal(0), m_minChange(MIN_FIXED_STEP)
KSaneDoubleOption::KSaneDoubleOption(const SANE_Handle handle, const int index)
: KSaneOption(handle, index)
{
m_optionType = KSaneOption::TypeFloat;
m_optionType = KSaneOption::TypeDouble;
}
void KSaneFloatOption::readOption()
void KSaneDoubleOption::readOption()
{
KSaneOption::readOption();
float step = MIN_FIXED_STEP;
double step = MIN_FIXED_STEP;
if (m_optDesc->constraint_type == SANE_CONSTRAINT_RANGE) {
step = SANE_UNFIX(m_optDesc->constraint.range->quant);
if (step < MIN_FIXED_STEP) {
......@@ -44,7 +45,7 @@ void KSaneFloatOption::readOption()
m_minChange = step;
}
void KSaneFloatOption::readValue()
void KSaneDoubleOption::readValue()
{
if (state() == StateHidden) {
return;
......@@ -59,32 +60,32 @@ void KSaneFloatOption::readValue()
return;
}
m_fVal = SANE_UNFIX(toSANE_Word(data.data()));
m_value = SANE_UNFIX(toSANE_Word(data.data()));
Q_EMIT valueChanged(m_fVal);
Q_EMIT valueChanged(m_value);
}
bool KSaneFloatOption::setValue(const QVariant &value)
bool KSaneDoubleOption::setValue(const QVariant &value)
{
if (state() == StateHidden) {
return false;
}
bool ok;
float newValue = value.toFloat(&ok);
if (ok && (((newValue - m_fVal) >= m_minChange) || ((m_fVal - newValue) >= m_minChange)) ) {
double newValue = value.toDouble(&ok);
if (ok && (((newValue - m_value) >= m_minChange) || ((m_value - newValue) >= m_minChange)) ) {
unsigned char data[4];
SANE_Word fixed;
//qCDebug(KSANE_LOG) <<m_optDesc->name << fVal << "!=" << val;
m_fVal = newValue;
m_value = newValue;
fixed = SANE_FIX(newValue);
fromSANE_Word(data, fixed);
writeData(data);
Q_EMIT valueChanged(m_fVal);
Q_EMIT valueChanged(m_value);
}
return ok;
}
QVariant KSaneFloatOption::getMinValue() const
QVariant KSaneDoubleOption::getMinValue() const
{
QVariant value;
if (m_optDesc->constraint_type == SANE_CONSTRAINT_RANGE) {
......@@ -95,7 +96,7 @@ QVariant KSaneFloatOption::getMinValue() const
return value;
}
QVariant KSaneFloatOption::getMaxValue() const
QVariant KSaneDoubleOption::getMaxValue() const
{
QVariant value;
if (m_optDesc->constraint_type == SANE_CONSTRAINT_RANGE) {
......@@ -106,7 +107,7 @@ QVariant KSaneFloatOption::getMaxValue() const
return value;
}
QVariant KSaneFloatOption::getStepValue() const
QVariant KSaneDoubleOption::getStepValue() const
{
QVariant value;
if (m_optDesc->constraint_type == SANE_CONSTRAINT_RANGE) {
......@@ -117,20 +118,20 @@ QVariant KSaneFloatOption::getStepValue() const
return value;
}
QVariant KSaneFloatOption::getValue() const
QVariant KSaneDoubleOption::getValue() const
{
if (state() == StateHidden) {
return QVariant();
}
return QVariant(m_fVal);
return QVariant(m_value);
}
QString KSaneFloatOption::getValueAsString() const
QString KSaneDoubleOption::getValueAsString() const
{
if (state() == StateHidden) {
return QString();
}
return QString::number(m_fVal, 'F', 6);
return QString::number(m_value, 'F', 6);
}
} // NameSpace KSaneIface
......@@ -2,29 +2,30 @@
*
* This file is part of the KDE project
*
* Date : 2009-01-21
* Date : 2021-04-6
* Description : Sane interface for KDE
*
* SPDX-FileCopyrightText: 2009 Kare Sars <kare dot sars at iki dot fi>
* SPDX-FileCopyrightText: 2021 Alexander Stippich <a.stippich@gmx.net>
*
* SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*
* ============================================================ */
#ifndef KSANE_FLOAT_OPTION_H
#define KSANE_FLOAT_OPTION_H
#ifndef KSANE_DOUBLE_OPTION_H
#define KSANE_DOUBLE_OPTION_H
#include "ksaneoption.h"
namespace KSaneIface
{
class KSaneFloatOption : public KSaneOption
class KSaneDoubleOption : public KSaneOption
{
Q_OBJECT
public:
KSaneFloatOption(const SANE_Handle handle, const int index);
KSaneDoubleOption(const SANE_Handle handle, const int index);
void readValue() override;
void readOption() override;
......@@ -39,8 +40,8 @@ public Q_SLOTS:
bool setValue(const QVariant &value) override;
private:
float m_fVal;
float m_minChange;
double m_value = 0;
double m_minChange = 0.0001;
};
} // NameSpace KSaneIface
......
......@@ -289,7 +289,7 @@ KSaneOption::KSaneOptionType KSaneOption::optionType(const SANE_Option_Descripto
break;
case SANE_TYPE_FIXED:
if (optDesc->size == sizeof(SANE_Word)) {
return TypeFloat;
return TypeDouble;
}
qCDebug(KSANE_LOG) << "Can not handle:" << optDesc->title;
qCDebug(KSANE_LOG) << "SANE_CONSTRAINT_NONE && SANE_TYPE_FIXED";
......@@ -324,7 +324,7 @@ KSaneOption::KSaneOptionType KSaneOption::optionType(const SANE_Option_Descripto
break;
case SANE_TYPE_FIXED:
if (optDesc->size == sizeof(SANE_Word)) {
return TypeFloat;
return TypeDouble;
}
qCDebug(KSANE_LOG) << "Can not handle:" << optDesc->title;
qCDebug(KSANE_LOG) << "SANE_CONSTRAINT_RANGE && SANE_TYPE_FIXED";
......
......@@ -48,7 +48,7 @@ public:
TypeDetectFail,
TypeBool,
TypeInteger,
TypeFloat,
TypeDouble,
TypeValueList,
TypeString,
TypeGamma,
......
......@@ -15,14 +15,14 @@
#include "labeledfslider.h"
#define FLOAT_MULTIP 32768.0
#define TO_FLOAT(v) ((float)v / FLOAT_MULTIP)
#define TO_FIX(v) ((int)(v * FLOAT_MULTIP))
#define TO_DOUBLE(v) (static_cast<double>(v) / FLOAT_MULTIP)
#define TO_FIX(v) (static_cast<int>(v * FLOAT_MULTIP))
namespace KSaneIface
{
LabeledFSlider::LabeledFSlider(QWidget *parent, const QString &ltext,
float min, float max, float step)
double min, double max, double step)
: KSaneOptionWidget(parent, ltext)
{
initFSlider(min, max, step);
......@@ -31,10 +31,10 @@ LabeledFSlider::LabeledFSlider(QWidget *parent, const QString &ltext,
LabeledFSlider::LabeledFSlider(QWidget *parent, KSaneOption *option)
: KSaneOptionWidget(parent, option)
{
float maxValueF = option->getMaxValue().toFloat();
float minValueF= option->getMinValue().toFloat();
float stepValueF = option->getStepValue().toFloat();
initFSlider(minValueF, maxValueF , stepValueF);
double maxValue = option->getMaxValue().toDouble();
double minValue = option->getMinValue().toDouble();
double stepValue = option->getStepValue().toDouble();
initFSlider(minValue, maxValue , stepValue);
QString unitSuffix;
KSaneOption::KSaneOptionUnit unit = option->getUnit();
......@@ -67,19 +67,19 @@ LabeledFSlider::LabeledFSlider(QWidget *parent, KSaneOption *option)
setToolTip(option->description());
connect(this, &LabeledFSlider::valueChanged, option, &KSaneOption::setValue);
connect(option, &KSaneOption::valueChanged, this, &LabeledFSlider::setValue);
float valueF = option->getValue().toFloat();
setValue(valueF);
double value = option->getValue().toDouble();
setValue(value);
}
void LabeledFSlider::initFSlider(float minValueF, float maxValueF, float stepValueF)
void LabeledFSlider::initFSlider(double minValue, double maxValue, double stepValue)
{
int imin = TO_FIX(minValueF);
int imax = TO_FIX(maxValueF);
m_istep = TO_FIX(stepValueF);
m_fstep = stepValueF;
int imin = TO_FIX(minValue);
int imax = TO_FIX(maxValue);
m_istep = TO_FIX(stepValue);
m_fstep = stepValue;
if (m_istep == 0) {
m_istep = 1;
m_fstep = TO_FLOAT(m_istep);
m_fstep = TO_DOUBLE(m_istep);
}
//std::cout << "min=" << min << ", max=" << max << ", m_fstep="<<m_fstep<<std::endl;
......@@ -92,11 +92,11 @@ void LabeledFSlider::initFSlider(float minValueF, float maxValueF, float stepVal
m_slider->setValue(imin);
m_spinb = new QDoubleSpinBox(this);
m_spinb->setMinimum(minValueF);
m_spinb->setMaximum(maxValueF);
m_spinb->setMinimum(minValue);
m_spinb->setMaximum(maxValue);
m_spinb->setSingleStep(m_fstep);
int decimals = 0;
float tmp_step = m_fstep;
double tmp_step = m_fstep;
while (tmp_step < 1) {
tmp_step *= 10;
decimals++;
......@@ -105,11 +105,11 @@ void LabeledFSlider::initFSlider(float minValueF, float maxValueF, float stepVal
}
}
m_spinb->setDecimals(decimals);
m_spinb->setValue(maxValueF);
m_spinb->setValue(maxValue);
//m_spinb->setMinimumWidth(m_spinb->sizeHint().width()+35);
m_spinb->setMinimumWidth(m_spinb->sizeHint().width());
m_spinb->setAlignment(Qt::AlignRight);
m_spinb->setValue(minValueF);
m_spinb->setValue(minValue);
m_label->setBuddy(m_spinb);
......@@ -128,12 +128,12 @@ LabeledFSlider::~LabeledFSlider()
{
}
float LabeledFSlider::value() const
double LabeledFSlider::value() const
{
return (float)m_spinb->value();
return m_spinb->value();
}
float LabeledFSlider::step() const
double LabeledFSlider::step() const
{
return m_fstep;
}
......@@ -143,7 +143,7 @@ void LabeledFSlider::setSuffix(const QString &text)
m_spinb->setSuffix(text);
}
void LabeledFSlider::setRange(float min, float max)
void LabeledFSlider::setRange(double min, double max)
{
//qCDebug(KSANE_LOG) << "min,max(" << m_spinb->minimum() << " - " << m_spinb->maximum();
//qCDebug(KSANE_LOG) << ") -> (" << min << " - " << max << ")" << std::endl;
......@@ -155,19 +155,19 @@ void LabeledFSlider::setRange(float min, float max)
m_spinb->setRange(min, max);
}
void LabeledFSlider::setStep(float step)
void LabeledFSlider::setStep(double step)
{
m_istep = TO_FIX(step);
m_fstep = step;
if (m_istep == 0) {
m_istep = 1;
m_fstep = TO_FLOAT(m_istep);
m_fstep = TO_DOUBLE(m_istep);
}
m_slider->setSingleStep(m_istep);
m_spinb->setSingleStep(m_fstep);
int decimals = 0;
float tmp_step = m_fstep;
double tmp_step = m_fstep;
while (tmp_step < 1) {
tmp_step *= 10;
decimals++;
......@@ -181,7 +181,7 @@ void LabeledFSlider::setStep(float step)
void LabeledFSlider::setValue(const QVariant &value)
{
bool ok;
float newValue = value.toFloat(&ok);
double newValue = value.toDouble(&ok);
if (!ok) {
return;
}
......@@ -196,7 +196,7 @@ void LabeledFSlider::setValue(const QVariant &value)
void LabeledFSlider::syncValues(int ivalue)
{
double value = TO_FLOAT(ivalue);
double value = TO_DOUBLE(ivalue);
if (((value - m_spinb->value()) > m_fstep) || ((m_spinb->value() - value) > m_fstep)) {
m_spinb->setValue(value);
} else if (ivalue != m_slider->value()) {
......@@ -229,7 +229,7 @@ void LabeledFSlider::fixValue()
} else {
m_slider->setValue(m_slider->value() - rest);
}
m_spinb->setValue(TO_FLOAT(m_slider->value()));
m_spinb->setValue(TO_DOUBLE(m_slider->value()));
}
}
......
......@@ -45,10 +45,10 @@ public:
* contains a '&', a buddy for the slider will be created.
* \param min minimum slider value
* \param max maximum slider value
* \param st is the step between values.
* \param step is the step between values.
*/
LabeledFSlider(QWidget *parent, const QString &text,
float min, float max, float step);
double min, double max, double step);
LabeledFSlider(QWidget *parent, KSaneOption *option);
~LabeledFSlider();
......@@ -56,15 +56,15 @@ public:
/**
* \return the slider value.
*/
float value() const;
float step() const;
double value() const;
double step() const;
public Q_SLOTS:
/** Set the slider/spinbox value */
void setValue(const QVariant &value);
void setRange(float min, float max);
void setStep(float step);
void setRange(double min, double max);
void setStep(double step);
/** Set the unit */
void setSuffix(const QString &text);
......@@ -86,11 +86,11 @@ Q_SIGNALS:
void valueChanged(const QVariant &val);
private:
void initFSlider(float minValueF, float maxValueF, float stepValueF);
void initFSlider(double minValue, double maxValue, double stepValue);
QSlider *m_slider;
QDoubleSpinBox *m_spinb;
float m_fstep;
double m_fstep;
int m_istep;
};
......
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