src/ldrawalgorithm.cpp

Sun, 09 Apr 2023 15:59:08 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sun, 09 Apr 2023 15:59:08 +0300
changeset 362
e1d646a4cbd8
parent 338
719b909a7d2b
permissions
-rw-r--r--

Extracted the state of the program into a MainState structure, and extracted local functions of main() into static functions.
I was planning to make the core logic and state of the program into a Main class, which would be a QObject that would
have lots of signals and slots, but it looks like this works even without it

#include <QTextBlock>
#include "src/ldrawalgorithm.h"

std::pair<Triangle, Triangle> splitTriangles(
	const Quadrilateral& q,
	ldraw::Diagonal diagonal)
{
	std::pair<Triangle, Triangle> result;
	switch (diagonal)
	{
	case ldraw::Diagonal::Diagonal_13:
		result = {Triangle{q.p1, q.p2, q.p3}, {q.p1, q.p3, q.p4}};
		break;
	case ldraw::Diagonal::Diagonal_24:
		result = {Triangle{q.p1, q.p2, q.p3}, {q.p2, q.p3, q.p4}};
		break;
	}
	return result;
}

std::vector<ModelAction> ldraw::makeUnofficial(const QTextDocument* model)
{
	std::vector<ModelAction> actions;
	constexpr int ldrawOrgLinePosition = 3;
	const QTextBlock block = model->findBlockByLineNumber(ldrawOrgLinePosition);
	if (block.isValid()) {
		const QString body = block.text().simplified();
		if (body.startsWith("0 !LDRAW_ORG ") and not body.startsWith("0 !LDRAW_ORG Unofficial_")) {
			// Add Unofficial_ to part type
			QStringList tokens = body.split(" ");
			tokens[2] = "Unofficial_" + tokens[2];
			// Remove the UPDATE tag if it's there
			if (tokens.size() >= 5 && tokens[3] == "UPDATE")
			{
				tokens.removeAt(4);
				tokens.removeAt(3);
			}
			actions.push_back(ModifyModel{
				.position = ldrawOrgLinePosition,
				.newElement = Comment{.text = tokens.mid(1).join(" ")}
			});
		}
	}
	return actions;
}

mercurial