Refactor LDDocument's API

Sat, 05 Sep 2015 23:55:06 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 05 Sep 2015 23:55:06 +0300
changeset 979
880d3fe9ac7c
parent 978
4603d8fd063e
child 980
4a95c6b06ebe

Refactor LDDocument's API

src/glCompiler.cpp file | annotate | diff | comparison | revisions
src/glRenderer.h file | annotate | diff | comparison | revisions
src/ldDocument.cpp file | annotate | diff | comparison | revisions
src/ldDocument.h file | annotate | diff | comparison | revisions
src/ldObject.cpp file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
src/primitives.cpp file | annotate | diff | comparison | revisions
src/toolsets/algorithmtoolset.cpp file | annotate | diff | comparison | revisions
src/toolsets/filetoolset.cpp file | annotate | diff | comparison | revisions
--- a/src/glCompiler.cpp	Sat Sep 05 23:03:24 2015 +0300
+++ b/src/glCompiler.cpp	Sat Sep 05 23:55:06 2015 +0300
@@ -316,7 +316,7 @@
 {
 //	print ("Compile %1\n", g_objectOrigins[obj]);
 
-	if (obj == null or obj->document() == null or obj->document()->isImplicit())
+	if (obj == null or obj->document() == null or obj->document()->isCache())
 		return;
 
 	ObjectVBOInfo info;
--- a/src/glRenderer.h	Sat Sep 05 23:03:24 2015 +0300
+++ b/src/glRenderer.h	Sat Sep 05 23:55:06 2015 +0300
@@ -271,7 +271,7 @@
 
 inline LDGLData& GLRenderer::currentDocumentData() const
 {
-	return *document()->getGLData();
+	return *document()->glData();
 }
 
 inline double& GLRenderer::rot (Axis ax)
--- a/src/ldDocument.cpp	Sat Sep 05 23:03:24 2015 +0300
+++ b/src/ldDocument.cpp	Sat Sep 05 23:55:06 2015 +0300
@@ -41,19 +41,16 @@
 static LDDocument* g_logoedStud;
 static LDDocument* g_logoedStud2;
 static bool g_loadingLogoedStuds = false;
-
 const QStringList g_specialSubdirectories ({ "s", "48", "8" });
 
-// =============================================================================
-//
 LDDocument::LDDocument (QObject* parent) :
 	QObject (parent),
 	HierarchyElement (parent),
 	m_history (new History),
-	m_isImplicit (true),
-	m_flags (0),
+	m_isCache (true),
 	m_verticesOutdated (true),
 	m_needVertexMerge (true),
+	m_beingDestroyed (false),
 	m_gldata (new LDGLData)
 {
 	setSavePosition (-1);
@@ -62,44 +59,141 @@
 	m_needsReCache = true;
 }
 
-// =============================================================================
-//
 LDDocument::~LDDocument()
 {
-	m_flags |= DOCF_IsBeingDestroyed;
+	m_beingDestroyed = true;
 	delete m_history;
 	delete m_gldata;
 }
 
-// =============================================================================
-//
-void LDDocument::setImplicit (bool const& a)
+QString LDDocument::name() const
+{
+	return m_name;
+}
+
+void LDDocument::setName (QString value)
+{
+	m_name = value;
+}
+
+const LDObjectList& LDDocument::objects() const
+{
+	return m_objects;
+}
+
+History* LDDocument::history() const
+{
+	return m_history;
+}
+
+QString LDDocument::fullPath()
+{
+	return m_fullPath;
+}
+
+void LDDocument::setFullPath (QString value)
 {
-	if (m_isImplicit != a)
-	{
-		m_isImplicit = a;
+	m_fullPath = value;
+}
+
+int LDDocument::tabIndex() const
+{
+	return m_tabIndex;
+}
+
+void LDDocument::setTabIndex (int value)
+{
+	m_tabIndex = value;
+}
+
+const QList<LDPolygon>& LDDocument::polygonData() const
+{
+	return m_polygonData;
+}
+
+long LDDocument::savePosition() const
+{
+	return m_savePosition;
+}
+
+void LDDocument::setSavePosition (long value)
+{
+	m_savePosition = value;
+}
 
-		if (a == false)
-		{
-			print ("Opened %1", name());
+QString LDDocument::defaultName() const
+{
+	return m_defaultName;
+}
+
+void LDDocument::setDefaultName (QString value)
+{
+	m_defaultName = value;
+}
+
+void LDDocument::openForEditing()
+{
+	if (m_isCache)
+	{
+		m_isCache = false;
+		print ("Opened %1", name());
+
+		// Cache files are not compiled by the GL renderer. Now that this file is open for editing, it needs to be
+		// compiled.
+		m_window->R()->compiler()->compileDocument (this);
+		m_window->updateDocumentList();
+	}
+}
+
+bool LDDocument::isCache() const
+{
+	return m_isCache;
+}
 
-			// Implicit files are not compiled by the GL renderer. Now that this
-			// part is no longer implicit, it needs to be compiled.
-			m_window->R()->compiler()->compileDocument (this);
-		}
-		else
-		{
-			print ("Closed %1", name());
-		}
+void LDDocument::addHistoryStep()
+{
+	history()->addStep();
+}
+
+void LDDocument::undo()
+{
+	history()->undo();
+}
+
+void LDDocument::redo()
+{
+	history()->redo();
+}
 
+void LDDocument::clearHistory()
+{
+	history()->clear();
+}
+
+void LDDocument::addToHistory (AbstractHistoryEntry* entry)
+{
+	*history() << entry;
+}
+
+void LDDocument::close()
+{
+	if (not isCache())
+	{
+		m_isCache = true;
+		print ("Closed %1", name());
 		m_window->updateDocumentList();
 
 		// If the current document just became implicit (i.e. user closed it), we need to get a new one to show.
-		if (currentDocument() == this and a)
+		if (currentDocument() == this)
 			m_window->currentDocumentClosed();
 	}
 }
 
+LDGLData* LDDocument::glData()
+{
+	return m_gldata;
+}
+
 // =============================================================================
 //
 LDDocument* FindDocument (QString name)
@@ -431,7 +525,7 @@
 
 	if (not ok)
 	{
-		load->dismiss();
+		load->close();
 		return nullptr;
 	}
 
@@ -450,6 +544,8 @@
 
 // =============================================================================
 //
+// Performs safety checks. Do this before closing any files!
+//
 bool LDDocument::isSafeToClose()
 {
 	using msgbox = QMessageBox;
@@ -509,7 +605,7 @@
 void CloseAllDocuments()
 {
 	for (LDDocument* file : g_win->allDocuments())
-		file->dismiss();
+		file->close();
 }
 
 // =============================================================================
@@ -588,7 +684,7 @@
 		return;
 	}
 
-	file->setImplicit (false);
+	file->openForEditing();
 	g_win->closeInitialDocument();
 	g_win->changeDocument (file);
 	g_win->doFullRefresh();
@@ -630,7 +726,7 @@
 //
 bool LDDocument::save (QString path, int64* sizeptr)
 {
-	if (isImplicit())
+	if (isCache())
 		return false;
 
 	if (not path.length())
@@ -1016,7 +1112,7 @@
 	
 
 #ifdef DEBUG
-	if (not isImplicit())
+	if (not isCache())
 		dprint ("Inserted object #%1 (%2) at %3\n", obj->id(), obj->typeName(), pos);
 #endif
 }
@@ -1046,7 +1142,7 @@
 	{
 		obj->deselect();
 
-		if (not isImplicit() and not (flags() & DOCF_IsBeingDestroyed))
+		if (not isCache() and not m_beingDestroyed)
 		{
 			history()->add (new DelHistory (idx, obj));
 			m_objectVertices.remove (obj);
@@ -1116,7 +1212,7 @@
 //
 bool LDDocument::hasUnsavedChanges() const
 {
-	return not isImplicit() and history()->position() != savePosition();
+	return not isCache() and history()->position() != savePosition();
 }
 
 // =============================================================================
--- a/src/ldDocument.h	Sat Sep 05 23:03:24 2015 +0300
+++ b/src/ldDocument.h	Sat Sep 05 23:55:06 2015 +0300
@@ -29,17 +29,6 @@
 class GLCompiler;
 
 //
-// Flags for LDDocument
-//
-enum LDDocumentFlag
-{
-	DOCF_IsBeingDestroyed = (1 << 0),
-};
-
-Q_DECLARE_FLAGS (LDDocumentFlags, LDDocumentFlag)
-Q_DECLARE_OPERATORS_FOR_FLAGS (LDDocumentFlags)
-
-//
 // This class stores a document either as a editable file for the user or for
 // subfile caching.
 //
@@ -52,111 +41,82 @@
 class LDDocument : public QObject, public HierarchyElement
 {
 	Q_OBJECT
-	PROPERTY (public,	QString,				name,			setName,			STOCK_WRITE)
-	PROPERTY (private,	LDObjectList,		objects, 		setObjects,			STOCK_WRITE)
-	PROPERTY (private,	LDObjectList,		cache, 			setCache,			STOCK_WRITE)
-	PROPERTY (private,	History*,			history,		setHistory,			STOCK_WRITE)
-	PROPERTY (public,	QString,				fullPath,		setFullPath,		STOCK_WRITE)
-	PROPERTY (public,	QString,				defaultName,	setDefaultName,		STOCK_WRITE)
-	PROPERTY (public,	bool,				isImplicit,		setImplicit,		CUSTOM_WRITE)
-	PROPERTY (public,	long,				savePosition,	setSavePosition,	STOCK_WRITE)
-	PROPERTY (public,	int,				tabIndex,		setTabIndex,		STOCK_WRITE)
-	PROPERTY (public,	QList<LDPolygon>,	polygonData,	setPolygonData,		STOCK_WRITE)
-	PROPERTY (private,	LDDocumentFlags,	flags,			setFlags,			STOCK_WRITE)
 
 public:
 	LDDocument (QObject* parent);
 	~LDDocument();
 
-	int addObject (LDObject* obj); // Adds an object to this file at the end of the file.
+	void addHistoryStep();
+	void addKnownVertices (LDObject* obj);
+	int addObject (LDObject* obj);
 	void addObjects (const LDObjectList& objs);
+	void addToHistory (AbstractHistoryEntry* entry);
+	void addToSelection (LDObject* obj);
+	void clear();
+	void clearHistory();
 	void clearSelection();
-	void forgetObject (LDObject* obj); // Deletes the given object from the object chain.
+	void close();
+	QString defaultName() const;
+	void forgetObject (LDObject* obj);
+	QString fullPath();
 	QString getDisplayName();
+	LDObject* getObject (int pos) const;
+	int getObjectCount() const;
 	const LDObjectList& getSelection() const;
-	bool hasUnsavedChanges() const; // Does this document have unsaved changes?
+	LDGLData* glData();
+	bool hasUnsavedChanges() const;
+	History* history() const;
 	void initializeCachedData();
 	LDObjectList inlineContents (bool deep, bool renderinline);
+	QList<LDPolygon> inlinePolygons();
+	const QVector<Vertex>& inlineVertices();
 	void insertObj (int pos, LDObject* obj);
-	int getObjectCount() const;
-	LDObject* getObject (int pos) const;
-	bool save (QString path = "", int64* sizeptr = null); // Saves this file to disk.
-	void swapObjects (LDObject* one, LDObject* other);
-	bool isSafeToClose(); // Perform safety checks. Do this before closing any files!
-	void setObject (int idx, LDObject* obj);
-	QList<LDPolygon> inlinePolygons();
-	void vertexChanged (const Vertex& a, const Vertex& b);
-	const QVector<Vertex>& inlineVertices();
-	void clear();
-	void addKnownVertices (LDObject* obj);
+	bool isCache() const;
+	bool isSafeToClose();
+	void mergeVertices();
+	QString name() const;
+	void needVertexMerge();
+	const LDObjectList& objects() const;
+	void openForEditing();
+	const QList<LDPolygon>& polygonData() const;
+	void redo();
 	void redoVertices();
-	void needVertexMerge();
 	void reloadAllSubfiles();
-
-	inline LDDocument& operator<< (LDObject* obj)
-	{
-		addObject (obj);
-		return *this;
-	}
-
-	inline void addHistoryStep()
-	{
-		history()->addStep();
-	}
-
-	inline void undo()
-	{
-		history()->undo();
-	}
+	void removeFromSelection (LDObject* obj);
+	bool save (QString path = "", int64* sizeptr = null);
+	long savePosition() const;
+	void setDefaultName (QString value);
+	void setFullPath (QString value);
+	void setImplicit (bool value);
+	void setName (QString value);
+	void setObject (int idx, LDObject* obj);
+	void setSavePosition (long value);
+	void setTabIndex (int value);
+	void swapObjects (LDObject* one, LDObject* other);
+	int tabIndex() const;
+	void undo();
+	void vertexChanged (const Vertex& a, const Vertex& b);
 
-	inline void redo()
-	{
-		history()->redo();
-	}
-
-	inline void clearHistory()
-	{
-		history()->clear();
-	}
-
-	inline void addToHistory (AbstractHistoryEntry* entry)
-	{
-		*history() << entry;
-	}
-
-	inline void dismiss()
-	{
-		setImplicit (true);
-	}
-
-	// Turns a full path into a relative path
-	static QString shortenName (QString a);
-	void mergeVertices();
-
-protected:
-	void addToSelection (LDObject* obj);
-	void removeFromSelection (LDObject* obj);
-
-	LDGLData* getGLData()
-	{
-		return m_gldata;
-	}
-
-	friend class LDObject;
-	friend class GLRenderer;
+	static QString shortenName (QString a); // Turns a full path into a relative path
 
 private:
-	
-	QMap<LDObject*, QVector<Vertex>> m_objectVertices;
-	QVector<Vertex> m_vertices;
+	QString m_name;
+	QString m_fullPath;
+	QString m_defaultName;
+	LDObjectList m_objects;
+	History* m_history;
+	bool m_isCache;
 	bool m_verticesOutdated;
 	bool m_needVertexMerge;
-	LDObjectList			m_sel;
-	LDGLData*				m_gldata;
-
-	// If set to true, next polygon inline of this document discards the
-	// stored polygon data and re-builds it.
-	bool					m_needsReCache;
+	bool m_needsReCache; // If true, next polygon inline of this document rebuilds stored polygon data.
+	bool m_beingDestroyed;
+	long m_savePosition;
+	int m_tabIndex;
+	QList<LDPolygon> m_polygonData;
+	QMap<LDObject*, QVector<Vertex>> m_objectVertices;
+	QVector<Vertex> m_vertices;
+	LDObjectList m_sel;
+	LDGLData* m_gldata;
 };
 
 // Opens the given file as the main file. Everything is closed first.
--- a/src/ldObject.cpp	Sat Sep 05 23:03:24 2015 +0300
+++ b/src/ldObject.cpp	Sat Sep 05 23:55:06 2015 +0300
@@ -293,8 +293,6 @@
 //
 void LDObject::destroy()
 {
-	print ("Destroying %1\n", this);
-
 	// Don't bother during program termination (FIXME)
 	if (IsExiting() == false)
 	{
@@ -882,7 +880,7 @@
 	// explicit file), we need to pre-compile the GL polygons for the document
 	// if they don't exist already.
 	if (a != null and
-		a->isImplicit() == false and
+		a->isCache() == false and
 		a->polygonData().isEmpty())
 	{
 		a->initializeCachedData();
--- a/src/mainwindow.cpp	Sat Sep 05 23:03:24 2015 +0300
+++ b/src/mainwindow.cpp	Sat Sep 05 23:55:06 2015 +0300
@@ -820,7 +820,7 @@
 //
 bool MainWindow::save (LDDocument* doc, bool saveAs)
 {
-	if (doc->isImplicit())
+	if (doc->isCache())
 		return false;
 
 	QString path = doc->fullPath();
@@ -928,7 +928,7 @@
 
 	for (LDDocument* document : m_documents)
 	{
-		if (not document->isImplicit())
+		if (not document->isCache())
 		{
 			// Add an item to the list for this file and store the tab index
 			// in the document so we can find documents by tab index.
@@ -984,7 +984,7 @@
 	// Find the file pointer of the item that was selected.
 	for (LDDocument* document : m_documents)
 	{
-		if (not document->isImplicit() and document->tabIndex() == tabIndex)
+		if (not document->isCache() and document->tabIndex() == tabIndex)
 		{
 			switchee = document;
 			break;
@@ -1049,7 +1049,7 @@
 	if (doc == null)
 		return;
 
-	doc->dismiss();
+	doc->close();
 }
 
 // ---------------------------------------------------------------------------------------------------------------------
@@ -1186,7 +1186,10 @@
 LDDocument* MainWindow::newDocument (bool cache)
 {
 	m_documents.append (new LDDocument (this));
-	m_documents.last()->setImplicit (cache);
+
+	if (not cache)
+		m_documents.last()->openForEditing();
+
 	return m_documents.last();
 }
 
@@ -1211,7 +1214,7 @@
 void MainWindow::changeDocument (LDDocument* document)
 {
 	// Implicit files were loaded for caching purposes and may never be switched to.
-	if (document != null and document->isImplicit())
+	if (document != null and document->isCache())
 		return;
 
 	m_currentDocument = document;
@@ -1239,7 +1242,7 @@
 		not m_documents[1]->name().isEmpty() and
 		not m_documents[0]->hasUnsavedChanges())
 	{
-		m_documents.first()->dismiss();
+		m_documents.first()->close();
 	}
 }
 
@@ -1259,7 +1262,7 @@
 	// Find a replacement document to use
 	for (LDDocument* doc : m_documents)
 	{
-		if (doc != old and not doc->isImplicit())
+		if (doc != old and not doc->isCache())
 		{
 			changeDocument (doc);
 			break;
--- a/src/primitives.cpp	Sat Sep 05 23:03:24 2015 +0300
+++ b/src/primitives.cpp	Sat Sep 05 23:55:06 2015 +0300
@@ -637,7 +637,7 @@
 		 << LDSpawn<LDBFC> (BFCStatement::CertifyCCW)
 		 << LDSpawn<LDEmpty>();
 
-	document->setImplicit (false);
+	document->openForEditing();
 	document->history()->setIgnoring (false);
 	document->addObjects (objs);
 	document->addObjects (MakePrimitive (type, segs, divs, num));
--- a/src/toolsets/algorithmtoolset.cpp	Sat Sep 05 23:03:24 2015 +0300
+++ b/src/toolsets/algorithmtoolset.cpp	Sat Sep 05 23:55:06 2015 +0300
@@ -536,7 +536,7 @@
 
 	// Create the new subfile document
 	LDDocument* doc = m_window->newDocument();
-	doc->setImplicit (false);
+	doc->openForEditing();
 	doc->setFullPath (fullsubname);
 	doc->setName (LDDocument::shortenName (fullsubname));
 
@@ -585,6 +585,6 @@
 	else
 	{
 		// Failed to save.
-		doc->dismiss();
+		doc->close();
 	}
 }
\ No newline at end of file
--- a/src/toolsets/filetoolset.cpp	Sat Sep 05 23:03:24 2015 +0300
+++ b/src/toolsets/filetoolset.cpp	Sat Sep 05 23:55:06 2015 +0300
@@ -81,7 +81,7 @@
 	if (not currentDocument()->isSafeToClose())
 		return;
 
-	currentDocument()->dismiss();
+	currentDocument()->close();
 }
 
 void FileToolset::closeAll()
@@ -181,10 +181,8 @@
 	{
 		LDSubfile* ref = dynamic_cast<LDSubfile*> (obj);
 
-		if (ref == null or not ref->fileInfo()->isImplicit())
-			continue;
-
-		ref->fileInfo()->setImplicit (false);
+		if (ref and ref->fileInfo()->isCache())
+			ref->fileInfo()->openForEditing();
 	}
 }
 
@@ -211,7 +209,7 @@
 		dlg->ui->rb_ring->isChecked()     ? Ring : Cone;
 
 	LDDocument* f = GeneratePrimitive (type, segs, divs, num);
-	f->setImplicit (false);
+	f->openForEditing();
 	m_window->save (f, false);
 }
 

mercurial