Added insert from action to import file contents from another file

Fri, 26 Apr 2013 18:11:34 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Fri, 26 Apr 2013 18:11:34 +0300
changeset 140
2e8c1626aef7
parent 139
5e31a96adaa2
child 141
184d117e1b12

Added insert from action to import file contents from another file

buttonbox.h 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
gui.h file | annotate | diff | comparison | revisions
gui_actions.cpp file | annotate | diff | comparison | revisions
ldtypes.cpp file | annotate | diff | comparison | revisions
types.cpp file | annotate | diff | comparison | revisions
types.h file | annotate | diff | comparison | revisions
zz_addObjectDialog.cpp file | annotate | diff | comparison | revisions
zz_addObjectDialog.h file | annotate | diff | comparison | revisions
--- a/buttonbox.h	Fri Apr 26 03:44:34 2013 +0300
+++ b/buttonbox.h	Fri Apr 26 18:11:34 2013 +0300
@@ -133,6 +133,10 @@
 		return buttonGroup->checkedId ();
 	}
 	
+	void setValue (int val) {
+		static_cast<R*> (buttonGroup->button (val))->setChecked (true);
+	}
+	
 	R* const& begin () {
 		return objects.begin ();
 	}
--- a/file.cpp	Fri Apr 26 03:44:34 2013 +0300
+++ b/file.cpp	Fri Apr 26 18:11:34 2013 +0300
@@ -109,6 +109,50 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
+ulong loadFileContents (FILE* fp, OpenFile* load, std::vector<LDObject*>* copies, ulong* idx) {
+	char line[1024];
+	vector<str> lines;
+	ulong numWarnings = 0;
+	
+	while (fgets (line, sizeof line, fp)) {
+		// Trim the trailing newline
+		str zLine = line;
+		while (zLine[~zLine - 1] == '\n' || zLine[~zLine - 1] == '\r')
+			zLine -= 1;
+		
+		lines.push_back (zLine);
+	}
+	
+	if (idx)
+		*idx = load->objects.size ();
+	
+	ulong lnum = 1;
+	for (str line : lines) {
+		LDObject* obj = parseLine (line);
+		
+		if (copies)
+			copies->push_back (obj->clone ());
+		
+		load->objects.push_back (obj);
+		
+		// Check for parse errors and warn about tthem
+		if (obj->getType() == OBJ_Gibberish) {
+			logf (LOG_Warning, "Couldn't parse line #%lu: %s\n",
+				lnum, static_cast<LDGibberish*> (obj)->zReason.chars());
+			
+			logf (LOG_Warning, "- Line was: %s\n", line.chars());
+			numWarnings++;
+		}
+		
+		lnum++;
+	}
+	
+	return numWarnings;
+}
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
 OpenFile* openDATFile (str path, bool search) {
 	logf ("Opening %s...\n", path.chars());
 	
@@ -127,41 +171,10 @@
 	}
 	
 	OpenFile* load = new OpenFile;
-	ulong numWarnings = 0;
-	
 	load->zFileName = path;
-	
-	vector<str> lines;
-	
-	{
-		char line[1024];
-		while (fgets (line, sizeof line, fp)) {
-			// Trim the trailing newline
-			str zLine = line;
-			while (zLine[~zLine - 1] == '\n' || zLine[~zLine - 1] == '\r')
-				zLine -= 1;
-			
-			lines.push_back (zLine);
-		}
-	}
+	ulong numWarnings	= loadFileContents (fp, load);
 	
 	fclose (fp);
-	
-	for (str line : lines) {
-		LDObject* obj = parseLine (line);
-		load->objects.push_back (obj);
-		
-		// Check for parse errors and warn about tthem
-		if (obj->getType() == OBJ_Gibberish) {
-			logf (LOG_Warning, "Couldn't parse line #%lu: %s\n",
-				(&line - &(lines[0])),
-				static_cast<LDGibberish*> (obj)->zReason.chars());
-			
-			logf (LOG_Warning, "- Line was: %s\n", line.chars());
-			numWarnings++;
-		}
-	}
-	
 	g_LoadedFiles.push_back (load);
 	
 	logf (LOG_Success, "File %s parsed successfully (%lu warning%s).\n",
--- a/file.h	Fri Apr 26 03:44:34 2013 +0300
+++ b/file.h	Fri Apr 26 18:11:34 2013 +0300
@@ -99,6 +99,8 @@
 // Init and parse parts.lst
 void initPartList ();
 
+ulong loadFileContents (FILE* fp, OpenFile* load, std::vector<LDObject*>* copies = null, ulong* idx = null);
+
 extern vector<OpenFile*> g_LoadedFiles;
 extern vector<partListEntry> g_PartList;
 
--- a/gui.cpp	Fri Apr 26 03:44:34 2013 +0300
+++ b/gui.cpp	Fri Apr 26 18:11:34 2013 +0300
@@ -84,6 +84,7 @@
 EXTERN_ACTION (gridMedium)
 EXTERN_ACTION (gridFine)
 EXTERN_ACTION (resetView)
+EXTERN_ACTION (insertFrom)
 
 #ifndef RELEASE
 EXTERN_ACTION (addTestQuad)
@@ -183,15 +184,17 @@
 	
 	// File menu
 	qFileMenu = menuBar ()->addMenu (tr ("&File"));
-	qFileMenu->addAction (ACTION (newFile));			// New
+	qFileMenu->addAction (ACTION (newFile));				// New
 	qFileMenu->addAction (ACTION (open));				// Open
-	qFileMenu->addMenu (qRecentFilesMenu);					// Open Recent
-	qFileMenu->addAction (ACTION (save));				// Save
-	qFileMenu->addAction (ACTION (saveAs));			// Save As
-	qFileMenu->addSeparator ();								// -------
-	qFileMenu->addAction (ACTION (settings));			// Settings
-	qFileMenu->addSeparator ();								// -------
-	qFileMenu->addAction (ACTION (exit));				// Exit
+	qFileMenu->addMenu (qRecentFilesMenu);				// Open Recent
+	qFileMenu->addAction (ACTION (save));					// Save
+	qFileMenu->addAction (ACTION (saveAs));				// Save As
+	qFileMenu->addSeparator ();							// -------
+	qFileMenu->addAction (ACTION (insertFrom));			// Insert from File
+	qFileMenu->addSeparator ();							// -------
+	qFileMenu->addAction (ACTION (settings));				// Settings
+	qFileMenu->addSeparator ();							// -------
+	qFileMenu->addAction (ACTION (exit));					// Exit
 	
 	// View menu
 	qViewMenu = menuBar ()->addMenu (tr ("&View"));
@@ -205,72 +208,72 @@
 	qInsertMenu->addAction (ACTION (newQuad));			// New Quad
 	qInsertMenu->addAction (ACTION (newCondLine));		// New Conditional Line
 	qInsertMenu->addAction (ACTION (newComment));		// New Comment
-	qInsertMenu->addAction (ACTION (newVertex));		// New Vertex
-	qInsertMenu->addAction (ACTION (newRadial));		// New Radial
+	qInsertMenu->addAction (ACTION (newVertex));			// New Vertex
+	qInsertMenu->addAction (ACTION (newRadial));			// New Radial
 	
 	// Edit menu
 	qEditMenu = menuBar ()->addMenu (tr ("&Edit"));
-	qEditMenu->addAction (ACTION (undo));				// Undo
+	qEditMenu->addAction (ACTION (undo));					// Undo
 	qEditMenu->addAction (ACTION (redo));				// Redo
-	qEditMenu->addSeparator ();								// -----
-	qEditMenu->addAction (ACTION (cut));				// Cut
-	qEditMenu->addAction (ACTION (copy));				// Copy
+	qEditMenu->addSeparator ();							// -----
+	qEditMenu->addAction (ACTION (cut));					// Cut
+	qEditMenu->addAction (ACTION (copy));					// Copy
 	qEditMenu->addAction (ACTION (paste));				// Paste
-	qEditMenu->addAction (ACTION (del));				// Delete
-	qEditMenu->addSeparator ();								// -----
+	qEditMenu->addAction (ACTION (del));					// Delete
+	qEditMenu->addSeparator ();							// -----
 	qEditMenu->addAction (ACTION (selectByColor));		// Select by Color
 	qEditMenu->addAction (ACTION (selectByType));		// Select by Type
-	qEditMenu->addSeparator ();								// -----
+	qEditMenu->addSeparator ();							// -----
 	qEditMenu->addAction (ACTION (setColor));			// Set Color
-	qEditMenu->addAction (ACTION (invert));			// Invert
-	qEditMenu->addAction (ACTION (inlineContents));	// Inline
-	qEditMenu->addAction (ACTION (deepInline));		// Deep Inline
-	qEditMenu->addAction (ACTION (splitQuads));		// Split Quads
-	qEditMenu->addAction (ACTION (setContents));		// Set Contents
-	qEditMenu->addAction (ACTION (makeBorders));		// Make Borders
-	qEditMenu->addAction (ACTION (makeCornerVerts));	// Make Corner Vertices
-	qEditMenu->addAction (ACTION (roundCoords));		// Round Coordinates
+	qEditMenu->addAction (ACTION (invert));				// Invert
+	qEditMenu->addAction (ACTION (inlineContents));		// Inline
+	qEditMenu->addAction (ACTION (deepInline));			// Deep Inline
+	qEditMenu->addAction (ACTION (splitQuads));			// Split Quads
+	qEditMenu->addAction (ACTION (setContents));			// Set Contents
+	qEditMenu->addAction (ACTION (makeBorders));			// Make Borders
+	qEditMenu->addAction (ACTION (makeCornerVerts));		// Make Corner Vertices
+	qEditMenu->addAction (ACTION (roundCoords));			// Round Coordinates
 	
 	// Move menu
 	qMoveMenu = menuBar ()->addMenu (tr ("&Move"));
-	qMoveMenu->addAction (ACTION (moveUp));			// Move Up
+	qMoveMenu->addAction (ACTION (moveUp));				// Move Up
 	qMoveMenu->addAction (ACTION (moveDown));			// Move Down
-	qMoveMenu->addSeparator ();								// -----
-	qMoveMenu->addAction (ACTION (gridCoarse));		// Coarse Grid
-	qMoveMenu->addAction (ACTION (gridMedium));		// Medium Grid
+	qMoveMenu->addSeparator ();							// -----
+	qMoveMenu->addAction (ACTION (gridCoarse));			// Coarse Grid
+	qMoveMenu->addAction (ACTION (gridMedium));			// Medium Grid
 	qMoveMenu->addAction (ACTION (gridFine));			// Fine Grid
-	qMoveMenu->addSeparator ();								// -----
+	qMoveMenu->addSeparator ();							// -----
 	qMoveMenu->addAction (ACTION (moveXPos));			// Move +X
 	qMoveMenu->addAction (ACTION (moveXNeg));			// Move -X
 	qMoveMenu->addAction (ACTION (moveYPos));			// Move +Y
 	qMoveMenu->addAction (ACTION (moveYNeg));			// Move -Y
 	qMoveMenu->addAction (ACTION (moveZPos));			// Move +Z
 	qMoveMenu->addAction (ACTION (moveZNeg));			// Move -Z
-	qMoveMenu->addSeparator ();								// -----
-	qMoveMenu->addAction (ACTION (rotateXPos));		// Rotate +X
-	qMoveMenu->addAction (ACTION (rotateXNeg));		// Rotate -X
-	qMoveMenu->addAction (ACTION (rotateYPos));		// Rotate +Y
-	qMoveMenu->addAction (ACTION (rotateYNeg));		// Rotate -Y
-	qMoveMenu->addAction (ACTION (rotateZPos));		// Rotate +Z
-	qMoveMenu->addAction (ACTION (rotateZNeg));		// Rotate -Z
+	qMoveMenu->addSeparator ();							// -----
+	qMoveMenu->addAction (ACTION (rotateXPos));			// Rotate +X
+	qMoveMenu->addAction (ACTION (rotateXNeg));			// Rotate -X
+	qMoveMenu->addAction (ACTION (rotateYPos));			// Rotate +Y
+	qMoveMenu->addAction (ACTION (rotateYNeg));			// Rotate -Y
+	qMoveMenu->addAction (ACTION (rotateZPos));			// Rotate +Z
+	qMoveMenu->addAction (ACTION (rotateZNeg));			// Rotate -Z
 	
 	// Control menu
 	qControlMenu = menuBar ()->addMenu (tr ("&Control"));
-	qControlMenu->addAction (ACTION (showHistory));	// Show History
+	qControlMenu->addAction (ACTION (showHistory));		// Show History
 	
 #ifndef RELEASE
 	// Debug menu
 	qDebugMenu = menuBar ()->addMenu (tr ("&Debug"));
 	qDebugMenu->addAction (ACTION (addTestQuad));		// Add Test Quad
-	qDebugMenu->addAction (ACTION (addTestRadial));	// Add Test Radial
+	qDebugMenu->addAction (ACTION (addTestRadial));		// Add Test Radial
 #endif // RELEASE
 	
 	// Help menu
 	qHelpMenu = menuBar ()->addMenu (tr ("&Help"));
 	qHelpMenu->addAction (ACTION (help));				// Help
-	qHelpMenu->addSeparator ();								// -----
+	qHelpMenu->addSeparator ();							// -----
 	qHelpMenu->addAction (ACTION (about));				// About
-	qHelpMenu->addAction (ACTION (aboutQt));			// About Qt
+	qHelpMenu->addAction (ACTION (aboutQt));				// About Qt
 }
 
 // =============================================================================
@@ -906,4 +909,10 @@
 bool confirm (str title, str msg) {
 	return QMessageBox::question (g_ForgeWindow, title, msg,
 		(QMessageBox::Yes | QMessageBox::No), QMessageBox::No) == QMessageBox::Yes;
+}
+
+// =============================================================================
+void critical (str msg) {
+	QMessageBox::critical (g_ForgeWindow, APPNAME_DISPLAY ": Critical Error", msg,
+		(QMessageBox::Close), QMessageBox::Close);
 }
\ No newline at end of file
--- a/gui.h	Fri Apr 26 03:44:34 2013 +0300
+++ b/gui.h	Fri Apr 26 18:11:34 2013 +0300
@@ -167,6 +167,7 @@
 std::vector<quickColorMetaEntry> parseQuickColorMeta ();
 bool confirm (str title, str msg);
 bool confirm (str msg);
+void critical (str msg);
 
 // -----------------------------------------------------------------------------
 // Pointer to the instance of ForgeWindow.
--- a/gui_actions.cpp	Fri Apr 26 03:44:34 2013 +0300
+++ b/gui_actions.cpp	Fri Apr 26 18:11:34 2013 +0300
@@ -119,35 +119,35 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 MAKE_ACTION (newSubfile, "New Subfile", "add-subfile", "Creates a new subfile reference.", 0) {
-	AddObjectDialog::staticDialog (OBJ_Subfile, g_ForgeWindow);
+	AddObjectDialog::staticDialog (OBJ_Subfile, null);
 }
 
 MAKE_ACTION (newLine, "New Line",  "add-line", "Creates a new line.", 0) {
-	AddObjectDialog::staticDialog (OBJ_Line, g_ForgeWindow);
+	AddObjectDialog::staticDialog (OBJ_Line, null);
 }
 
 MAKE_ACTION (newTriangle, "New Triangle", "add-triangle", "Creates a new triangle.", 0) {
-	AddObjectDialog::staticDialog (OBJ_Triangle, g_ForgeWindow);
+	AddObjectDialog::staticDialog (OBJ_Triangle, null);
 }
 
 MAKE_ACTION (newQuad, "New Quadrilateral", "add-quad", "Creates a new quadrilateral.", 0) {
-	AddObjectDialog::staticDialog (OBJ_Quad, g_ForgeWindow);
+	AddObjectDialog::staticDialog (OBJ_Quad, null);
 }
 
 MAKE_ACTION (newCondLine, "New Conditional Line", "add-condline", "Creates a new conditional line.", 0) {
-	AddObjectDialog::staticDialog (OBJ_CondLine, g_ForgeWindow);
+	AddObjectDialog::staticDialog (OBJ_CondLine, null);
 }
 
 MAKE_ACTION (newComment, "New Comment", "add-comment", "Creates a new comment.", 0) {
-	AddObjectDialog::staticDialog (OBJ_Comment, g_ForgeWindow);
+	AddObjectDialog::staticDialog (OBJ_Comment, null);
 }
 
 MAKE_ACTION (newVertex, "New Vertex", "add-vertex", "Creates a new vertex.", 0) {
-	AddObjectDialog::staticDialog (OBJ_Vertex, g_ForgeWindow);
+	AddObjectDialog::staticDialog (OBJ_Vertex, null);
 }
 
 MAKE_ACTION (newRadial, "New Radial", "add-radial", "Creates a new radial.", 0) {
-	AddObjectDialog::staticDialog (OBJ_Radial, g_ForgeWindow);
+	AddObjectDialog::staticDialog (OBJ_Radial, null);
 }
 
 MAKE_ACTION (help, "Help", "help", "Shows the " APPNAME_DISPLAY " help manual.", KEY (F1)) {
@@ -249,6 +249,34 @@
 }
 
 // =============================================================================
+MAKE_ACTION (insertFrom, "Insert from File", "insert-from", "Insert LDraw data from a file.", (0)) {
+	str fname = QFileDialog::getOpenFileName ();
+	
+	if (!~fname)
+		return;
+	
+	FILE* fp = fopen (fname, "r");
+	if (!fp) {
+		critical (format ("Couldn't open %s\n%s", fname.chars(), strerror (errno)));
+		return;
+	}
+	
+	std::vector<LDObject*> historyCopies;
+	std::vector<ulong> historyIndices;
+	ulong idx;
+	
+	loadFileContents (fp, g_CurrentFile, &historyCopies, &idx);
+	
+	for (LDObject* obj : historyCopies)
+		historyIndices.push_back (idx++);
+	
+	if (historyCopies.size() > 0) {
+		History::addEntry (new AddHistory (historyIndices, historyCopies));
+		g_ForgeWindow->refresh ();
+	}
+}
+
+// =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 // Debug things
--- a/ldtypes.cpp	Fri Apr 26 03:44:34 2013 +0300
+++ b/ldtypes.cpp	Fri Apr 26 18:11:34 2013 +0300
@@ -411,36 +411,37 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 str LDObject::objectListContents (const std::vector<LDObject*>& objs) {
-	bool bFirstDetails = true;
-	str zText = "";
+	bool firstDetails = true;
+	str text = "";
 	
 	if (objs.size() == 0)
 		return "nothing"; // :)
 	
 	for (long i = 0; i < NUM_ObjectTypes; ++i) {
-		LDObjectType_e eType = (LDObjectType_e) i;
-		ulong ulCount = 0;
+		LDObjectType_e objType = (LDObjectType_e) i;
+		ulong objCount = 0;
 		
 		for (LDObject* obj : objs)
-			if (obj->getType() == eType)
-				ulCount++;
+			if (obj->getType() == objType)
+				objCount++;
+		
+		if (objCount == 0)
+			continue;
+		
+		if (!firstDetails)
+			text += ", ";
 		
-		if (ulCount > 0) {
-			if (!bFirstDetails)
-				zText += ", ";
-			
-			str zNoun = format ("%s%s", g_saObjTypeNames[eType], PLURAL (ulCount));
-			
-			// Plural of "vertex" is "vertices". Stupid English.
-			if (eType == OBJ_Vertex && ulCount != 1)
-				zNoun = "vertices";
-			
-			zText.appendformat ("%lu %s", ulCount, zNoun.chars ());
-			bFirstDetails = false;
-		}
+		str noun = format ("%s%s", g_saObjTypeNames[objType], PLURAL (objCount));
+		
+		// Plural of "vertex" is "vertices". Stupid English.
+		if (objType == OBJ_Vertex && objCount != 1)
+			noun = "vertices";
+		
+		text.appendformat ("%lu %s", objCount, noun.chars ());
+		firstDetails = false;
 	}
 	
-	return zText;
+	return text;
 }
 
 // =============================================================================
--- a/types.cpp	Fri Apr 26 03:44:34 2013 +0300
+++ b/types.cpp	Fri Apr 26 18:11:34 2013 +0300
@@ -17,10 +17,10 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-str vertex::stringRep (const bool bMangled) {
-	const char* sFormat = (bMangled) ? "(%s, %s, %s)" : "%s %s %s";
+str vertex::stringRep (const bool mangled) {
+	const char* fmt = mangled ? "(%s, %s, %s)" : "%s %s %s";
 	
-	return format (sFormat,
+	return format (fmt,
 		ftoa (x).chars(),
 		ftoa (y).chars(),
 		ftoa (z).chars());
@@ -29,11 +29,11 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-void vertex::transform (matrix mMatrix, vertex pos) {
+void vertex::transform (matrix transmatrx, vertex pos) {
 	double x2, y2, z2;
-	x2 = (mMatrix[0] * x) + (mMatrix[1] * y) + (mMatrix[2] * z) + pos.x;
-	y2 = (mMatrix[3] * x) + (mMatrix[4] * y) + (mMatrix[5] * z) + pos.y;
-	z2 = (mMatrix[6] * x) + (mMatrix[7] * y) + (mMatrix[8] * z) + pos.z;
+	x2 = (transmatrx[0] * x) + (transmatrx[1] * y) + (transmatrx[2] * z) + pos.x;
+	y2 = (transmatrx[3] * x) + (transmatrx[4] * y) + (transmatrx[5] * z) + pos.y;
+	z2 = (transmatrx[6] * x) + (transmatrx[7] * y) + (transmatrx[8] * z) + pos.z;
 	
 	x = x2;
 	y = y2;
--- a/types.h	Fri Apr 26 03:44:34 2013 +0300
+++ b/types.h	Fri Apr 26 18:11:34 2013 +0300
@@ -88,9 +88,14 @@
 	}
 	
 	// =========================================================================
+	double coord (const ushort n) const {
+		return *(&x + n);
+	}
+	
+	// =========================================================================
 	// Midpoint between this vertex and another vertex.
 	vertex midpoint (vertex& other);
-	str stringRep (const bool bMangled);
+	str stringRep (const bool mangled);
 	void transform (matrix mMatrix, vertex pos);
 };
 
--- a/zz_addObjectDialog.cpp	Fri Apr 26 03:44:34 2013 +0300
+++ b/zz_addObjectDialog.cpp	Fri Apr 26 18:11:34 2013 +0300
@@ -47,7 +47,7 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-AddObjectDialog::AddObjectDialog (const LDObjectType_e type, QWidget* parent) :
+AddObjectDialog::AddObjectDialog (const LDObjectType_e type, LDObject* obj, QWidget* parent) :
 	QDialog (parent)
 {
 	short coordCount = 0;
@@ -55,11 +55,13 @@
 	LDObject* defaults = LDObject::getDefault (type);
 	
 	lb_typeIcon = new QLabel;
-	lb_typeIcon->setPixmap (QPixmap (iconName.chars ()));
+	lb_typeIcon->setPixmap (QPixmap (iconName));
 	
 	switch (type) {
 	case OBJ_Comment:
 		le_comment = new QLineEdit;
+		if (obj)
+			le_comment->setText (static_cast<LDComment*> (obj)->zText);
 		break;
 	
 	case OBJ_Line:
@@ -151,6 +153,15 @@
 		
 		sb_radRingNum = new QSpinBox;
 		sb_radRingNum->setEnabled (false);
+		
+		if (obj) {
+			LDRadial* rad = static_cast<LDRadial*> (obj);
+			
+			bb_radType->setValue (rad->eRadialType);
+			sb_radSegments->setValue (rad->dSegments);
+			cb_radHiRes->setChecked ((rad->dDivisions == 48) ? Qt::Checked : Qt::Unchecked);
+			sb_radRingNum->setValue (rad->dRingNum);
+		}
 		break;
 	
 	default:
@@ -178,6 +189,18 @@
 	layout->addWidget (lb_typeIcon, 0, 0);
 	
 	switch (type) {
+	case OBJ_Line:
+	case OBJ_CondLine:
+	case OBJ_Triangle:
+	case OBJ_Quad:
+		// Apply coordinates
+		if (obj) {
+			for (short i = 0; i < coordCount / 3; ++i)
+			for (short j = 0; j < 3; ++j)
+				dsb_coords[(i * 3) + j]->setValue (obj->vaCoords[i].coord (j));
+		}
+		break;
+	
 	case OBJ_Comment:
 		layout->addWidget (le_comment, 0, 1);
 		break;
@@ -189,11 +212,20 @@
 		layout->addWidget (sb_radSegments, 2, 3);
 		layout->addWidget (lb_radRingNum, 3, 2);
 		layout->addWidget (sb_radRingNum, 3, 3);
+		
+		if (obj)
+			for (short i = 0; i < 3; ++i)
+				dsb_coords[0]->setValue (static_cast<LDRadial*> (obj)->vPosition.coord (i));
 		break;
 	
 	case OBJ_Subfile:
 		layout->addWidget (tw_subfileList, 1, 1);
 		layout->addWidget (le_subfileName, 2, 1);
+		
+		if (obj)
+			for (short i = 0; i < 3; ++i)
+				dsb_coords[0]->setValue (static_cast<LDSubfile*> (obj)->vPosition.coord (i));
+		break;
 	
 	default:
 		break;
@@ -269,71 +301,78 @@
 		le_subfileName->setText (name);
 }
 
+template<class T> T* initObj (LDObject*& obj) {
+	if (obj == null)
+		obj = new T;
+	
+	return static_cast<T*> (obj);
+}
+
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-void AddObjectDialog::staticDialog (const LDObjectType_e type, ForgeWindow* window) {
-	AddObjectDialog dlg (type, window);
-	LDObject* obj = null;
+void AddObjectDialog::staticDialog (const LDObjectType_e type, LDObject* obj) {
+	AddObjectDialog dlg (type, obj);
+	
+	if (obj)
+		assert (obj->getType () == type);
 	
 	if (dlg.exec () == false)
 		return;
 	
 	switch (type) {
 	case OBJ_Comment:
-		obj = new LDComment (dlg.le_comment->text ());
+		{
+			LDComment* comm = initObj<LDComment> (obj);
+			comm->zText = dlg.le_comment->text ();
+		}
 		break;
 	
 	case OBJ_Line:
 		{
-			LDLine* line = new LDLine;
+			LDLine* line = initObj<LDLine> (obj);
 			line->dColor = dlg.dColor;
 			APPLY_COORDS (line, 2)
-			obj = line;
 		}
 		break;
 	
 	case OBJ_Triangle:
 		{
-			LDTriangle* tri = new LDTriangle;
+			LDTriangle* tri = initObj<LDTriangle> (obj);
 			tri->dColor = dlg.dColor;
 			APPLY_COORDS (tri, 3)
-			obj = tri;
 		}
 		break;
 	
 	case OBJ_Quad:
 		{
-			LDQuad* quad = new LDQuad;
+			LDQuad* quad = initObj<LDQuad> (obj);
 			quad->dColor = dlg.dColor;
 			APPLY_COORDS (quad, 4)
-			obj = quad;
 		}
 		break;
 	
 	case OBJ_CondLine:
 		{
-			LDCondLine* line = new LDCondLine;
+			LDCondLine* line = initObj<LDCondLine> (obj);
 			line->dColor = dlg.dColor;
 			APPLY_COORDS (line, 4)
-			obj = line;
 		}
 		break;
 	
 	case OBJ_Vertex:
 		{
-			LDVertex* vert = new LDVertex;
+			LDVertex* vert = initObj<LDVertex> (obj);
 			vert->dColor = dlg.dColor;
 			vert->vPosition.x = dlg.dsb_coords[0]->value ();
 			vert->vPosition.y = dlg.dsb_coords[1]->value ();
 			vert->vPosition.z = dlg.dsb_coords[2]->value ();
-			obj = vert;
 		}
 		break;
 	
 	case OBJ_Radial:
 		{
-			LDRadial* pRad = new LDRadial;
+			LDRadial* pRad = initObj<LDRadial> (obj);
 			pRad->dColor = dlg.dColor;
 			pRad->vPosition.x = dlg.dsb_coords[0]->value ();
 			pRad->vPosition.y = dlg.dsb_coords[1]->value ();
@@ -343,8 +382,6 @@
 			pRad->eRadialType = (LDRadial::Type) dlg.bb_radType->value ();
 			pRad->dRingNum = dlg.sb_radRingNum->value ();
 			pRad->mMatrix = g_mIdentity;
-			
-			obj = pRad;
 		}
 		break;
 	
@@ -354,7 +391,7 @@
 			if (~name == 0)
 				return; // no subfile filename
 			
-			LDSubfile* ref = new LDSubfile;
+			LDSubfile* ref = initObj<LDSubfile> (obj);
 			ref->dColor = dlg.dColor;
 			ref->vPosition.x = dlg.dsb_coords[0]->value ();
 			ref->vPosition.y = dlg.dsb_coords[1]->value ();
@@ -362,8 +399,6 @@
 			ref->zFileName = name;
 			ref->mMatrix = g_mIdentity;
 			ref->pFile = loadSubfile (name);
-			
-			obj = ref;
 		}
 		break;
 	
@@ -373,5 +408,5 @@
 	
 	ulong idx = g_CurrentFile->addObject (obj);
 	History::addEntry (new AddHistory ({idx}, {obj->clone ()}));
-	window->refresh ();
+	g_ForgeWindow->refresh ();
 }
\ No newline at end of file
--- a/zz_addObjectDialog.h	Fri Apr 26 03:44:34 2013 +0300
+++ b/zz_addObjectDialog.h	Fri Apr 26 18:11:34 2013 +0300
@@ -34,8 +34,8 @@
 	Q_OBJECT
 	
 public:
-    AddObjectDialog (const LDObjectType_e type, QWidget* parent = null);
-	static void staticDialog (const LDObjectType_e type, ForgeWindow* window);
+    AddObjectDialog (const LDObjectType_e type, LDObject* obj, QWidget* parent = null);
+	static void staticDialog (const LDObjectType_e type, LDObject* obj);
 	
 	QLabel* lb_typeIcon;
 	

mercurial