Skip to content

Commit

Permalink
add insert row
Browse files Browse the repository at this point in the history
Pressing `Ins` in the pattern editor will insert an empty row at the cursor,
shifting all rows below it down by 1.
  • Loading branch information
stoneface86 committed Apr 17, 2024
1 parent 9b5b1bf commit ac1dfb8
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ out/
tools/
CMakeSettings.json
CMakeLists.txt.user
*.kdev4
.kdev4/
2 changes: 2 additions & 0 deletions src/config/data/ShortcutTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ QKeySequence ShortcutTable::getDefault(Shortcut shortcut) noexcept {
return tr("Return");
case PasteMix:
return tr("Ctrl+M");
case InsertRow:
return tr("Ins");
case TransposeDecNote:
return tr("Ctrl+F1");
case TransposeIncNote:
Expand Down
1 change: 1 addition & 0 deletions src/config/data/ShortcutTable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ShortcutTable {
PlayStop,
// QAction
PasteMix,
InsertRow,
TransposeDecNote,
TransposeIncNote,
TransposeDecOctave,
Expand Down
1 change: 1 addition & 0 deletions src/config/tabs/KeyboardConfigTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static std::array const SHORTCUT_NAMES = {
QT_TR_NOOP("Decrement octave"),
QT_TR_NOOP("Play / Stop"),
QT_TR_NOOP("Mix paste"),
QT_TR_NOOP("Insert row"),
QT_TR_NOOP("Transpose, decrease note"),
QT_TR_NOOP("Transpose, increase note"),
QT_TR_NOOP("Transpose, decrease octave"),
Expand Down
5 changes: 5 additions & 0 deletions src/forms/MainWindow/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ void MainWindow::createActions(TableActions const& instrumentActions, TableActio
act->setData(ShortcutTable::PasteMix);
connectActionTo(act, mPatternEditor, pasteMix);

act = setupAction(menuEdit, tr("&Insert Row"), tr("Inserts an empty row at the cursor"));
act->setData(ShortcutTable::InsertRow);
connectActionTo(act, mPatternEditor, insertRow);


act = setupAction(menuEdit, tr("&Erase"), tr("Erases selection contents"), QKeySequence::Delete);
connectActionTo(act, mPatternEditor, erase);

Expand Down
7 changes: 7 additions & 0 deletions src/model/PatternModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,13 @@ void PatternModel::backspace() {
}
}

void PatternModel::insertRow() {
auto cmd = new InsertRowCmd(*this);
cmd->setText(tr("insert row"));
mModule.undoStack()->push(cmd);

}

void PatternModel::setOrderRow(trackerboy::OrderRow row) {
if (order()[mCursorPattern] != row) {
auto cmd = new OrderEditCmd(*this, row, mCursorPattern);
Expand Down
3 changes: 3 additions & 0 deletions src/model/PatternModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ class PatternModel : public QObject {

void backspace();

void insertRow();

// order

//
Expand Down Expand Up @@ -281,6 +283,7 @@ class PatternModel : public QObject {
friend class OrderRemoveCmd;
friend class OrderDuplicateCmd;
friend class OrderSwapCmd;
friend class InsertRowCmd;

Q_DISABLE_COPY(PatternModel)

Expand Down
37 changes: 37 additions & 0 deletions src/model/commands/pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,40 @@ void BackspaceCmd::undo() {
mModel.invalidate(mPattern, true);
}

InsertRowCmd::InsertRowCmd(PatternModel& model, QUndoCommand* parent) :
QUndoCommand(parent),
mModel(model),
mPattern(model.mCursorPattern),
mTrack(model.mCursor.track),
mRow(model.mCursor.row),
mLastRow(model.source()->patterns().length() - 1),
mTruncated(model.currentPattern()[mLastRow][mTrack])
{
}

void InsertRowCmd::redo() {
{
auto editor = mModel.mModule.edit();
auto &track = mModel.source()->patterns().getTrack(static_cast<trackerboy::ChType>(mTrack), mPattern);
// shift down
for (auto i = mLastRow; i > mRow; --i) {
track[i] = track[i - 1];
}
track[mRow] = {};
}
mModel.invalidate(mPattern, true);
}

void InsertRowCmd::undo() {
{
auto editor = mModel.mModule.edit();
auto &track = mModel.source()->patterns().getTrack(static_cast<trackerboy::ChType>(mTrack), mPattern);
// shift up
for (auto i = mRow; i < mLastRow; ++i) {
track[i] = track[i + 1];
}
track[mLastRow] = mTruncated;
}
mModel.invalidate(mPattern, true);
}

17 changes: 17 additions & 0 deletions src/model/commands/pattern.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,20 @@ class BackspaceCmd : public QUndoCommand {
virtual void undo() override;

};

class InsertRowCmd : public QUndoCommand {

PatternModel &mModel;
int const mPattern;
int const mTrack;
int const mRow;
int const mLastRow;
trackerboy::TrackRow const mTruncated;

public:
explicit InsertRowCmd(PatternModel &model, QUndoCommand *parent = nullptr);

virtual void redo() override;

virtual void undo() override;
};
4 changes: 4 additions & 0 deletions src/widgets/PatternEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,10 @@ void PatternEditor::pasteMix() {
pasteImpl(true);
}

void PatternEditor::insertRow() {
mModel.insertRow();
}

void PatternEditor::pasteImpl(bool mix) {
if (mClipboard.hasClip()) {
auto &clip = mClipboard.clip();
Expand Down
2 changes: 2 additions & 0 deletions src/widgets/PatternEditor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class PatternEditor : public QFrame, public IMidiReceiver {

void pasteMix();

void insertRow();

void erase();

void selectAll();
Expand Down

0 comments on commit ac1dfb8

Please sign in to comment.