Skip to content

Commit

Permalink
send correct changed items when undoing changes
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanPudashkin committed Dec 18, 2024
1 parent 24fc271 commit 196c64b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 41 deletions.
57 changes: 18 additions & 39 deletions src/engraving/dom/cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ using namespace muse::io;
using namespace mu::engraving;

namespace mu::engraving {
static UndoMacro::ChangesInfo changesInfo(const UndoStack* stack, bool undo)
static UndoMacro::ChangesInfo changesInfo(const UndoStack* stack, bool undo = false)
{
IF_ASSERT_FAILED(stack) {
static UndoMacro::ChangesInfo empty;
Expand All @@ -111,13 +111,12 @@ static UndoMacro::ChangesInfo changesInfo(const UndoStack* stack, bool undo)
return actualMacro->changesInfo(undo);
}

static std::pair<int, int> changedTicksRange(const CmdState& cmdState, const std::map<EngravingItem*,
std::unordered_set<CommandType> >& changedItems)
static ScoreChangesRange buildChangesRange(const CmdState& cmdState, const UndoMacro::ChangesInfo& changes)
{
int startTick = cmdState.startTick().ticks();
int endTick = cmdState.endTick().ticks();

for (const auto& pair : changedItems) {
for (const auto& pair : changes.changedItems) {
int tick = pair.first->tick().ticks();

if (startTick > tick) {
Expand All @@ -129,7 +128,12 @@ static std::pair<int, int> changedTicksRange(const CmdState& cmdState, const std
}
}

return { startTick, endTick };
return { startTick, endTick,
cmdState.startStaff(), cmdState.endStaff(),
std::move(changes.changedItems),
std::move(changes.changedObjectTypes),
std::move(changes.changedPropertyIdSet),
std::move(changes.changedStyleIdSet) };
}

//---------------------------------------------------------
Expand Down Expand Up @@ -357,36 +361,22 @@ void Score::undoRedo(bool undo, EditData* ed)
//! NOTE: the order of operations is very important here
//! 1. for the undo operation, the list of changed elements is available before undo()
//! 2. for the redo operation, the list of changed elements will be available after redo()
UndoMacro::ChangesInfo changes = changesInfo(undoStack(), undo);
UndoMacro::ChangesInfo changes;

cmdState().reset();
if (undo) {
changes = changesInfo(undoStack(), undo);
undoStack()->undo(ed);
} else {
undoStack()->redo(ed);
changes = changesInfo(undoStack());
}

update(false);
masterScore()->setPlaylistDirty(); // TODO: flag all individual operations
updateSelection();

ScoreChangesRange range = changesRange(undo);

if (range.changedItems.empty()) {
range.changedItems = std::move(changes.changedItems);
}

if (range.changedTypes.empty()) {
range.changedTypes = std::move(changes.changedObjectTypes);
}

if (range.changedPropertyIdSet.empty()) {
range.changedPropertyIdSet = std::move(changes.changedPropertyIdSet);
}

if (range.changedStyleIdSet.empty()) {
range.changedStyleIdSet = std::move(changes.changedStyleIdSet);
}

ScoreChangesRange range = buildChangesRange(cmdState(), changes);
changesChannel().send(range);
}

Expand Down Expand Up @@ -418,7 +408,10 @@ void Score::endCmd(bool rollback, bool layoutAllParts)

update(false, layoutAllParts);

ScoreChangesRange range = changesRange();
ScoreChangesRange range;
if (!rollback) {
range = buildChangesRange(cmdState(), changesInfo(undoStack()));
}

LOGD() << "Undo stack current macro child count: " << undoStack()->activeCommand()->childCount();

Expand All @@ -436,20 +429,6 @@ void Score::endCmd(bool rollback, bool layoutAllParts)
}
}

ScoreChangesRange Score::changesRange(bool undo) const
{
const CmdState& cmdState = score()->cmdState();
UndoMacro::ChangesInfo changes = changesInfo(undoStack(), undo);
auto ticksRange = changedTicksRange(cmdState, changes.changedItems);

return { ticksRange.first, ticksRange.second,
cmdState.startStaff(), cmdState.endStaff(),
std::move(changes.changedItems),
std::move(changes.changedObjectTypes),
std::move(changes.changedPropertyIdSet),
std::move(changes.changedStyleIdSet) };
}

#ifndef NDEBUG
//---------------------------------------------------------
// CmdState::dump
Expand Down
2 changes: 0 additions & 2 deletions src/engraving/dom/score.h
Original file line number Diff line number Diff line change
Expand Up @@ -1052,8 +1052,6 @@ class Score : public EngravingObject, public muse::Injectable

static std::set<Score*> validScores;

ScoreChangesRange changesRange(bool undo = false) const;

Note* getSelectedNote();
ChordRest* nextTrack(ChordRest* cr, bool skipMeasureRepeatRests = true);
ChordRest* prevTrack(ChordRest* cr, bool skipMeasureRepeatRests = true);
Expand Down

0 comments on commit 196c64b

Please sign in to comment.