Skip to content
Snippets Groups Projects
Commit 51143893 authored by Ludwig Jaeck's avatar Ludwig Jaeck
Browse files

moved functionality of DoubleFormatter inside DoubleSpinBox

parent ca32f8e4
No related branches found
No related tags found
1 merge request!1101GUI: introducing single precission formatting for spinboxes with 'angle' property
Pipeline #77494 failed
#include "DoubleFormatter.h"
#include <QString>
#include <QtMath>
void DoubleFormatter::setPrecision(int digits)
{
m_precision = digits;
}
QString DoubleFormatter::textFromValue(double val) const
{
return QString::number(val, 'f', m_precision);
}
void DoubleFormatter::setStepSize(double stepSize)
{
m_stepSize = stepSize;
}
double DoubleFormatter::getStepSize()
{
return m_stepSize;
}
#ifndef NUMBERFORMATER_H
#define NUMBERFORMATER_H
class QString;
class DoubleFormatter {
public:
DoubleFormatter()
: m_precision(2){};
QString textFromValue(double val) const;
void setPrecision(int digits);
void setStepSize(double stepSize);
double getStepSize();
private:
int m_precision;
double m_stepSize;
};
#endif // NUMBERFORMATER_H
...@@ -13,14 +13,14 @@ ...@@ -13,14 +13,14 @@
// ************************************************************************************************ // ************************************************************************************************
#include "GUI/View/Common/DoubleSpinBox.h" #include "GUI/View/Common/DoubleSpinBox.h"
#include "GUI/View/Tool/EditUtil.h" #include "GUI/View/Tool/EditUtil.h"
#include <QWheelEvent> #include <QWheelEvent>
DoubleSpinBox::DoubleSpinBox(const DoubleDescriptor& d, bool easyScrollable, QWidget* parent) DoubleSpinBox::DoubleSpinBox(const DoubleDescriptor& d, bool easyScrollable,int precision,double stepSize,QWidget* parent)
: QDoubleSpinBox(parent) : QDoubleSpinBox(parent)
, m_valueDescriptor(d) , m_valueDescriptor(d)
, easyScrollable(easyScrollable) , easyScrollable(easyScrollable)
, m_precision(precision)
{ {
setFocusPolicy(Qt::StrongFocus); setFocusPolicy(Qt::StrongFocus);
GUI::View::EditUtil::configSpinbox(this, d.decimals, d.limits); GUI::View::EditUtil::configSpinbox(this, d.decimals, d.limits);
...@@ -34,9 +34,8 @@ DoubleSpinBox::DoubleSpinBox(const DoubleDescriptor& d, bool easyScrollable, QWi ...@@ -34,9 +34,8 @@ DoubleSpinBox::DoubleSpinBox(const DoubleDescriptor& d, bool easyScrollable, QWi
QObject::connect(this, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, QObject::connect(this, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
&DoubleSpinBox::onDisplayValueChanged); &DoubleSpinBox::onDisplayValueChanged);
m_formatter.setPrecision(2); setSingleStep(stepSize);
} }
void DoubleSpinBox::setDisplayUnit(Unit displayUnit) void DoubleSpinBox::setDisplayUnit(Unit displayUnit)
{ {
m_displayUnit = displayUnit; m_displayUnit = displayUnit;
...@@ -107,13 +106,8 @@ void DoubleSpinBox::updateValue() ...@@ -107,13 +106,8 @@ void DoubleSpinBox::updateValue()
QSignalBlocker b(this); QSignalBlocker b(this);
setBaseValue(m_valueDescriptor.get()); setBaseValue(m_valueDescriptor.get());
} }
void DoubleSpinBox::setFormatter(DoubleFormatter formatter)
{
m_formatter = formatter;
setSingleStep(m_formatter.getStepSize());
}
QString DoubleSpinBox::textFromValue(double val) const QString DoubleSpinBox::textFromValue(double val) const
{ {
return m_formatter.textFromValue(val); return QString::number(val, 'f', m_precision);
} }
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
#ifndef BORNAGAIN_GUI_VIEW_COMMON_DOUBLESPINBOX_H #ifndef BORNAGAIN_GUI_VIEW_COMMON_DOUBLESPINBOX_H
#define BORNAGAIN_GUI_VIEW_COMMON_DOUBLESPINBOX_H #define BORNAGAIN_GUI_VIEW_COMMON_DOUBLESPINBOX_H
#include "GUI/Model/Descriptor/DoubleDescriptor.h" #include "GUI/Model/Descriptor/DoubleDescriptor.h"
#include "GUI/View/Common/DoubleFormatter.h"
#include <QDoubleSpinBox> #include <QDoubleSpinBox>
//! SpinBox for DoubleDescriptors, supporting units. //! SpinBox for DoubleDescriptors, supporting units.
...@@ -28,9 +27,8 @@ public: ...@@ -28,9 +27,8 @@ public:
//! Furthermore, the spin box will prohibit accidental changes by the mouse wheel. Otherwise it //! Furthermore, the spin box will prohibit accidental changes by the mouse wheel. Otherwise it
//! would be dangerous if the spin box is on a scrollable form - unintended and unnoticed //! would be dangerous if the spin box is on a scrollable form - unintended and unnoticed
//! changes would take place when just scrolling through the form. //! changes would take place when just scrolling through the form.
DoubleSpinBox(const DoubleDescriptor& d, bool easyScrollable = false, DoubleSpinBox(const DoubleDescriptor& d, bool easyScrollable = false,int precision = 2,double singleStepSize = 0.1,
QWidget* parent = nullptr); QWidget* parent = nullptr);
//! Set a display unit. //! Set a display unit.
//! //!
//! The caller has to make sure that the new display unit has a conversion to/from the contained //! The caller has to make sure that the new display unit has a conversion to/from the contained
...@@ -39,7 +37,6 @@ public: ...@@ -39,7 +37,6 @@ public:
//! Set the base value (unit is the one of the contained descriptor). //! Set the base value (unit is the one of the contained descriptor).
void setBaseValue(double baseValue); void setBaseValue(double baseValue);
void setFormatter(DoubleFormatter formatter);
//! The display unit as human readable string. //! The display unit as human readable string.
QString displayUnitAsString() const; QString displayUnitAsString() const;
...@@ -85,7 +82,9 @@ private: ...@@ -85,7 +82,9 @@ private:
//! selectable once, therefore the code is kept and controlled by this flag //! selectable once, therefore the code is kept and controlled by this flag
bool m_showUnitAsSuffix = false; bool m_showUnitAsSuffix = false;
DoubleFormatter m_formatter; //! stores the decimal places used for displaying the value. A user can still
//! type more decimal places, these places will get rounded up to the places allowed.
const int m_precision;
// QDoubleSpinBox interface // QDoubleSpinBox interface
public: public:
......
...@@ -85,13 +85,13 @@ void LayerEditorUtils::addMultiPropertyToGrid(QGridLayout* m_gridLayout, int fir ...@@ -85,13 +85,13 @@ void LayerEditorUtils::addMultiPropertyToGrid(QGridLayout* m_gridLayout, int fir
{ {
int col = firstCol; int col = firstCol;
for (const auto& valueDescriptor : valueDescriptors) { for (const auto& valueDescriptor : valueDescriptors) {
auto* editor = new DoubleSpinBox(valueDescriptor); DoubleSpinBox * editor;
if (valueDescriptor.label == "Angle") { if (valueDescriptor.label == "Angle") {
DoubleFormatter f; editor = new DoubleSpinBox(valueDescriptor,false,1,1.0);
f.setPrecision(1); }else{
f.setStepSize(1.0); editor = new DoubleSpinBox(valueDescriptor);
editor->setFormatter(f);
} }
QObject::connect(editor, &DoubleSpinBox::baseValueChanged, QObject::connect(editor, &DoubleSpinBox::baseValueChanged,
[=](double newValue) { setNewValue(newValue, valueDescriptor); }); [=](double newValue) { setNewValue(newValue, valueDescriptor); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment