feat: Centralised Undo/Redo System via Command Pattern - #1424
Open
anushkagupta200615-jpg wants to merge 2 commits into
Open
feat: Centralised Undo/Redo System via Command Pattern#1424anushkagupta200615-jpg wants to merge 2 commits into
anushkagupta200615-jpg wants to merge 2 commits into
Conversation
Contributor
|
I noticed the new files in the PR doesn't include the standard GPL/copyright license header that's present at the top of the other source files in the repository. Could you add the same header here as well for consistency with the rest of the codebase? |
tfmoraes
reviewed
Jul 6, 2026
| Publisher.sendMessage('Remove mask', index=self.index) | ||
|
|
||
| def undo(self): | ||
| from invesalius.project import Project |
Member
There was a problem hiding this comment.
Why are importing inside methods?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR implements a robust Centralised Undo/Redo System for InVesalius 3, resolving the "Dual-Stack Problem" encountered in previous attempts (such as PR #1058) and addressing Issue #1062.
By utilizing the standard Command Pattern and unifying the history stack, this PR ensures that all operations (mask creation, duplication, deletion, and importantly, pixel-level voxel editing) are executed and reversed in strictly chronological order.
Changes Included
HistoryManager (Singleton): Tracks all project state modifications in a unified stack, guaranteeing safe memory handling (max 50 operations) and emitting PubSub events to update GUI menu labels dynamically (e.g., "Undo Add Mask", "Undo Mask Edit").
Commands Architecture: Created AddMaskCommand, RemoveMaskCommand, DuplicateMaskCommand, and MaskEditCommand.
Project Re-indexing: Modified Project.RemoveMask with a non-destructive cleanup=False parameter, allowing deleted masks to stay in memory to be seamlessly recovered upon Undo. Introduced Project.InsertMask to restore deleted masks to their exact previous dictionary indices.
Pixel-Level Stack Unification: Instead of isolating mask brushing/painting into an independent EditionHistory stack that desynchronizes with the global state, pixel edits are now wrapped in a MaskEditCommand and dispatched to the global HistoryManager.
This prevents the race-condition bug from previous PRs where pressing Ctrl+Z could accidentally delete an entire mask instead of undoing the latest brush stroke!
Issue Resolved
Closes #1062