src/main.cpp

Thu, 09 Jun 2022 13:32:55 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Thu, 09 Jun 2022 13:32:55 +0300
changeset 210
232e7634cc8a
parent 206
654661eab7f3
child 212
27259810da6d
permissions
-rw-r--r--

more refactoring, dosn't build yet

#include <QApplication>
#include <QFileDialog>
#include <QMessageBox>
#include <QMdiSubWindow>
#include <QStackedWidget>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "version.h"
#include "document.h"
#include "settingseditor/settingseditor.h"
#include "widgets/colorselectdialog.h"

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

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

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 LibraryManager* libraries,
	DocumentManager* documents,
	QWidget* parent)
{
	QString errorString;
	QTextStream errorStream{&errorString};
	const std::optional<ModelId> modelIdOpt = documents->openModel(
		path,
		errorStream,
		DocumentManager::OpenType::ManuallyOpened);
	if (modelIdOpt.has_value()) {
		documents->loadDependenciesForModel(modelIdOpt.value(), path, *libraries, errorStream);
		if (not errorString.isEmpty()) {
			QMessageBox::warning(
				parent,
				QObject::tr("Problem loading references"),
				errorString);
		}
	}
	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);
		}
	}
}

/*
void MainWindow::handleDocumentSplitterChange()
{
	EditorTabWidget* currentDocument = this->currentDocument();
	if (currentDocument != nullptr)
	{
		this->documentSplitterState = currentDocument->saveSplitterState();
		for (int i = 0; i < this->ui->tabs->count(); i += 1)
		{
			EditorTabWidget* document = qobject_cast<EditorTabWidget*>(this->ui->tabs->widget(i));
			if (document != nullptr and document != currentDocument)
			{
				document->restoreSplitterState(this->documentSplitterState);
			}
		}
		this->settings.setMainSplitterState(this->documentSplitterState);
	}
}
*/

static EditorTabWidget* currentTabWidget(Ui_MainWindow* ui)
{
	QMdiSubWindow* activeSubWindow = ui->mdiArea->activeSubWindow();
	if (activeSubWindow == nullptr) {
		return nullptr;
	}
	else {
		return qobject_cast<EditorTabWidget*>(activeSubWindow->widget());
	}
};


static void closeDocument(DocumentManager* documents, EditorTabWidget *document)
{
	std::optional<ModelId> modelId = documents->findIdForModel(document->model);
	if (modelId.has_value()) {
		documents->closeDocument(modelId.value());
		delete document;
	}
}

static std::optional<ModelId> findCurrentModelId(Ui_MainWindow* ui, DocumentManager* documents)
{
	const EditorTabWidget* tab = currentTabWidget(ui);
	if (tab != nullptr) {
		return documents->findIdForModel(tab->model);
	}
	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 LibraryManager* 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);
	}
}

static void updateRenderPreferences(
	Ui_MainWindow* ui,
	const gl::RenderPreferences* renderPreferences)
{
	for (QMdiSubWindow* subWindow : ui->mdiArea->subWindowList()) {
		EditorTabWidget* tab = qobject_cast<EditorTabWidget*>(subWindow->widget());
		if (tab != nullptr) {
			tab->canvas->setRenderPreferences(*renderPreferences);
		}
	}
	for (auto data : ::renderStyleButtons) {
		QAction* action = data.memberInstance(ui);
		action->setChecked(renderPreferences->style == data.payload);
	}
	ui->actionDrawAxes->setChecked(renderPreferences->drawAxes);
};

static gl::RenderPreferences loadRenderPreferences(Configuration* settings)
{
	return gl::RenderPreferences{
		.style = static_cast<gl::RenderStyle>(settings->renderStyle()),
		.mainColor = settings->mainColor(),
		.backgroundColor = settings->backgroundColor(),
		.selectedColor = settings->selectedColor(),
		.lineThickness = settings->lineThickness(),
		.lineAntiAliasing = settings->lineAntiAliasing(),
		.drawAxes = settings->drawAxes(),
	};
}

void initializeTools(Ui_MainWindow* ui, 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 = this->objectEditor,
		},
		{
			.name = QObject::tr("Draw"),
			.tooltip = QObject::tr("Draw new elements into the model."),
			.icon = {":/icons/pencil-outline.png"},
			.widget = nullptr,
		},
	};
	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);
	}
}

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{&mainWindow};
	QString currentLanguage = "en";
	QTranslator translator{&mainWindow};
	Configuration settings;
	LibraryManager libraries{&mainWindow};
	QByteArray documentSplitterState;
	QStringList recentlyOpenedFiles;
	ColorTable colorTable;
	gl::RenderPreferences renderPreferences;
	QMap<Model*, QItemSelectionModel*> itemSelectionModels;
	ui.setupUi(&mainWindow);
	const uiutilities::KeySequenceMap defaultKeyboardShortcuts =
		uiutilities::makeKeySequenceMap(uiutilities::collectActions(&mainWindow));
	const auto saveSettings = [&]{
		settings.setMainWindowGeometry(mainWindow.saveGeometry());
		settings.setRecentFiles(recentlyOpenedFiles);
		settings.setMainSplitterState(documentSplitterState);
		settings.setRenderStyle(static_cast<int>(renderPreferences.style));
		settings.setDrawAxes(renderPreferences.drawAxes);
		libraries.storeToSettings(&settings);
	};
	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 = settings.recentFiles();
		documentSplitterState = settings.mainSplitterState();
		renderPreferences = loadRenderPreferences(&settings);
		changeLanguage(settings.locale(), &translator);
		libraries.restoreFromSettings(&settings);
		updateRecentlyOpenedDocumentsMenu();
		colorTable = loadColors(&libraries);
		updateRenderPreferences(&ui, &renderPreferences);
		ui.mdiArea->setViewMode(static_cast<QMdiArea::ViewMode>(settings.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 openModelForEditing = [&](const ModelId modelId){
		Model* model = documents.getModelById(modelId);
		EditorTabWidget* document = new EditorTabWidget{model, &documents, colorTable};
		QObject::connect(
			document,
			&EditorTabWidget::modelAction,
			std::bind(executeAction, model, std::placeholders::_1));
		QItemSelectionModel* selectionModel = new QItemSelectionModel{model};
		itemSelectionModels[model] = selectionModel;
		QObject::connect(selectionModel, &QItemSelectionModel::selectionChanged,
			[model, document](const QItemSelection& selected, const QItemSelection& deselected)
		{
			auto resolveIndex = [&model](const QModelIndex& index){
				return model->idAt(index.row());
			};
			auto resolve = [&resolveIndex](const QItemSelection& selection)
			{
				return fn::map<QSet<ModelId>>(selection.indexes(), resolveIndex);
			};
			document->canvas->handleSelectionChange(resolve(selected), resolve(deselected));
		});
		document->canvas->setRenderPreferences(renderPreferences);
		QObject::connect(
			document,
			&EditorTabWidget::newStatusText,
			[&](const QString& newStatusText) {
				mainWindow.statusBar()->showMessage(newStatusText);
			});
		const QFileInfo fileInfo{*documents.modelPath(modelId)};
		QMdiSubWindow* subWindow = ui.mdiArea->addSubWindow(document);
		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{&settings, defaultKeyboardShortcuts, &mainWindow};
		const int result = settingsEditor.exec();
		if (result == QDialog::Accepted)
		{
			restoreSettings();
		}
	});
	QObject::connect(ui.actionQuit, &QAction::triggered, &mainWindow, &QMainWindow::close);
	QObject::connect(ui.actionAdjustGridToView, &QAction::triggered, [&ui]{
		EditorTabWidget* tab = currentTabWidget(&ui);
		if (tab != nullptr)
		{
			adjustGridToView(tab->canvas);
		}
	});
	QObject::connect(ui.actionClose, &QAction::triggered, [&ui, &documents]{
		EditorTabWidget* tab = currentTabWidget(&ui);
		if (tab != nullptr)
		{
			closeDocument(&documents, tab);
		}
	});
	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, &documents);
		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, [&]{
		if (currentTabWidget(&ui) != nullptr) {
			const std::optional<ModelId> modelId = findCurrentModelId(&ui, &documents);
			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, [&]{
		EditorTabWidget* tab = currentTabWidget(&ui);
		if (tab != nullptr) {
			std::vector<int> selectedRows = rows(ui.modelListView->selectionModel()->selectedRows());
			std::sort(selectedRows.begin(), selectedRows.end(), std::greater<int>{});
			for (int row : selectedRows) {
				executeAction(tab->model, DeleteFromModel{.position = row});
			}
		}
	});
	QObject::connect(ui.actionDrawAxes, &QAction::triggered, [&](bool drawAxes){
		renderPreferences.drawAxes = drawAxes;
		saveSettings();
		updateRenderPreferences(&ui, &renderPreferences);
	});
	for (auto data : ::renderStyleButtons) {
		QAction* action = data.memberInstance(&ui);
		QObject::connect(action, &QAction::triggered, [&, data]{
			renderPreferences.style = data.payload;
			saveSettings();
			updateRenderPreferences(&ui, &renderPreferences);
		});
	}
	const auto checkEditingModeAction = [&ui](EditingMode mode) {
		for (QAction* action : ui.editingModesToolBar->actions()) {
			action->setChecked(action->data().value<EditingMode>() == mode);
		}
	};
	initializeTools(&ui, &mainWindow);
	for (QAction* action : ui.editingModesToolBar->actions()) {
		QObject::connect(action, &QAction::triggered, [&, action]{
			EditorTabWidget* tab = currentTabWidget(&ui);
			if (tab != nullptr) {
				const EditingMode mode = action->data().value<EditingMode>();
				tab->setEditMode(mode);
				checkEditingModeAction(mode);
			}
		});
	}
	QObject::connect(ui.mdiArea, &QMdiArea::subWindowActivated,
	[&](QMdiSubWindow* subWindow) {
		if (subWindow != nullptr) {
			EditorTabWidget* tab = qobject_cast<EditorTabWidget*>(subWindow->widget());
			if (tab != nullptr) {
				checkEditingModeAction(tab->currentEditingMode());
				QItemSelectionModel* selectionModel = itemSelectionModels.value(tab->model);
				if (selectionModel != nullptr) {
					ui.modelListView->setModel(tab->model);
					ui.modelListView->setSelectionModel(selectionModel);
				}
			}
		}
	});
	mainWindow.setWindowTitle(title());
	mainWindow.restoreGeometry(settings.mainWindowGeometry());
	restoreSettings();
	updateRenderPreferences(&ui, &renderPreferences);
	mainWindow.show();
	const int result = app.exec();
	saveSettings();
	return result;
}

mercurial