src/main.cpp

changeset 340
e17e07661f4c
parent 338
719b909a7d2b
child 350
676d0b43b577
--- a/src/main.cpp	Sat Jul 23 01:38:06 2022 +0300
+++ b/src/main.cpp	Sat Jul 23 01:38:43 2022 +0300
@@ -53,7 +53,7 @@
 	std::unique_ptr<AxesLayer> axesLayer;
 	std::unique_ptr<GridLayer> gridLayer;
 	std::unique_ptr<QTextCursor> textcursor;
-	Model* model;
+	QTextDocument* model;
 };
 
 class Signal final : public QObject
@@ -86,6 +86,7 @@
 	qRegisterMetaTypeStreamOperators<Libraries>("Libraries");
 	qRegisterMetaTypeStreamOperators<gl::RenderStyle>();
 	qRegisterMetaTypeStreamOperators<QMdiArea::ViewMode>();
+	qRegisterMetaTypeStreamOperators<Qt::ToolButtonStyle>();
 #endif
 }
 
@@ -168,16 +169,6 @@
 	}
 }
 
-static Model* currentModelBody(Ui_MainWindow* ui, DocumentManager* documents)
-{
-	if (auto* const activeSubWindow = currentModelSubWindow(ui)) {
-		return documents->getModelById(activeSubWindow->modelId);
-	}
-	else {
-		return nullptr;
-	}
-}
-
 static std::optional<ModelId> findCurrentModelId(Ui_MainWindow* ui)
 {
 	ModelSubWindow* activeSubWindow = qobject_cast<ModelSubWindow*>(ui->mdiArea->activeSubWindow());
@@ -331,15 +322,6 @@
 	}
 }
 
-static QFont monospace()
-{
-	QFont font{"Monospace"};
-	font.setStyleHint(QFont::TypeWriter);
-	font.setPointSize(10);
-	font.setFixedPitch(true);
-	return font;
-}
-
 constexpr bool sortModelIndexesByRow(const QModelIndex& a, const QModelIndex& b)
 {
 	return a.row() < b.row();
@@ -408,17 +390,6 @@
 	return subWindow;
 }
 
-static QSet<ElementId> resolveIdsFromSelection(const ModelData* data)
-{
-//	const auto selection = data->itemSelectionModel->selection();
-	QSet<ElementId> selectedIndexes;
-/*	for (const QModelIndex& qindex : selection.indexes()) {
-		const std::size_t row = unsigned_cast(qindex.row());
-		selectedIndexes.insert(data->model->idAt(row));
-	}
-*/	return selectedIndexes;
-}
-
 int main(int argc, char *argv[])
 {
 	doQtRegistrations();
@@ -468,21 +439,30 @@
 		}
 	};
 	const auto executeAction = [&](
-		Model* model, const ModelAction& action
+		QTextDocument* model, const ModelAction& action
 	) {
-		/*
 		std::visit(overloaded{
 			[model](const AppendToModel& action){
-				model->append(action.newElement);
+				QTextCursor cursor{model};
+				cursor.movePosition(QTextCursor::End);
+				const QString newText = modelElementToString(action.newElement);
+				// Make sure we have an empty line
+				if (not model->lastBlock().text().isEmpty()) {
+					cursor.insertBlock();
+				}
+				cursor.insertText(newText);
 			},
-			[model](const DeleteFromModel& action){
-					model->remove(action.position);
-			},
+			[](const DeleteFromModel&){},
 			[model](const ModifyModel& action){
-				model->assignAt(action.position, action.newElement);
+				QTextBlock block = model->findBlockByLineNumber((int) action.position);
+				if (block.isValid()) {
+					QTextCursor cursor{block};
+					cursor.select(QTextCursor::LineUnderCursor);
+					cursor.insertText(modelElementToString(action.newElement));
+				}
+				//model->assignAt(action.position, action.newElement);
 			},
 		}, action);
-		*/
 		
 	};
 	const auto restoreSettings = [&]{
@@ -494,6 +474,7 @@
 		updateRenderPreferences(&ui, &renderPreferences, &documents);
 		ui.mdiArea->setViewMode(setting<Setting::ViewMode>());
 		ui.retranslateUi(&mainWindow);
+		mainWindow.setToolButtonStyle(setting<Setting::ToolButtonStyle>());
 		settingsChanged.emit();
 	};
 	const auto addRecentlyOpenedFile = [&](const QString& path){
@@ -508,7 +489,7 @@
 		updateRecentlyOpenedDocumentsMenu();
 	};
 	const auto openModelForEditing = [&](const ModelId modelId){
-		Model* model = documents.getModelById(modelId);
+		QTextDocument* model = documents.getModelById(modelId);
 		if (model != nullptr) {
 			ModelData* data = new ModelData(&documents);
 			data->tools = std::make_unique<EditTools>();
@@ -745,7 +726,7 @@
 		&QAction::triggered,
 		[&]{
 			if (ModelData* data = currentModelData(&ui, &documents)) {
-				Model* const model = data->model;
+				QTextDocument* const model = data->model;
 				for (const ModelAction& action : ldraw::makeUnofficial(model)) {
 					executeAction(model, action);
 				}

mercurial