src/main.cpp

Sat, 08 Apr 2023 22:09:29 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sat, 08 Apr 2023 22:09:29 +0300
changeset 357
66c75604f5b8
parent 356
65b4741b302d
child 359
73b6c478378e
permissions
-rw-r--r--

Add settings import and export

#include <QApplication>
#include <QClipboard>
#include <QCloseEvent>
#include <QFileDialog>
#include <QMdiSubWindow>
#include <QMessageBox>
#include <QScrollBar>
#include <QStackedWidget>
#include <QTranslator>
#include "src/gl/partrenderer.h"
#include "src/layers/axeslayer.h"
#include "src/layers/edittools.h"
#include "src/layers/gridlayer.h"
#include "src/ldrawalgorithm.h"
#include "src/mainwindow.h"
#include "src/messagelog.h"
#include "src/modelsubwindow.h"
#include "src/settings.h"
#include "src/settingseditor/settingseditor.h"
#include "src/ui/circletooloptionswidget.h"
#include "src/version.h"
#include "src/widgets/colorselectdialog.h"
#include "src/parser.h"
#include "src/ldrawsyntaxhighlighter.h"
#include <GL/glew.h>

static const QDir LOCALE_DIR {":/locale"};

class ModelData : public QObject
{
	Q_OBJECT
public:
	ModelData(QObject* parent) : QObject {parent} {}
	std::unique_ptr<PartRenderer> canvas;
	std::unique_ptr<EditTools> tools;
	std::unique_ptr<AxesLayer> axesLayer;
	std::unique_ptr<GridLayer> gridLayer;
	std::unique_ptr<QTextCursor> textcursor;
	QTextDocument* model;
};

class Signal final : public QObject
{
	Q_OBJECT
public:
	Signal() : QObject{}{}
	virtual ~Signal(){}
	void emit()
	{
		Q_EMIT this->triggered();
	}
Q_SIGNALS:
	void triggered();
};
#include <main.moc>

static void doQtRegistrations()
{
	QCoreApplication::setApplicationName(QStringLiteral(CMAKE_PROJECT_NAME));
	QCoreApplication::setOrganizationName("hecknology.net");
	QCoreApplication::setOrganizationDomain("hecknology.net");
	qRegisterMetaType<Message>();
	qRegisterMetaType<Library>();
	qRegisterMetaType<QList<Library>>();
	qRegisterMetaType<QMdiArea::ViewMode>();
	qRegisterMetaType<gl::RenderStyle>();
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
	qRegisterMetaTypeStreamOperators<Library>("Library");
	qRegisterMetaTypeStreamOperators<Libraries>("Libraries");
	qRegisterMetaTypeStreamOperators<gl::RenderStyle>();
	qRegisterMetaTypeStreamOperators<QMdiArea::ViewMode>();
	qRegisterMetaTypeStreamOperators<Qt::ToolButtonStyle>();
#endif
}

static std::optional<ModelId> openModelFromPath(
	const QString& path,
	const LibrariesModel* libraries,
	DocumentManager* documents,
	QWidget* parent)
{
	QString errorString;
	QTextStream errorStream{&errorString};
	const std::optional<ModelId> modelIdOpt = documents->openModel(
		path,
		errorStream,
		OpenType::ManuallyOpened);
	if (modelIdOpt.has_value()) {
		const DocumentManager::MissingDependencies missing = documents->loadDependenciesForAllModels(*libraries);
		if (not missing.empty()) {
			QMessageBox::warning(
				parent,
				QObject::tr("Problem loading references"),
				errorStringFromMissingDependencies(missing));
		}
	}
	else {
		QMessageBox::critical(
			parent,
			QObject::tr("Problem opening file"),
			QObject::tr("Could not open %1: %2").arg(quoted(path), errorString)
		);
	}
	return modelIdOpt;
}

static QString getOpenModelPath(QWidget* parent)
{
	return QFileDialog::getOpenFileName(
		parent,
		QObject::tr("Open model"),
		"",
		QObject::tr("LDraw models (*.ldr *.dat)"));
}

static ModelData* findModelData(const DocumentManager* documents, ModelId modelId)
{
	return documents->findPayload<ModelData>(modelId);
}

static ModelSubWindow* currentModelSubWindow(MainWindow* ui)
{
	auto* w = ui->mdiArea->activeSubWindow();
	return qobject_cast<ModelSubWindow*>(w);
}

static ModelData* currentModelData(MainWindow* ui, const DocumentManager* documents)
{
	if (auto* const activeSubWindow = currentModelSubWindow(ui)) {
		return findModelData(documents, activeSubWindow->modelId);
	}
	else {
		return nullptr;
	}
}

static std::optional<ModelId> findCurrentModelId(MainWindow* ui)
{
	ModelSubWindow* activeSubWindow = qobject_cast<ModelSubWindow*>(ui->mdiArea->activeSubWindow());
	if (activeSubWindow != nullptr) {
		return activeSubWindow->modelId;
	}
	else {
		return {};
	}
}

static QString title(MainWindow* ui)
{
	QMdiSubWindow* subWindow = ui->mdiArea->activeSubWindow();
	QString titlestring;
	const QString versionString = fullVersionString(QLocale::ShortFormat);
	if (subWindow != nullptr) {
		titlestring = QObject::tr("%1 - %2").arg(subWindow->windowTitle(), versionString);
	}
	else {
		titlestring = versionString;
	}
	if (/* DISABLES CODE */ (true)
		and std::strcmp(CMAKE_BUILD_TYPE, "Release") != 0
		and std::strcmp(CMAKE_BUILD_TYPE, "MinSizeRel") != 0
	) {
		titlestring += QObject::tr(" [%1]").arg(CMAKE_BUILD_TYPE);
	}
	return titlestring;
}

static ColorTable loadColors(const LibrariesModel* libraries)
{
	QTextStream errors;
	return libraries->loadColorTable(errors);
}

static QString tabName(const QFileInfo& fileInfo)
{
	QString result = fileInfo.baseName();
	if (result.isEmpty()) {
		result = QObject::tr("<unnamed>");
	}
	return result;
}

static void rebuildRecentFilesMenu(QMenu* menu, const QStringList& strings, QWidget* parent)
{
	menu->clear();
	for (const QString& path : strings) {
		QAction* action = new QAction{path, parent};
		action->setData(path);
		menu->addAction(action);
	}
}

template<typename Fn>
static void forEachModel(const DocumentManager* documents, Fn&& fn)
{
	forValueInMap(*documents, [&fn](const DocumentManager::ModelInfo& info)
	{
		ModelData* modelSpecificData = qobject_cast<ModelData*>(info.payload);
		if (modelSpecificData != nullptr) {
			fn(&info, modelSpecificData);
		}
	});
}

static void updateRenderPreferences(
	MainWindow* ui,
	const gl::RenderPreferences* renderPreferences,
	const DocumentManager* documents)
{
	forEachModel(documents, [&renderPreferences](const void*, const ModelData* data){
		if (data->canvas != nullptr) {
			data->canvas->setRenderPreferences(*renderPreferences);
			data->canvas->setLayerEnabled(data->axesLayer.get(), renderPreferences->drawAxes);
		}
	});
	ui->setRenderStyle(renderPreferences->style);
	ui->actionDrawAxes->setChecked(renderPreferences->drawAxes);
	ui->actionWireframe->setChecked(renderPreferences->wireframe);
}

static gl::RenderPreferences loadRenderPreferences()
{
	return gl::RenderPreferences{
		.style = setting<Setting::RenderStyle>(),
		.mainColor = setting<Setting::MainColor>(),
		.backgroundColor = setting<Setting::BackgroundColor>(),
		.selectedColor = setting<Setting::SelectedColor>(),
		.lineThickness = setting<Setting::LineThickness>(),
		.lineAntiAliasing = setting<Setting::LineAntiAliasing>(),
		.drawAxes = setting<Setting::DrawAxes>(),
		.wireframe = setting<Setting::Wireframe>(),
	};
}

struct ToolWidgets
{
	CircleToolOptionsWidget* circleToolOptions;
};

static void initializeTools(MainWindow* ui, ToolWidgets* toolWidgets, QWidget* parent)
{
	const struct
	{
		QString name, tooltip;
		QPixmap icon;
		QWidget* widget;
	} editingModesInfo[] = {
		{
			.name = QObject::tr("Select"),
			.tooltip = QObject::tr("Select elements from the model."),
			.icon = {":/icons/navigate-outline.png"},
			.widget = nullptr,
		},
		{
			.name = QObject::tr("Draw"),
			.tooltip = QObject::tr("Draw new elements into the model."),
			.icon = {":/icons/pencil-outline.png"},
			.widget = nullptr,
		},
		{
			.name = QObject::tr("Circle"),
			.tooltip = QObject::tr("Draw circular primitives."),
			.icon = {":/icons/linetype-circularprimitive.png"},
			.widget = toolWidgets->circleToolOptions,
		},
	};
	for (int i = 0; i < countof(editingModesInfo); ++i) {
		const auto& editingModeInfo = editingModesInfo[i];
		QAction* action = new QAction{editingModeInfo.name, parent};
		action->setCheckable(true);
		action->setEnabled(false);
		action->setData(static_cast<EditingMode>(i));
		action->setToolTip(editingModeInfo.tooltip);
		action->setIcon(QPixmap{editingModeInfo.icon});
		ui->editingModesToolBar->addAction(action);
		QWidget* widget = editingModeInfo.widget;
		if (widget == nullptr) {
			widget = new QWidget{parent};
		}
		ui->toolWidgetStack->addWidget(widget);
		QObject::connect(action, &QAction::triggered, [ui, i]{
			ui->toolWidgetStack->setCurrentIndex(i);
		});
	}
}

template<class SubWindow, class... Args>
SubWindow* createSubWindow(QMdiArea* mdiArea, Args&&... args)
{
	// Qt seems to have a bug where the first created sub window does not render
	// properly until it is minimized and maximized again. This only happens
	// if we give the mdi area as a parent argument. As a work-around, we create
	// the sub window with parent=nullptr, and add it manually.
	// c.f. https://bugreports.qt.io/browse/QTBUG-69495
	SubWindow* subWindow = new SubWindow{args..., nullptr};
	mdiArea->addSubWindow(subWindow);
	return subWindow;
}

static void executeAction(QTextDocument* model, const ModelAction& action)
{
	std::visit(overloaded{
		[model](const AppendToModel& action){
			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);
		},
		[](const DeleteFromModel&){},
		[model](const ModifyModel& action){
			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);
}

QFont codeEditorFontFromSettings()
{
	QFont font{};
	if (setting<Setting::CodeEditorUseSystemFont>())
	{
		font.setStyleHint(QFont::Monospace);
	}
	else
	{
		font.setFamily(setting<Setting::CodeEditorFontFamily>());
		font.setPointSize(setting<Setting::CodeEditorFontSize>());
	}
	return font;
}

constexpr glm::mat4 DEFAULT_GRID_MATRIX = {{1, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 1}};

int main(int argc, char *argv[])
{
	doQtRegistrations();
	QApplication app{argc, argv};
	QApplication::setWindowIcon(QIcon{":/icons/appicon.png"});
	MainWindow mainWindow;
	DocumentManager documents;
	QString currentLanguage = "en";
	QTranslator translator{&mainWindow};
	LibrariesModel libraries{&mainWindow};
	QStringList recentlyOpenedFiles;
	ColorTable colorTable;
	gl::RenderPreferences renderPreferences;
	MessageLog messageLog;
	Signal settingsChanged;
	ToolWidgets toolWidgets{
		.circleToolOptions = new CircleToolOptionsWidget{&mainWindow},
	};
	const auto updateTitle = [&mainWindow]{
		mainWindow.setWindowTitle(title(&mainWindow));
	};
	const uiutilities::KeySequenceMap defaultKeyboardShortcuts =
		uiutilities::makeKeySequenceMap(uiutilities::collectActions(&mainWindow));
	const auto saveSettings = [
		&libraries,
		&mainWindow,
		&recentlyOpenedFiles,
		&renderPreferences,
		&settingsChanged]
	{
		setSetting<Setting::MainWindowGeometry>(mainWindow.saveGeometry());
		setSetting<Setting::MainWindowState>(mainWindow.saveState());
		setSetting<Setting::RecentFiles>(recentlyOpenedFiles);
		setSetting<Setting::RenderStyle>(renderPreferences.style);
		setSetting<Setting::DrawAxes>(renderPreferences.drawAxes);
		setSetting<Setting::Wireframe>(renderPreferences.wireframe);
		libraries.storeToSettings();
		settingsChanged.emit();
	};
	const auto openModelForEditing = [
		&colorTable,
		&documents,
		&mainWindow,
		&messageLog,
		&renderPreferences,
		&settingsChanged]
		(const ModelId modelId)
	{
		QTextDocument* model = documents.getModelById(modelId);
		if (model != nullptr) {
			ModelData* data = new ModelData(&documents);
			data->tools = std::make_unique<EditTools>();
			data->canvas = std::make_unique<PartRenderer>(model, &documents, colorTable);
			data->axesLayer = std::make_unique<AxesLayer>();
			data->gridLayer = std::make_unique<GridLayer>();
			data->gridLayer->setGridMatrix(DEFAULT_GRID_MATRIX);
			data->tools->setGridMatrix(DEFAULT_GRID_MATRIX);
			data->model = model;
			data->canvas->addRenderLayer(data->axesLayer.get());
			data->canvas->setLayerEnabled(data->axesLayer.get(), setting<Setting::DrawAxes>());
			data->canvas->addRenderLayer(data->gridLayer.get());
			data->canvas->addRenderLayer(data->tools.get());
			new LDrawSyntaxHighlighter{model};
			data->textcursor = std::make_unique<QTextCursor>(model);
			documents.setModelPayload(modelId, data);
			QObject::connect(
				data->tools.get(),
				&EditTools::modelAction,
				std::bind(executeAction, model, std::placeholders::_1));
			data->canvas->setRenderPreferences(renderPreferences);
			QObject::connect(
				data->tools.get(),
				&EditTools::newStatusText,
				[&mainWindow](const QString& newStatusText) {
					mainWindow.statusBar()->showMessage(newStatusText);
				});
#if 0
			QObject::connect(
				data->tools.get(),
				&EditTools::select,
				[modelId, &documents](const QSet<ElementId>& indices, bool retain) {
					ModelData* data = findModelData(&documents, modelId);
					if (data != nullptr) {
						if (not retain) {
							data->textcursor->clearSelection();
						}
						for (const ElementId id : indices) {
							opt<int> index = data->model->find(id);
							if (index.has_value()) {
								const QModelIndex qindex = data->model->index(*index);
								data->itemSelectionModel->select(qindex, QItemSelectionModel::Select);
							}
						}
					}
				});
#endif
			QObject::connect(&settingsChanged, &Signal::triggered, [modelId, &documents]{
				ModelData* data = findModelData(&documents, modelId);
				if (data != nullptr) {
					data->gridLayer->settingsChanged();
				}
			});
			QObject::connect(data->canvas.get(), &PartRenderer::message, &messageLog, &MessageLog::addMessage);
			QObject::connect(
				data->tools.get(),
				&EditTools::suggestCursor,
				data->canvas.get(),
				&QWidget::setCursor);
			data->tools->setEditMode(SelectMode);
			const QFileInfo fileInfo{*documents.modelPath(modelId)};
			auto* const subWindow = createSubWindow<ModelSubWindow>(mainWindow.mdiArea, modelId);
			subWindow->setMinimumSize({96, 96});
			subWindow->resize({320, 200});
			subWindow->setWidget(data->canvas.get());
			subWindow->setWindowTitle(tabName(fileInfo));
			subWindow->show();
		}
	};
	const auto updateRecentlyOpenedDocumentsMenu = [
		&documents,
		&libraries,
		&mainWindow,
		&openModelForEditing,
		&recentlyOpenedFiles]
	{
		rebuildRecentFilesMenu(mainWindow.menuRecentFiles, recentlyOpenedFiles, &mainWindow);
		for (QAction* action : mainWindow.menuRecentFiles->actions()) {
			QString path = action->data().toString();
			QObject::connect(
				action,
				&QAction::triggered,
				[path, &libraries, &documents, &mainWindow, &openModelForEditing]() {
					const auto id = openModelFromPath(path, &libraries, &documents, &mainWindow);
					if (id.has_value())
					{
						openModelForEditing(id.value());
					}
				}
			);
		}
	};
	const auto restoreSettings = [
		&colorTable,
		&documents,
		&libraries,
		&mainWindow,
		&recentlyOpenedFiles,
		&renderPreferences,
		&settingsChanged,
		&updateRecentlyOpenedDocumentsMenu]
	{
		recentlyOpenedFiles = setting<Setting::RecentFiles>();
		renderPreferences = loadRenderPreferences();
		libraries.restoreFromSettings();
		updateRecentlyOpenedDocumentsMenu();
		colorTable = loadColors(&libraries);
		updateRenderPreferences(&mainWindow, &renderPreferences, &documents);
		mainWindow.mdiArea->setViewMode(setting<Setting::ViewMode>());
		mainWindow.retranslateUi(&mainWindow);
		mainWindow.setToolButtonStyle(setting<Setting::ToolButtonStyle>());
		settingsChanged.emit();
	};
	const auto addRecentlyOpenedFile = [
		&recentlyOpenedFiles,
		&saveSettings,
		&updateRecentlyOpenedDocumentsMenu]
		(const QString& path)
	{
		constexpr int maxRecentlyOpenedFiles = 10;
		recentlyOpenedFiles.removeAll(path);
		recentlyOpenedFiles.insert(0, path);
		while (recentlyOpenedFiles.size() > maxRecentlyOpenedFiles)
		{
			recentlyOpenedFiles.removeLast();
		}
		saveSettings();
		updateRecentlyOpenedDocumentsMenu();
	};
	QObject::connect(mainWindow.actionNew, &QAction::triggered,
		[&documents, &openModelForEditing]{
			openModelForEditing(documents.newModel());
		}
	);
	QObject::connect(mainWindow.actionOpen, &QAction::triggered,
		[&addRecentlyOpenedFile, &documents, &libraries, &mainWindow, &openModelForEditing]
		{
			const QString path = getOpenModelPath(&mainWindow);
			if (not path.isEmpty())
			{
				const std::optional<ModelId> id = openModelFromPath(path, &libraries, &documents, &mainWindow);
				if (id.has_value()) {
					openModelForEditing(id.value());
					addRecentlyOpenedFile(path);
				}
			}
		}
	);
	QObject::connect(mainWindow.actionSettingsEditor, &QAction::triggered, [&defaultKeyboardShortcuts, &restoreSettings, &settingsChanged, &mainWindow]{
		if (mainWindow.mdiArea->findChildren<SettingsEditor*>().isEmpty()) {
			auto* const settingsEditor = createSubWindow<SettingsEditor>(mainWindow.mdiArea, defaultKeyboardShortcuts);
			QObject::connect(&settingsChanged, &Signal::triggered, settingsEditor, &SettingsEditor::loadSettings);
			QObject::connect(settingsEditor, &SettingsEditor::settingsChanged, restoreSettings);
			settingsEditor->setAttribute(Qt::WA_DeleteOnClose);
			settingsEditor->show();
		}
	});
	QObject::connect(mainWindow.actionQuit, &QAction::triggered, &mainWindow, &QMainWindow::close);
#if 0
	QObject::connect(ui.actionAdjustGridToView, &QAction::triggered, [&]{
		if (ModelData* data = currentModelData(&ui, &documents)) {
			adjustGridToView(data->canvas.get());
		}
	});
#endif
	QObject::connect(mainWindow.actionClose, &QAction::triggered, [&mainWindow, &documents]{
		if (ModelData* data = currentModelData(&mainWindow, &documents)) {
			// TODO
		}
	});
	const auto save = [&addRecentlyOpenedFile, &mainWindow](DocumentManager* documents, ModelId modelId){
		QString error;
		QTextStream errorStream{&error};
		const bool succeeded = documents->saveModel(modelId, errorStream);
		if (not succeeded)
		{
			QMessageBox::critical(&mainWindow, QObject::tr("Save error"), error);
		}
		else
		{
			const QString* pathPtr = documents->modelPath(modelId);
			if (pathPtr != nullptr) {
				addRecentlyOpenedFile(*pathPtr);
			}
		}
	};
	const auto actionSaveAs = [&documents, &libraries, &mainWindow, &save]{
		const std::optional<ModelId> modelId = findCurrentModelId(&mainWindow);
		if (modelId.has_value())
		{
			const QString* pathPtr = documents.modelPath(*modelId);
			QString defaultPath = (pathPtr != nullptr) ? *pathPtr : "";
			const QString newPath = QFileDialog::getSaveFileName(
				&mainWindow,
				QObject::tr("Save as…"),
				QFileInfo{defaultPath}.absoluteDir().path(), 
				QObject::tr("LDraw files (*.ldr *dat);;All files (*)")
			);
			if (not newPath.isEmpty()) {
				QString error;
				QTextStream errorStream{&error};
				documents.setModelPath(*modelId, newPath, libraries, errorStream);
				QMdiSubWindow* const subWindow = mainWindow.mdiArea->currentSubWindow();
				if (subWindow != nullptr) {
					subWindow->setWindowTitle(tabName(QFileInfo{newPath}));
				}
				save(&documents, *modelId);
			}
		}
	};
	QObject::connect(mainWindow.actionSaveAs, &QAction::triggered, actionSaveAs);
	QObject::connect(mainWindow.actionSave, &QAction::triggered, [
		&actionSaveAs,
		&documents,
		&save,
		&mainWindow]
	{
		const std::optional<ModelId> modelId = findCurrentModelId(&mainWindow);
		if (modelId.has_value()) {
			const QString* path = documents.modelPath(*modelId);
			if (path == nullptr or path->isEmpty()) {
				actionSaveAs();
			}
			else {
				save(&documents, *modelId);
			}
		}
	});
	QObject::connect(mainWindow.actionDrawAxes, &QAction::triggered, [
		&documents,
		&renderPreferences,
		&saveSettings,
		&mainWindow]
		(bool drawAxes)
	{
		renderPreferences.drawAxes = drawAxes;
		saveSettings();
		updateRenderPreferences(&mainWindow, &renderPreferences, &documents);
	});
	QObject::connect(mainWindow.actionWireframe, &QAction::triggered, [
		&documents,
		&renderPreferences,
		&saveSettings,
		&mainWindow]
		(bool enabled)
	{
		renderPreferences.wireframe = enabled;
		saveSettings();
		updateRenderPreferences(&mainWindow, &renderPreferences, &documents);
	});
	QObject::connect(&mainWindow, &MainWindow::renderStyleSelected, [
		&documents,
		&mainWindow,
		&renderPreferences,
		&saveSettings]
		(gl::RenderStyle newStyle)
	{
		renderPreferences.style = newStyle;
		saveSettings();
		updateRenderPreferences(&mainWindow, &renderPreferences, &documents);
	});
	const auto checkEditingModeAction = [&mainWindow, &documents](EditingMode mode) {
		const bool hasDocument = currentModelData(&mainWindow, &documents) != nullptr;
		for (QAction* action : mainWindow.editingModesToolBar->actions()) {
			action->setEnabled(hasDocument);
			action->setChecked(hasDocument and action->data().value<EditingMode>() == mode);
		}
	};
	initializeTools(&mainWindow, &toolWidgets, &mainWindow);
	for (QAction* action : mainWindow.editingModesToolBar->actions()) {
		QObject::connect(action, &QAction::triggered, [
			action,
			&checkEditingModeAction,
			&documents,
			&mainWindow]
		{
			if (ModelData* data = currentModelData(&mainWindow, &documents)) {
				const EditingMode mode = action->data().value<EditingMode>();
				data->tools->setEditMode(mode);
				checkEditingModeAction(mode);
			}
		});
	}
	QObject::connect(mainWindow.mdiArea, &QMdiArea::subWindowActivated, [
		&checkEditingModeAction,
		&documents,
		&mainWindow,
		&updateTitle]
		(QMdiSubWindow* subWindow)
	{
		ModelSubWindow* modelSubWindow = qobject_cast<ModelSubWindow*>(subWindow);
		if (modelSubWindow != nullptr) {
			if (ModelData* data = documents.findPayload<ModelData>(modelSubWindow->modelId)) {
				checkEditingModeAction(data->tools->currentEditingMode());
				mainWindow.modelEdit->setDocument(data->model);
				mainWindow.modelEdit->setTextCursor(*data->textcursor);
				mainWindow.modelEdit->setFont(codeEditorFontFromSettings());
			}
		}
		else {
			checkEditingModeAction(EditingMode::SelectMode);
		}
		mainWindow.modelEdit->setEnabled(modelSubWindow != nullptr);
		updateTitle();
	});
	mainWindow.messageLog->setModel(&messageLog);
	QObject::connect(&documents, &DocumentManager::message, &messageLog, &MessageLog::addMessage);
	QObject::connect(&messageLog, &MessageLog::rowsAboutToBeInserted, [&mainWindow]{
		const auto bar = mainWindow.messageLog->verticalScrollBar();
		mainWindow.messageLog->setProperty("shouldAutoScroll", bar->value() == bar->maximum());
	});
	QObject::connect(&messageLog, &MessageLog::rowsInserted, [&mainWindow]{
		mainWindow.messageLog->resizeRowsToContents();
		if (mainWindow.messageLog->property("shouldAutoScroll").toBool()) {
			mainWindow.messageLog->scrollToBottom();
		}
	});
	QObject::connect(
		toolWidgets.circleToolOptions,
		&CircleToolOptionsWidget::optionsChanged,
		[&mainWindow, &documents](const CircleToolOptions& options) {
			if (ModelData* data = currentModelData(&mainWindow, &documents)) {
				data->tools->setCircleToolOptions(options);
			}
		});
	QObject::connect(
		mainWindow.actionMakeUnofficial,
		&QAction::triggered,
		[&documents, &mainWindow]{
			if (ModelData* data = currentModelData(&mainWindow, &documents)) {
				QTextDocument* const model = data->model;
				for (const ModelAction& action : ldraw::makeUnofficial(model)) {
					executeAction(model, action);
				}
			}
		});
	QObject::connect(mainWindow.actionAboutQt, &QAction::triggered, &app, &QApplication::aboutQt);
	QObject::connect(
		mainWindow.modelEdit,
		&QPlainTextEdit::textChanged,
		[&documents, &libraries, &mainWindow]{
			if (ModelData* data = currentModelData(&mainWindow, &documents)) {
				documents.loadDependenciesForAllModels(libraries);
				data->canvas->update();
			}
		});
	QObject::connect(
		mainWindow.gridMatrix,
		&MatrixEditor::valueChanged,
		[&](const glm::mat4& newGridMatrix)
		{
			forEachModel(&documents, [&](const void*, const ModelData* data)
			{
				if (data->gridLayer != nullptr and data->tools != nullptr and data->canvas != nullptr)
				{
					data->gridLayer->setGridMatrix(newGridMatrix);
					data->tools->setGridMatrix(newGridMatrix);
					data->canvas->update();
				}
			});
		}
	);
	mainWindow.gridMatrix->setValue(DEFAULT_GRID_MATRIX);
	mainWindow.tabifyDockWidget(mainWindow.messageLogDock, mainWindow.toolOptionsDock);
	mainWindow.restoreGeometry(setting<Setting::MainWindowGeometry>());
	mainWindow.restoreState(setting<Setting::MainWindowState>());
	// If a dock is made floating and the app is closed, the dock becomes invisible
	// after the restoreState call. So we make them visible again here.
	for (QDockWidget* dock : mainWindow.findChildren<QDockWidget*>()) {
		dock->setVisible(true);
	}
	restoreSettings();
	updateRenderPreferences(&mainWindow, &renderPreferences, &documents);
	mainWindow.actionAbout->setText(mainWindow.actionAbout->text().arg(CMAKE_PROJECT_NAME));
	updateTitle();
	mainWindow.show();
	const int result = app.exec();
	saveSettings();
	return result;
}

mercurial