Skip to content

Commit

Permalink
[sliders] Fix precision issue with xy / xyz spinboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Nov 14, 2024
1 parent f2180d8 commit a06f1c3
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 49 deletions.
46 changes: 25 additions & 21 deletions src/lib/score/graphics/widgets/QGraphicsXYSpinbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,23 @@ void QGraphicsXYSpinboxChooser::paint(
{
}

std::array<float, 2> QGraphicsXYSpinboxChooser::value() const noexcept
std::array<double, 2> QGraphicsXYSpinboxChooser::value() const noexcept
{
return {(float)m_x.value(), (float)m_y.value()};
return {m_x.value(), m_y.value()};
}

std::array<float, 2> QGraphicsXYSpinboxChooser::getMin() const noexcept
std::array<double, 2> QGraphicsXYSpinboxChooser::getMin() const noexcept
{
return {(float)m_x.min, (float)m_y.min};
return {m_x.min, m_y.min};
}
std::array<float, 2> QGraphicsXYSpinboxChooser::getMax() const noexcept
std::array<double, 2> QGraphicsXYSpinboxChooser::getMax() const noexcept
{
return {(float)m_x.max, (float)m_y.max};
return {m_x.max, m_y.max};
}

ossia::vec2f QGraphicsXYSpinboxChooser::scaledValue(float x, float y) const noexcept
std::array<double, 2>
QGraphicsXYSpinboxChooser::scaledValue(double x, double y) const noexcept
{
return {
(float)(m_x.min + x * (m_x.max - m_x.min)),
(float)(m_y.min + y * (m_y.max - m_y.min))};
return {(m_x.min + x * (m_x.max - m_x.min)), (m_y.min + y * (m_y.max - m_y.min))};
}

void QGraphicsXYSpinboxChooser::setValue(ossia::vec2f v)
Expand Down Expand Up @@ -160,25 +158,25 @@ void QGraphicsIntXYSpinboxChooser::paint(
{
}

std::array<float, 2> QGraphicsIntXYSpinboxChooser::value() const noexcept
std::array<double, 2> QGraphicsIntXYSpinboxChooser::value() const noexcept
{
return {float(m_x.value()), float(m_y.value())};
return {(double)m_x.value(), (double)m_y.value()};
}

std::array<float, 2> QGraphicsIntXYSpinboxChooser::getMin() const noexcept
std::array<double, 2> QGraphicsIntXYSpinboxChooser::getMin() const noexcept
{
return {(float)m_x.min, (float)m_y.min};
return {(double)m_x.min, (double)m_y.min};
}
std::array<float, 2> QGraphicsIntXYSpinboxChooser::getMax() const noexcept

std::array<double, 2> QGraphicsIntXYSpinboxChooser::getMax() const noexcept
{
return {(float)m_x.max, (float)m_y.max};
return {(double)m_x.max, (double)m_y.max};
}

ossia::vec2f QGraphicsIntXYSpinboxChooser::scaledValue(float x, float y) const noexcept
std::array<double, 2>
QGraphicsIntXYSpinboxChooser::scaledValue(double x, double y) const noexcept
{
return {
(float)(m_x.min + x * (m_x.max - m_x.min)),
(float)(m_y.min + y * (m_y.max - m_y.min))};
return {(m_x.min + x * (m_x.max - m_x.min)), (m_y.min + y * (m_y.max - m_y.min))};
}

void QGraphicsIntXYSpinboxChooser::setValue(ossia::vec2f v)
Expand All @@ -187,6 +185,12 @@ void QGraphicsIntXYSpinboxChooser::setValue(ossia::vec2f v)
m_y.setValue(v[1]);
update();
}
void QGraphicsIntXYSpinboxChooser::setValue(std::array<double, 2> v)
{
m_x.setValue(v[0]);
m_y.setValue(v[1]);
update();
}

void QGraphicsIntXYSpinboxChooser::setRange(
ossia::vec2f min, ossia::vec2f max, ossia::vec2f init)
Expand Down
22 changes: 12 additions & 10 deletions src/lib/score/graphics/widgets/QGraphicsXYSpinbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ class SCORE_LIB_BASE_EXPORT QGraphicsXYSpinboxChooser final
~QGraphicsXYSpinboxChooser();

void setPoint(const QPointF& r);
void setValue(ossia::vec2f v);
void setValue(std::array<float, 2> v);
void setValue(std::array<double, 2> v);
void setRange(
ossia::vec2f min = {0.f, 0.f}, ossia::vec2f max = {1.f, 1.f},
ossia::vec2f init = {0.f, 0.f});

ossia::vec2f value() const noexcept;
ossia::vec2f getMin() const noexcept;
ossia::vec2f getMax() const noexcept;
std::array<double, 2> value() const noexcept;
std::array<double, 2> getMin() const noexcept;
std::array<double, 2> getMax() const noexcept;

bool moving = false;

Expand All @@ -47,7 +48,7 @@ class SCORE_LIB_BASE_EXPORT QGraphicsXYSpinboxChooser final
void sliderReleased() E_SIGNAL(SCORE_LIB_BASE_EXPORT, sliderReleased)

private:
ossia::vec2f scaledValue(float x, float y) const noexcept;
std::array<double, 2> scaledValue(double x, double y) const noexcept;
QRectF boundingRect() const override;
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
override;
Expand All @@ -71,14 +72,15 @@ class SCORE_LIB_BASE_EXPORT QGraphicsIntXYSpinboxChooser final
~QGraphicsIntXYSpinboxChooser();

void setPoint(const QPointF& r);
void setValue(ossia::vec2f v);
void setValue(std::array<float, 2> v);
void setValue(std::array<double, 2> v);
void setRange(
ossia::vec2f min = {0.f, 0.f}, ossia::vec2f max = {1.f, 1.f},
ossia::vec2f init = {0.f, 0.f});

ossia::vec2f value() const noexcept;
ossia::vec2f getMin() const noexcept;
ossia::vec2f getMax() const noexcept;
std::array<double, 2> value() const noexcept;
std::array<double, 2> getMin() const noexcept;
std::array<double, 2> getMax() const noexcept;

bool moving = false;

Expand All @@ -87,7 +89,7 @@ class SCORE_LIB_BASE_EXPORT QGraphicsIntXYSpinboxChooser final
void sliderReleased() E_SIGNAL(SCORE_LIB_BASE_EXPORT, sliderReleased)

private:
ossia::vec2f scaledValue(float x, float y) const noexcept;
std::array<double, 2> scaledValue(double x, double y) const noexcept;
QRectF boundingRect() const override;
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
override;
Expand Down
30 changes: 18 additions & 12 deletions src/lib/score/graphics/widgets/QGraphicsXYZSpinbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,37 +57,43 @@ void QGraphicsXYZSpinboxChooser::paint(
{
}

std::array<float, 3> QGraphicsXYZSpinboxChooser::value() const noexcept
std::array<double, 3> QGraphicsXYZSpinboxChooser::value() const noexcept
{
return {(float)m_x.value(), (float)m_y.value(), (float)m_z.value()};
return {m_x.value(), m_y.value(), m_z.value()};
}

std::array<float, 3> QGraphicsXYZSpinboxChooser::getMin() const noexcept
std::array<double, 3> QGraphicsXYZSpinboxChooser::getMin() const noexcept
{
return {(float)m_x.min, (float)m_y.min, (float)m_z.min};
return {m_x.min, m_y.min, m_z.min};
}
std::array<float, 3> QGraphicsXYZSpinboxChooser::getMax() const noexcept
std::array<double, 3> QGraphicsXYZSpinboxChooser::getMax() const noexcept
{
return {(float)m_x.max, (float)m_y.max, (float)m_z.max};
return {m_x.max, m_y.max, m_z.max};
}

ossia::vec3f
QGraphicsXYZSpinboxChooser::scaledValue(float x, float y, float z) const noexcept
std::array<double, 3>
QGraphicsXYZSpinboxChooser::scaledValue(double x, double y, double z) const noexcept
{
return {
(float)(m_x.min + x * (m_x.max - m_x.min)),
(float)(m_y.min + y * (m_y.max - m_y.min)),
(float)(m_z.min + z * (m_z.max - m_z.min))};
(m_x.min + x * (m_x.max - m_x.min)), (m_y.min + y * (m_y.max - m_y.min)),
(m_z.min + z * (m_z.max - m_z.min))};
}

void QGraphicsXYZSpinboxChooser::setValue(ossia::vec3f v)
void QGraphicsXYZSpinboxChooser::setValue(std::array<double, 3> v)
{
m_x.setValue(v[0]);
m_y.setValue(v[1]);
m_z.setValue(v[2]);
update();
}

void QGraphicsXYZSpinboxChooser::setValue(std::array<float, 3> v)
{
m_x.setValue(v[0]);
m_y.setValue(v[1]);
m_z.setValue(v[2]);
update();
}
void QGraphicsXYZSpinboxChooser::setRange(
ossia::vec3f min, ossia::vec3f max, ossia::vec3f init)
{
Expand Down
11 changes: 6 additions & 5 deletions src/lib/score/graphics/widgets/QGraphicsXYZSpinbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ class SCORE_LIB_BASE_EXPORT QGraphicsXYZSpinboxChooser final
~QGraphicsXYZSpinboxChooser();

void setPoint(const QPointF& r);
void setValue(ossia::vec3f v);
void setValue(std::array<float, 3> v);
void setValue(std::array<double, 3> v);
void setRange(
ossia::vec3f min = {0.f, 0.f, 0.f}, ossia::vec3f max = {1.f, 1.f, 1.f},
ossia::vec3f init = {0.f, 0.f, 0.f});
ossia::vec3f value() const noexcept;
ossia::vec3f getMin() const noexcept;
ossia::vec3f getMax() const noexcept;
std::array<double, 3> value() const noexcept;
std::array<double, 3> getMin() const noexcept;
std::array<double, 3> getMax() const noexcept;

bool moving = false;

Expand All @@ -42,7 +43,7 @@ class SCORE_LIB_BASE_EXPORT QGraphicsXYZSpinboxChooser final
void sliderReleased() E_SIGNAL(SCORE_LIB_BASE_EXPORT, sliderReleased)

private:
ossia::vec3f scaledValue(float x, float y, float z) const noexcept;
std::array<double, 3> scaledValue(double x, double y, double z) const noexcept;
QRectF boundingRect() const override;
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ struct LinearNormalizer
res[3] = from01(min[3], max[3] - min[3], val[3]);
return res;
}
template <typename T, std::size_t N>
static std::array<float, N> from01(const T& slider, std::array<double, N> val) noexcept
{
std::array<float, N> res;
const auto min = getMin<std::array<double, N>>(slider);
const auto max = getMax<std::array<double, N>>(slider);
for(int i = 0; i < N; i++)
res[i] = from01(min[i], max[i] - min[i], val[i]);
return res;
}
};

struct LogNormalizer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,8 @@ struct XYSpinboxes
sl, &score::QGraphicsIntXYSpinboxChooser::sliderMoved, context,
[=, &inlet, &ctx] {
sl->moving = true;
ctx.dispatcher.submit<SetControlValue<Control_T>>(inlet, sl->value());
auto [x, y] = sl->value();
ctx.dispatcher.submit<SetControlValue<Control_T>>(inlet, ossia::make_vec(x, y));
});
QObject::connect(
sl, &score::QGraphicsIntXYSpinboxChooser::sliderReleased, context,
Expand Down

0 comments on commit a06f1c3

Please sign in to comment.