src/ldtypes.h

changeset 667
31540c1f22ea
parent 618
de0d51349f6c
--- a/src/ldtypes.h	Wed Oct 23 13:14:17 2013 +0300
+++ b/src/ldtypes.h	Mon Jan 20 15:04:26 2014 +0200
@@ -1,6 +1,6 @@
 /*
  *  LDForge: LDraw parts authoring CAD
- *  Copyright (C) 2013 Santeri Piippo
+ *  Copyright (C) 2013, 2014 Santeri Piippo
  *
  *  This program is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -16,27 +16,30 @@
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef LDTYPES_H
-#define LDTYPES_H
+#ifndef LDFORGE_LDTYPES_H
+#define LDFORGE_LDTYPES_H
 
-#include "common.h"
+#include "main.h"
 #include "types.h"
+#include "misc/documentPointer.h"
 
-#define LDOBJ(T) \
-public: \
-	virtual ~LD##T() {} \
-	virtual LDObject::Type getType() const override { \
-		return LDObject::T; \
-	} \
-	virtual str raw(); \
-	virtual LD##T* clone() { \
-		return new LD##T (*this); \
-	} \
-	virtual void move (vertex where); \
-	virtual void invert();
+#define LDOBJ(T)										\
+protected:												\
+	virtual LD##T* clone() override						\
+	{													\
+		return new LD##T (*this);						\
+	}													\
+														\
+public:													\
+	virtual LDObject::Type getType() const override		\
+	{													\
+		return LDObject::E##T;							\
+	}													\
+	virtual QString raw() const override;				\
+	virtual void invert() override;
 
-#define LDOBJ_NAME(N)          virtual str typeName() const override { return #N; }
-#define LDOBJ_VERTICES(V)      virtual short vertices() const override { return V; }
+#define LDOBJ_NAME(N)          virtual QString getTypeName() const override { return #N; }
+#define LDOBJ_VERTICES(V)      virtual int vertices() const override { return V; }
 #define LDOBJ_SETCOLORED(V)    virtual bool isColored() const override { return V; }
 #define LDOBJ_COLORED          LDOBJ_SETCOLORED (true)
 #define LDOBJ_UNCOLORED        LDOBJ_SETCOLORED (false)
@@ -51,7 +54,7 @@
 
 class QListWidgetItem;
 class LDSubfile;
-class LDFile;
+class LDDocument;
 class LDSharedVertex;
 
 // =============================================================================
@@ -63,62 +66,113 @@
 // sub-classes based on this enumerator.
 // =============================================================================
 class LDObject
-{	PROPERTY (bool, hidden, setHidden)
-	PROPERTY (bool, selected, setSelected)
-	PROPERTY (LDObject*, parent, setParent)
-	PROPERTY (LDFile*, file, setFile)
-	READ_PROPERTY (int32, id, setID)
-	DECLARE_PROPERTY (short, color, setColor)
+{
+	PROPERTY (public,		bool,			Hidden,			BOOL_OPS,	STOCK_WRITE)
+	PROPERTY (public,		bool,			Selected,		BOOL_OPS,	STOCK_WRITE)
+	PROPERTY (public,		LDObject*,		Parent,			NO_OPS,		STOCK_WRITE)
+	PROPERTY (public,		LDDocument*,	File,			NO_OPS,		STOCK_WRITE) // TODO: rename~
+	PROPERTY (private,		int,			ID,				NUM_OPS,	STOCK_WRITE)
+	PROPERTY (public,		int,			Color,			NUM_OPS,	CUSTOM_WRITE)
+	PROPERTY (public,		bool,			GLInit,			BOOL_OPS,	STOCK_WRITE)
 
 	public:
-		// Object type codes. Codes are sorted in order of significance.
+		// Object type codes.
 		enum Type
-		{	Subfile,        // Object represents a sub-file reference
-			Quad,           // Object represents a quadrilateral
-			Triangle,       // Object represents a triangle
-			Line,           // Object represents a line
-			CndLine,        // Object represents a conditional line
-			Vertex,         // Object is a vertex, LDForge extension object
-			BFC,            // Object represents a BFC statement
-			Overlay,        // Object contains meta-info about an overlay image.
-			Comment,        // Object represents a comment
-			Error,          // Object is the result of failed parsing
-			Empty,          // Object represents an empty line
-			Unidentified,   // Object is an uninitialized (SHOULD NEVER HAPPEN)
-			NumTypes        // Amount of object types
+		{
+			ESubfile,        // Object represents a sub-file reference
+			EQuad,           // Object represents a quadrilateral
+			ETriangle,       // Object represents a triangle
+			ELine,           // Object represents a line
+			ECondLine,       // Object represents a conditional line
+			EVertex,         // Object is a vertex, LDForge extension object
+			EBFC,            // Object represents a BFC statement
+			EOverlay,        // Object contains meta-info about an overlay image.
+			EComment,        // Object represents a comment
+			EError,          // Object is the result of failed parsing
+			EEmpty,          // Object represents an empty line
+			EUnidentified,   // Unknown object type (some functions return this; TODO: they probably should not)
+			ENumTypes        // Amount of object types
 		};
 
 		LDObject();
-		virtual ~LDObject();
+
+		// Makes a copy of this object
+		LDObject*					createCopy() const;
+
+		// Deletes this object
+		void						deleteSelf();
+
+		// Index (i.e. line number) of this object
+		long						getIndex() const;
+
+		// Type enumerator of this object
+		virtual Type				getType() const = 0;
+
+		// Get a vertex by index
+		const Vertex&				getVertex (int i) const;
+
+		// Type name of this object
+		virtual QString				getTypeName() const = 0;
+
+		// Does this object have a matrix and position? (see LDMatrixObject)
+		virtual bool				hasMatrix() const = 0;
+
+		// Inverts this object (winding is reversed)
+		virtual void				invert() = 0;
+
+		// Is this object colored?
+		virtual bool				isColored() const = 0;
+
+		// Does this object have meaning in the part model?
+		virtual bool				isScemantic() const = 0;
+
+		// Moves this object using the given vertex as a movement List
+		void						move (Vertex vect);
+
+		// Object after this in the current file
+		LDObject*					next() const;
 
-		virtual LDObject* clone()
-		{	return 0;   // Creates a new LDObject identical to this one.
-		}
-		long getIndex() const;                      // Index (i.e. line number) of this object
-		virtual LDObject::Type getType() const;     // Type enumerator of this object
-		const vertex& getVertex (int i) const;      // Get a vertex by index
-		virtual bool hasMatrix() const;             // Does this object have a matrix and position? (see LDMatrixObject)
-		virtual void invert();                      // Inverts this object (winding is reversed)
-		virtual bool isColored() const;             // Is this object colored?
-		virtual bool isScemantic() const;           // Does this object have meaning in the part model?
-		virtual void move (vertex vect);            // Moves this object using the given vertex as a movement List
-		LDObject* next() const;                     // Object after this in the current file
-		LDObject* prev() const;                     // Object prior to this in the current file
-		virtual str raw() {	return ""; }            // This object as LDraw code
-		void replace (LDObject* other);             // Replace this LDObject with another LDObject. Object is deleted in the process.
-		void select();
-		void setVertex (int i, const vertex& vert); // Set a vertex to the given value
-		void setVertexCoord (int i, Axis ax, double value); // Set a single coordinate of a vertex
-		void swap (LDObject* other);                // Swap this object with another.
-		LDObject* topLevelParent();                 // What object in the current file ultimately references this?
-		virtual str typeName() const;               // Type name of this object
-		void unselect();
-		virtual short vertices() const;             // Number of vertices this object has
+		// Object prior to this in the current file
+		LDObject*					prev() const;
+
+		// This object as LDraw code
+		virtual						QString raw() const = 0;
+
+		// Replace this LDObject with another LDObject. Object is deleted in the process.
+		void						replace (LDObject* other);
+
+		// Selects this object.
+		void						select();
+
+		// Set a vertex to the given value
+		void						setVertex (int i, const Vertex& vert);
+
+		// Set a single coordinate of a vertex
+		void						setVertexCoord (int i, Axis ax, double value);
+
+		// Swap this object with another.
+		void						swap (LDObject* other);
 
-		static str typeName (LDObject::Type type); // Get type name by enumerator
-		static LDObject* getDefault (const LDObject::Type type); // Returns a sample object by the given enumerator
-		static void moveObjects (QList<LDObject*> objs, const bool up); // TODO: move this to LDFile?
-		static str objectListContents (const QList<LDObject*>& objs); // Get a description of a list of LDObjects
+		// What object in the current file ultimately references this?
+		LDObject*					topLevelParent();
+
+		// Removes this object from selection // TODO: rename to deselect?
+		void						unselect();
+
+		// Number of vertices this object has // TODO: rename to getNumVertices
+		virtual int					vertices() const = 0;
+
+		// Get type name by enumerator
+		static QString typeName (LDObject::Type type);
+
+		// Returns a default-constructed LDObject by the given type
+		static LDObject* getDefault (const LDObject::Type type);
+
+		// TODO: move this to LDDocument?
+		static void moveObjects (LDObjectList objs, const bool up);
+
+		// Get a description of a list of LDObjects
+		static QString describeObjects (const LDObjectList& objs);
 		static LDObject* fromID (int id);
 
 		// TODO: make these private!
@@ -129,11 +183,18 @@
 		QListWidgetItem* qObjListEntry;
 
 	protected:
-		bool m_glinit;
-		friend class GLRenderer;
+		// LDObjects are to be deleted with the deleteSelf() method, not with
+		// operator delete. This is because it seems virtual functions cannot
+		// be properly called from the destructor, thus a normal method must
+		// be used instead. The destructor also doesn't seem to be able to
+		// be private without causing a truckload of problems so it's protected
+		// instead.
+		virtual ~LDObject();
+		void chooseID();
 
 	private:
-		LDSharedVertex* m_coords[4];
+		virtual LDObject* clone() = 0;
+		LDSharedVertex*	m_coords[4];
 };
 
 // =============================================================================
@@ -142,26 +203,29 @@
 // For use as coordinates of LDObjects. Keeps count of references.
 // -----------------------------------------------------------------------------
 class LDSharedVertex
-{	public:
-		inline const vertex& data() const
-		{	return m_data;
+{
+	public:
+		inline const Vertex& data() const
+		{
+			return m_data;
 		}
 
-		inline operator const vertex&() const
-		{	return m_data;
+		inline operator const Vertex&() const
+		{
+			return m_data;
 		}
 
 		void addRef (LDObject* a);
 		void delRef (LDObject* a);
 
-		static LDSharedVertex* getSharedVertex (const vertex& a);
+		static LDSharedVertex* getSharedVertex (const Vertex& a);
 
 	protected:
-		LDSharedVertex (const vertex& a) : m_data (a) {}
+		LDSharedVertex (const Vertex& a) : m_data (a) {}
 
 	private:
-		QList<LDObject*> m_refs;
-		vertex m_data;
+		LDObjectList m_refs;
+		Vertex m_data;
 };
 
 // =============================================================================
@@ -180,30 +244,34 @@
 // this class distinct in case I get new extension ideas. :)
 // =============================================================================
 class LDMatrixObject
-{	DECLARE_PROPERTY (matrix, transform, setTransform)
-	PROPERTY (LDObject*, linkPointer, setLinkPointer)
+{
+	PROPERTY (public,	LDObject*,			LinkPointer,	NO_OPS,	STOCK_WRITE)
+	PROPERTY (public,	Matrix,				Transform,		NO_OPS,	CUSTOM_WRITE)
 
 	public:
-		LDMatrixObject() {}
-		LDMatrixObject (const matrix& transform, const vertex& pos) :
-			m_transform (transform), m_position (LDSharedVertex::getSharedVertex (pos)) {}
+		LDMatrixObject() :
+			m_Position (LDSharedVertex::getSharedVertex (g_origin)) {}
 
-		const vertex& position() const
-		{	return m_position->data();
+		LDMatrixObject (const Matrix& transform, const Vertex& pos) :
+			m_Transform (transform),
+			m_Position (LDSharedVertex::getSharedVertex (pos)) {}
+
+		inline const Vertex& getPosition() const
+		{
+			return m_Position->data();
 		}
 
-		void setPosition (const vertex& a);
-
-		const double& setCoordinate (const Axis ax, double value)
-		{	vertex v = position();
+		void setCoordinate (const Axis ax, double value)
+		{
+			Vertex v = getPosition();
 			v[ax] = value;
 			setPosition (v);
-
-			return position() [ax];
 		}
 
+		void setPosition (const Vertex& a);
+
 	private:
-		LDSharedVertex* m_position;
+		LDSharedVertex*	m_Position;
 };
 
 // =============================================================================
@@ -215,23 +283,24 @@
 // zContent contains the contents of the unparsable line.
 // =============================================================================
 class LDError : public LDObject
-{	LDOBJ (Error)
+{
+	LDOBJ (Error)
 	LDOBJ_NAME (error)
 	LDOBJ_VERTICES (0)
 	LDOBJ_UNCOLORED
 	LDOBJ_SCEMANTIC
 	LDOBJ_NO_MATRIX
-	PROPERTY (str, fileRef, setFileRef)
+	PROPERTY (public,	QString, FileReferenced, STR_OPS,	STOCK_WRITE)
 
 	public:
 		LDError();
-		LDError (str contents, str reason) : contents (contents), reason (reason) {}
+		LDError (QString contents, QString reason) : contents (contents), reason (reason) {}
 
 		// Content of this unknown line
-		str contents;
+		QString contents;
 
 		// Why is this gibberish?
-		str reason;
+		QString reason;
 };
 
 // =============================================================================
@@ -240,7 +309,9 @@
 // Represents an empty line in the LDraw code file.
 // =============================================================================
 class LDEmpty : public LDObject
-{	LDOBJ (Empty)
+{
+	LDOBJ (Empty)
+	LDOBJ_NAME (empty)
 	LDOBJ_VERTICES (0)
 	LDOBJ_UNCOLORED
 	LDOBJ_NON_SCEMANTIC
@@ -254,7 +325,8 @@
 // the text of the comment.
 // =============================================================================
 class LDComment : public LDObject
-{	LDOBJ (Comment)
+{
+	LDOBJ (Comment)
 	LDOBJ_NAME (comment)
 	LDOBJ_VERTICES (0)
 	LDOBJ_UNCOLORED
@@ -263,9 +335,9 @@
 
 	public:
 		LDComment() {}
-		LDComment (str text) : text (text) {}
+		LDComment (QString text) : text (text) {}
 
-		str text; // The text of this comment
+		QString text; // The text of this comment
 };
 
 // =============================================================================
@@ -275,9 +347,11 @@
 // of this statement.
 // =============================================================================
 class LDBFC : public LDObject
-{	public:
+{
+	public:
 		enum Type
-		{	CertifyCCW,
+		{
+			CertifyCCW,
 			CCW,
 			CertifyCW,
 			CW,
@@ -299,7 +373,8 @@
 
 	public:
 		LDBFC() {}
-		LDBFC (const LDBFC::Type type) : type (type) {}
+		LDBFC (const LDBFC::Type type) :
+			type (type) {}
 
 		// Statement strings
 		static const char* statements[];
@@ -313,17 +388,19 @@
 // Represents a single code-1 subfile reference.
 // =============================================================================
 class LDSubfile : public LDObject, public LDMatrixObject
-{	LDOBJ (Subfile)
+{
+	LDOBJ (Subfile)
 	LDOBJ_NAME (subfile)
 	LDOBJ_VERTICES (0)
 	LDOBJ_COLORED
 	LDOBJ_SCEMANTIC
 	LDOBJ_HAS_MATRIX
-	PROPERTY (LDFile*, fileInfo, setFileInfo)
+	PROPERTY (public,	LDDocumentPointer, FileInfo, NO_OPS,	STOCK_WRITE)
 
 	public:
 		enum InlineFlag
-		{	DeepInline     = (1 << 0),
+		{
+			DeepInline     = (1 << 0),
 			CacheInline    = (1 << 1),
 			RendererInline = (1 << 2),
 
@@ -333,12 +410,16 @@
 		Q_DECLARE_FLAGS (InlineFlags, InlineFlag)
 
 		LDSubfile()
-		{	setLinkPointer (this);
+		{
+			setLinkPointer (this);
 		}
 
 		// Inlines this subfile. Note that return type is an array of heap-allocated
-		// LDObject-clones, they must be deleted one way or another.
-		QList<LDObject*> inlineContents (InlineFlags flags);
+		// LDObject copies, they must be deleted manually.
+		LDObjectList inlineContents (InlineFlags flags);
+
+	protected:
+		~LDSubfile();
 };
 
 Q_DECLARE_OPERATORS_FOR_FLAGS (LDSubfile::InlineFlags)
@@ -351,7 +432,8 @@
 // set.
 // =============================================================================
 class LDLine : public LDObject
-{	LDOBJ (Line)
+{
+	LDOBJ (Line)
 	LDOBJ_NAME (line)
 	LDOBJ_VERTICES (2)
 	LDOBJ_COLORED
@@ -360,17 +442,18 @@
 
 	public:
 		LDLine() {}
-		LDLine (vertex v1, vertex v2);
+		LDLine (Vertex v1, Vertex v2);
 };
 
 // =============================================================================
-// LDCndLine
+// LDCondLine
 //
 // Represents a single code-5 conditional line. The end-points v0 and v1 are
 // inherited from LDLine, c0 and c1 are the control points of this line.
 // =============================================================================
-class LDCndLine : public LDLine
-{	LDOBJ (CndLine)
+class LDCondLine : public LDLine
+{
+	LDOBJ (CondLine)
 	LDOBJ_NAME (condline)
 	LDOBJ_VERTICES (4)
 	LDOBJ_COLORED
@@ -378,7 +461,7 @@
 	LDOBJ_NO_MATRIX
 
 	public:
-		LDCndLine() {}
+		LDCondLine() {}
 		LDLine* demote();
 };
 
@@ -390,7 +473,8 @@
 // triangle is colored with.
 // =============================================================================
 class LDTriangle : public LDObject
-{	LDOBJ (Triangle)
+{
+	LDOBJ (Triangle)
 	LDOBJ_NAME (triangle)
 	LDOBJ_VERTICES (3)
 	LDOBJ_COLORED
@@ -399,8 +483,9 @@
 
 	public:
 		LDTriangle() {}
-		LDTriangle (vertex v0, vertex v1, vertex v2)
-		{	setVertex (0, v0);
+		LDTriangle (Vertex v0, Vertex v1, Vertex v2)
+		{
+			setVertex (0, v0);
 			setVertex (1, v1);
 			setVertex (2, v2);
 		}
@@ -413,7 +498,8 @@
 // of the quad, dColor is the color used for the quad.
 // =============================================================================
 class LDQuad : public LDObject
-{	LDOBJ (Quad)
+{
+	LDOBJ (Quad)
 	LDOBJ_NAME (quad)
 	LDOBJ_VERTICES (4)
 	LDOBJ_COLORED
@@ -422,7 +508,7 @@
 
 	public:
 		LDQuad() {}
-		LDQuad (const vertex& v0, const vertex& v1, const vertex& v2, const vertex& v3);
+		LDQuad (const Vertex& v0, const Vertex& v1, const Vertex& v2, const Vertex& v3);
 
 		// Split this quad into two triangles (note: heap-allocated)
 		QList<LDTriangle*> splitToTriangles();
@@ -437,7 +523,8 @@
 // finished parts.
 // =============================================================================
 class LDVertex : public LDObject
-{	LDOBJ (Vertex)
+{
+	LDOBJ (Vertex)
 	LDOBJ_NAME (vertex)
 	LDOBJ_VERTICES (0) // TODO: move pos to vaCoords[0]
 	LDOBJ_COLORED
@@ -447,7 +534,7 @@
 	public:
 		LDVertex() {}
 
-		vertex pos;
+		Vertex pos;
 };
 
 // =============================================================================
@@ -457,24 +544,27 @@
 // information.
 // =============================================================================
 class LDOverlay : public LDObject
-{	LDOBJ (Overlay)
+{
+	LDOBJ (Overlay)
 	LDOBJ_NAME (overlay)
 	LDOBJ_VERTICES (0)
 	LDOBJ_UNCOLORED
 	LDOBJ_NON_SCEMANTIC
 	LDOBJ_NO_MATRIX
-	PROPERTY (int, camera, setCamera)
-	PROPERTY (int, x, setX)
-	PROPERTY (int, y, setY)
-	PROPERTY (int, width, setWidth)
-	PROPERTY (int, height, setHeight)
-	PROPERTY (str, filename, setFilename)
+	PROPERTY (public,	int,	 Camera,	NUM_OPS,	STOCK_WRITE)
+	PROPERTY (public,	int,	 X,			NUM_OPS,	STOCK_WRITE)
+	PROPERTY (public,	int,	 Y,			NUM_OPS,	STOCK_WRITE)
+	PROPERTY (public,	int,	 Width,		NUM_OPS,	STOCK_WRITE)
+	PROPERTY (public,	int,	 Height,	NUM_OPS,	STOCK_WRITE)
+	PROPERTY (public,	QString, FileName,	STR_OPS,	STOCK_WRITE)
 };
 
 // Other common LDraw stuff
-static const str CALicense = "!LICENSE Redistributable under CCAL version 2.0 : see CAreadme.txt",
+static const QString CALicense = "!LICENSE Redistributable under CCAL version 2.0 : see CAreadme.txt",
 				 NonCALicense = "!LICENSE Not redistributable : see NonCAreadme.txt";
-static const short lores = 16;
-static const short hires = 48;
+static const int lores = 16;
+static const int hires = 48;
 
-#endif // LDTYPES_H
+QString getLicenseText (int id);
+
+#endif // LDFORGE_LDTYPES_H

mercurial