Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give DrawnElement a copy constructor #4763

Conversation

kwvanderlinde
Copy link
Collaborator

@kwvanderlinde kwvanderlinde commented Apr 17, 2024

Identify the Bug or Feature request

Fixes #2433
Properly fixes #4526

Description of the Change

DrawnElement now has a copy constructor that will copy the underlying Drawable and Pen. Pen already has a copy constructor, but Drawable did not have anything for this. So a new Drawable.copy() method is added, with concrete child classes deciding how they should be copied. For practical reasons, this is always implemented via copy constructors.

The above is mostly straightforward, but ShapeDrawable is a unique case. It wraps a general Shape, but not all Shape implementations are easily copied. So for this we do case work on the type of the shape: the common cases of Area, Polygon, and RectangularShape (covering Rectangle, Ellipse2D and more), are handled explicitly. There shouldn't be other type encountered in ShapeDrawable, but to cover our bases we simply do a shallow a copy if some unknown Shape implementation is found.

In the copy constructor of Zone, each drawing is copied using the new DrawnElement copy constructor. If keepIDs is false, a new GUID will also be generated and set for each copy.

Finally, I took the opportunity to refactor Drawable.fromDto() so that each concrete class has its own fromDto() method, with Drawable.fromDto() doing nothing but delegating base on type case. I find this easier to read with the toDto() and fromDto() methods for each type sitting right next to each other.

Possible Drawbacks

Should be none.

Documentation Notes

N/A

Release Notes

  • Fixed a bug where modify a drawing on a copy of a map would affect the original drawing on the original map.

This change is Reviewable

To enable this, `Drawable` now has a `copy()` method, which is consistently implemented in child classes as delegating
to each implementation's copy constructor. The copy constructor always keeps the same ID, but callers can now modify the
ID through a new method `Drawable.setId(GUID)` for those cases where the ID needs to be given a differnt ID from the
original.
If the flag to keep IDs is not set, then new GUIDs are created and written to the copies. Otherwise, drawing IDs are
kept the same.
@kwvanderlinde kwvanderlinde self-assigned this Apr 17, 2024
Now each implementation of `Drawable` has its own `fromDto()` method that focuses only on that implementation's
needs. `Drawable.fromDto()` simply delegates to each implementation based on the type case of the parent DTO.
@kwvanderlinde kwvanderlinde force-pushed the bugfix/2433-copy-drawings-when-copying-maps branch from 5b4e34a to f452a8f Compare April 17, 2024 17:59
@cwisniew cwisniew added this pull request to the merge queue May 7, 2024
Merged via the queue into RPTools:develop with commit 4b3f0a1 May 7, 2024
4 checks passed
@kwvanderlinde kwvanderlinde deleted the bugfix/2433-copy-drawings-when-copying-maps branch May 7, 2024 04:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Merged
3 participants