src/documentmanager.cpp

changeset 340
e17e07661f4c
parent 338
719b909a7d2b
child 374
75efc3ba5a56
--- a/src/documentmanager.cpp	Sat Jul 23 01:38:06 2022 +0300
+++ b/src/documentmanager.cpp	Sat Jul 23 01:38:43 2022 +0300
@@ -20,6 +20,7 @@
 #include <QDir>
 #include <QFileInfo>
 #include <QSaveFile>
+#include <QPlainTextDocumentLayout>
 #include <deque>
 #include "src/documentmanager.h"
 #include "src/parser.h"
@@ -29,6 +30,13 @@
 {
 }
 
+static std::unique_ptr<QTextDocument> newTextDocument()
+{
+	std::unique_ptr<QTextDocument> newModel = std::make_unique<QTextDocument>(nullptr);
+	newModel->setDocumentLayout(new QPlainTextDocumentLayout{newModel.get()});
+	return newModel;
+}
+
 /**
  * @brief Creates a new model.
  * @returns the ID of the new model
@@ -37,7 +45,7 @@
 {
 	const ModelId modelId{++this->modelIdCounter};
 	this->openModels.emplace(std::make_pair(modelId, ModelInfo{
-		.model = std::make_unique<Model>(this),
+		.model = newTextDocument(),
 		.id = modelId,
 		.opentype = OpenType::ManuallyOpened,
 	}));
@@ -46,7 +54,7 @@
 	return modelId;
 }
 
-Model* DocumentManager::findDependencyByName(const ModelId modelId, const QString& name)
+QTextDocument* DocumentManager::findDependencyByName(const ModelId modelId, const QString& name)
 {
 	const auto modelsIterator = this->openModels.find(modelId);
 	if (modelsIterator != std::end(this->openModels)) {
@@ -70,7 +78,7 @@
  * @param modelId id of model to find
  * @returns model pointer or null
  */
-Model *DocumentManager::getModelById(ModelId modelId)
+QTextDocument *DocumentManager::getModelById(ModelId modelId)
 {
 	const auto iterator = this->openModels.find(modelId);
 	if (iterator != this->openModels.end())
@@ -111,7 +119,6 @@
  * @param openType rationale behind opening this file
  * @returns model id, or no value on error
  */
-#include <QPlainTextDocumentLayout>
 std::optional<ModelId> DocumentManager::openModel(
 	const QString& path,
 	QTextStream& errorStream,
@@ -120,8 +127,7 @@
 	QFile file{path};
 	const QString name = pathToName(QFileInfo{path});
 	file.open(QFile::ReadOnly | QFile::Text);
-	std::unique_ptr<QTextDocument> newModel = std::make_unique<QTextDocument>(nullptr);
-	newModel->setDocumentLayout(new QPlainTextDocumentLayout{newModel.get()});
+	std::unique_ptr<QTextDocument> newModel = newTextDocument();
 	newModel->setPlainText(file.readAll());
 	std::optional<ModelId> result;
 	if (file.error() == QFile::NoError)
@@ -195,7 +201,7 @@
 		QSaveFile file{info->path};
 		file.setDirectWriteFallback(true);
 		if (file.open(QSaveFile::WriteOnly)) {
-			file.write(info->model->toPlainText().toUtf8());
+			file.write(info->model->toPlainText().replace("\n", "\r\n").toUtf8());
 			const bool commitSucceeded = file.commit();
 			if (not commitSucceeded) {
 				errors << QObject::tr("Could not save: %1").arg(file.errorString());
@@ -222,7 +228,7 @@
  * @param model model to look for
  * @return id or no value if not found
  */
-std::optional<ModelId> DocumentManager::findIdForModel(const Model *model) const
+std::optional<ModelId> DocumentManager::findIdForModel(const QTextDocument *model) const
 {
 	std::optional<ModelId> result;
 	for (auto it = this->openModels.begin(); it != this->openModels.end(); ++it)
@@ -319,7 +325,7 @@
 
 void DocumentManager::makePolygonCacheForModel(const ModelId modelId)
 {
-	Model* model = this->getModelById(modelId);
+	QTextDocument* model = this->getModelById(modelId);
 	if (model != nullptr)
 	{
 		const auto modelModified = [this, model]{
@@ -352,7 +358,7 @@
 	return referencedFilePath;
 }
 
-static std::set<QString> referenceNames(const Model* model)
+static std::set<QString> referenceNames(const QTextDocument* model)
 {
 	std::set<QString> result;
 	for (const QString& line : model->toPlainText().split("\n")) {

mercurial