src/ldObject.h

changeset 988
ac4a2ae54f76
parent 968
4b93b7963456
child 995
7986584e7498
equal deleted inserted replaced
987:91281e39c50c 988:ac4a2ae54f76
53 53
54 class QListWidgetItem; 54 class QListWidgetItem;
55 class LDSubfile; 55 class LDSubfile;
56 class LDDocument; 56 class LDDocument;
57 57
58 class LDBFC; 58 class LDBfc;
59 59
60 // 60 //
61 // Object type codes. 61 // Object type codes.
62 // 62 //
63 enum LDObjectType 63 enum LDObjectType
65 OBJ_Subfile, // Object represents a sub-file reference 65 OBJ_Subfile, // Object represents a sub-file reference
66 OBJ_Quad, // Object represents a quadrilateral 66 OBJ_Quad, // Object represents a quadrilateral
67 OBJ_Triangle, // Object represents a triangle 67 OBJ_Triangle, // Object represents a triangle
68 OBJ_Line, // Object represents a line 68 OBJ_Line, // Object represents a line
69 OBJ_CondLine, // Object represents a conditional line 69 OBJ_CondLine, // Object represents a conditional line
70 OBJ_BFC, // Object represents a BFC statement 70 OBJ_Bfc, // Object represents a BFC statement
71 OBJ_Overlay, // Object contains meta-info about an overlay image. 71 OBJ_Overlay, // Object contains meta-info about an overlay image.
72 OBJ_Comment, // Object represents a comment 72 OBJ_Comment, // Object represents a comment
73 OBJ_Error, // Object is the result of failed parsing 73 OBJ_Error, // Object is the result of failed parsing
74 OBJ_Empty, // Object represents an empty line 74 OBJ_Empty, // Object represents an empty line
75 75
87 // which is a token of the object's type. The object can be casted into 87 // which is a token of the object's type. The object can be casted into
88 // sub-classes based on this enumerator. 88 // sub-classes based on this enumerator.
89 // 89 //
90 class LDObject 90 class LDObject
91 { 91 {
92 PROPERTY (public, bool, isHidden, setHidden, STOCK_WRITE)
93 PROPERTY (public, bool, isSelected, setSelected, STOCK_WRITE)
94 PROPERTY (public, LDObject*, parent, setParent, STOCK_WRITE)
95 PROPERTY (public, LDDocument*, document, setDocument, CUSTOM_WRITE)
96 PROPERTY (private, int32, id, setID, STOCK_WRITE)
97 PROPERTY (public, LDColor, color, setColor, CUSTOM_WRITE)
98 PROPERTY (private, QColor, randomColor, setRandomColor, STOCK_WRITE)
99
100 public: 92 public:
101 LDObject (LDDocument* document = nullptr); 93 LDObject (LDDocument* document = nullptr);
102 94
103 // This object as LDraw code 95 virtual QString asText() const = 0; // This object as LDraw code
104 virtual QString asText() const = 0; 96 LDColor color() const;
105 97 LDObject* createCopy() const;
106 // Makes a copy of this object 98 virtual LDColor defaultColor() const = 0; // What color does the object default to?
107 LDObject* createCopy() const; 99 void deselect();
108 100 void destroy();
109 // What color does the object default to? 101 LDDocument* document() const;
110 virtual LDColor defaultColor() const = 0; 102 LDPolygon* getPolygon();
111 103 virtual void getVertices (QVector<Vertex>& verts) const;
112 // Deletes this object 104 virtual bool hasMatrix() const = 0; // Does this object have a matrix and position? (see LDMatrixObject)
113 void destroy(); 105 qint32 id() const;
114 106 virtual void invert() = 0; // Inverts this object (winding is reversed)
115 // Removes this object from selection 107 virtual bool isColored() const = 0;
116 void deselect(); 108 bool isDestroyed() const;
117 109 bool isHidden() const;
118 virtual void getVertices (QVector<Vertex>& verts) const; 110 virtual bool isScemantic() const = 0; // Does this object have meaning in the part model?
119 111 bool isSelected() const;
120 // Does this object have a matrix and position? (see LDMatrixObject) 112 int lineNumber() const;
121 virtual bool hasMatrix() const = 0; 113 void move (Vertex vect);
122 114 LDObject* next() const;
123 // Inverts this object (winding is reversed) 115 virtual int numVertices() const = 0;
124 virtual void invert() = 0; 116 LDObject* parent() const;
125 117 LDObject* previous() const;
126 // Is this object colored? 118 bool previousIsInvertnext (LDBfc*& ptr);
127 virtual bool isColored() const = 0; 119 QColor randomColor() const;
128 120 void replace (LDObject* other);
129 // Does this object have meaning in the part model? 121 void select();
130 virtual bool isScemantic() const = 0; 122 void setColor (LDColor color);
131 123 void setDocument (LDDocument* document);
132 // Index (i.e. line number) of this object 124 void setHidden (bool value);
133 long lineNumber() const; 125 void setParent (LDObject* parent);
134 126 void setVertex (int i, const Vertex& vert);
135 // Moves this object using the given vertex as a movement List 127 void swap (LDObject* other);
136 void move (Vertex vect); 128 LDObject* topLevelParent();
137 129 virtual LDObjectType type() const = 0;
138 // Object after this in the current file 130 virtual QString typeName() const = 0;
139 LDObject* next() const; 131 const Vertex& vertex (int i) const;
140 132
141 // Number of vertices this object has
142 virtual int numVertices() const = 0;
143
144 // Object prior to this in the current file
145 LDObject* previous() const;
146
147 // Is the previous object INVERTNEXT?
148 bool previousIsInvertnext (LDBFC*& ptr);
149
150 // Replace this LDObject with another LDObject. Object is deleted in the process.
151 void replace (LDObject* other);
152
153 // Selects this object.
154 void select();
155
156 // Set a vertex to the given value
157 void setVertex (int i, const Vertex& vert);
158
159 // Set a single coordinate of a vertex
160 void setVertexCoord (int i, Axis ax, double value);
161
162 // Swap this object with another.
163 void swap (LDObject* other);
164
165 // What object in the current file ultimately references this?
166 LDObject* topLevelParent();
167
168 // Type enumerator of this object
169 virtual LDObjectType type() const = 0;
170
171 // Type name of this object
172 virtual QString typeName() const = 0;
173
174 // Get a vertex by index
175 const Vertex& vertex (int i) const;
176
177 // Get type name by enumerator
178 static QString typeName (LDObjectType type);
179
180 // Returns a default-constructed LDObject by the given type
181 static LDObject* getDefault (const LDObjectType type);
182
183 // TODO: move this to LDDocument?
184 static void moveObjects (LDObjectList objs, const bool up);
185
186 // Get a description of a list of LDObjects
187 static QString describeObjects (const LDObjectList& objs); 133 static QString describeObjects (const LDObjectList& objs);
188 static LDObject* fromID (int id); 134 static LDObject* fromID (int id);
189 LDPolygon* getPolygon(); 135 static LDObject* getDefault (const LDObjectType type);
190 bool isDestroyed() const { return m_isDestroyed; } 136 static void moveObjects (LDObjectList objs, const bool up); // TODO: move this to LDDocument?
191 137 static QString typeName (LDObjectType type);
192 // TODO: make this private!
193 QListWidgetItem* qObjListEntry;
194 138
195 protected: 139 protected:
196 virtual ~LDObject(); 140 virtual ~LDObject();
197 141
198 private: 142 private:
143 bool m_isHidden;
144 bool m_isSelected;
145 bool m_isDestroyed;
146 LDObject* m_parent;
147 LDDocument* m_document;
148 qint32 m_id;
149 LDColor m_color;
150 QColor m_randomColor;
199 Vertex m_coords[4]; 151 Vertex m_coords[4];
200 bool m_isDestroyed; 152 };
201 153
202 void chooseID();
203 };
204
205 //
206 // Makes a new LDObject. This makes the shared pointer always use the custom
207 // deleter so that all deletions go through finalDelete();
208 //
209 template<typename T, typename... Args> 154 template<typename T, typename... Args>
210 T* LDSpawn (Args... args) 155 T* LDSpawn (Args... args)
211 { 156 {
212 static_assert (std::is_base_of<LDObject, T>::value, 157 static_assert (std::is_base_of<LDObject, T>::value,
213 "spawn may only be used with LDObject-derivatives"); 158 "spawn may only be used with LDObject-derivatives");
231 // matrix as well. Even though right now only LDSubfile uses this, I'm keeping 176 // matrix as well. Even though right now only LDSubfile uses this, I'm keeping
232 // this class distinct in case I get new extension ideas. :) 177 // this class distinct in case I get new extension ideas. :)
233 // 178 //
234 class LDMatrixObject : public LDObject 179 class LDMatrixObject : public LDObject
235 { 180 {
236 PROPERTY (public, Matrix, transform, setTransform, CUSTOM_WRITE)
237 Vertex m_position; 181 Vertex m_position;
238 182
239 public: 183 public:
240 LDMatrixObject (LDDocument* document = nullptr) : 184 LDMatrixObject (LDDocument* document = nullptr);
241 LDObject (document), 185 LDMatrixObject (const Matrix& transform, const Vertex& pos, LDDocument* document = nullptr);
242 m_position (Origin) {} 186
243 187 const Vertex& position() const;
244 LDMatrixObject (const Matrix& transform, const Vertex& pos, LDDocument* document = nullptr) : 188 void setCoordinate (const Axis ax, double value);
245 LDObject (document),
246 m_transform (transform),
247 m_position (pos) {}
248
249 inline const Vertex& position() const
250 {
251 return m_position;
252 }
253
254 void setCoordinate (const Axis ax, double value)
255 {
256 Vertex v = position();
257
258 switch (ax)
259 {
260 case X: v.setX (value); break;
261 case Y: v.setY (value); break;
262 case Z: v.setZ (value); break;
263 }
264
265 setPosition (v);
266 }
267
268 void setPosition (const Vertex& a); 189 void setPosition (const Vertex& a);
190 const Matrix& transform() const;
191 void setTransform (const Matrix& value);
192
193 private:
194 Matrix m_transform;
269 }; 195 };
270 196
271 // 197 //
272 // 198 //
273 // Represents a line in the LDraw file that could not be properly parsed. It is 199 // Represents a line in the LDraw file that could not be properly parsed. It is
280 LDOBJ_NAME (error) 206 LDOBJ_NAME (error)
281 LDOBJ_VERTICES (0) 207 LDOBJ_VERTICES (0)
282 LDOBJ_UNCOLORED 208 LDOBJ_UNCOLORED
283 LDOBJ_SCEMANTIC 209 LDOBJ_SCEMANTIC
284 LDOBJ_NO_MATRIX 210 LDOBJ_NO_MATRIX
285 PROPERTY (public, QString, fileReferenced, setFileReferenced, STOCK_WRITE) 211
286 PROPERTY (private, QString, contents, setContents, STOCK_WRITE) 212 public:
287 PROPERTY (private, QString, reason, setReason, STOCK_WRITE) 213 LDError (QString contents, QString reason, LDDocument* document = nullptr);
288 214 QString reason() const;
289 public: 215 QString contents() const;
290 LDError (QString contents, QString reason, LDDocument* document = nullptr) : 216 QString fileReferenced() const;
291 LDObject (document), 217 void setFileReferenced (QString value);
292 m_contents (contents), 218
293 m_reason (reason) {} 219 private:
220 QString m_fileReferenced; // If this error was caused by inability to open a file, what file was that?
221 QString m_contents; // The LDraw code that was being parsed
222 QString m_reason;
294 }; 223 };
295 224
296 // 225 //
297 // 226 //
298 // Represents an empty line in the LDraw code file. 227 // Represents an empty line in the LDraw code file.
311 // 240 //
312 // Represents a code-0 comment in the LDraw code file. 241 // Represents a code-0 comment in the LDraw code file.
313 // 242 //
314 class LDComment : public LDObject 243 class LDComment : public LDObject
315 { 244 {
316 PROPERTY (public, QString, text, setText, STOCK_WRITE)
317 LDOBJ (Comment) 245 LDOBJ (Comment)
318 LDOBJ_NAME (comment) 246 LDOBJ_NAME (comment)
319 LDOBJ_VERTICES (0) 247 LDOBJ_VERTICES (0)
320 LDOBJ_UNCOLORED 248 LDOBJ_UNCOLORED
321 LDOBJ_NON_SCEMANTIC 249 LDOBJ_NON_SCEMANTIC
322 LDOBJ_NO_MATRIX 250 LDOBJ_NO_MATRIX
323 251
324 public: 252 public:
325 LDComment (QString text, LDDocument* document = nullptr) : 253 LDComment (QString text, LDDocument* document = nullptr);
326 LDObject (document), 254 QString text() const;
327 m_text (text) {} 255 void setText (QString value);
256
257 private:
258 QString m_text;
328 }; 259 };
329 260
330 // 261 //
331 // 262 //
332 // Represents a 0 BFC statement in the LDraw code. 263 // Represents a 0 BFC statement in the LDraw code.
333 // 264 //
334 enum class BFCStatement 265 enum class BfcStatement
335 { 266 {
336 CertifyCCW, 267 CertifyCCW,
337 CCW, 268 CCW,
338 CertifyCW, 269 CertifyCW,
339 CW, 270 CW,
346 277
347 NumValues, 278 NumValues,
348 FirstValue = CertifyCCW 279 FirstValue = CertifyCCW
349 }; 280 };
350 281
351 class LDBFC : public LDObject 282 class LDBfc : public LDObject
352 { 283 {
353 public: 284 public:
354 LDOBJ (BFC) 285 LDOBJ (Bfc)
355 LDOBJ_NAME (bfc) 286 LDOBJ_NAME (bfc)
356 LDOBJ_VERTICES (0) 287 LDOBJ_VERTICES (0)
357 LDOBJ_UNCOLORED 288 LDOBJ_UNCOLORED
358 LDOBJ_CUSTOM_SCEMANTIC { return (statement() == BFCStatement::InvertNext); } 289 LDOBJ_CUSTOM_SCEMANTIC { return (statement() == BfcStatement::InvertNext); }
359 LDOBJ_NO_MATRIX 290 LDOBJ_NO_MATRIX
360 PROPERTY (public, BFCStatement, statement, setStatement, STOCK_WRITE) 291
361 292 public:
362 public: 293 LDBfc (const BfcStatement type, LDDocument* document = nullptr);
363 LDBFC (const BFCStatement type, LDDocument* document = nullptr) : 294
364 LDObject (document), 295 BfcStatement statement() const;
365 m_statement (type) {} 296 void setStatement (BfcStatement value);
366 297 QString statementToString() const;
367 // Statement strings 298
368 static const char* StatementStrings[]; 299 static QString statementToString (BfcStatement statement);
300
301 private:
302 BfcStatement m_statement;
369 }; 303 };
370 304
371 // 305 //
372 // LDSubfile 306 // LDSubfile
373 // 307 //
380 LDOBJ_VERTICES (0) 314 LDOBJ_VERTICES (0)
381 LDOBJ_COLORED 315 LDOBJ_COLORED
382 LDOBJ_DEFAULTCOLOR (MainColor) 316 LDOBJ_DEFAULTCOLOR (MainColor)
383 LDOBJ_SCEMANTIC 317 LDOBJ_SCEMANTIC
384 LDOBJ_HAS_MATRIX 318 LDOBJ_HAS_MATRIX
385 PROPERTY (public, LDDocument*, fileInfo, setFileInfo, CUSTOM_WRITE) 319
386 320 public:
387 public:
388 enum InlineFlag
389 {
390 DeepInline = (1 << 0),
391 CacheInline = (1 << 1),
392 RendererInline = (1 << 2),
393 DeepCacheInline = (DeepInline | CacheInline),
394 };
395
396 Q_DECLARE_FLAGS (InlineFlags, InlineFlag)
397
398 // Inlines this subfile. 321 // Inlines this subfile.
322 LDDocument* fileInfo() const;
323 virtual void getVertices (QVector<Vertex>& verts) const override;
399 LDObjectList inlineContents (bool deep, bool render); 324 LDObjectList inlineContents (bool deep, bool render);
400 QList<LDPolygon> inlinePolygons(); 325 QList<LDPolygon> inlinePolygons();
401 virtual void getVertices (QVector<Vertex>& verts) const override; 326 void setFileInfo (LDDocument* fileInfo);
402 }; 327
403 328 private:
404 Q_DECLARE_OPERATORS_FOR_FLAGS (LDSubfile::InlineFlags) 329 LDDocument* m_fileInfo;
330 };
405 331
406 // 332 //
407 // LDLine 333 // LDLine
408 // 334 //
409 // Represents a single code-2 line in the LDraw code file. 335 // Represents a single code-2 line in the LDraw code file.
487 }; 413 };
488 414
489 // 415 //
490 // LDOverlay 416 // LDOverlay
491 // 417 //
492 // Overlay image meta, stored in the header of parts so as to preserve overlay 418 // Overlay image meta, stored in the header of parts so as to preserve overlay information.
493 // information.
494 // 419 //
495 class LDOverlay : public LDObject 420 class LDOverlay : public LDObject
496 { 421 {
497 LDOBJ (Overlay) 422 LDOBJ (Overlay)
498 LDOBJ_NAME (overlay) 423 LDOBJ_NAME (overlay)
499 LDOBJ_VERTICES (0) 424 LDOBJ_VERTICES (0)
500 LDOBJ_UNCOLORED 425 LDOBJ_UNCOLORED
501 LDOBJ_NON_SCEMANTIC 426 LDOBJ_NON_SCEMANTIC
502 LDOBJ_NO_MATRIX 427 LDOBJ_NO_MATRIX
503 PROPERTY (public, int, camera, setCamera, STOCK_WRITE) 428
504 PROPERTY (public, int, x, setX, STOCK_WRITE) 429 public:
505 PROPERTY (public, int, y, setY, STOCK_WRITE) 430 int camera() const;
506 PROPERTY (public, int, width, setWidth, STOCK_WRITE) 431 QString fileName() const;
507 PROPERTY (public, int, height, setHeight, STOCK_WRITE) 432 int height() const;
508 PROPERTY (public, QString, fileName, setFileName, STOCK_WRITE) 433 void setCamera (int value);
434 void setFileName (QString value);
435 void setHeight (int value);
436 void setWidth (int value);
437 void setX (int value);
438 void setY (int value);
439 int width() const;
440 int x() const;
441 int y() const;
442
443 private:
444 int m_camera;
445 int m_x;
446 int m_y;
447 int m_width;
448 int m_height;
449 QString m_fileName;
509 }; 450 };
510 451
511 // Other common LDraw stuff 452 // Other common LDraw stuff
512 static const QString CALicenseText ("!LICENSE Redistributable under CCAL version 2.0 : " 453 static const QString CALicenseText ("!LICENSE Redistributable under CCAL version 2.0 : "
513 "see CAreadme.txt"); 454 "see CAreadme.txt");

mercurial