AutomatableModel: renamed to controllerConnection() + coding style fixes

Renamed getControllerConnection() to controllerConnection().
This commit is contained in:
Tobias Doerffel
2014-01-20 22:11:58 +01:00
parent 7644d2c559
commit f0d6d44545
8 changed files with 221 additions and 267 deletions

View File

@@ -29,33 +29,28 @@
#include "ControllerConnection.h"
float AutomatableModel::__copiedValue = 0;
float AutomatableModel::s_copiedValue = 0;
AutomatableModel::AutomatableModel( DataType _type,
const float _val,
const float _min,
const float _max,
const float _step,
::Model * _parent,
const QString & _display_name,
bool _default_constructed ) :
Model( _parent, _display_name, _default_constructed ),
m_dataType( _type ),
m_value( _val ),
m_initValue( _val ),
m_minValue( _min ),
m_maxValue( _max ),
m_step( _step ),
m_range( _max - _min ),
AutomatableModel::AutomatableModel( DataType type,
const float val, const float min, const float max, const float step,
Model* parent, const QString & displayName, bool defaultConstructed ) :
Model( parent, displayName, defaultConstructed ),
m_dataType( type ),
m_value( val ),
m_initValue( val ),
m_minValue( min ),
m_maxValue( max ),
m_step( step ),
m_range( max - min ),
m_journalEntryReady( false ),
m_setValueDepth( 0 ),
m_hasLinkedModels( false ),
m_controllerConnection( NULL )
{
setInitValue( _val );
setInitValue( val );
}
@@ -88,55 +83,54 @@ bool AutomatableModel::isAutomated() const
void AutomatableModel::saveSettings( QDomDocument & _doc, QDomElement & _this,
const QString & _name )
void AutomatableModel::saveSettings( QDomDocument& doc, QDomElement& element, const QString& name )
{
if( isAutomated() )
{
QDomElement me = _doc.createElement( _name );
QDomElement me = doc.createElement( name );
me.setAttribute( "id", id() );
me.setAttribute( "value", m_value );
_this.appendChild( me );
element.appendChild( me );
}
else
{
_this.setAttribute( _name, m_value );
element.setAttribute( name, m_value );
}
if( m_controllerConnection )
{
QDomElement controller_element;
QDomNode node = _this.namedItem( "connection" );
QDomElement controllerElement;
QDomNode node = element.namedItem( "connection" );
if( node.isElement() )
{
controller_element = node.toElement();
controllerElement = node.toElement();
}
else
{
controller_element = _doc.createElement( "connection" );
_this.appendChild( controller_element );
controllerElement = doc.createElement( "connection" );
element.appendChild( controllerElement );
}
QDomElement element = _doc.createElement( _name );
m_controllerConnection->saveSettings( _doc, element );
controller_element.appendChild( element );
QDomElement element = doc.createElement( name );
m_controllerConnection->saveSettings( doc, element );
controllerElement.appendChild( element );
}
}
void AutomatableModel::loadSettings( const QDomElement & _this,
const QString & _name )
void AutomatableModel::loadSettings( const QDomElement& element, const QString& name )
{
// compat code
QDomNode node = _this.namedItem( AutomationPattern::classNodeName() );
QDomNode node = element.namedItem( AutomationPattern::classNodeName() );
if( node.isElement() )
{
node = node.namedItem( _name );
node = node.namedItem( name );
if( node.isElement() )
{
AutomationPattern * p = AutomationPattern::
globalAutomationPattern( this );
AutomationPattern * p = AutomationPattern::globalAutomationPattern( this );
p->loadSettings( node.toElement() );
setValue( p->valueAt( 0 ) );
// in older projects we sometimes have odd automations
@@ -149,7 +143,7 @@ void AutomatableModel::loadSettings( const QDomElement & _this,
}
}
node = _this.namedItem( _name );
node = element.namedItem( name );
if( node.isElement() )
{
changeID( node.toElement().attribute( "id" ).toInt() );
@@ -157,10 +151,10 @@ void AutomatableModel::loadSettings( const QDomElement & _this,
return;
}
node = _this.namedItem( "connection" );
node = element.namedItem( "connection" );
if( node.isElement() )
{
node = node.namedItem( _name );
node = node.namedItem( name );
if( node.isElement() )
{
setControllerConnection( new ControllerConnection( (Controller*)NULL ) );
@@ -169,9 +163,9 @@ void AutomatableModel::loadSettings( const QDomElement & _this,
}
}
if( _this.hasAttribute( _name ) )
if( element.hasAttribute( name ) )
{
setInitValue( _this.attribute( _name ).toFloat() );
setInitValue( element.attribute( name ).toFloat() );
}
else
{
@@ -182,29 +176,24 @@ void AutomatableModel::loadSettings( const QDomElement & _this,
void AutomatableModel::setValue( const float _value )
void AutomatableModel::setValue( const float value )
{
++m_setValueDepth;
const float old_val = m_value;
m_value = fittedValue( _value );
m_value = fittedValue( value );
if( old_val != m_value )
{
// add changes to history so user can undo it
addJournalEntry( JournalEntry( 0, m_value - old_val ) );
// notify linked models
for( AutoModelVector::Iterator it =
m_linkedModels.begin();
it != m_linkedModels.end(); ++it )
for( AutoModelVector::Iterator it = m_linkedModels.begin(); it != m_linkedModels.end(); ++it )
{
if( (*it)->m_setValueDepth < 1 &&
(*it)->fittedValue( _value ) !=
(*it)->m_value )
if( (*it)->m_setValueDepth < 1 && (*it)->fittedValue( value ) != (*it)->m_value )
{
bool journalling = (*it)->testAndSetJournalling(
isJournalling() );
(*it)->setValue( _value );
bool journalling = (*it)->testAndSetJournalling( isJournalling() );
(*it)->setValue( value );
(*it)->setJournalling( journalling );
}
}
@@ -220,13 +209,13 @@ void AutomatableModel::setValue( const float _value )
void AutomatableModel::setAutomatedValue( const float _value )
void AutomatableModel::setAutomatedValue( const float value )
{
++m_setValueDepth;
const float old_val = m_value;
const float oldValue = m_value;
m_value = fittedValue( _value );
if( old_val != m_value )
m_value = fittedValue( value );
if( oldValue != m_value )
{
// notify linked models
for( AutoModelVector::Iterator it = m_linkedModels.begin();
@@ -247,19 +236,20 @@ void AutomatableModel::setAutomatedValue( const float _value )
void AutomatableModel::setRange( const float _min, const float _max,
const float _step )
void AutomatableModel::setRange( const float min, const float max,
const float step )
{
if( ( m_maxValue != _max ) || ( m_minValue != _min ) )
if( ( m_maxValue != max ) || ( m_minValue != min ) )
{
m_minValue = _min;
m_maxValue = _max;
m_minValue = min;
m_maxValue = max;
if( m_minValue > m_maxValue )
{
qSwap<float>( m_minValue, m_maxValue );
}
m_range = m_maxValue - m_minValue;
setStep( _step );
setStep( step );
// re-adjust value
setInitValue( value<float>() );
@@ -271,11 +261,11 @@ void AutomatableModel::setRange( const float _min, const float _max,
void AutomatableModel::setStep( const float _step )
void AutomatableModel::setStep( const float step )
{
if( m_step != _step )
if( m_step != step )
{
m_step = _step;
m_step = step;
emit propertiesChanged();
}
}
@@ -283,63 +273,61 @@ void AutomatableModel::setStep( const float _step )
float AutomatableModel::fittedValue( float _value ) const
float AutomatableModel::fittedValue( float value ) const
{
_value = tLimit<float>( _value, m_minValue, m_maxValue );
value = tLimit<float>( value, m_minValue, m_maxValue );
if( m_step != 0 )
{
_value = roundf( _value / m_step ) * m_step;
value = roundf( value / m_step ) * m_step;
}
else
{
_value = m_minValue;
value = m_minValue;
}
// correct rounding error at the border
if( qAbs<float>( _value - m_maxValue ) <
typeInfo<float>::minEps() * qAbs<float>( m_step ) )
if( qAbs<float>( value - m_maxValue ) < typeInfo<float>::minEps() * qAbs<float>( m_step ) )
{
_value = m_maxValue;
value = m_maxValue;
}
// correct rounding error if value = 0
if( qAbs<float>( _value ) < typeInfo<float>::minEps() *
qAbs<float>( m_step ) )
if( qAbs<float>( value ) < typeInfo<float>::minEps() * qAbs<float>( m_step ) )
{
_value = 0;
value = 0;
}
if( _value < m_minValue )
if( value < m_minValue )
{
return m_minValue;
}
else if( _value > m_maxValue )
else if( value > m_maxValue )
{
return m_maxValue;
}
return _value;
return value;
}
void AutomatableModel::redoStep( JournalEntry & _je )
void AutomatableModel::redoStep( JournalEntry& je )
{
bool journalling = testAndSetJournalling( false );
setValue( value<float>() + (float) _je.data().toDouble() );
setValue( value<float>() + (float) je.data().toDouble() );
setJournalling( journalling );
}
void AutomatableModel::undoStep( JournalEntry & _je )
void AutomatableModel::undoStep( JournalEntry& je )
{
JournalEntry je( _je.actionID(), -_je.data().toDouble() );
redoStep( je );
JournalEntry inv( je.actionID(), -je.data().toDouble() );
redoStep( inv );
}
@@ -362,8 +350,7 @@ void AutomatableModel::addJournalEntryFromOldToCurVal()
restoreJournallingState();
if( value<float>() != m_oldValue )
{
addJournalEntry( JournalEntry( 0, value<float>() -
m_oldValue ) );
addJournalEntry( JournalEntry( 0, value<float>() - m_oldValue ) );
}
m_journalEntryReady = false;
}
@@ -372,16 +359,16 @@ void AutomatableModel::addJournalEntryFromOldToCurVal()
void AutomatableModel::linkModel( AutomatableModel * _model )
void AutomatableModel::linkModel( AutomatableModel* model )
{
if( !m_linkedModels.contains( _model ) )
if( !m_linkedModels.contains( model ) )
{
m_linkedModels.push_back( _model );
m_linkedModels.push_back( model );
m_hasLinkedModels = true;
if( !_model->m_hasLinkedModels )
if( !model->hasLinkedModels() )
{
QObject::connect( this, SIGNAL( dataChanged() ),
_model, SIGNAL( dataChanged() ) );
QObject::connect( this, SIGNAL( dataChanged() ), model, SIGNAL( dataChanged() ) );
}
}
}
@@ -389,10 +376,9 @@ void AutomatableModel::linkModel( AutomatableModel * _model )
void AutomatableModel::unlinkModel( AutomatableModel * _model )
void AutomatableModel::unlinkModel( AutomatableModel* model )
{
AutoModelVector::Iterator it =
qFind( m_linkedModels.begin(), m_linkedModels.end(), _model );
AutoModelVector::Iterator it = qFind( m_linkedModels.begin(), m_linkedModels.end(), model );
if( it != m_linkedModels.end() )
{
m_linkedModels.erase( it );
@@ -405,21 +391,19 @@ void AutomatableModel::unlinkModel( AutomatableModel * _model )
void AutomatableModel::linkModels( AutomatableModel * _model1,
AutomatableModel * _model2 )
void AutomatableModel::linkModels( AutomatableModel* model1, AutomatableModel* model2 )
{
_model1->linkModel( _model2 );
_model2->linkModel( _model1 );
model1->linkModel( model2 );
model2->linkModel( model1 );
}
void AutomatableModel::unlinkModels( AutomatableModel * _model1,
AutomatableModel * _model2 )
void AutomatableModel::unlinkModels( AutomatableModel* model1, AutomatableModel* model2 )
{
_model1->unlinkModel( _model2 );
_model2->unlinkModel( _model1 );
model1->unlinkModel( model2 );
model2->unlinkModel( model1 );
}
@@ -438,41 +422,37 @@ void AutomatableModel::unlinkAllModels()
void AutomatableModel::setControllerConnection( ControllerConnection * _c )
void AutomatableModel::setControllerConnection( ControllerConnection* c )
{
m_controllerConnection = _c;
if( _c )
m_controllerConnection = c;
if( c )
{
QObject::connect( m_controllerConnection,
SIGNAL( valueChanged() ),
this, SIGNAL( dataChanged() ) );
QObject::connect( m_controllerConnection,
SIGNAL( destroyed() ),
this, SLOT( unlinkControllerConnection() ) );
QObject::connect( m_controllerConnection, SIGNAL( valueChanged() ), this, SIGNAL( dataChanged() ) );
QObject::connect( m_controllerConnection, SIGNAL( destroyed() ), this, SLOT( unlinkControllerConnection() ) );
emit dataChanged();
}
}
float AutomatableModel::controllerValue( int _frameOffset ) const
float AutomatableModel::controllerValue( int frameOffset ) const
{
if( m_controllerConnection )
{
const float v = m_minValue +
( m_range * m_controllerConnection->currentValue(
_frameOffset ) );
const float v = minValue<float>() + ( range() * controllerConnection()->currentValue( frameOffset ) );
if( typeInfo<float>::isEqual( m_step, 1 ) )
{
return qRound( v );
}
return v;
}
AutomatableModel * lm = m_linkedModels.first();
if( lm->m_controllerConnection )
AutomatableModel* lm = m_linkedModels.first();
if( lm->controllerConnection() )
{
return lm->controllerValue( _frameOffset );
return lm->controllerValue( frameOffset );
}
return lm->m_value;
}
@@ -492,13 +472,13 @@ void AutomatableModel::unlinkControllerConnection()
void AutomatableModel::setInitValue( const float _value )
void AutomatableModel::setInitValue( const float value )
{
m_initValue = fittedValue( _value );
m_initValue = fittedValue( value );
bool journalling = testAndSetJournalling( false );
setValue( _value );
setValue( value );
setJournalling( journalling );
emit initValueChanged( _value );
emit initValueChanged( value );
}
@@ -514,7 +494,7 @@ void AutomatableModel::reset()
void AutomatableModel::copyValue()
{
__copiedValue = value<float>();
s_copiedValue = value<float>();
}
@@ -522,7 +502,7 @@ void AutomatableModel::copyValue()
void AutomatableModel::pasteValue()
{
setValue( __copiedValue );
setValue( copiedValue() );
}

View File

@@ -203,7 +203,7 @@ bool Controller::hasModel( const Model * m )
return true;
}
ControllerConnection * cc = am->getControllerConnection();
ControllerConnection * cc = am->controllerConnection();
if( cc != NULL )
{
if( cc->getController()->hasModel( m ) )

View File

@@ -102,9 +102,9 @@ void AutomatableModelView::addDefaultActions( QMenu* menu )
}
QString controllerTxt;
if( model->getControllerConnection() )
if( model->controllerConnection() )
{
Controller* cont = model->getControllerConnection()->getController();
Controller* cont = model->controllerConnection()->getController();
if( cont )
{
controllerTxt = AutomatableModel::tr( "Connected to %1" ).arg( cont->name() );
@@ -185,9 +185,9 @@ void AutomatableModelViewSlots::execConnectionDialog()
if( d.chosenController() )
{
// Update
if( m->getControllerConnection() )
if( m->controllerConnection() )
{
m->getControllerConnection()->setController( d.chosenController() );
m->controllerConnection()->setController( d.chosenController() );
}
// New
else
@@ -212,9 +212,9 @@ void AutomatableModelViewSlots::removeConnection()
{
AutomatableModel* m = m_amv->modelUntyped();
if( m->getControllerConnection() )
if( m->controllerConnection() )
{
delete m->getControllerConnection();
delete m->controllerConnection();
m->setControllerConnection( NULL );
}
}

View File

@@ -49,8 +49,8 @@
class AutoDetectMidiController : public MidiController
{
public:
AutoDetectMidiController( Model * _parent ) :
MidiController( _parent ),
AutoDetectMidiController( Model* parent ) :
MidiController( parent ),
m_detectedMidiChannel( 0 ),
m_detectedMidiController( 0 )
{
@@ -63,17 +63,14 @@ public:
}
virtual void processInEvent( const midiEvent & _me,
const midiTime & _time )
virtual void processInEvent( const midiEvent& event, const midiTime& time )
{
if( _me.m_type == MidiControlChange &&
( m_midiPort.inputChannel() == _me.m_channel + 1 ||
m_midiPort.inputChannel() == 0 ) )
if( event.m_type == MidiControlChange &&
( m_midiPort.inputChannel() == event.m_channel + 1 || m_midiPort.inputChannel() == 0 ) )
{
m_detectedMidiChannel = _me.m_channel + 1;
m_detectedMidiController = ( _me.m_data.m_bytes[0] & 0x7F ) + 1;
m_detectedMidiPort =
engine::mixer()->midiClient()->sourcePortName( _me );
m_detectedMidiChannel = event.m_channel + 1;
m_detectedMidiController = ( event.m_data.m_bytes[0] & 0x7F ) + 1;
m_detectedMidiPort = engine::mixer()->midiClient()->sourcePortName( event );
emit valueChanged();
}
@@ -82,9 +79,9 @@ public:
// Would be a nice copy ctor, but too hard to add copy ctor because
// model has none.
MidiController * copyToMidiController( Model * _parent )
MidiController* copyToMidiController( Model* parent )
{
MidiController * c = new MidiController( _parent );
MidiController* c = new MidiController( parent );
c->m_midiPort.setInputChannel( m_midiPort.inputChannel() );
c->m_midiPort.setInputController( m_midiPort.inputController() );
c->subscribeReadablePorts( m_midiPort.readablePorts() );
@@ -98,12 +95,12 @@ public:
{
m_midiPort.setInputChannel( m_detectedMidiChannel );
m_midiPort.setInputController( m_detectedMidiController );
MidiPort::Map map = m_midiPort.readablePorts();
for( MidiPort::Map::Iterator it = map.begin(); it != map.end(); ++it )
const MidiPort::Map& map = m_midiPort.readablePorts();
for( MidiPort::Map::ConstIterator it = map.begin(); it != map.end(); ++it )
{
m_midiPort.subscribeReadablePort( it.key(),
m_detectedMidiPort.isEmpty() ||
( it.key() == m_detectedMidiPort ) );
m_detectedMidiPort.isEmpty() || ( it.key() == m_detectedMidiPort ) );
}
}
@@ -243,31 +240,25 @@ ControllerConnectionDialog::ControllerConnectionDialog( QWidget * _parent,
ControllerConnection * cc = NULL;
if( m_targetModel )
{
cc = m_targetModel->getControllerConnection();
cc = m_targetModel->controllerConnection();
if( cc && cc->getController()->type() !=
Controller::DummyController && engine::getSong() )
if( cc && cc->getController()->type() != Controller::DummyController && engine::getSong() )
{
if ( cc->getController()->type() ==
Controller::MidiController )
if ( cc->getController()->type() == Controller::MidiController )
{
m_midiGroupBox->model()->setValue( true );
// ensure controller is created
midiToggled();
MidiController * cont =
(MidiController*)( cc->getController() );
m_midiChannelSpinBox->model()->setValue(
cont->m_midiPort.inputChannel() );
m_midiControllerSpinBox->model()->setValue(
cont->m_midiPort.inputController() );
MidiController * cont = (MidiController*)( cc->getController() );
m_midiChannelSpinBox->model()->setValue( cont->m_midiPort.inputChannel() );
m_midiControllerSpinBox->model()->setValue( cont->m_midiPort.inputController() );
m_midiController->subscribeReadablePorts( static_cast<MidiController*>( cc->getController() )->m_midiPort.readablePorts() );
}
else
{
int idx = engine::getSong()->controllers().indexOf(
cc->getController() );
int idx = engine::getSong()->controllers().indexOf( cc->getController() );
if( idx >= 0 )
{
@@ -293,8 +284,7 @@ ControllerConnectionDialog::~ControllerConnectionDialog()
{
delete m_readablePorts;
if( m_midiController )
delete m_midiController;
delete m_midiController;
}
@@ -308,8 +298,7 @@ void ControllerConnectionDialog::selectController()
if( m_midiControllerSpinBox->model()->value() > 0 )
{
MidiController * mc;
mc = m_midiController->copyToMidiController(
engine::getSong() );
mc = m_midiController->copyToMidiController( engine::getSong() );
/*
if( m_targetModel->getTrack() &&
@@ -367,24 +356,21 @@ void ControllerConnectionDialog::midiToggled()
m_midiController = new AutoDetectMidiController( engine::getSong() );
MidiPort::Map map = m_midiController->m_midiPort.readablePorts();
for( MidiPort::Map::Iterator it = map.begin();
it != map.end(); ++it )
for( MidiPort::Map::Iterator it = map.begin(); it != map.end(); ++it )
{
it.value() = true;
}
m_midiController->subscribeReadablePorts( map );
m_midiChannelSpinBox->setModel(
&m_midiController->m_midiPort.m_inputChannelModel );
m_midiControllerSpinBox->setModel(
&m_midiController->m_midiPort.m_inputControllerModel );
m_midiChannelSpinBox->setModel( &m_midiController->m_midiPort.m_inputChannelModel );
m_midiControllerSpinBox->setModel( &m_midiController->m_midiPort.m_inputControllerModel );
if( m_readablePorts )
{
m_readablePorts->setModel( &m_midiController->m_midiPort );
}
connect( m_midiController, SIGNAL( valueChanged() ),
this, SLOT( midiValueChanged() ) );
connect( m_midiController, SIGNAL( valueChanged() ), this, SLOT( midiValueChanged() ) );
}
}
m_midiAutoDetect.setValue( enabled );

View File

@@ -1,7 +1,7 @@
/*
* knob.cpp - powerful knob-widget
*
* Copyright (c) 2004-2011 Tobias Doerffel <tobydox/at/users.sourceforge.net>
* Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
*
@@ -635,9 +635,8 @@ void knob::enterValue()
void knob::friendlyUpdate()
{
if( model()->getControllerConnection() == NULL ||
model()->getControllerConnection()->getController()->
frequentUpdates() == false ||
if( model()->controllerConnection() == NULL ||
model()->controllerConnection()->getController()->frequentUpdates() == false ||
Controller::runningFrames() % (256*4) == 0 )
{
update();