-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
97 lines (89 loc) · 2.73 KB
/
Copy pathModel.cpp
File metadata and controls
97 lines (89 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "Model.h"
#include <iostream>
#include <fstream>
#include "GrObject.h"
#include "GrLine.h"
#include "GrCircle.h"
#include "GrRectangle.h"
void Model::createDoc(const std::string& name)
{
currentDocument.reset();
currentDocument = std::make_shared<Document>(name);
std::cout << "A new document with the name '" << name << "' has been created" << std::endl;
}
void Model::importDoc(const std::string& path)
{
std::ifstream file(path);
if (!file.is_open()) {
std::cerr << "Failed to open file for import: " << path << std::endl;
return;
}
if (deserializeDoc(path))
std::cout << "Document imported from file: " << path << std::endl;
else
std::cerr << "Import Error" << path << std::endl;
}
void Model::exportDoc(const std::string& path)
{
std::ofstream file(path);
if (!file.is_open()) {
std::cerr << "Failed to open file for export: " << path << std::endl;
return;
}
if (serializeDoc(path))
std::cout << "The document has been exported to a file: " << path << std::endl;
else
std::cerr << "Export Error" << path << std::endl;
}
void Model::addGrObject(const std::string& type)
{
auto result = GrObject::DecodeGrObjectType(type);
if (result.has_value()) {
std::shared_ptr<GrObject> newObj = nullptr;
switch (result.value()) {
case GrObjectType::Line:
newObj = std::make_shared<GrLine>();
break;
case GrObjectType::Circle:
newObj = std::make_shared<GrCircle>();
break;
case GrObjectType::Rectangle:
newObj = std::make_shared<GrRectangle>();
break;
default:
break;
}
currentDocument->addGrObject(std::move(newObj));
return;
}
else
std::cerr << "Invalid grObject type." << std::endl;
}
void Model::showListGrObject()
{
currentDocument->showListGrObject();
}
void Model::removeGrObject(const std::string& id)
{
for (auto& grObj : currentDocument->getObjects()) {
//TODO: не обработана ситуация, когда id не число
if (grObj->getId() == stoi(id)) {
currentDocument->removeGrObject(grObj);
break;
}
}
}
bool Model::serializeDoc(const std::string& path)
{
//Serialization here (import grObjects)
return true;
}
bool Model::deserializeDoc(const std::string& filename)
{
//Deserialization header here
auto name = "this name gets from file";
currentDocument.reset();
currentDocument = std::make_shared<Document>(name);
//Deserialization body here (export grObjects)
return true;
}