Skip to content

Commit

Permalink
Get rid of m_presetMode and isPresetMode()
Browse files Browse the repository at this point in the history
Get rid of the protected member `m_presetMode` and its corresponding
method `isPresetMode()`.

This is accomplished by introducing two new methods `saveTrack` and
`loadTrack`. These methods have a similar interface to `saveSettings`
and `loadSettings` but they additionally contain a boolean which
indicates if a preset is saved/loaded or a whole track. Both new
methods contain the previous code of `saveSettings` and `loadSettings`.
The latter two now only delegate to the new methods assuming that the
full track is to be stored/loaded if called via the overridden methods
`saveSettings` and `loadSettings`.

Adjust `saveTrackSpecificSettings` so that it also passes information
of whether a preset or a whole track is stored. This leads to changes
in the interfaces of `AutomationTrack`, `InstrumentTrack`,
`PatternTrack` and `SampleTrack`. Only the implementation of
`InstrumentTrack` uses the new information though.

Remove the `BoolGuard` class and adjust the implementation of
`InstrumentTrack::savePreset` and `InstrumentTrack::loadPreset` to
simply delegate to the `saveTrack` and `loadTrack` methods with the
correct parameters. The methods `savePreset` and `loadPreset` are kept
because they make the code more readable.

Implementation notes
---------------------
It could be considered to move `savePreset` and `loadPreset` into
`Track`.

The method `loadTrackSpecificSettings` was not adjusted with an
additional boolean because for now no implementation needs to
explicitly know if a preset or a track is restored. It could be
considered to make the interfaces symmetrical though.
  • Loading branch information
michaelgregorius committed Aug 17, 2024
1 parent e3bccb0 commit 327aa9e
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 73 deletions.
3 changes: 1 addition & 2 deletions include/AutomationTrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ class AutomationTrack : public Track
gui::TrackView * createView( gui::TrackContainerView* ) override;
Clip* createClip(const TimePos & pos) override;

void saveTrackSpecificSettings( QDomDocument & _doc,
QDomElement & _parent ) override;
void saveTrackSpecificSettings(QDomDocument& doc, QDomElement& parent, bool presetMode) override;
void loadTrackSpecificSettings( const QDomElement & _this ) override;

private:
Expand Down
3 changes: 1 addition & 2 deletions include/InstrumentTrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ class LMMS_EXPORT InstrumentTrack : public Track, public MidiEventProcessor


// called by track
void saveTrackSpecificSettings( QDomDocument & _doc,
QDomElement & _parent ) override;
void saveTrackSpecificSettings(QDomDocument& doc, QDomElement& parent, bool presetMode) override;
void loadTrackSpecificSettings( const QDomElement & _this ) override;

void savePreset(QDomDocument & doc, QDomElement & element);
Expand Down
3 changes: 1 addition & 2 deletions include/PatternTrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ class LMMS_EXPORT PatternTrack : public Track
gui::TrackView * createView( gui::TrackContainerView* tcv ) override;
Clip* createClip(const TimePos & pos) override;

void saveTrackSpecificSettings( QDomDocument & _doc,
QDomElement & _parent ) override;
void saveTrackSpecificSettings(QDomDocument& doc, QDomElement& parent, bool presetMode) override;
void loadTrackSpecificSettings( const QDomElement & _this ) override;

static PatternTrack* findPatternTrack(int pattern_num);
Expand Down
3 changes: 1 addition & 2 deletions include/SampleTrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ class SampleTrack : public Track
Clip* createClip(const TimePos & pos) override;


void saveTrackSpecificSettings( QDomDocument & _doc,
QDomElement & _parent ) override;
void saveTrackSpecificSettings(QDomDocument& doc, QDomElement& parent, bool presetMode) override;
void loadTrackSpecificSettings( const QDomElement & _this ) override;

inline IntModel * mixerChannelModel()
Expand Down
11 changes: 3 additions & 8 deletions include/Track.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,12 @@ class LMMS_EXPORT Track : public Model, public JournallingObject
virtual gui::TrackView * createView( gui::TrackContainerView * view ) = 0;
virtual Clip * createClip( const TimePos & pos ) = 0;

virtual void saveTrackSpecificSettings( QDomDocument & doc,
QDomElement & parent ) = 0;
virtual void saveTrackSpecificSettings(QDomDocument& doc, QDomElement& parent, bool presetMode) = 0;
virtual void loadTrackSpecificSettings( const QDomElement & element ) = 0;


void saveTrack(QDomDocument& doc, QDomElement& element, bool presetMode);
void loadTrack(const QDomElement& element, bool presetMode);
void saveSettings( QDomDocument & doc, QDomElement & element ) override;
void loadSettings( const QDomElement & element ) override;

Expand Down Expand Up @@ -204,12 +205,6 @@ public slots:

void toggleSolo();

protected:
bool isPresetMode() const { return m_presetMode; }

protected:
bool m_presetMode = false;

private:
TrackContainer* m_trackContainer;
Type m_type;
Expand Down
34 changes: 18 additions & 16 deletions src/core/Track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,6 @@ Track* Track::clone()
}






/*! \brief Save this track's settings to file
*
* We save the track type and its muted state and solo state, then append the track-
Expand All @@ -185,12 +181,13 @@ Track* Track::clone()
*
* \param doc The QDomDocument to use to save
* \param element The The QDomElement to save into
* \param presetMode Describes whether to save the track as a preset or not.
* \todo Does this accurately describe the parameters? I think not!?
* \todo Save the track height
*/
void Track::saveSettings( QDomDocument & doc, QDomElement & element )
void Track::saveTrack(QDomDocument& doc, QDomElement& element, bool presetMode)
{
if (!isPresetMode())
if (!presetMode)
{
element.setTagName( "track" );
}
Expand All @@ -214,11 +211,10 @@ void Track::saveSettings( QDomDocument & doc, QDomElement & element )
QDomElement tsDe = doc.createElement( nodeName() );
// let actual track (InstrumentTrack, PatternTrack, SampleTrack etc.) save its settings
element.appendChild( tsDe );
saveTrackSpecificSettings( doc, tsDe );
saveTrackSpecificSettings(doc, tsDe, presetMode);

if (isPresetMode())
if (presetMode)
{
// No need to unset preset mode here as this will done by the guard in InstrumentTrack::savePreset
return;
}

Expand All @@ -229,9 +225,6 @@ void Track::saveSettings( QDomDocument & doc, QDomElement & element )
}
}




/*! \brief Load the settings from a file
*
* We load the track's type and muted state and solo state, then clear out our
Expand All @@ -242,9 +235,10 @@ void Track::saveSettings( QDomDocument & doc, QDomElement & element )
* one at a time.
*
* \param element the QDomElement to load track settings from
* \param presetMode Indicates if a preset or a full track is loaded
* \todo Load the track height.
*/
void Track::loadSettings( const QDomElement & element )
void Track::loadTrack(const QDomElement& element, bool presetMode)
{
if( static_cast<Type>(element.attribute( "type" ).toInt()) != type() )
{
Expand All @@ -266,7 +260,7 @@ void Track::loadSettings( const QDomElement & element )
setColor(QColor{element.attribute("color")});
}

if (isPresetMode())
if (presetMode)
{
QDomNode node = element.firstChild();
while( !node.isNull() )
Expand All @@ -279,8 +273,6 @@ void Track::loadSettings( const QDomElement & element )
node = node.nextSibling();
}

// No need to unset preset mode here as this will done by the guard in InstrumentTrack::loadPreset

return;
}

Expand Down Expand Up @@ -317,6 +309,16 @@ void Track::loadSettings( const QDomElement & element )
}
}

void Track::saveSettings(QDomDocument& doc, QDomElement& element)
{
saveTrack(doc, element, false);
}

void Track::loadSettings(const QDomElement& element)
{
loadTrack(element, false);
}




Expand Down
3 changes: 1 addition & 2 deletions src/tracks/AutomationTrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ Clip* AutomationTrack::createClip(const TimePos & pos)



void AutomationTrack::saveTrackSpecificSettings( QDomDocument & _doc,
QDomElement & _this )
void AutomationTrack::saveTrackSpecificSettings(QDomDocument& doc, QDomElement& _this, bool presetMode)
{
}

Expand Down
40 changes: 4 additions & 36 deletions src/tracks/InstrumentTrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ gui::TrackView* InstrumentTrack::createView( gui::TrackContainerView* tcv )



void InstrumentTrack::saveTrackSpecificSettings( QDomDocument& doc, QDomElement & thisElement )
void InstrumentTrack::saveTrackSpecificSettings(QDomDocument& doc, QDomElement& thisElement, bool presetMode)
{
m_volumeModel.saveSettings( doc, thisElement, "vol" );
m_panningModel.saveSettings( doc, thisElement, "pan" );
Expand Down Expand Up @@ -869,7 +869,7 @@ void InstrumentTrack::saveTrackSpecificSettings( QDomDocument& doc, QDomElement
autoAssignMidiDevice(false);

// Only save the MIDI port information if we are not saving a preset.
if (!isPresetMode())
if (!presetMode)
{
m_midiPort.saveState(doc, thisElement);
}
Expand Down Expand Up @@ -994,46 +994,14 @@ void InstrumentTrack::loadTrackSpecificSettings( const QDomElement & thisElement
unlock();
}

/**
* @brief RAII "guard" used to safely change and reset booleans even during exceptions
*
* Needed to safely reset m_presetMode in savePreset and loadPreset. For this reason
* only defined locally because at least the pattern used by these methods should not
* spread outside.
*/
class BoolGuard
{
public:
BoolGuard(bool& member, bool temporaryValue) :
m_member(member),
m_oldValue(member)
{
m_member = temporaryValue;
}

~BoolGuard()
{
m_member = m_oldValue;
}

BoolGuard(const BoolGuard&) = delete;
BoolGuard& operator=(const BoolGuard&) = delete;

private:
bool & m_member;
bool const m_oldValue;
};

void InstrumentTrack::savePreset(QDomDocument & doc, QDomElement & element)
{
BoolGuard guard(m_presetMode, true);
saveSettings(doc, element);
saveTrack(doc, element, true);
}

void InstrumentTrack::loadPreset(const QDomElement & element)
{
BoolGuard guard(m_presetMode, true);
loadSettings(element);
loadTrack(element, true);
}


Expand Down
2 changes: 1 addition & 1 deletion src/tracks/PatternTrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Clip* PatternTrack::createClip(const TimePos & pos)



void PatternTrack::saveTrackSpecificSettings(QDomDocument& doc, QDomElement& _this)
void PatternTrack::saveTrackSpecificSettings(QDomDocument& doc, QDomElement& _this, bool presetMode)
{
// _this.setAttribute( "icon", m_trackLabel->pixmapFile() );
/* _this.setAttribute( "current", s_infoMap[this] ==
Expand Down
3 changes: 1 addition & 2 deletions src/tracks/SampleTrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ Clip * SampleTrack::createClip(const TimePos & pos)



void SampleTrack::saveTrackSpecificSettings( QDomDocument & _doc,
QDomElement & _this )
void SampleTrack::saveTrackSpecificSettings(QDomDocument& _doc, QDomElement& _this, bool presetMode)
{
m_audioPort.effects()->saveState( _doc, _this );
#if 0
Expand Down

0 comments on commit 327aa9e

Please sign in to comment.