Skip to content

Commit 7f2761d

Browse files
authored
Fix incorrect graph step size behavior (LMMS#7703)
The y-axis quantization for graph values was being calculated incorrectly. This would have gone unnoticed since the only step sizes currently used are 0 (continuous) or 1. Any other values produce "sloped" quantization. This fix prevents this error from affecting any future uses of graphModel.
1 parent 6233c5b commit 7f2761d

File tree

2 files changed

+25
-35
lines changed

2 files changed

+25
-35
lines changed

include/Graph.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class LMMS_EXPORT graphModel : public Model
182182

183183
public slots:
184184
//! Set range of y values
185-
void setRange( float _min, float _max );
185+
void setRange(float ymin, float ymax);
186186

187187
void setLength( int _size );
188188
//! Update one sample

src/gui/widgets/Graph.cpp

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -459,39 +459,31 @@ void Graph::updateGraph()
459459

460460
} // namespace gui
461461

462-
graphModel::graphModel( float _min, float _max, int _length,
463-
Model* _parent, bool _default_constructed, float _step ) :
464-
Model( _parent, tr( "Graph" ), _default_constructed ),
465-
m_samples( _length ),
466-
m_length( _length ),
467-
m_minValue( _min ),
468-
m_maxValue( _max ),
469-
m_step( _step )
462+
graphModel::graphModel(float ymin, float ymax, int length, Model* parent, bool defaultConstructed, float step) :
463+
Model(parent, tr("Graph"), defaultConstructed),
464+
m_samples(length),
465+
m_length(length),
466+
m_minValue(ymin),
467+
m_maxValue(ymax),
468+
m_step(std::clamp(step, 0.f, std::abs(ymax - ymin)))
470469
{
471470
}
472471

473-
void graphModel::setRange( float _min, float _max )
474-
{
475-
if( _min != m_minValue || _max != m_maxValue )
476-
{
477-
m_minValue = _min;
478-
m_maxValue = _max;
479-
480-
if( !m_samples.isEmpty() )
481-
{
482-
// Trim existing values
483-
for( int i=0; i < length(); i++ )
484-
{
485-
m_samples[i] = fmaxf( _min, fminf( m_samples[i], _max ) );
486-
}
487-
}
488-
489-
emit rangeChanged();
472+
void graphModel::setRange(float ymin, float ymax)
473+
{
474+
if (ymin == m_minValue && ymax == m_maxValue) { return; }
475+
// Step sizes less than zero or larger than the entire range of
476+
// values are nonsense, clamp those
477+
m_step = std::clamp(m_step, 0.f, std::abs(ymax - ymin));
478+
m_minValue = ymin;
479+
m_maxValue = ymax;
480+
// Trim existing values
481+
for (float& sample : m_samples) {
482+
sample = std::clamp(sample, ymin, ymax);
490483
}
484+
emit rangeChanged();
491485
}
492486

493-
494-
495487
void graphModel::setLength( int _length )
496488
{
497489
if( _length != m_length )
@@ -732,15 +724,13 @@ void graphModel::clearInvisible()
732724
emit samplesChanged( graph_length, full_graph_length - 1 );
733725
}
734726

735-
void graphModel::drawSampleAt( int x, float val )
727+
void graphModel::drawSampleAt(int x, float val)
736728
{
737-
//snap to the grid
738-
val -= (m_step != 0.0) ? std::fmod(val, m_step) * m_step : 0;
739-
729+
// snap to the grid
730+
if (m_step > 0) { val = std::floor(val / m_step) * m_step; }
740731
// boundary crop
741-
x = qMax( 0, qMin( length()-1, x ) );
742-
val = qMax( minValue(), qMin( maxValue(), val ) );
743-
732+
x = std::clamp(x, 0, length() - 1);
733+
val = std::clamp(val, minValue(), maxValue());
744734
// change sample shape
745735
m_samples[x] = val;
746736
}

0 commit comments

Comments
 (0)