Skip to content

Commit

Permalink
Actions fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lgolouz committed Aug 28, 2022
1 parent 4651d83 commit 7f72cc0
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 0 deletions.
33 changes: 33 additions & 0 deletions sources/actions/actionbase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//*******************************************************************************
// ZX Tape Reviver
//-----------------
//
// Author: Leonid Golouz
// E-mail: [email protected]
// YouTube channel: https://www.youtube.com/channel/UCz_ktTqWVekT0P4zVW8Xgcg
// YouTube channel e-mail: [email protected]
//
// Code modification and distribution of any kind is not allowed without direct
// permission of the Author.
//*******************************************************************************

#include "actionbase.h"

ActionBase::ActionBase(int channel, const QString& name) :
m_channel(channel),
m_actionName(name)
{

}

int ActionBase::channel() const {
return m_channel;
}

const QString& ActionBase::actionName() const {
return m_actionName;
}

bool ActionBase::isActionValid(const QSharedPointer<QWavVector>& wf) const {
return !wf.isNull();
}
40 changes: 40 additions & 0 deletions sources/actions/actionbase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//*******************************************************************************
// ZX Tape Reviver
//-----------------
//
// Author: Leonid Golouz
// E-mail: [email protected]
// YouTube channel: https://www.youtube.com/channel/UCz_ktTqWVekT0P4zVW8Xgcg
// YouTube channel e-mail: [email protected]
//
// Code modification and distribution of any kind is not allowed without direct
// permission of the Author.
//*******************************************************************************

#ifndef ACTIONBASE_H
#define ACTIONBASE_H

#include "sources/defines.h"
#include <QString>
#include <QSharedPointer>

class ActionBase
{
const int m_channel;
const QString m_actionName;

public:
ActionBase(int channel, const QString& name = { });
virtual ~ActionBase() = default;

int channel() const;
const QString& actionName() const;

virtual bool apply() = 0;
virtual void undo() = 0;

protected:
virtual bool isActionValid(const QSharedPointer<QWavVector>& wf) const;
};

#endif // ACTIONBASE_H
42 changes: 42 additions & 0 deletions sources/actions/editsampleaction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//*******************************************************************************
// ZX Tape Reviver
//-----------------
//
// Author: Leonid Golouz
// E-mail: [email protected]
// YouTube channel: https://www.youtube.com/channel/UCz_ktTqWVekT0P4zVW8Xgcg
// YouTube channel e-mail: [email protected]
//
// Code modification and distribution of any kind is not allowed without direct
// permission of the Author.
//*******************************************************************************

#include "editsampleaction.h"
#include "sources/models/waveformmodel.h"
#include "sources/translations/translations.h"

EditSampleAction::EditSampleAction(int channel, const EditSampleActionParams& params) :
ActionBase(channel, qtTrId(ID_EDIT_ACTION)),
m_params(params)
{

}

bool EditSampleAction::apply() {
auto wf { WaveFormModel::instance()->getChannel(channel()) };
const bool valid { isActionValid(wf) };
if (valid) {
wf->operator[](m_params.sample) = m_params.newValue;
}

return valid;
}

void EditSampleAction::undo() {
auto wf { WaveFormModel::instance()->getChannel(channel()) };
wf->operator[](m_params.sample) = m_params.previousValue;
}

bool EditSampleAction::isActionValid(const QSharedPointer<QWavVector>& wf) const {
return ActionBase::isActionValid(wf) && m_params.sample >= 0 && m_params.sample < wf->size();
}
40 changes: 40 additions & 0 deletions sources/actions/editsampleaction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//*******************************************************************************
// ZX Tape Reviver
//-----------------
//
// Author: Leonid Golouz
// E-mail: [email protected]
// YouTube channel: https://www.youtube.com/channel/UCz_ktTqWVekT0P4zVW8Xgcg
// YouTube channel e-mail: [email protected]
//
// Code modification and distribution of any kind is not allowed without direct
// permission of the Author.
//*******************************************************************************

#ifndef EDITSAMPLEACTION_H
#define EDITSAMPLEACTION_H

#include "actionbase.h"

struct EditSampleActionParams {
QWavVectorType previousValue;
QWavVectorType newValue;
int sample;
};

class EditSampleAction : public ActionBase
{
const EditSampleActionParams m_params;

public:
EditSampleAction(int channel, const EditSampleActionParams& params);
virtual ~EditSampleAction() = default;

virtual bool apply() override;
virtual void undo() override;

private:
virtual bool isActionValid(const QSharedPointer<QWavVector>& wf) const;
};

#endif // EDITSAMPLEACTION_H
41 changes: 41 additions & 0 deletions sources/actions/shiftwaveformaction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//*******************************************************************************
// ZX Tape Reviver
//-----------------
//
// Author: Leonid Golouz
// E-mail: [email protected]
// YouTube channel: https://www.youtube.com/channel/UCz_ktTqWVekT0P4zVW8Xgcg
// YouTube channel e-mail: [email protected]
//
// Code modification and distribution of any kind is not allowed without direct
// permission of the Author.
//*******************************************************************************

#include "shiftwaveformaction.h"
#include "sources/models/waveformmodel.h"
#include "sources/translations/translations.h"

ShiftWaveFormAction::ShiftWaveFormAction(int channel, const ShiftWaveFormActionParams& params) :
ActionBase(channel, qtTrId(ID_SHIFT_WAVEFORM_ACTION)),
m_params(params)
{

}

bool ShiftWaveFormAction::apply() {
auto wf { WaveFormModel::instance()->getChannel(channel()) };
const bool valid { isActionValid(wf) };
if (valid) {
std::for_each(wf->begin(), wf->end(), [this](QWavVectorType& itm) {
itm += m_params.offsetValue;
});
}
return valid;
}

void ShiftWaveFormAction::undo() {
auto wf { WaveFormModel::instance()->getChannel(channel()) };
std::for_each(wf->begin(), wf->end(), [this](QWavVectorType& itm) {
itm -= m_params.offsetValue;
});
}
35 changes: 35 additions & 0 deletions sources/actions/shiftwaveformaction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//*******************************************************************************
// ZX Tape Reviver
//-----------------
//
// Author: Leonid Golouz
// E-mail: [email protected]
// YouTube channel: https://www.youtube.com/channel/UCz_ktTqWVekT0P4zVW8Xgcg
// YouTube channel e-mail: [email protected]
//
// Code modification and distribution of any kind is not allowed without direct
// permission of the Author.
//*******************************************************************************

#ifndef SHIFTWAVEFORMACTION_H
#define SHIFTWAVEFORMACTION_H

#include "actionbase.h"

struct ShiftWaveFormActionParams {
QWavVectorType offsetValue;
};

class ShiftWaveFormAction : public ActionBase
{
const ShiftWaveFormActionParams m_params;

public:
ShiftWaveFormAction(int channel, const ShiftWaveFormActionParams& params);
virtual ~ShiftWaveFormAction() = default;

virtual bool apply() override;
virtual void undo() override;
};

#endif // SHIFTWAVEFORMACTION_H

0 comments on commit 7f72cc0

Please sign in to comment.