Generalized SetContentsHistory to an EditHistory

Fri, 19 Apr 2013 15:21:46 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Fri, 19 Apr 2013 15:21:46 +0300
changeset 117
7eb7a43a511b
parent 116
4fde8fdf258a
child 118
649110bb36a8

Generalized SetContentsHistory to an EditHistory

colors.cpp file | annotate | diff | comparison | revisions
file.cpp file | annotate | diff | comparison | revisions
file.h file | annotate | diff | comparison | revisions
gui.cpp file | annotate | diff | comparison | revisions
history.cpp file | annotate | diff | comparison | revisions
history.h file | annotate | diff | comparison | revisions
ldtypes.cpp file | annotate | diff | comparison | revisions
ldtypes.h file | annotate | diff | comparison | revisions
main.cpp file | annotate | diff | comparison | revisions
misc.cpp file | annotate | diff | comparison | revisions
misc.h file | annotate | diff | comparison | revisions
zz_historyDialog.cpp file | annotate | diff | comparison | revisions
zz_setContentsDialog.cpp file | annotate | diff | comparison | revisions
--- a/colors.cpp	Wed Apr 17 14:09:38 2013 +0300
+++ b/colors.cpp	Fri Apr 19 15:21:46 2013 +0300
@@ -29,7 +29,7 @@
 	if (g_bColorsInit)
 		return;
 	
-	printf ("%s: initializing color information.\n", __func__);
+	logf ("%s: initializing color information.\n", __func__);
 	
 	// Always make sure there's 16 and 24 available. They're special like that.
 	color* maincolor = new color;
@@ -62,7 +62,7 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-static bool parseLDConfigTag (stringparser& pars, char const* sTag, str& zVal) {
+static bool parseLDConfigTag (StringParser& pars, char const* sTag, str& zVal) {
 	short dPos;
 	if (!pars.findToken (dPos, sTag, 1))
 		return false;
@@ -104,7 +104,7 @@
 		zLine.replace ("\n", "");
 		zLine.replace ("\r", "");
 		
-		stringparser pars (zLine, ' ');
+		StringParser pars (zLine, ' ');
 		short dCode = 0, dAlpha = 255;
 		str zName, zColor, zEdge, zValue;
 		
--- a/file.cpp	Wed Apr 17 14:09:38 2013 +0300
+++ b/file.cpp	Fri Apr 19 15:21:46 2013 +0300
@@ -59,7 +59,7 @@
 		// Try with just the LDraw path first
 		zFilePath = format ("%s" DIRSLASH "%s",
 			io_ldpath.value.chars(), zTruePath.chars());
-		printf ("try %s\n", zFilePath.chars());
+		logf ("Trying %s\n", zFilePath.chars());
 		
 		fp = fopen (zFilePath, "r");
 		if (fp != null)
@@ -562,4 +562,59 @@
 	
 	// Erase it from memory
 	objects.erase (objects.begin() + ulIndex);
+}
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
+std::vector<partListEntry> g_PartList;
+
+void initPartList () {
+	logf ("%s: initializing parts.lst\n", __func__);
+	
+	FILE* fp = openLDrawFile ("parts.lst", false);
+	
+	if (!fp)
+		return;
+	
+	char sLine[1024];
+	while (fgets (sLine, sizeof sLine, fp)) {
+		// Locate the first whitespace
+		char* cpWhite = strstr (sLine, " ");
+		
+		char sName[65];
+		size_t uLength = (cpWhite - sLine);
+		
+		if (uLength >= 64)
+			continue; // too long
+		
+		strncpy (sName, sLine, uLength);
+		sName[uLength] = '\0';
+		
+		// Surf through the whitespace sea!
+		while (*cpWhite == ' ')
+			cpWhite++;
+		
+		// Get the end point
+		char* cpEnd = strstr (sLine, "\r");
+		
+		if (cpEnd == null) {
+			// must not be DOS-formatted
+			cpEnd = strstr (sLine, "\n");
+		}
+		
+		assert (cpEnd != null);
+		
+		// Make the file title now
+		char sTitle[81];
+		uLength = (cpEnd - cpWhite);
+		strncpy (sTitle, cpWhite, uLength);
+		sTitle[uLength] = '\0';
+		
+		// Add it to the array.
+		partListEntry entry;
+		strcpy (entry.sName, sName);
+		strcpy (entry.sTitle, sTitle);
+		g_PartList.push_back (entry);
+	}
 }
\ No newline at end of file
--- a/file.h	Wed Apr 17 14:09:38 2013 +0300
+++ b/file.h	Fri Apr 19 15:21:46 2013 +0300
@@ -80,6 +80,13 @@
 // Re-caches all subfiles.
 void reloadAllSubfiles ();
 
+typedef struct {
+	char sName[65], sTitle[81];
+} partListEntry;
+
+// Init and parse parts.lst
+void initPartList ();
+
 extern vector<OpenFile*> g_LoadedFiles;
 
 #endif // __FILE_H__
\ No newline at end of file
--- a/gui.cpp	Wed Apr 17 14:09:38 2013 +0300
+++ b/gui.cpp	Fri Apr 19 15:21:46 2013 +0300
@@ -425,7 +425,7 @@
 	}
 	
 	if (!pMeta) {
-		fprintf (stderr, "unknown signal sender %p!\n", qAct);
+		logf (LOG_Warning, "unknown signal sender %p!\n", qAct);
 		return;
 	}
 	
@@ -708,7 +708,6 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 // ========================================================================= //
 void ForgeWindow::refresh () {
-	printf ("refreshing.. (%lu)\n", paSelection.size ());
 	buildObjList ();
 	R->hardRefresh ();
 }
@@ -744,8 +743,6 @@
 	for (LDObject* obj : paSelection)
 		obj->qObjListEntry->setSelected (true);
 	
-	printf ("updateSelection: %lu objects selected\n", paSelection.size ());
-	
 	g_bSelectionLocked = false;
 	slot_selectionChanged ();
 }
--- a/history.cpp	Wed Apr 17 14:09:38 2013 +0300
+++ b/history.cpp	Fri Apr 19 15:21:46 2013 +0300
@@ -142,21 +142,29 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-void SetContentsHistory::undo () {
-	LDObject* obj = g_CurrentFile->objects[ulIndex];
-	obj->replace (oldObj->clone ());
+void EditHistory::undo () {
+	for (ulong idx : ulaIndices) {
+		LDObject* obj = g_CurrentFile->objects[idx];
+		obj->replace (paOldObjs[idx]->clone ());
+	}
+	
 	g_ForgeWindow->refresh ();
 }
 
-void SetContentsHistory::redo () {
-	LDObject* obj = g_CurrentFile->objects[ulIndex];
-	obj->replace (newObj->clone ());
+void EditHistory::redo () {
+	for (ulong idx : ulaIndices) {
+		LDObject* obj = g_CurrentFile->objects[idx];
+		obj->replace (paNewObjs[idx]->clone ());
+	}
+	
 	g_ForgeWindow->refresh ();
 }
 
-SetContentsHistory::~SetContentsHistory () {
-	delete oldObj;
-	delete newObj;
+EditHistory::~EditHistory () {
+	for (ulong idx : ulaIndices) {
+		delete paOldObjs[idx];
+		delete paNewObjs[idx];
+	}
 }
 
 // =============================================================================
--- a/history.h	Wed Apr 17 14:09:38 2013 +0300
+++ b/history.h	Fri Apr 19 15:21:46 2013 +0300
@@ -32,7 +32,7 @@
 enum HistoryType {
 	HISTORY_Del,
 	HISTORY_SetColor,
-	HISTORY_SetContents,
+	HISTORY_Edit,
 	HISTORY_ListMove,
 	HISTORY_Add,
 	HISTORY_QuadSplit,
@@ -89,15 +89,16 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-class SetContentsHistory : public HistoryEntry {
+class EditHistory : public HistoryEntry {
 public:
-	IMPLEMENT_HISTORY_TYPE (SetContents)
+	IMPLEMENT_HISTORY_TYPE (Edit)
 	
-	ulong ulIndex;
-	LDObject* oldObj, *newObj;
+	const std::vector<ulong> ulaIndices;
+	const std::vector<LDObject*> paOldObjs, paNewObjs;
 	
-	SetContentsHistory (ulong ulIndex, LDObject* oldObj, LDObject* newObj) :
-		ulIndex (ulIndex), oldObj (oldObj), newObj (newObj) {}
+	EditHistory (std::vector<ulong> ulaIndices, std::vector<LDObject*> paOldObjs,
+		std::vector<LDObject*> paNewObjs) :
+		ulaIndices (ulaIndices), paOldObjs (paOldObjs), paNewObjs (paNewObjs) {}
 };
 
 // =============================================================================
--- a/ldtypes.cpp	Wed Apr 17 14:09:38 2013 +0300
+++ b/ldtypes.cpp	Fri Apr 19 15:21:46 2013 +0300
@@ -429,7 +429,7 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-str LDObject::objectListContents (std::vector<LDObject*>& objs) {
+str LDObject::objectListContents (const std::vector<LDObject*>& objs) {
 	bool bFirstDetails = true;
 	str zText = "";
 	
--- a/ldtypes.h	Wed Apr 17 14:09:38 2013 +0300
+++ b/ldtypes.h	Fri Apr 19 15:21:46 2013 +0300
@@ -113,7 +113,7 @@
 	LDObject* topLevelParent ();
 	
 	static void moveObjects (std::vector<LDObject*> objs, const bool bUp);
-	static str objectListContents (std::vector<LDObject*>& objs);
+	static str objectListContents (const std::vector<LDObject*>& objs);
 	
 	QTreeWidgetItem* qObjListEntry;
 };
--- a/main.cpp	Wed Apr 17 14:09:38 2013 +0300
+++ b/main.cpp	Fri Apr 19 15:21:46 2013 +0300
@@ -34,6 +34,9 @@
 const vertex g_Origin (0.0f, 0.0f, 0.0f);
 const matrix g_mIdentity (1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
 
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
 int main (int dArgc, char* saArgv[]) {
 	// Load or create the configuration
 	if (!config::load()) {
@@ -45,6 +48,7 @@
 	}
 	
 	initColors ();
+	initPartList ();
 	
 	QApplication app (dArgc, saArgv);
 	ForgeWindow* win = new ForgeWindow;
@@ -58,65 +62,59 @@
 }
 
 // =============================================================================
-// void logVA (logtype_e, const char*, va_list) [static]
-//
-// Common code for the two logfs
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
+// Common code for the two logfs
 static void logVA (logtype_e eType, const char* fmt, va_list va) {
-	// Log it to standard output
-	vprintf (fmt, va);
-	
 	return;
 	
-	str zText;
-	char* sBuffer;
+	char* sBuffer = vdynformat (fmt, va, 128);
+	printf ("buffer: %s\n", sBuffer);
+	str zText (sBuffer);
+	delete[] sBuffer;
 	
-	sBuffer = vdynformat (fmt, va, 128);
-	zText = sBuffer;
-	delete[] sBuffer;
+	// Log it to standard output
+	printf ("%s", zText.chars ());
 	
 	// Replace some things out with HTML entities
 	zText.replace ("<", "&lt;");
 	zText.replace (">", "&gt;");
 	zText.replace ("\n", "<br />");
 	
-	str* zpHTML = &g_ForgeWindow->zMessageLogHTML;
+	str& zLog = g_ForgeWindow->zMessageLogHTML;
 	
 	switch (eType) {
 	case LOG_Normal:
-		zpHTML->append (zText);
+		printf ("appending \"%s\"\n", zText.chars ());
+		zLog.append (zText);
 		break;
 	
 	case LOG_Error:
-		zpHTML->appendformat ("<span style=\"color: #F8F8F8; background-color: #800\"><b>[ERROR]</b> %s</span>",
+		zLog.appendformat ("<span style=\"color: #F8F8F8; background-color: #800\"><b>[ERROR]</b> %s</span>",
 			zText.chars());
 		break;
 	
 	case LOG_Info:
-		zpHTML->appendformat ("<span style=\"color: #04F\"><b>[INFO]</b> %s</span>",
+		zLog.appendformat ("<span style=\"color: #04F\"><b>[INFO]</b> %s</span>",
 			zText.chars());
 		break;
 	
 	case LOG_Success:
-		zpHTML->appendformat ("<span style=\"color: #6A0\"><b>[SUCCESS]</b> %s</span>",
+		zLog.appendformat ("<span style=\"color: #6A0\"><b>[SUCCESS]</b> %s</span>",
 			zText.chars());
 		break;
 	
 	case LOG_Warning:
-		zpHTML->appendformat ("<span style=\"color: #C50\"><b>[WARNING]</b> %s</span>",
+		zLog.appendformat ("<span style=\"color: #C50\"><b>[WARNING]</b> %s</span>",
 			zText.chars());
 		break;
 	}
 	
-	g_ForgeWindow->qMessageLog->setHtml (*zpHTML);
+	g_ForgeWindow->qMessageLog->setHtml (zLog);
 }
 
-
 // =============================================================================
-// logf (const char*, ...)
-// logf (logtype_e eType, const char*, ...)
-//
-// Outputs a message into the message log
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 void logf (const char* fmt, ...) {
 	va_list va;
@@ -132,4 +130,4 @@
 	va_start (va, fmt);
 	logVA (eType, fmt, va);
 	va_end (va);
-}
\ No newline at end of file
+}
--- a/misc.cpp	Wed Apr 17 14:09:38 2013 +0300
+++ b/misc.cpp	Fri Apr 19 15:21:46 2013 +0300
@@ -78,17 +78,6 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-void stripWhitespace (str& s) {
-	str other;
-	
-	for (size_t i = 0; i < ~s; i++)
-		if (s[i] > 32 && s[i] < 127)
-			other += s[i];
-}
-
-// =============================================================================
-// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-// =============================================================================
 str ftoa (double fCoord) {
 	// Disable the locale first so that the decimal point will not
 	// turn into anything weird (like commas)
@@ -143,7 +132,7 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-void simplify (short& dNumerator, short& dDenominator) {
+void simplify (short& dNum, short& dDenom) {
 	bool bRepeat;
 	
 	do {
@@ -151,14 +140,14 @@
 		
 		for (ulong x = 0; x < NUM_PRIMES; x++) {
 			ulong i = NUM_PRIMES - x - 1;
+			ushort uPrime = g_uaPrimes[i];
 			
-			ushort uPrime = g_uaPrimes[i];
-			if (dNumerator <= uPrime || dDenominator <= uPrime)
+			if (dNum <= uPrime || dDenom <= uPrime)
 				continue;
 			
-			if (dNumerator % uPrime == 0 && dDenominator % uPrime == 0) {
-				dNumerator /= uPrime;
-				dDenominator /= uPrime;
+			if ((dNum % uPrime == 0) && (dDenom % uPrime == 0)) {
+				dNum /= uPrime;
+				dDenom /= uPrime;
 				bRepeat = true;
 				break;
 			}
@@ -169,23 +158,23 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-stringparser::stringparser (str zInText, char cSeparator) {
+StringParser::StringParser (str zInText, char cSeparator) {
 	zaTokens = zInText.split (cSeparator, true);
 	dPos = -1;
 }
 
 // -----------------------------------------------------------------------------
-bool stringparser::atBeginning () {
+bool StringParser::atBeginning () {
 	return (dPos == -1);
 }
 
 // -----------------------------------------------------------------------------
-bool stringparser::atEnd () {
-	return (dPos == (signed)zaTokens.size() - 1);
+bool StringParser::atEnd () {
+	return (dPos == (signed) zaTokens.size () - 1);
 }
 
 // -----------------------------------------------------------------------------
-bool stringparser::getToken (str& zVal, const ushort uInPos) {
+bool StringParser::getToken (str& zVal, const ushort uInPos) {
 	if (uInPos >= zaTokens.size())
 		return false;
 	
@@ -194,18 +183,18 @@
 }
 
 // -----------------------------------------------------------------------------
-bool stringparser::next (str& zVal) {
+bool StringParser::next (str& zVal) {
 	return getToken (zVal, ++dPos);
 }
 
 // -----------------------------------------------------------------------------
-bool stringparser::peekNext (str& zVal) {
+bool StringParser::peekNext (str& zVal) {
 	return getToken (zVal, dPos + 1);
 }
 
 // -----------------------------------------------------------------------------
-bool stringparser::findToken (short& dResult, char const* sNeedle, short dArgs) {
-	for (ushort i = 0; i < (zaTokens.size() - dArgs); ++i) {
+bool StringParser::findToken (short& dResult, char const* sNeedle, short dArgs) {
+	for (ushort i = 0; i < (zaTokens.size () - dArgs); ++i) {
 		if (zaTokens[i] == sNeedle) {
 			dResult = i;
 			return true;
@@ -216,22 +205,23 @@
 }
 
 // -----------------------------------------------------------------------------
-void stringparser::rewind () {
+void StringParser::rewind () {
 	dPos = -1;
 }
 
 // -----------------------------------------------------------------------------
-void stringparser::seek (short int dAmount, bool bRelative) {
+void StringParser::seek (short int dAmount, bool bRelative) {
 	dPos = (bRelative ? dPos : 0) + dAmount;
 }
 
 // -----------------------------------------------------------------------------
-size_t stringparser::size () {
+size_t StringParser::size () {
 	return zaTokens.size();
 }
 
 // -----------------------------------------------------------------------------
-bool stringparser::tokenCompare (short int dInPos, const char* sOther) {
+bool StringParser
+::tokenCompare (short int dInPos, const char* sOther) {
 	str tok;
 	if (!getToken (tok, dInPos))
 		return false;
--- a/misc.h	Wed Apr 17 14:09:38 2013 +0300
+++ b/misc.h	Fri Apr 19 15:21:46 2013 +0300
@@ -23,53 +23,30 @@
 #include "str.h"
 
 #define NUM_PRIMES 500
+
+// Prime numbers
 extern const ushort g_uaPrimes[NUM_PRIMES];
 
-inline str GetWord (str& zString, ulong ulIndex) {
-	return (zString / " ")[ulIndex];
-}
-
-void stripWhitespace (str& s);
-
-// Returns whether a given string represents a floating point number
-// TODO: Does LDraw support scientific notation?
+// Returns whether a given string represents a floating point number.
 bool isNumber (str& zToken);
 
 // Converts a float value to a string value.
 str ftoa (double fCoord);
 
-template<class T> bool in (T needle, std::initializer_list<T> haystack) {
-	for (T hay : haystack) {
-		printf ("%s: %ld <-> %ld\n", __func__, needle, hay);
-		if (needle == hay)
-			return true;
-	}
-	
-	return false;
-}
-
-template<class T> std::vector<T> reverseVector (std::vector<T> in) {
-	std::vector<T> out;
-	
-	for (T stuff : in)
-		out.insert (out.begin(), in);
-	
-	return out;
-}
-
-void simplify (short& dNumerator, short& dDenominator);
+// Simplifies the given fraction.
+void simplify (short& dNum, short& dDenom);
 
 // =============================================================================
-// stringparser
+// StringParser
 //
 // String parsing utility
 // =============================================================================
-class stringparser {
+class StringParser {
 public:
 	std::vector<str> zaTokens;
 	short dPos;
 	
-	stringparser (str zInText, char cSeparator);
+	StringParser (str zInText, char cSeparator);
 	
 	bool atEnd ();
 	bool atBeginning ();
--- a/zz_historyDialog.cpp	Wed Apr 17 14:09:38 2013 +0300
+++ b/zz_historyDialog.cpp	Fri Apr 19 15:21:46 2013 +0300
@@ -179,14 +179,13 @@
 			}
 			break;
 		
-		case HISTORY_SetContents:
+		case HISTORY_Edit:
 			{
-				SetContentsHistory* setentry = static_cast<SetContentsHistory*> (entry);
+				EditHistory* pEntry = static_cast<EditHistory*> (entry);
 				
-				zText.format ("Set contents of %s\n%s (%s)",
-					g_saObjTypeNames [setentry->oldObj->getType ()],
-					setentry->newObj->getContents ().chars (),
-					g_saObjTypeNames [setentry->newObj->getType ()]);
+				zText.format ("Edited %u objects\n%s",
+					pEntry->paNewObjs.size(),
+					LDObject::objectListContents (pEntry->paOldObjs).chars ());
 				qEntryIcon = getIcon ("set-contents");
 			}
 			break;
--- a/zz_setContentsDialog.cpp	Wed Apr 17 14:09:38 2013 +0300
+++ b/zz_setContentsDialog.cpp	Fri Apr 19 15:21:46 2013 +0300
@@ -97,8 +97,8 @@
 	
 	// Mark down the history now before we perform the replacement (which
 	// destroys the old object)
-	History::addEntry (new SetContentsHistory (oldobj->getIndex (g_CurrentFile),
-		oldobj->clone (), obj->clone ()));
+	History::addEntry (new EditHistory ({oldobj->getIndex (g_CurrentFile)},
+		{oldobj->clone ()}, {obj->clone ()}));
 	
 	oldobj->replace (obj);
 	

mercurial