41 {
42 if (
file_->open(QIODevice::ReadOnly | QFile::Text)) {
44
45 if (!(root.contains("Targets") && root.contains("Trappy_Circles") &&
46 root.contains("Trappy_Lines") && root.contains("Hills")))
47 throw std::invalid_argument(
48 "Invalid file format: missing some objects fields!");
49
50 QJsonArray json_targets = root["Targets"].toArray();
51 QJsonArray json_trappy_circles = root["Trappy_Circles"].toArray();
52 QJsonArray json_trappy_lines = root["Trappy_Lines"].toArray();
53 QJsonArray json_hills = root["Hills"].toArray();
54
55 std::vector<lib::Target> targets;
56
57 std::vector<unsigned short> targets_ids;
58 for (size_t i = 0; i < static_cast<size_t>(json_targets.size()); i++) {
61 targets.push_back(t);
62
64 throw std::invalid_argument(
65 "Invalid file format: there are identical id's in 'Targets'!");
66
67 targets_ids.push_back(t.
GetId());
68 }
70
71 std::vector<unsigned short> trappy_circles_ids;
72 for (size_t i = 0; i < static_cast<size_t>(json_trappy_circles.size());
73 i++) {
75 trc.
SetJsonInfo(json_trappy_circles.at(i).toObject());
76
78 throw std::invalid_argument(
79 "Invalid file format: there are identical id's in "
80 "'Trappy_Circles'!");
81
82 trappy_circles_ids.push_back(trc.
GetId());
83 }
85
86 std::vector<unsigned short> trappy_lines_ids;
87 for (size_t i = 0; i < static_cast<size_t>(json_trappy_lines.size()); i++) {
88 QJsonObject json_tr_line = json_trappy_lines.at(i).toObject();
89
90 unsigned short id1 =
91 static_cast<unsigned short>(json_tr_line.value("Id_P1").toInt());
92 unsigned short id2 =
93 static_cast<unsigned short>(json_tr_line.value("Id_P2").toInt());
94
95 std::pair<gui::Target*, gui::Target*> targets_ptrs;
96 for (const auto& target : manager->GetTargetsPtrs()) {
97 if (target->GetData().GetId() == id1) targets_ptrs.first = target;
98 if (target->GetData().GetId() == id2) targets_ptrs.second = target;
99 }
100
101 if (targets_ptrs.first == nullptr || targets_ptrs.second == nullptr)
102 throw std::invalid_argument(
103 "Invalid file format: non-existent id for 'Target' in "
104 "'Trappy_Line'!");
105
108
109 unsigned short id =
110 static_cast<unsigned short>(json_tr_line.value("Id").toInt());
112 throw std::invalid_argument(
113 "Invalid file format: there are identical id's in "
114 "'Trappy_Lines'!");
115 if (id < 30000 || id > 39999)
116 throw std::invalid_argument(
117 "Invalid file format: incorrect id in 'Trappy_Line'!");
118
119 trappy_lines_ids.push_back(id);
120 trl->GetData().SetId(id);
121 manager->Add(trl);
122 }
123
124 std::vector<unsigned short> hills_ids;
125 for (size_t i = 0; i < static_cast<size_t>(json_hills.size()); i++) {
128
130 throw std::invalid_argument(
131 "Invalid file format: there are identical id's in 'Hills'!");
132
133 hills_ids.push_back(h.
GetId());
134 }
136
138
139 }
140}
Фигура линии опасного перелета
Definition trappy_line.h:14
void SetJsonInfo(const QJsonObject &hill_obj) override
Definition hill.cpp:25
virtual unsigned short GetId() const
Возвращает значение ид. объекта внутри файла
Definition base.h:26
void SetJsonInfo(const QJsonObject &target_obj) override
Definition target.cpp:16
void SetJsonInfo(const QJsonObject &trappy_circle_obj) override
Definition trappy_circle.cpp:29
bool IsExistId(const std::vector< unsigned short > &ids, unsigned short curr_id)
Definition open.cpp:34
std::vector< lib::Target > GetTargetsFromFile(QJsonArray arr)
Definition open.cpp:4
std::vector< lib::Hill > GetHillsFromFile(QJsonArray arr)
Definition open.cpp:24
std::vector< lib::TrappyCircle > GetTrappyCirclesFromFile(QJsonArray arr)
Definition open.cpp:14