LibreCAD
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
entitybuilder.cpp
Go to the documentation of this file.
1 #include "entitybuilder.h"
3 #include <memory>
4 
5 using namespace lc;
6 using namespace operation;
7 
8 EntityBuilder::EntityBuilder(std::shared_ptr<Document> document) :
9  DocumentOperation(document, "EntityBuilder") {
10 }
11 
12 EntityBuilder* EntityBuilder::appendEntity(entity::CADEntity_CSPtr cadEntity) {
13  _workingBuffer.push_back(cadEntity);
14  return this;
15 }
16 
18  _stack.push_back(operation);
19  return this;
20 }
21 
23  processStack();
24 
25  auto ec = document()->entityContainer();
26 
27  // Build a buffer with all entities we need to remove during a undo cycle
28  for (auto entity : _workingBuffer) {
29  auto org = ec.entityByID(entity->id());
30 
31  if (org.get() != nullptr) {
32  _entitiesThatWhereUpdated.push_back(org);
33  }
34  }
35 
36  // Remove entities
37  for (auto entity : _entitiesThatNeedsRemoval) {
38  document()->removeEntity(entity);
39  }
40 
41  // Add/Update all entities in the document
42  for (auto entity : _workingBuffer) {
43  document()->insertEntity(entity);
44  }
45 }
46 
47 void EntityBuilder::undo() const {
48  for (auto entity : _workingBuffer) {
49  document()->removeEntity(entity);
50  }
51 
52  for (auto entity : _entitiesThatWhereUpdated) {
53  document()->insertEntity(entity);
54  }
55 
56  for (auto entity : _entitiesThatNeedsRemoval) {
57  document()->insertEntity(entity);
58  }
59 }
60 
61 void EntityBuilder::redo() const {
62  for (auto entity : _entitiesThatNeedsRemoval) {
63  document()->removeEntity(entity);
64  }
65 
66  for (auto entity : _workingBuffer) {
67  document()->insertEntity(entity);
68  }
69 }
70 
72  std::vector<entity::CADEntity_CSPtr> entitySet;
73 
74  for (auto it = _stack.begin(); it != _stack.end(); ++it) {
75  // Get looping stack, we currently support only one single loop!!
76  std::vector<Base_SPtr> stack(_stack.begin(), it);
77  entitySet = (*it)->process(document(), entitySet, _workingBuffer, _entitiesThatNeedsRemoval, stack);
78  }
79 
80  _stack.clear();
81 
82  _workingBuffer.insert(_workingBuffer.end(), entitySet.begin(), entitySet.end());
83 }
std::vector< entity::CADEntity_CSPtr > _workingBuffer
Definition: entitybuilder.h:53
EntityBuilder(Document_SPtr document)
Builder constructor.
std::vector< entity::CADEntity_CSPtr > _entitiesThatNeedsRemoval
Definition: entitybuilder.h:56
std::vector< entity::CADEntity_CSPtr > _entitiesThatWhereUpdated
Definition: entitybuilder.h:55
EntityBuilder * appendOperation(Base_SPtr operation)
Append operation to the stack.
EntityBuilder * appendEntity(entity::CADEntity_CSPtr cadEntity)
append entity to the stack
Definition: cadentity.h:12
std::vector< Base_SPtr > _stack
Definition: entitybuilder.h:52
virtual void redo() const
Redo a given operation.
virtual void undo() const
Undo a given operation.
void processStack()
Apply the operations Apply operations on the entities without updating the document, and clear the stack.