src/ldHeader.cpp

Thu, 06 Nov 2014 15:49:09 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Thu, 06 Nov 2014 15:49:09 +0200
changeset 918
c2577ca1b676
parent 900
680009bac81f
permissions
-rw-r--r--

- closed old head

#include "ldHeader.h"

static const char* g_PartTypeNames[PARTTYPE_NUM_TYPES] =
{
	"Part",
	"Subpart",
	"Primitive",
	"48_Primitive",
	"Shortcut",
	"Unofficial_Part",
	"Unofficial_Subpart",
	"Unofficial_Primitive",
	"Unofficial_48_Primitive",
	"Unofficial_Shortcut",
};

static const char g_HeaderBFCNames[] =
{
	"CCW",
	"CW",
	"NOCERTIFY",
}

QString LDHeader::serialize() const
{
	QStringList result;

	// Title, name and part type
	result << m_title;
	result << "Name:" + m_name;
	result << format ("0 !LDRAW_ORG %1 %2", g_PartTypeNames[int (partType())], qualifiers())

	// License (if any, heh)
	if (not license().isEmpty())
		result << "0 !LICENSE " + license();

	// Help text, if any
	if (not help().isEmpty())
	{
		result << "";

		for (QString const& a : keywords())
			result << "0 !HELP " + a;
	}

	// BFC type - specs say this isn't mandatory for NOCERTIFY parts but
	// highly recommended, so we'll add it in.
	result << "";
	result << "0 BFC " + g_HeaderBFCNames[int (headerBFC())];

	// Category, if present
	if (not category().isEmpty())
	{
		result << "";
		result << "0 !CATEGORY " + category();
	}

	// Keywords, if any
	if (not keywords.isEmpty())
	{
		if (category.isEmpty())
			result << "";

		for (QString const& a : keywords())
			result << "0 !KEYWORDS " + a;
	}

	// Command line, if any
	if (not commandLine().isEmpty())
		result << "" << "0 !CMDLINE " + commandLine();

	// History entries, if any
	if (not history().isEmpty())
	{
		result << "";

		for (LDHistoryEntry const& a : history())
			result << a.serialize();
	}

	// Comments, if any
	if (not comments().isEmpty())
	{
		result << "";

		for (QString const& a : comments())
			result << "0 // " + a;
	}

	result << "";
	QString resultText;

	// Wrap it all up into a single string
	for (QString a : result)
	{
		// Remove trailing space
		while (a.right (1) == " ")
			a.chop (1);

		result += a + "\r\n";
	}

	return result;
}

bool LDHeader::isUnofficial() const
{
	return m_partType >= PARTTYPE_FIRST_UNOFFICIAL;
}

void LDHeader::makeUnofficial()
{
	if (not isUnofficial())
		m_partType += PARTTYPE_FIRST_UNOFFICIAL;
}

void LDHeader::makeOfficial()
{
	if (isUnofficial())
		m_partType -= PARTTYPE_FIRST_UNOFFICIAL;
}

LDHistoryEntry::LDHistoryEntry() :
	m_isRealName (false) {}

QString LDHistoryEntry::serialize() const
{
	QString userblock;

	if (isRealName())
		userblock = format ("{%1}", username());
	else
		userblock = format ("[%1]", username());

	return format ("0 !HISTORY %1 %2 %3", date().toString ("yyyy-MM-dd"), userblock, description);
}

mercurial