the header is now also saved

Sat, 17 Mar 2018 20:47:35 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 17 Mar 2018 20:47:35 +0200
changeset 1295
ac8f0297885b
parent 1294
adaad0b329e2
child 1296
aee618d087ff

the header is now also saved

src/lddocument.cpp file | annotate | diff | comparison | revisions
src/lddocument.h file | annotate | diff | comparison | revisions
src/parser.cpp file | annotate | diff | comparison | revisions
src/parser.h file | annotate | diff | comparison | revisions
--- a/src/lddocument.cpp	Sat Mar 17 12:29:52 2018 +0200
+++ b/src/lddocument.cpp	Sat Mar 17 20:47:35 2018 +0200
@@ -25,6 +25,7 @@
 #include "dialogs/openprogressdialog.h"
 #include "documentmanager.h"
 #include "linetypes/comment.h"
+#include "parser.h"
 
 LDDocument::LDDocument (DocumentManager* parent) :
     Model {parent},
@@ -220,6 +221,120 @@
 	return true;
 }
 
+static QString headerToString(const LDHeader& header)
+{
+	QString result;
+
+	if (header.type != LDHeader::NoHeader)
+	{
+		QString partTypeString;
+
+		for (
+			auto iterator = Parser::typeStrings.begin();
+			iterator != Parser::typeStrings.end();
+			++iterator
+		) {
+			if (iterator.value() == header.type)
+			{
+				partTypeString += "Unofficial_" + iterator.key();
+				break;
+			}
+		}
+
+		if (header.qualfiers & LDHeader::Physical_Color)
+			partTypeString += " Physical_Colour";
+
+		if (header.qualfiers & LDHeader::Flexible_Section)
+			partTypeString += " Flexible_Section";
+
+		if (header.qualfiers & LDHeader::Alias)
+			partTypeString += " Alias";
+
+		result += "0 " + header.description + "\r\n";
+		result += "0 Name: " + header.name + "\r\n";
+		result += "0 Author: " + header.author + "\r\n";
+		result += "0 !LDRAW_ORG " + partTypeString + "\r\n";
+
+		switch (header.license)
+		{
+		case LDHeader::CaLicense:
+			result += "0 !LICENSE Redistributable under CCAL version 2.0 : see CAreadme.txt\r\n";
+			break;
+		case LDHeader::NonCaLicense:
+			result += "0 !LICENSE Not redistributable : see NonCAreadme.txt\r\n";
+			break;
+		case LDHeader::UnspecifiedLicense:
+			break;
+		}
+
+		if (not header.help.isEmpty())
+		{
+			result += "\r\n";
+			for (QString line : header.help.split("\n"))
+				result += "0 !HELP " + line + "\r\n";
+		}
+
+		result += "\r\n";
+
+		switch (header.winding)
+		{
+		case CounterClockwise:
+			result += "0 BFC CERTIFY CCW\r\n";
+			break;
+
+		case Clockwise:
+			result += "0 BFC CERTIFY CW\r\n";
+			break;
+
+		case NoWinding:
+			result += "0 BFC NOCERTIFY\r\n";
+			break;
+		}
+
+		if (not header.category.isEmpty() or not header.keywords.isEmpty())
+		{
+			result += "\r\n";
+
+			if (not header.category.isEmpty())
+				result += "0 !CATEGORY " + header.category + "\r\n";
+
+			if (not header.keywords.isEmpty())
+			{
+				for (QString line : header.keywords.split("\n"))
+					result += "0 !KEYWORDS " + line + "\r\n";
+			}
+		}
+
+		if (not header.cmdline.isEmpty())
+		{
+			result += "\r\n";
+			result += "0 !CMDLINE " + header.cmdline + "\r\n";
+		}
+
+		if (not header.history.isEmpty())
+		{
+			result += "\r\n";
+
+			for (const LDHeader::HistoryEntry& historyEntry : header.history)
+			{
+				QString author = historyEntry.author;
+
+				if (not author.startsWith("{"))
+					author = "[" + author + "]";
+
+				result += "0 !HISTORY ";
+				result += historyEntry.date.toString(Qt::ISODate) + " ";
+				result += author + " ";
+				result += historyEntry.description + "\r\n";
+			}
+		}
+
+		result += "\r\n";
+	}
+
+	return result.toUtf8();
+}
+
 // =============================================================================
 //
 bool LDDocument::save (QString path, qint64* sizeptr)
@@ -230,22 +345,14 @@
 	if (path.isEmpty())
 		path = fullPath();
 
-	// If the second object in the list holds the file name, update that now.
-	LDObject* nameObject = getObject (1);
-
-	if (nameObject and nameObject->type() == LDObjectType::Comment)
-	{
-		LDComment* nameComment = static_cast<LDComment*> (nameObject);
+	QByteArray data;
 
-		if (nameComment->text().left (6) == "Name: ")
-		{
-			QString newname = shortenName (path);
-			nameComment->setText (format ("Name: %1", newname));
-		}
+	if (this->header.type != LDHeader::NoHeader)
+	{
+		header.name = LDDocument::shortenName(path);
+		data += headerToString(this->header).toUtf8();
 	}
 
-	QByteArray data;
-
 	// File is open, now save the model to it. Note that LDraw requires files to have DOS line endings.
 	for (LDObject* obj : objects())
 	{
--- a/src/lddocument.h	Sat Mar 17 12:29:52 2018 +0200
+++ b/src/lddocument.h	Sat Mar 17 20:47:35 2018 +0200
@@ -72,10 +72,10 @@
 	Winding winding = NoWinding;
 	enum
 	{
-		Unspecified,
+		UnspecifiedLicense,
 		CaLicense,
 		NonCaLicense
-	} license = Unspecified;
+	} license = UnspecifiedLicense;
 };
 
 Q_DECLARE_OPERATORS_FOR_FLAGS(QFlags<LDHeader::Qualifier>)
--- a/src/parser.cpp	Sat Mar 17 12:29:52 2018 +0200
+++ b/src/parser.cpp	Sat Mar 17 20:47:35 2018 +0200
@@ -40,6 +40,16 @@
 	return QString::fromUtf8(this->device.readLine()).trimmed();
 }
 
+const QMap<QString, decltype(LDHeader::type)> Parser::typeStrings {
+	{"Part", LDHeader::Part},
+	{"Subpart", LDHeader::Subpart},
+	{"Shortcut", LDHeader::Shortcut},
+	{"Primitive", LDHeader::Primitive},
+	{"8_Primitive", LDHeader::Primitive_8},
+	{"48_Primitive", LDHeader::Primitive_48},
+	{"Configuration", LDHeader::Configuration},
+};
+
 /*
  * Parses a single line of the header.
  * Possible parse results:
@@ -65,22 +75,13 @@
 
 		if (not tokens.isEmpty())
 		{
-			static const QMap<QString, decltype(LDHeader::type)> typeStrings {
-				{"Part", LDHeader::Part},
-				{"Subpart", LDHeader::Subpart},
-				{"Shortcut", LDHeader::Shortcut},
-				{"Primitive", LDHeader::Primitive},
-				{"8_Primitive", LDHeader::Primitive_8},
-				{"48_Primitive", LDHeader::Primitive_48},
-				{"Configuration", LDHeader::Configuration},
-			};
 			QString partTypeString = tokens[0];
 			// Anything that enters LDForge becomes unofficial in any case if saved.
 			// Therefore we don't need to give the Unofficial type any special
 			// consideration.
 			if (partTypeString.startsWith("Unofficial_"))
 				partTypeString = partTypeString.mid(strlen("Unofficial_"));
-			header.type = typeStrings.value(partTypeString, LDHeader::Part);
+			header.type = Parser::typeStrings.value(partTypeString, LDHeader::Part);
 			header.qualfiers = 0;
 			if (tokens.contains("Alias"))
 				header.qualfiers |= LDHeader::Alias;
--- a/src/parser.h	Sat Mar 17 12:29:52 2018 +0200
+++ b/src/parser.h	Sat Mar 17 20:47:35 2018 +0200
@@ -18,6 +18,7 @@
 
 #pragma once
 #include "main.h"
+#include "lddocument.h"
 
 class LDHeader;
 class Model;
@@ -36,6 +37,8 @@
 
 	static LDObject* parseFromString(Model& model, int position, QString line);
 
+	static const QMap<QString, decltype(LDHeader::type)> typeStrings;
+
 /*
 signals:
 	void parseErrorMessage(QString message);

mercurial