Issue: When creating and modifying (all except delete) an entity, doing some undos will make the program crash.

Cause: the Builder always appending new entities to document instead of updating them. When doing the first undo (for modification), it will remove the entity instead of undo the modification, and when doing the second (for creation), it'll access a nullptr and crash.

The Builder check if the entity needs to be updated here. It calls EntityContainer which only returns QuadTree entityByID() method.

First debug: Checking what happens at Builder/EntityContainer/QuadTree.

During line creation:

Builder search: 2
EntityContainer search: 2
QuadTree search: 2
QuadTree (2) not found
EntityContainer (2) return: 0
Builder found: 0

EntityContainer search: 2
QuadTree search: 2
QuadTree (2) not found
EntityContainer (2) return: 0

In the first part the Builder search if the line with ID = 2 already exists, it's not as the address is 0 (nullptr).
Second part correspond to the document checking if the entity is already present, it's not.
Creation is good.

During selection:
Lots of these lines are appearing during selection when the line is in selection area:

EntityContainer search: 2
QuadTree search: 2
QuadTree (2) not found
EntityContainer (2) return: 0

That correspond to the selection, when checking if the entity is already selected
It return the address when it's selected, so no problems in these logs and QuadTree entityByID() method.

During modification:

Builder search: 2
EntityContainer search: 2
QuadTree search: 2
QuadTree (2) not found
EntityContainer (2) return: 0
Builder found: 0

EntityContainer search: 2
QuadTree search: 2
QuadTree (2) return: 0x1dea1a0
EntityContainer (2) return: 0x1dea1a0
EntityContainer search: 2
QuadTree search: 2
QuadTree (2) return: 0x1dea4a0
EntityContainer (2) return: 0x1dea4a0

First part correspond to the Builder checking if the entity needs to be updated, no entity with the same ID was found so it consider it's a new entity.
Second part is the document receiving the modified entity and search if another entity have the same ID, and found one.

Builder and Document doesn't seem to share the same EntityContainer.
Nope.

Let's take a look at how the entity is created:
The line was created with the Builder, which calls insertEntity() method of Document given as parameter.

Here the Document checks if the entity exists and insert it. The _storageManager created the debug lines which the entity was found.

The only difference is Document using StorageManager directly and Builder use EntityContainer of the StorageManager, but both methods use EntityContainer entityByID() method.

A new test show the Builder EntityContainer contains all the entities.

Builder EC size:1
Found entity ID: 2

Search ID: 2
Not found

EntityContainer is passed by value and not reference, and QuadTree entityByID() uses a map to have fast lookups. This map might not be copied to new QuadTree in the new EntityContainer.
It's indeed not copied to new QuadTree

And it's fixed \o/