made LDFile interfacing a bit clearer

Sat, 24 Aug 2013 16:52:45 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sat, 24 Aug 2013 16:52:45 +0300
changeset 484
5b5c77c7f3dd
parent 483
138b7c5c3c59
child 485
543a6696096f

made LDFile interfacing a bit clearer

src/download.cpp file | annotate | diff | comparison | revisions
src/extprogs.cpp file | annotate | diff | comparison | revisions
src/file.cpp file | annotate | diff | comparison | revisions
src/file.h file | annotate | diff | comparison | revisions
src/gldraw.cpp file | annotate | diff | comparison | revisions
src/gui.cpp file | annotate | diff | comparison | revisions
src/gui_actions.cpp file | annotate | diff | comparison | revisions
src/gui_editactions.cpp file | annotate | diff | comparison | revisions
src/ldtypes.cpp file | annotate | diff | comparison | revisions
src/primitives.cpp file | annotate | diff | comparison | revisions
src/types.cpp file | annotate | diff | comparison | revisions
--- a/src/download.cpp	Sat Aug 24 16:30:30 2013 +0300
+++ b/src/download.cpp	Sat Aug 24 16:52:45 2013 +0300
@@ -413,7 +413,7 @@
 	// from unknown file references, try resolve that by downloading the reference.
 	// This is why downloading a part may end up downloading multiple files, as
 	// it resolves dependencies.
-	for (LDObject* obj : *f) {
+	for (LDObject* obj : f->objects()) {
 		LDError* err = dynamic_cast<LDError*> (obj);
 		if (!err || err->fileRef().isEmpty())
 			continue;
--- a/src/extprogs.cpp	Sat Aug 24 16:30:30 2013 +0300
+++ b/src/extprogs.cpp	Sat Aug 24 16:52:45 2013 +0300
@@ -188,7 +188,7 @@
 void writeColorGroup (const short colnum, str fname) {
 	List<LDObject*> objects;
 	
-	for (LDObject* obj : *LDFile::current()) {
+	for (LDObject* obj : LDFile::current()->objects()) {
 		if (obj->isColored() == false || obj->color() != colnum)
 			continue;
 		
--- a/src/file.cpp	Sat Aug 24 16:30:30 2013 +0300
+++ b/src/file.cpp	Sat Aug 24 16:52:45 2013 +0300
@@ -113,11 +113,11 @@
 // -----------------------------------------------------------------------------
 LDFile::~LDFile() {
 	// Clear everything from the model
-	for (LDObject* obj : m_objs)
+	for (LDObject* obj : objects())
 		delete obj;
 	
 	// Clear the cache as well
-	for (LDObject* obj : m_cache)
+	for (LDObject* obj : cache())
 		delete obj;
 	
 	// Remove this file from the list of files
@@ -595,7 +595,7 @@
 	
 	// File is open, now save the model to it. Note that LDraw requires files to
 	// have DOS line endings, so we terminate the lines with \r\n.
-	for (LDObject* obj : objs())
+	for (LDObject* obj : objects())
 		f.write (obj->raw() + "\r\n");
 	
 	// File is saved, now clean up.
@@ -827,7 +827,7 @@
 	g_loadedFiles << LDFile::current();
 	
 	// Go through all objects in the current file and reload the subfiles
-	for (LDObject* obj : LDFile::current()->objs()) {
+	for (LDObject* obj : LDFile::current()->objects()) {
 		if (obj->getType() == LDObject::Subfile) {
 			LDSubfile* ref = static_cast<LDSubfile*> (obj);
 			LDFile* fileInfo = getFile (ref->fileInfo()->name());
@@ -851,8 +851,8 @@
 // =============================================================================
 // -----------------------------------------------------------------------------
 ulong LDFile::addObject (LDObject* obj) {
-	m_history.add (new AddHistory (m_objs.size(), obj));
-	m_objs << obj;
+	m_history.add (new AddHistory (objects().size(), obj));
+	m_objects << obj;
 	
 	if (obj->getType() == LDObject::Vertex)
 		m_vertices << obj;
@@ -863,9 +863,17 @@
 
 // =============================================================================
 // -----------------------------------------------------------------------------
+void LDFile::addObjects (const List<LDObject*> objs) {
+	for (LDObject* obj : objs)
+		if (obj)
+			addObject (obj);
+}
+
+// =============================================================================
+// -----------------------------------------------------------------------------
 void LDFile::insertObj (const ulong pos, LDObject* obj) {
 	m_history.add (new AddHistory (pos, obj));
-	m_objs.insert (pos, obj);
+	m_objects.insert (pos, obj);
 	obj->setFile (this);
 }
 
@@ -874,7 +882,7 @@
 void LDFile::forgetObject (LDObject* obj) {
 	ulong idx = obj->getIndex();
 	m_history.add (new DelHistory (idx, obj));
-	m_objs.erase (idx);
+	m_objects.erase (idx);
 	obj->setFile (null);
 }
 
@@ -899,7 +907,7 @@
 	m_history << new EditHistory (idx, oldcode, newcode);
 	
 	obj->setFile (this);
-	m_objs[idx] = obj;
+	m_objects[idx] = obj;
 }
 
 // =============================================================================
@@ -907,7 +915,7 @@
 static List<LDFile*> getFilesUsed (LDFile* node) {
 	List<LDFile*> filesUsed;
 	
-	for (LDObject* obj : *node) {
+	for (LDObject* obj : node->objects()) {
 		if (obj->getType() != LDObject::Subfile)
 			continue;
 		
@@ -955,10 +963,10 @@
 // =============================================================================
 // -----------------------------------------------------------------------------
 LDObject* LDFile::object (ulong pos) const {
-	if (m_objs.size() <= pos)
+	if (m_objects.size() <= pos)
 		return null;
 	
-	return m_objs[pos];
+	return m_objects[pos];
 }
 
 // =============================================================================
@@ -970,16 +978,7 @@
 // =============================================================================
 // -----------------------------------------------------------------------------
 ulong LDFile::numObjs() const {
-	return m_objs.size();
-}
-
-// =============================================================================
-// -----------------------------------------------------------------------------
-LDFile& LDFile::operator<< (List<LDObject*> objs) {
-	for (LDObject* obj : objs)
-		addObject (obj);
-	
-	return *this;
+	return objects().size();
 }
 
 // =============================================================================
@@ -1023,7 +1022,7 @@
 		if (!deep)
 			doCache = false;
 		
-		for (LDObject* obj : m_objs) {
+		for (LDObject* obj : objects()) {
 			// Skip those without scemantic meaning
 			if (!obj->isScemantic())
 				continue;
--- a/src/file.h	Sat Aug 24 16:30:30 2013 +0300
+++ b/src/file.h	Sat Aug 24 16:52:45 2013 +0300
@@ -50,7 +50,7 @@
 // =============================================================================
 class LDFile : public QObject {
 	Q_OBJECT
-	READ_PROPERTY (List<LDObject*>, objs, setObjects)
+	READ_PROPERTY (List<LDObject*>, objects, setObjects)
 	READ_PROPERTY (History, history, setHistory)
 	READ_PROPERTY (List<LDObject*>, vertices, setVertices)
 	PROPERTY (str, name, setName)
@@ -67,8 +67,11 @@
 	~LDFile();
 	
 	ulong addObject (LDObject* obj);                 // Adds an object to this file at the end of the file.
+	void addObjects (const List<LDObject*> objs);
 	void forgetObject (LDObject* obj);               // Deletes the given object from the object chain.
+	str getShortName();
 	bool hasUnsavedChanges() const;                  // Does this file have unsaved changes?
+	List<LDObject*> inlineContents (LDSubfile::InlineFlags flags);
 	void insertObj (const ulong pos, LDObject* obj);
 	ulong numObjs() const;
 	LDObject* object (ulong pos) const;
@@ -77,60 +80,19 @@
 	bool safeToClose();                              // Perform safety checks. Do this before closing any files!
 	void setObject (ulong idx, LDObject* obj);
 
-	LDFile& operator<< (LDObject* obj) {
-		addObject (obj);
-		return *this;
-	}
-	
-	LDFile& operator<< (List<LDObject*> objs);
-	
-	it begin() {
-		return PROP_NAME (objs).begin();
-	}
-	
-	c_it begin() const {
-		return PROP_NAME (objs).begin();
-	}
-	
-	it end() {
-		return PROP_NAME (objs).end();
-	}
-	
-	c_it end() const {
-		return PROP_NAME (objs).end();
-	}
-	
-	void openHistory() {
-		m_history.open();
-	}
-	
-	void closeHistory() {
-		m_history.close();
-	}
-	
-	void undo() {
-		m_history.undo();
-	}
-	
-	void redo() {
-		m_history.redo();
-	}
-	
-	void clearHistory() {
-		m_history.clear();
-	}
-	
-	void addToHistory (AbstractHistoryEntry* entry) {
-		m_history << entry;
-	}
+	inline LDFile& operator<< (LDObject* obj) { addObject (obj); return *this; }
+	inline void openHistory() { m_history.open(); }
+	inline void closeHistory() { m_history.close(); }
+	inline void undo() { m_history.undo(); }
+	inline void redo() { m_history.redo(); }
+	inline void clearHistory() { m_history.clear(); }
+	inline void addToHistory (AbstractHistoryEntry* entry) { m_history << entry; }
 	
 	static void closeUnused();
 	static LDFile* current();
 	static void setCurrent (LDFile* f);
 	static void closeInitialFile();
 	static int countExplicitFiles();
-	str getShortName();
-	List<LDObject*> inlineContents (LDSubfile::InlineFlags flags);
 	
 private:
 	static LDFile* m_curfile;
--- a/src/gldraw.cpp	Sat Aug 24 16:30:30 2013 +0300
+++ b/src/gldraw.cpp	Sat Aug 24 16:52:45 2013 +0300
@@ -394,7 +394,7 @@
 	if (gl_colorbfc && !m_picking && !drawOnly()) {
 		glEnable (GL_CULL_FACE);
 		
-		for (LDObject* obj : file()->objs()) {
+		for (LDObject* obj : file()->objects()) {
 			if (obj->hidden())
 				continue;
 			
@@ -407,7 +407,7 @@
 		
 		glDisable (GL_CULL_FACE);
 	} else {
-		for (LDObject* obj : file()->objs()) {
+		for (LDObject* obj : file()->objects()) {
 			if (obj->hidden())
 				continue;
 			
@@ -683,7 +683,7 @@
 	
 	m_knownVerts.clear();
 	
-	for (LDObject* obj : file()->objs())
+	for (LDObject* obj : file()->objects())
 		compileObject (obj);
 	
 	// Compile axes
@@ -1609,7 +1609,7 @@
 LDOverlay* GLRenderer::findOverlayObject (GLRenderer::Camera cam) {
 	LDOverlay* ovlobj = null;
 	
-	for (LDObject * obj : *file()) {
+	for (LDObject * obj : file()->objects()) {
 		if (obj->getType() == LDObject::Overlay && static_cast<LDOverlay*> (obj)->camera() == cam) {
 			ovlobj = static_cast<LDOverlay*> (obj);
 			break;
--- a/src/gui.cpp	Sat Aug 24 16:30:30 2013 +0300
+++ b/src/gui.cpp	Sat Aug 24 16:52:45 2013 +0300
@@ -285,7 +285,7 @@
 	
 	ui->objectList->clear();
 	
-	for (LDObject* obj : LDFile::current()->objs()) {
+	for (LDObject* obj : LDFile::current()->objects()) {
 		str descr;
 		
 		switch (obj->getType()) {
@@ -410,7 +410,7 @@
 	m_sel.clear();
 	const QList<QListWidgetItem*> items = ui->objectList->selectedItems();
 	
-	for (LDObject* obj : LDFile::current()->objs())
+	for (LDObject* obj : LDFile::current()->objects())
 	for (QListWidgetItem* item : items) {
 		if (item == obj->qObjListEntry) {
 			m_sel << obj;
@@ -501,7 +501,7 @@
 void ForgeWindow::updateSelection() {
 	g_bSelectionLocked = true;
 	
-	for (LDObject* obj : LDFile::current()->objs())
+	for (LDObject* obj : LDFile::current()->objects())
 		obj->setSelected (false);
 	
 	ui->objectList->clearSelection();
@@ -628,7 +628,7 @@
 // -----------------------------------------------------------------------------
 void ForgeWindow::deleteByColor (const short colnum) {
 	List<LDObject*> objs;
-	for (LDObject* obj : LDFile::current()->objs()) {
+	for (LDObject* obj : LDFile::current()->objects()) {
 		if (!obj->isColored() || obj->color() != colnum)
 			continue;
 		
@@ -650,7 +650,7 @@
 // -----------------------------------------------------------------------------
 void ForgeWindow::slot_editObject (QListWidgetItem* listitem) {
 	LDObject* obj = null;
-	for (LDObject* it : *LDFile::current()) {
+	for (LDObject* it : LDFile::current()->objects()) {
 		if (it->qObjListEntry == listitem) {
 			obj = it;
 			break;
@@ -785,7 +785,7 @@
 void makeColorSelector (QComboBox* box) {
 	std::map<short, ulong> counts;
 	
-	for (LDObject* obj : LDFile::current()->objs()) {
+	for (LDObject* obj : LDFile::current()->objects()) {
 		if (!obj->isColored())
 			continue;
 		
--- a/src/gui_actions.cpp	Sat Aug 24 16:30:30 2013 +0300
+++ b/src/gui_actions.cpp	Sat Aug 24 16:52:45 2013 +0300
@@ -88,18 +88,17 @@
 		ui.rb_license_ca->isChecked()    ? CALicense :
 		ui.rb_license_nonca->isChecked() ? NonCALicense : "";
 	
-	LDFile* f = LDFile::current();
-	*f << new LDComment (ui.le_title->text());
-	*f << new LDComment ("Name: <untitled>.dat" );
-	*f << new LDComment (fmt ("Author: %1", ui.le_author->text()));
-	*f << new LDComment (fmt ("!LDRAW_ORG Unofficial_Part"));
-	
-	if (license != "")
-		*f << new LDComment (license);
-	
-	*f << new LDEmpty;
-	*f << new LDBFC (BFCType);
-	*f << new LDEmpty;
+	LDFile::current()->addObjects ({new LDComment (ui.le_title->text()),
+		new LDComment ("Name: <untitled>.dat" ),
+		new LDComment (fmt ("Author: %1", ui.le_author->text())),
+		new LDComment (fmt ("!LDRAW_ORG Unofficial_Part")),
+		(license != "" ?
+			new LDComment (license) :
+			null),
+		new LDEmpty,
+		new LDBFC (BFCType),
+		new LDEmpty,
+	});
 	
 	g_win->fullRefresh();
 }
@@ -267,7 +266,7 @@
 DEFINE_ACTION (SelectAll, CTRL (A)) {
 	g_win->sel().clear();
 	
-	for (LDObject* obj : LDFile::current()->objs())
+	for (LDObject* obj : LDFile::current()->objects())
 		g_win->sel() << obj;
 	
 	g_win->updateSelection();
@@ -282,7 +281,7 @@
 		return; // no consensus on color
 	
 	g_win->sel().clear();
-	for (LDObject* obj : LDFile::current()->objs())
+	for (LDObject* obj : LDFile::current()->objects())
 		if (obj->color() == colnum)
 			g_win->sel() << obj;
 	
@@ -313,7 +312,7 @@
 	}
 	
 	g_win->sel().clear();
-	for (LDObject* obj : LDFile::current()->objs()) {
+	for (LDObject* obj : LDFile::current()->objects()) {
 		if (obj->getType() != type)
 			continue;
 		
--- a/src/gui_editactions.cpp	Sat Aug 24 16:30:30 2013 +0300
+++ b/src/gui_editactions.cpp	Sat Aug 24 16:52:45 2013 +0300
@@ -632,7 +632,7 @@
 // =============================================================================
 // -----------------------------------------------------------------------------
 static bool isColorUsed (short colnum) {
-	for (LDObject* obj : LDFile::current()->objs())
+	for (LDObject* obj : LDFile::current()->objects())
 		if (obj->isColored() && obj->color() == colnum)
 			return true;
 	
@@ -648,8 +648,7 @@
 		colnum++;
 	
 	if (colnum >= MAX_COLORS) {
-		//: Auto-colorer error message
-		critical (ForgeWindow::tr ("Out of unused colors! What are you doing?!"));
+		log (ForgeWindow::tr ("Cannot auto-color: all colors are in use!"));
 		return;
 	}
 	
--- a/src/ldtypes.cpp	Sat Aug 24 16:30:30 2013 +0300
+++ b/src/ldtypes.cpp	Sat Aug 24 16:52:45 2013 +0300
@@ -229,11 +229,14 @@
 // =============================================================================
 // -----------------------------------------------------------------------------
 void LDObject::swap (LDObject* other) {
-	for (LDObject*& obj : *file()) {
+	int i = 0;
+	for (LDObject* obj : file()->objects()) {
 		if (obj == this)
-			obj = other;
+			file()->setObject (i, other);
 		elif (obj == other)
-			obj = this;
+			file()->setObject (i, this);
+		
+		++i;
 	}
 
 	file()->addToHistory (new SwapHistory (id(), other->id()));
@@ -348,7 +351,7 @@
 		const long idx = obj->getIndex(),
 			target = idx + (up ? -1 : 1);
 		
-		if ((up && idx == 0) || (!up && idx == (long) (file->objs().size() - 1))) {
+		if ((up && idx == 0) || (!up && idx == (long) (file->objects().size() - 1))) {
 			// One of the objects hit the extrema. If this happens, this should be the first
 			// object to be iterated on. Thus, nothing has changed yet and it's safe to just
 			// abort the entire operation.
--- a/src/primitives.cpp	Sat Aug 24 16:30:30 2013 +0300
+++ b/src/primitives.cpp	Sat Aug 24 16:52:45 2013 +0300
@@ -124,7 +124,7 @@
 			info.title.remove (0, 1);  // remove 0
 			info.title = info.title.simplified();
 		}
-
+		
 		m_prims << info;
 		emit update (++i);
 	}
@@ -186,7 +186,7 @@
 		cat.setName (g_Other);
 		unmatched = & (g_PrimitiveCategories << cat);
 	}
-
+	
 	for (Primitive& prim : g_primitives) {
 		bool matched = false;
 		prim.cat = null;
@@ -524,15 +524,18 @@
 	LDFile* f = new LDFile;
 	f->setName (QFileDialog::getSaveFileName (null, QObject::tr ("Save Primitive"), name));
 	
-	*f << new LDComment (descr);
-	*f << new LDComment (fmt ("Name: %1", name));
-	*f << new LDComment (fmt ("Author: LDForge"));
-	*f << new LDComment (fmt ("!LDRAW_ORG Unofficial_%1Primitive", divs == hires ? "48_" : ""));
-	*f << new LDComment (CALicense);
-	*f << new LDEmpty;
-	*f << new LDBFC (LDBFC::CertifyCCW);
-	*f << new LDEmpty;
-	*f << makePrimitive (type, segs, divs, num);
+	f->addObjects ({
+		new LDComment (descr),
+		new LDComment (fmt ("Name: %1", name)),
+		new LDComment (fmt ("Author: LDForge")),
+		new LDComment (fmt ("!LDRAW_ORG Unofficial_%1Primitive", divs == hires ? "48_" : "")),
+		new LDComment (CALicense),
+		new LDEmpty,
+		new LDBFC (LDBFC::CertifyCCW),
+		new LDEmpty,
+	});
+	
+	f->addObjects (makePrimitive (type, segs, divs, num));
 	
 	g_win->save (f, false);
 	delete f;
--- a/src/types.cpp	Sat Aug 24 16:30:30 2013 +0300
+++ b/src/types.cpp	Sat Aug 24 16:52:45 2013 +0300
@@ -504,7 +504,7 @@
 	if (!LDFile::current())
 		return;
 	
-	for (LDObject* obj : LDFile::current()->objs())
+	for (LDObject* obj : LDFile::current()->objects())
 		calcObject (obj);
 }
 

mercurial