Locus No Pilotus
Project of four first grade MIPT DAFE/RSE students (for engineering practical work in the second semester) in Qt C++
Loading...
Searching...
No Matches
GuiJsonFile Class Reference

#include <gui_json_file.h>

Public Member Functions

 GuiJsonFile ()=default
 
 GuiJsonFile (QFile *file)
 
void Close ()
 
QString GetAbsolutePath () const
 
QString GetFileName () const
 
QString GetParentPath () const
 
bool IsChanged (data_tools::DataManager *plot_area) const
 
bool IsExistsFile () const
 
void Open (data_tools::DataManager *plot_area)
 
void Save (data_tools::DataManager *plot_area)
 
void SetFile (const QString &file_name)
 
void SetUntitledFile ()
 Нужно для того, чтобы при открытии приложения менялось имя "Untitled" файла, так как может быть такое, что пользователь уже создавал такие файлы
 

Private Member Functions

QJsonObject LoadJson () const
 

Private Attributes

QFile * file_ {new QFile}
 

Constructor & Destructor Documentation

◆ GuiJsonFile() [1/2]

GuiJsonFile::GuiJsonFile ( )
default

◆ GuiJsonFile() [2/2]

GuiJsonFile::GuiJsonFile ( QFile * file)
inline
9: file_{file} {};
QFile * file_
Definition gui_json_file.h:36

Member Function Documentation

◆ Close()

void GuiJsonFile::Close ( )
inline
13{ file_->close(); }
Here is the caller graph for this function:

◆ GetAbsolutePath()

QString GuiJsonFile::GetAbsolutePath ( ) const
inline
20 {
21 return QString::fromStdString(file_->filesystemFileName().string());
22 }
Here is the caller graph for this function:

◆ GetFileName()

QString GuiJsonFile::GetFileName ( ) const
inline
15 {
16 return QString::fromStdString(
17 file_->filesystemFileName().filename().string());
18 }
Here is the caller graph for this function:

◆ GetParentPath()

QString GuiJsonFile::GetParentPath ( ) const
inline
24 {
25 return QString::fromStdString(
26 file_->filesystemFileName().parent_path().string());
27 }
Here is the caller graph for this function:

◆ IsChanged()

bool GuiJsonFile::IsChanged ( data_tools::DataManager * plot_area) const
31 {
32 file_->open(QIODevice::ReadOnly | QFile::Text);
33 QJsonObject root = LoadJson();
34
35 QJsonArray json_targets = root["Targets"].toArray();
36 QJsonArray json_trappy_circles = root["Trappy_Circles"].toArray();
37 QJsonArray json_trappy_lines = root["Trappy_Lines"].toArray();
38 QJsonArray json_hills = root["Hills"].toArray();
39
40 if (manager->GetTargets().size() ==
41 static_cast<size_t>(json_targets.size()) &&
42 manager->GetTrappyCircles().size() ==
43 static_cast<size_t>(json_trappy_circles.size()) &&
44 manager->GetTrappyLines().size() ==
45 static_cast<size_t>(json_trappy_lines.size()) &&
46 manager->GetHills().size() == static_cast<size_t>(json_hills.size())) {
47 for (size_t i = 0; i < manager->GetTargets().size(); i++) {
48 lib::Target t = manager->GetTargets()[i].GetData();
49 if (t.IsChanged(json_targets.at(i).toObject())) {
50 file_->close();
51 return true;
52 }
53 }
54
55 for (size_t i = 0; i < manager->GetTrappyCircles().size(); i++) {
56 lib::TrappyCircle tc = manager->GetTrappyCircles()[i].GetData();
57 if (tc.IsChanged(json_trappy_circles.at(i).toObject())) {
58 file_->close();
59 return true;
60 }
61 }
62
63 for (size_t i = 0; i < manager->GetTrappyLines().size(); i++) {
64 lib::TrappyLine tl = manager->GetTrappyLines()[i].GetData();
65 if (tl.IsChanged(json_trappy_lines.at(i).toObject())) {
66 file_->close();
67 return true;
68 }
69 }
70
71 for (size_t i = 0; i < manager->GetHills().size(); i++) {
72 lib::Hill h = manager->GetHills()[i].GetData();
73 if (h.IsChanged(json_hills.at(i).toObject())) {
74 file_->close();
75 return true;
76 }
77 }
78
79 file_->close();
80 return false;
81 }
82
83 file_->close();
84 return true;
85}
QJsonObject LoadJson() const
Definition new.cpp:25
Рельеф с высотой
Definition hill.h:13
bool IsChanged(const QJsonObject &hill_obj) const override
Definition hill.cpp:51
Контрольная точка
Definition target.h:12
bool IsChanged(const QJsonObject &target_obj) const override
Definition target.cpp:36
Опасная зона
Definition trappy_circle.h:9
bool IsChanged(const QJsonObject &trappy_circle_obj) const override
Definition trappy_circle.cpp:54
Линия опасного перелета
Definition trappy_line.h:13
bool IsChanged(const QJsonObject &trappy_line_obj) const override
Definition trappy_line.cpp:19
Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsExistsFile()

bool GuiJsonFile::IsExistsFile ( ) const
inline
32{ return file_->exists(); }
Here is the caller graph for this function:

◆ LoadJson()

QJsonObject GuiJsonFile::LoadJson ( ) const
private
25 {
26 QString json_text = file_->readAll();
27 QJsonDocument json_file = QJsonDocument::fromJson(json_text.toUtf8());
28 return json_file.object();
29}
Here is the caller graph for this function:

◆ Open()

void GuiJsonFile::Open ( data_tools::DataManager * plot_area)
41 {
42 if (file_->open(QIODevice::ReadOnly | QFile::Text)) {
43 QJsonObject root = LoadJson();
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++) {
60 t.SetJsonInfo(json_targets.at(i).toObject());
61 targets.push_back(t);
62
63 if (IsExistId(targets_ids, t.GetId()))
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 }
69 manager->Set(GetTargetsFromFile(json_targets));
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
77 if (IsExistId(trappy_circles_ids, trc.GetId()))
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 }
84 manager->Set(GetTrappyCirclesFromFile(json_trappy_circles));
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
106 gui::TrappyLine* trl{
107 new gui::TrappyLine(targets_ptrs.first, targets_ptrs.second)};
108
109 unsigned short id =
110 static_cast<unsigned short>(json_tr_line.value("Id").toInt());
111 if (IsExistId(trappy_lines_ids, id))
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++) {
126 lib::Hill h;
127 h.SetJsonInfo(json_hills.at(i).toObject());
128
129 if (IsExistId(hills_ids, h.GetId()))
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 }
135 manager->Set(GetHillsFromFile(json_hills));
136
137 file_->close();
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Save()

void GuiJsonFile::Save ( data_tools::DataManager * plot_area)
4 {
5 QJsonObject root;
6
7 QJsonArray targets_array;
8 QJsonArray trappy_circles_array;
9 QJsonArray trappy_lines_array;
10 QJsonArray hills_array;
11
12 for (size_t i = 0; i < manager->GetTargets().size(); i++)
13 targets_array.append(manager->GetTargets()[i].GetData().GetJsonInfo());
14 root.insert("Targets", targets_array);
15
16 for (size_t i = 0; i < manager->GetTrappyCircles().size(); i++)
17 trappy_circles_array.append(
18 manager->GetTrappyCircles()[i].GetData().GetJsonInfo());
19 root.insert("Trappy_Circles", trappy_circles_array);
20
21 for (size_t i = 0; i < manager->GetTrappyLines().size(); i++)
22 trappy_lines_array.append(
23 manager->GetTrappyLines()[i].GetData().GetJsonInfo());
24 root.insert("Trappy_Lines", trappy_lines_array);
25
26 for (size_t i = 0; i < manager->GetHills().size(); i++)
27 hills_array.append(manager->GetHills()[i].GetData().GetJsonInfo());
28 root.insert("Hills", hills_array);
29
30 QJsonDocument json_file(root);
31 QString json_string = json_file.toJson(QJsonDocument::Indented);
32
33 file_->open(QIODevice::WriteOnly | QIODevice::Text);
34 QTextStream text_stream(file_);
35 text_stream << json_string;
36 file_->close();
37}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetFile()

void GuiJsonFile::SetFile ( const QString & file_name)
inline
29{ file_->setFileName(file_name); }
Here is the caller graph for this function:

◆ SetUntitledFile()

void GuiJsonFile::SetUntitledFile ( )

Нужно для того, чтобы при открытии приложения менялось имя "Untitled" файла, так как может быть такое, что пользователь уже создавал такие файлы

8 {
9 std::filesystem::path path;
10 if (file_->filesystemFileName().has_parent_path())
11 path = file_->filesystemFileName().parent_path();
12 else
13 path = std::filesystem::current_path();
14 file_->setFileName(path / "Untitled.json");
15
16 int num = 2;
17 while (file_->exists()) {
18 std::string untitled_filename =
19 "Untitled (" + std::to_string(num) + ").json";
20 file_->setFileName(path / untitled_filename);
21 num++;
22 }
23}
Here is the caller graph for this function:

Member Data Documentation

◆ file_

QFile* GuiJsonFile::file_ {new QFile}
private
36{new QFile};

The documentation for this class was generated from the following files: