Skip to content

Commit

Permalink
suggest a fix for precision on knob dragging (LMMS#3075)
Browse files Browse the repository at this point in the history
* fixes precision on knob dragging

* fixes precision on fader dragging

* diaplay a rounded float on knobs

* enter rounded value on fader

* add getRoundedValue() and getDigitCount() to floatmodel

* whitespace
  • Loading branch information
BaraMGB authored Jan 12, 2017
1 parent 585f21c commit 489553b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
3 changes: 2 additions & 1 deletion include/AutomatableModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,8 @@ class FloatModel : public AutomatableModel
AutomatableModel( Float, val, min, max, step, parent, displayName, defaultConstructed )
{
}

float getRoundedValue() const;
float getDigitCount();
defaultTypedMethods(float);

} ;
Expand Down
20 changes: 19 additions & 1 deletion src/core/AutomatableModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,10 @@ float AutomatableModel::inverseScaledValue( float value ) const

QString AutomatableModel::displayValue( const float val ) const
{
const FloatModel *floatmodel = dynamic_cast<const FloatModel*>( this );
switch( m_dataType )
{
case Float: return QString::number( castValue<float>( scaledValue( val ) ) );
case Float: return QString::number( castValue<float>( scaledValue( floatmodel->getRoundedValue() ) ) );
case Integer: return QString::number( castValue<int>( scaledValue( val ) ) );
case Bool: return QString::number( castValue<bool>( scaledValue( val ) ) );
}
Expand Down Expand Up @@ -713,6 +714,23 @@ float AutomatableModel::globalAutomationValueAt( const MidiTime& time )
}
}

float FloatModel::getRoundedValue() const
{
return static_cast<float>( static_cast<int>( value() / step<float>() + 0.5 ) ) * step<float>();
}




float FloatModel::getDigitCount()
{
float steptemp = step<float>();
int digits = 0;
while ( steptemp < 1 )
{
steptemp = steptemp / 0.1f;
digits++;
}
return digits;
}

13 changes: 7 additions & 6 deletions src/gui/widgets/Fader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ void Fader::mouseMoveEvent( QMouseEvent *mouseEvent )

float delta = dy * ( model()->maxValue() - model()->minValue() ) / (float) ( height() - ( *m_knob ).height() );

model()->setValue( m_startValue + delta );
const float step = model()->step<float>();
float newValue = static_cast<float>( static_cast<int>( ( m_startValue + delta ) / step + 0.5 ) ) * step;
model()->setValue( newValue );

updateTextFloat();
}
Expand Down Expand Up @@ -215,27 +217,26 @@ void Fader::mouseDoubleClickEvent( QMouseEvent* mouseEvent )
{
bool ok;
float newValue;

// TODO: dbV handling
if( m_displayConversion )
{
newValue = QInputDialog::getDouble( this, windowTitle(),
tr( "Please enter a new value between %1 and %2:" ).
arg( model()->minValue() * 100 ).
arg( model()->maxValue() * 100 ),
model()->value() * 100,
model()->getRoundedValue() * 100,
model()->minValue() * 100,
model()->maxValue() * 100, 4, &ok ) * 0.01f;
model()->maxValue() * 100, model()->getDigitCount(), &ok ) * 0.01f;
}
else
{
newValue = QInputDialog::getDouble( this, windowTitle(),
tr( "Please enter a new value between %1 and %2:" ).
arg( model()->minValue() ).
arg( model()->maxValue() ),
model()->value(),
model()->getRoundedValue(),
model()->minValue(),
model()->maxValue(), 4, &ok );
model()->maxValue(), model()->getDigitCount(), &ok );
}

if( ok )
Expand Down
22 changes: 12 additions & 10 deletions src/gui/widgets/Knob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
#include "templates.h"
#include "TextFloat.h"


TextFloat * Knob::s_textFloat = NULL;


Expand Down Expand Up @@ -729,6 +728,7 @@ void Knob::setPosition( const QPoint & _p )
const float oldValue = model()->value();



if( model()->isScaleLogarithmic() ) // logarithmic code
{
const float pos = model()->minValue() < 0
Expand All @@ -738,7 +738,8 @@ void Knob::setPosition( const QPoint & _p )
float newValue = value * ratio;
if( qAbs( newValue ) >= step )
{
model()->setValue( oldValue - newValue );
float roundedValue = static_cast<float>( static_cast<int>( ( oldValue - newValue ) / step + 0.5 ) ) * step;
model()->setValue( roundedValue );
m_leftOver = 0.0f;
}
else
Expand All @@ -747,12 +748,12 @@ void Knob::setPosition( const QPoint & _p )
}
}


else // linear code
{
if( qAbs( value ) >= step )
{
model()->setValue( oldValue - value );
float roundedValue = static_cast<float>( static_cast<int>( ( oldValue - value ) / step + 0.5 ) ) * step;
model()->setValue( roundedValue );
m_leftOver = 0.0f;
}
else
Expand All @@ -769,15 +770,16 @@ void Knob::enterValue()
{
bool ok;
float new_val;

if( isVolumeKnob() &&
ConfigManager::inst()->value( "app", "displaydbfs" ).toInt() )
{
new_val = QInputDialog::getDouble(
this, windowTitle(),
tr( "Please enter a new value between "
"-96.0 dBFS and 6.0 dBFS:" ),
20.0 * log10( model()->value() / 100.0 ),
-96.0, 6.0, 4, &ok );
20.0 * log10( model()->getRoundedValue() / 100.0 ),
-96.0, 6.0, model()->getDigitCount(), &ok );
if( new_val <= -96.0 )
{
new_val = 0.0f;
Expand All @@ -795,9 +797,9 @@ void Knob::enterValue()
"%1 and %2:" ).
arg( model()->minValue() ).
arg( model()->maxValue() ),
model()->value(),
model()->getRoundedValue(),
model()->minValue(),
model()->maxValue(), 4, &ok );
model()->maxValue(), model()->getDigitCount(), &ok );
}

if( ok )
Expand Down Expand Up @@ -828,11 +830,11 @@ QString Knob::displayValue() const
ConfigManager::inst()->value( "app", "displaydbfs" ).toInt() )
{
return m_description.trimmed() + QString( " %1 dBFS" ).
arg( 20.0 * log10( model()->value() / volumeRatio() ),
arg( 20.0 * log10( model()->getRoundedValue() / volumeRatio() ),
3, 'f', 2 );
}
return m_description.trimmed() + QString( " %1" ).
arg( model()->value() ) + m_unit;
arg( model()->getRoundedValue() ) + m_unit;
}


Expand Down

0 comments on commit 489553b

Please sign in to comment.