corrections to inserting from file

Fri, 26 Apr 2013 18:28:07 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Fri, 26 Apr 2013 18:28:07 +0300
changeset 141
184d117e1b12
parent 140
2e8c1626aef7
child 142
6e8aee653dab

corrections to inserting from file

file.cpp file | annotate | diff | comparison | revisions
file.h file | annotate | diff | comparison | revisions
gui_actions.cpp file | annotate | diff | comparison | revisions
gui_editactions.cpp file | annotate | diff | comparison | revisions
zz_configDialog.cpp file | annotate | diff | comparison | revisions
zz_configDialog.h file | annotate | diff | comparison | revisions
--- a/file.cpp	Fri Apr 26 18:11:34 2013 +0300
+++ b/file.cpp	Fri Apr 26 18:28:07 2013 +0300
@@ -109,45 +109,40 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-ulong loadFileContents (FILE* fp, OpenFile* load, std::vector<LDObject*>* copies, ulong* idx) {
+std::vector<LDObject*> loadFileContents (FILE* fp, ulong* numWarnings) {
 	char line[1024];
 	vector<str> lines;
-	ulong numWarnings = 0;
+	vector<LDObject*> objs;
+	ulong lnum = 0;
+	
+	if (numWarnings)
+		*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;
+		str data = line;
+		while (data[~data - 1] == '\n' || data[~data - 1] == '\r')
+			data -= 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);
+		LDObject* obj = parseLine (data);
+		assert (obj != null);
 		
 		// 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++;
+			logf (LOG_Warning, "- Line was: %s\n", data.chars());
+			
+			if (numWarnings)
+				(*numWarnings)++;
 		}
 		
+		objs.push_back (obj);
 		lnum++;
 	}
 	
-	return numWarnings;
+	return objs;
 }
 
 // =============================================================================
@@ -172,7 +167,11 @@
 	
 	OpenFile* load = new OpenFile;
 	load->zFileName = path;
-	ulong numWarnings	= loadFileContents (fp, load);
+	ulong numWarnings;
+	std::vector<LDObject*> objs = loadFileContents (fp, &numWarnings);
+	
+	for (LDObject* obj : objs)
+		load->objects.push_back (obj);
 	
 	fclose (fp);
 	g_LoadedFiles.push_back (load);
--- a/file.h	Fri Apr 26 18:11:34 2013 +0300
+++ b/file.h	Fri Apr 26 18:28:07 2013 +0300
@@ -99,7 +99,7 @@
 // Init and parse parts.lst
 void initPartList ();
 
-ulong loadFileContents (FILE* fp, OpenFile* load, std::vector<LDObject*>* copies = null, ulong* idx = null);
+std::vector< LDObject* > loadFileContents (FILE* fp, ulong* numWarnings);
 
 extern vector<OpenFile*> g_LoadedFiles;
 extern vector<partListEntry> g_PartList;
--- a/gui_actions.cpp	Fri Apr 26 18:11:34 2013 +0300
+++ b/gui_actions.cpp	Fri Apr 26 18:28:07 2013 +0300
@@ -251,6 +251,7 @@
 // =============================================================================
 MAKE_ACTION (insertFrom, "Insert from File", "insert-from", "Insert LDraw data from a file.", (0)) {
 	str fname = QFileDialog::getOpenFileName ();
+	ulong idx = g_ForgeWindow->getInsertionPoint ();
 	
 	if (!~fname)
 		return;
@@ -263,16 +264,23 @@
 	
 	std::vector<LDObject*> historyCopies;
 	std::vector<ulong> historyIndices;
-	ulong idx;
+	std::vector<LDObject*> objs = loadFileContents (fp, null);
+	
+	g_ForgeWindow->sel.clear ();
 	
-	loadFileContents (fp, g_CurrentFile, &historyCopies, &idx);
-	
-	for (LDObject* obj : historyCopies)
-		historyIndices.push_back (idx++);
+	for (LDObject* obj : objs) {
+		historyCopies.push_back (obj->clone ());
+		historyIndices.push_back (idx);
+		g_CurrentFile->objects.insert (g_CurrentFile->objects.begin () + idx, obj);
+		g_ForgeWindow->sel.push_back (obj);
+		
+		idx++;
+	}
 	
 	if (historyCopies.size() > 0) {
 		History::addEntry (new AddHistory (historyIndices, historyCopies));
 		g_ForgeWindow->refresh ();
+		g_ForgeWindow->scrollToSelection ();
 	}
 }
 
--- a/gui_editactions.cpp	Fri Apr 26 18:11:34 2013 +0300
+++ b/gui_editactions.cpp	Fri Apr 26 18:28:07 2013 +0300
@@ -28,6 +28,8 @@
 
 vector<LDObject*> g_Clipboard;
 
+cfg (bool, edit_insertSchemanticsOnly, false);
+
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
--- a/zz_configDialog.cpp	Fri Apr 26 18:11:34 2013 +0300
+++ b/zz_configDialog.cpp	Fri Apr 26 18:28:07 2013 +0300
@@ -39,6 +39,7 @@
 extern_cfg (int, gui_toolbar_iconsize);
 extern_cfg (str, gui_colortoolbar);
 extern_cfg (bool, gl_selflash);
+extern_cfg (bool, edit_insertSchemanticsOnly);
 
 ConfigDialog* g_ConfigDialog = null;
 
@@ -131,6 +132,9 @@
 	cb_selFlash = new QCheckBox ("Selection flash");
 	INIT_CHECKBOX (cb_selFlash, gl_selflash)
 	
+	cb_insertSchemanticsOnly = new QCheckBox ("Only insert schemantic objects from a file");
+	INIT_CHECKBOX (cb_insertSchemanticsOnly, edit_insertSchemanticsOnly)
+	
 	QGridLayout* layout = new QGridLayout;
 	layout->addWidget (lb_LDrawPath, 0, 0);
 	layout->addLayout (qLDrawPathLayout, 0, 1, 1, 3);
@@ -151,6 +155,7 @@
 	layout->addWidget (cb_colorize, 4, 0, 1, 4);
 	layout->addWidget (cb_colorBFC, 5, 0, 1, 4);
 	layout->addWidget (cb_selFlash, 6, 0, 1, 4);
+	layout->addWidget (cb_insertSchemanticsOnly, 7, 0, 1, 4);
 	mainTab->setLayout (layout);
 	
 	// Add the tab to the manager
@@ -603,6 +608,7 @@
 		APPLY_CHECKBOX (dlg.cb_colorize, lv_colorize)
 		APPLY_CHECKBOX (dlg.cb_colorBFC, gl_colorbfc)
 		APPLY_CHECKBOX (dlg.cb_selFlash, gl_selflash)
+		APPLY_CHECKBOX (dlg.cb_insertSchemanticsOnly, edit_insertSchemanticsOnly)
 		
 		gl_maincolor_alpha = ((double)dlg.sl_viewFgAlpha->value ()) / 10.0f;
 		gl_linethickness = dlg.sl_lineThickness->value ();
--- a/zz_configDialog.h	Fri Apr 26 18:11:34 2013 +0300
+++ b/zz_configDialog.h	Fri Apr 26 18:28:07 2013 +0300
@@ -44,7 +44,7 @@
 	QLineEdit* le_LDrawPath;
 	QPushButton* pb_findLDrawPath;
 	QPushButton* pb_viewBg, *pb_viewFg;
-	QCheckBox* cb_colorize, *cb_colorBFC, *cb_selFlash;
+	QCheckBox* cb_colorize, *cb_colorBFC, *cb_selFlash, *cb_insertSchemanticsOnly;
 	QSlider* sl_viewFgAlpha, *sl_lineThickness, *sl_iconSize;
 	
 	// =========================================================================

mercurial