|
1 #include <QFile> |
|
2 #include <QDir> |
|
3 #include <QFileInfo> |
1 #include "documentmanager.h" |
4 #include "documentmanager.h" |
|
5 #include "modeleditcontext.h" |
|
6 #include "objecttypes/comment.h" |
|
7 #include "parser.h" |
2 |
8 |
|
9 /** |
|
10 * @brief Constructs a new document manager |
|
11 * @param parent Parent object |
|
12 */ |
3 DocumentManager::DocumentManager(QObject* parent) : |
13 DocumentManager::DocumentManager(QObject* parent) : |
4 QObject{parent} |
14 QObject{parent} |
5 { |
15 { |
6 } |
16 } |
7 |
17 |
8 Model* DocumentManager::newModel() |
18 /** |
|
19 * @brief Creates a new model. |
|
20 * @returns the name to the new model |
|
21 */ |
|
22 QString DocumentManager::newModel() |
9 { |
23 { |
10 openModels.emplace_back(); |
24 const QString name = makeNewModelName(); |
11 return openModels.back().get(); |
25 this->openModels.emplace(name, new Model); |
|
26 return name; |
12 } |
27 } |
|
28 |
|
29 /** |
|
30 * @brief Looks for a model by name |
|
31 * @param name Name of the model |
|
32 * @returns model or null |
|
33 * ' |
|
34 */ |
|
35 Model* DocumentManager::findModelByName(const QString& name) |
|
36 { |
|
37 const auto iterator = this->openModels.find(name); |
|
38 if (iterator == std::end(this->openModels)) |
|
39 { |
|
40 return nullptr; |
|
41 } |
|
42 else |
|
43 { |
|
44 return iterator->second.get(); |
|
45 } |
|
46 } |
|
47 |
|
48 QString pathToName(const QFileInfo& path) |
|
49 { |
|
50 static const char* paths[] = { |
|
51 "s", |
|
52 "48" |
|
53 "8" |
|
54 }; |
|
55 const QString baseName = path.fileName(); |
|
56 const QString dirName = QFileInfo{path.dir().path()}.fileName(); |
|
57 QString result; |
|
58 if (utility::contains(paths, dirName)) |
|
59 { |
|
60 result = dirName + "\\" + baseName; |
|
61 } |
|
62 else |
|
63 { |
|
64 result = baseName; |
|
65 } |
|
66 return result; |
|
67 } |
|
68 |
|
69 QString DocumentManager::openModel(const QString& path, QTextStream& errorStream) |
|
70 { |
|
71 QFile file{path}; |
|
72 const QString name = pathToName(path); |
|
73 file.open(QFile::ReadOnly | QFile::Text); |
|
74 std::unique_ptr<Model> newModel = std::make_unique<Model>(); |
|
75 QTextStream textStream{&file}; |
|
76 Model::EditContext editor = newModel->edit(); |
|
77 Parser parser{file}; |
|
78 parser.parseBody(editor); |
|
79 QString result; |
|
80 if (file.error() == QFile::NoError) |
|
81 { |
|
82 openModels[name] = std::move(newModel); |
|
83 result = name; |
|
84 } |
|
85 else |
|
86 { |
|
87 errorStream << file.errorString(); |
|
88 } |
|
89 return result; |
|
90 } |
|
91 |
|
92 QString DocumentManager::makeNewModelName() |
|
93 { |
|
94 untitledNameCounter += 1; |
|
95 return "untitled-" + QString::number(untitledNameCounter); |
|
96 } |