src/main.cpp

Mon, 20 Jun 2022 17:27:30 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Mon, 20 Jun 2022 17:27:30 +0300
changeset 235
7ef03c2b46ab
parent 233
5509bec02c81
child 236
1fa0e1de9f0a
permissions
-rw-r--r--

Add a basic message log

#include <QApplication>
#include <QFileDialog>
#include <QMessageBox>
#include <QMdiSubWindow>
#include <QStackedWidget>
#include <QCloseEvent>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "version.h"
#include "gl/axesprogram.h"
#include "gl/gridprogram.h"
#include "gl/partrenderer.h"
#include "document.h"
#include "settingseditor/settingseditor.h"
#include "widgets/colorselectdialog.h"
#include "settings.h"
#include "ui/circletooloptionswidget.h"

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

class ModelSubWindow : public QMdiSubWindow
{
	Q_OBJECT
public:
	const ModelId modelId;
	ModelSubWindow(ModelId modelId, QWidget* widget) :
		QMdiSubWindow{widget},
		modelId{modelId}
	{
	}
protected:
	void closeEvent(QCloseEvent* event) override
	{
		event->ignore();
	}
};

class ModelData : public QObject
{
	Q_OBJECT
public:
	ModelData(QObject* parent) : QObject {parent} {}
	std::unique_ptr<PartRenderer> canvas;
	std::unique_ptr<QItemSelectionModel> itemSelectionModel;
	std::unique_ptr<EditTools> tools;
	std::unique_ptr<AxesLayer> axesLayer;
	std::unique_ptr<GridLayer> gridLayer;
	Model* model;
};
#include "main.moc"

static void doQtRegistrations()
{
	QCoreApplication::setApplicationName(::appName);
	QCoreApplication::setOrganizationName("hecknology.net");
	QCoreApplication::setOrganizationDomain("hecknology.net");
	qRegisterMetaTypeStreamOperators<Library>("Library");
	qRegisterMetaTypeStreamOperators<Libraries>("Libraries");
	qRegisterMetaTypeStreamOperators<gl::RenderStyle>();
	qRegisterMetaTypeStreamOperators<QMdiArea::ViewMode>();
}

template<typename BaseType, typename MemberType, typename DataType>
struct MemberData
{
	std::size_t member;
	DataType payload;
	constexpr MemberType memberInstance(BaseType* instance) const
	{
		return *reinterpret_cast<MemberType*>(reinterpret_cast<char*>(instance) + this->member);
	}
};

static constexpr MemberData<Ui_MainWindow, QAction*, gl::RenderStyle> renderStyleButtons[] = {
	{ offsetof(Ui_MainWindow, actionRenderStyleNormal), gl::RenderStyle::Normal },
	{ offsetof(Ui_MainWindow, actionRenderStyleBfc), gl::RenderStyle::BfcRedGreen },
	{ offsetof(Ui_MainWindow, actionRenderStyleRandom), gl::RenderStyle::RandomColors },
	{ offsetof(Ui_MainWindow, actionRenderStylePickScene), gl::RenderStyle::PickScene },
};

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 const QString localeCode(const QString& locale)
{
	if (locale == "system") {
		return QLocale::system().name();
	}
	else {
		return locale;
	}
}

/**
 * @brief Changes the application language to the specified language
 */
static void changeLanguage(const QString& locale, QTranslator* translator)
{
	if (not locale.isEmpty()) {
		const QString localeCode = ::localeCode(locale);
		QLocale::setDefault({localeCode});
		qApp->removeTranslator(translator);
		const QString path = LOCALE_DIR.filePath(localeCode + ".qm");
		const bool loadSuccessful = translator->load(path);
		if (loadSuccessful)
		{
			qApp->installTranslator(translator);
		}
	}
}

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

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

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

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());
	if (activeSubWindow != nullptr) {
		return activeSubWindow->modelId;
	}
	else {
		return {};
	}
}

/**
 * @brief Updates the title of the main window so to contain the app's name
 * and version as well as the open document name.
 */
static QString title()
{
	QString title = ::appName;
	title += " ";
	title += fullVersionString();
	return title;
}

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;
}

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 forModel(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(
	Ui_MainWindow* ui,
	const gl::RenderPreferences* renderPreferences,
	const DocumentManager* documents)
{
	forModel(documents, [&renderPreferences](const void*, const ModelData* data){
		if (data->canvas != nullptr) {
			data->canvas->setRenderPreferences(*renderPreferences);
			data->canvas->setLayerEnabled(data->axesLayer.get(), renderPreferences->drawAxes);
		}
	});
	for (auto data : ::renderStyleButtons) {
		QAction* action = data.memberInstance(ui);
		action->setChecked(renderPreferences->style == data.payload);
	}
	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>(),
	};
}

void initializeTools(Ui_MainWindow* ui, DocumentManager* documents, QWidget* parent)
{
	CircleToolOptionsWidget* circleToolOptions = new CircleToolOptionsWidget{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 = this->objectEditor,
		},
		{
			.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 = 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->setChecked(i == 0);
		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);
		});
	}
	QObject::connect(
		circleToolOptions,
		&CircleToolOptionsWidget::optionsChanged,
		[ui, documents](const CircleToolOptions& options) {
			if (ModelData* data = currentModelData(ui, documents)) {
				data->tools->setCircleToolOptions(options);
			}
		});
}

constexpr bool sortModelIndexesByRow(const QModelIndex& a, const QModelIndex& b)
{
	return a.row() < b.row();
}

std::vector<int> rows(const QModelIndexList& indexList)
{
	std::vector<int> result;
	result.reserve(indexList.size());
	for (const QModelIndex& index : indexList)
	{
		result.push_back(index.row());
	}
	return result;
}

int main(int argc, char *argv[])
{
	doQtRegistrations();
	QApplication app{argc, argv};
	QMainWindow mainWindow;
	Ui_MainWindow ui;
	DocumentManager documents;
	QString currentLanguage = "en";
	QTranslator translator{&mainWindow};
	LibrariesModel libraries{&mainWindow};
	QStringList recentlyOpenedFiles;
	ColorTable colorTable;
	gl::RenderPreferences renderPreferences;
	ui.setupUi(&mainWindow);
	const uiutilities::KeySequenceMap defaultKeyboardShortcuts =
		uiutilities::makeKeySequenceMap(uiutilities::collectActions(&mainWindow));
	const auto saveSettings = [&]{
		setSetting<Setting::MainWindowGeometry>(mainWindow.saveGeometry());
		setSetting<Setting::RecentFiles>(recentlyOpenedFiles);
		setSetting<Setting::RenderStyle>(renderPreferences.style);
		setSetting<Setting::DrawAxes>(renderPreferences.drawAxes);
		setSetting<Setting::Wireframe>(renderPreferences.wireframe);
		libraries.storeToSettings();
	};
	const auto updateRecentlyOpenedDocumentsMenu = [&]{
		rebuildRecentFilesMenu(ui.menuRecentFiles, recentlyOpenedFiles, &mainWindow);
		for (QAction* action : ui.menuRecentFiles->findChildren<QAction*>()) {
			QString path = action->data().toString();
			QObject::connect(
				action,
				&QAction::triggered,
				[path, &libraries, &documents, &mainWindow]() {
					openModelFromPath(path, &libraries, &documents, &mainWindow);
				}
			);
		}
	};
	static constexpr auto executeAction = [&](
		Model* model, const ModelAction& action
	) {
		std::visit(overloaded{
			[model](const AppendToModel& action){
				model->append(action.newElement);
			},
			[model](const DeleteFromModel& action){
				model->remove(action.position);
			},
		}, action);
	};
	const auto restoreSettings = [&]{
		recentlyOpenedFiles = setting<Setting::RecentFiles>();
		renderPreferences = loadRenderPreferences();
		changeLanguage(setting<Setting::Locale>(), &translator);
		libraries.restoreFromSettings();
		updateRecentlyOpenedDocumentsMenu();
		colorTable = loadColors(&libraries);
		updateRenderPreferences(&ui, &renderPreferences, &documents);
		ui.mdiArea->setViewMode(setting<Setting::ViewMode>());
		ui.retranslateUi(&mainWindow);
	};
	const auto addRecentlyOpenedFile = [&](const QString& path){
		constexpr int maxRecentlyOpenedFiles = 10;
		recentlyOpenedFiles.removeAll(path);
		recentlyOpenedFiles.insert(0, path);
		while (recentlyOpenedFiles.size() > maxRecentlyOpenedFiles)
		{
			recentlyOpenedFiles.removeLast();
		}
		saveSettings();
		updateRecentlyOpenedDocumentsMenu();
	};
	const auto logMessage = [&ui](const Message& message){
		QString messagetext = message.time.toString(QObject::tr("[hh:mm:ss]"));
		switch(message.type) {
		case Message::Info:
			messagetext += QObject::tr(" [INFO] ");
			break;
		case Message::Warning:
			messagetext += QObject::tr(" [WARN] ");
			break;
		case Message::Error:
			messagetext += QObject::tr(" [ERR!] ");
			break;
		}
		messagetext += message.text;
		ui.messageLog->append(messagetext);
	};
	const auto openModelForEditing = [&](const ModelId modelId){
		Model* 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->itemSelectionModel = std::make_unique<QItemSelectionModel>();
			data->itemSelectionModel->setModel(model);
			data->axesLayer = std::make_unique<AxesLayer>();
			constexpr glm::mat4 XZ = {{1, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 1}};
			data->gridLayer = std::make_unique<GridLayer>();
			data->gridLayer->setGridMatrix(XZ);
			data->tools->setGridMatrix(XZ);
			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());
			documents.setModelPayload(modelId, data);
			QObject::connect(
				data->tools.get(),
				&EditTools::modelAction,
				std::bind(executeAction, model, std::placeholders::_1));
			QObject::connect(
				data->itemSelectionModel.get(),
				&QItemSelectionModel::selectionChanged,
				[modelId, &documents]{
				ModelData* data = findModelData(&documents, modelId);
				if (data != nullptr) {
					auto resolveIndex = [&data](const QModelIndex& index){
						return data->model->idAt(index.row());
					};
					const auto selection = data->itemSelectionModel->selection();
					const auto indices = fn::map<QSet<ModelId>>(selection.indexes(), resolveIndex);
					data->canvas->setSelection(indices);
				}
			});
			data->canvas->setRenderPreferences(renderPreferences);
			QObject::connect(
				data->tools.get(),
				&EditTools::newStatusText,
				[&](const QString& newStatusText) {
					mainWindow.statusBar()->showMessage(newStatusText);
				});
			QObject::connect(
				data->tools.get(),
				&EditTools::select,
				[modelId, &documents](const QSet<ModelId>& indices, bool retain) {
					ModelData* data = findModelData(&documents, modelId);
					if (data != nullptr) {
						if (not retain) {
							data->itemSelectionModel->clear();
						}
						for (const ModelId 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);
							}
						}
					}
				});
			const QFileInfo fileInfo{*documents.modelPath(modelId)};
			ModelSubWindow* subWindow = new ModelSubWindow{modelId, ui.mdiArea};
			subWindow->setWidget(data->canvas.get());
			subWindow->setWindowTitle(tabName(fileInfo));
			subWindow->show();
		}
	};
	QObject::connect(ui.actionNew, &QAction::triggered, [&]{
		openModelForEditing(documents.newModel());
	});
	QObject::connect(ui.actionOpen, &QAction::triggered, [&]{
		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(ui.actionSettingsEditor, &QAction::triggered, [&]{
		SettingsEditor settingsEditor{defaultKeyboardShortcuts, &mainWindow};
		const int result = settingsEditor.exec();
		if (result == QDialog::Accepted)
		{
			restoreSettings();
		}
	});
	QObject::connect(ui.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(ui.actionClose, &QAction::triggered, [&ui, &documents]{
		if (ModelData* data = currentModelData(&ui, &documents)) {
			// TODO
		}
	});
	const auto save = [&](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 = [&]{
		const std::optional<ModelId> modelId = findCurrentModelId(&ui);
		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 = ui.mdiArea->currentSubWindow();
				if (subWindow != nullptr) {
					subWindow->setWindowTitle(tabName(QFileInfo{newPath}));
				}
				save(*modelId);
			}
		}
	};
	QObject::connect(ui.actionSaveAs, &QAction::triggered, actionSaveAs);
	QObject::connect(ui.actionSave, &QAction::triggered, [&]{
		const std::optional<ModelId> modelId = findCurrentModelId(&ui);
		if (modelId.has_value()) {
			const QString* path = documents.modelPath(*modelId);
			if (path == nullptr or path->isEmpty()) {
				actionSaveAs();
			}
			else {
				save(*modelId);
			}
		}
	});
	QObject::connect(ui.actionDelete, &QAction::triggered, [&]{
		if (Model* model = currentModelBody(&ui, &documents)) {
			std::vector<int> selectedRows = rows(ui.modelListView->selectionModel()->selectedRows());
			std::sort(selectedRows.begin(), selectedRows.end(), std::greater<int>{});
			for (int row : selectedRows) {
				executeAction(model, DeleteFromModel{.position = row});
			}
		}
	});
	QObject::connect(ui.actionDrawAxes, &QAction::triggered, [&](bool drawAxes){
		renderPreferences.drawAxes = drawAxes;
		saveSettings();
		updateRenderPreferences(&ui, &renderPreferences, &documents);
	});
	QObject::connect(ui.actionWireframe, &QAction::triggered, [&](bool enabled){
		renderPreferences.wireframe = enabled;
		saveSettings();
		updateRenderPreferences(&ui, &renderPreferences, &documents);
	});
	for (auto data : ::renderStyleButtons) {
		QAction* action = data.memberInstance(&ui);
		QObject::connect(action, &QAction::triggered, [&, data]{
			renderPreferences.style = data.payload;
			saveSettings();
			updateRenderPreferences(&ui, &renderPreferences, &documents);
		});
	}
	const auto checkEditingModeAction = [&ui](EditingMode mode) {
		for (QAction* action : ui.editingModesToolBar->actions()) {
			action->setChecked(action->data().value<EditingMode>() == mode);
		}
	};
	initializeTools(&ui, &documents, &mainWindow);
	for (QAction* action : ui.editingModesToolBar->actions()) {
		QObject::connect(action, &QAction::triggered, [&, action]{
			if (ModelData* data = currentModelData(&ui, &documents)) {
				const EditingMode mode = action->data().value<EditingMode>();
				data->tools->setEditMode(mode);
				checkEditingModeAction(mode);
			}
		});
	}
	QObject::connect(ui.mdiArea, &QMdiArea::subWindowActivated,
	[&](QMdiSubWindow* subWindow) {
		ModelSubWindow* modelSubWindow = qobject_cast<ModelSubWindow*>(subWindow);
		if (modelSubWindow != nullptr) {
			if (ModelData* data = documents.findPayload<ModelData>(modelSubWindow->modelId)) {
				checkEditingModeAction(data->tools->currentEditingMode());
				if (data->itemSelectionModel != nullptr) {
					ui.modelListView->setModel(data->model);
					ui.modelListView->setSelectionModel(data->itemSelectionModel.get());
				}
			}
		}
	});
	QObject::connect(ui.actionAboutQt, &QAction::triggered, &app, &QApplication::aboutQt);
	QObject::connect(&documents, &DocumentManager::message, logMessage);
	mainWindow.setWindowTitle(title());
	mainWindow.restoreGeometry(setting<Setting::MainWindowGeometry>());
	restoreSettings();
	updateRenderPreferences(&ui, &renderPreferences, &documents);
	mainWindow.show();
	const int result = app.exec();
	saveSettings();
	return result;
}

mercurial