src/ldObject.h

changeset 944
1a6f1997fcbe
parent 943
af81220741d9
child 945
c310073e4f22
equal deleted inserted replaced
943:af81220741d9 944:1a6f1997fcbe
24 #include "colors.h" 24 #include "colors.h"
25 25
26 #define LDOBJ(T) \ 26 #define LDOBJ(T) \
27 public: \ 27 public: \
28 static constexpr LDObjectType SubclassType = OBJ_##T; \ 28 static constexpr LDObjectType SubclassType = OBJ_##T; \
29 LD##T (LDObjectPtr* selfptr); \ 29 LD##T (LDObject** selfptr); \
30 \ 30 \
31 virtual LDObjectType type() const override \ 31 virtual LDObjectType type() const override \
32 { \ 32 { \
33 return OBJ_##T; \ 33 return OBJ_##T; \
34 } \ 34 } \
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 using LDBFCPtr = QSharedPointer<LDBFC>;
60 59
61 // 60 //
62 // Object type codes. 61 // Object type codes.
63 // 62 //
64 enum LDObjectType 63 enum LDObjectType
91 // 90 //
92 class LDObject 91 class LDObject
93 { 92 {
94 PROPERTY (public, bool, isHidden, setHidden, STOCK_WRITE) 93 PROPERTY (public, bool, isHidden, setHidden, STOCK_WRITE)
95 PROPERTY (public, bool, isSelected, setSelected, STOCK_WRITE) 94 PROPERTY (public, bool, isSelected, setSelected, STOCK_WRITE)
96 PROPERTY (public, bool, isDestructed, setDestructed, STOCK_WRITE)
97 PROPERTY (public, LDObject*, parent, setParent, STOCK_WRITE) 95 PROPERTY (public, LDObject*, parent, setParent, STOCK_WRITE)
98 PROPERTY (public, LDDocument*, document, setDocument, CUSTOM_WRITE) 96 PROPERTY (public, LDDocument*, document, setDocument, CUSTOM_WRITE)
99 PROPERTY (private, int32, id, setID, STOCK_WRITE) 97 PROPERTY (private, int32, id, setID, STOCK_WRITE)
100 PROPERTY (public, LDColor, color, setColor, CUSTOM_WRITE) 98 PROPERTY (public, LDColor, color, setColor, CUSTOM_WRITE)
101 PROPERTY (private, QColor, randomColor, setRandomColor, STOCK_WRITE) 99 PROPERTY (private, QColor, randomColor, setRandomColor, STOCK_WRITE)
102 PROPERTY (private, LDObjectWeakPtr, self, setSelf, STOCK_WRITE) 100 PROPERTY (private, LDObjectWeakPtr, self, setSelf, STOCK_WRITE)
103 101
104 public: 102 public:
105 LDObject (LDDocument* document = nullptr); 103 LDObject (LDDocument* document = nullptr);
104 virtual ~LDObject();
106 105
107 // This object as LDraw code 106 // This object as LDraw code
108 virtual QString asText() const = 0; 107 virtual QString asText() const = 0;
109 108
110 // Makes a copy of this object 109 // Makes a copy of this object
111 LDObjectPtr createCopy() const; 110 LDObject* createCopy() const;
112 111
113 // What color does the object default to? 112 // What color does the object default to?
114 virtual LDColor defaultColor() const = 0; 113 virtual LDColor defaultColor() const = 0;
115 114
116 // Deletes this object 115 // Deletes this object
138 137
139 // Moves this object using the given vertex as a movement List 138 // Moves this object using the given vertex as a movement List
140 void move (Vertex vect); 139 void move (Vertex vect);
141 140
142 // Object after this in the current file 141 // Object after this in the current file
143 LDObjectPtr next() const; 142 LDObject* next() const;
144 143
145 // Number of vertices this object has 144 // Number of vertices this object has
146 virtual int numVertices() const = 0; 145 virtual int numVertices() const = 0;
147 146
148 // Object prior to this in the current file 147 // Object prior to this in the current file
149 LDObjectPtr previous() const; 148 LDObject* previous() const;
150 149
151 // Is the previous object INVERTNEXT? 150 // Is the previous object INVERTNEXT?
152 bool previousIsInvertnext (LDBFCPtr& ptr); 151 bool previousIsInvertnext (LDBFCPtr& ptr);
153 152
154 // Replace this LDObject with another LDObject. Object is deleted in the process. 153 // Replace this LDObject with another LDObject. Object is deleted in the process.
155 void replace (LDObjectPtr other); 154 void replace (LDObject* other);
156 155
157 // Selects this object. 156 // Selects this object.
158 void select(); 157 void select();
159 158
160 // Set a vertex to the given value 159 // Set a vertex to the given value
162 161
163 // Set a single coordinate of a vertex 162 // Set a single coordinate of a vertex
164 void setVertexCoord (int i, Axis ax, double value); 163 void setVertexCoord (int i, Axis ax, double value);
165 164
166 // Swap this object with another. 165 // Swap this object with another.
167 void swap (LDObjectPtr other); 166 void swap (LDObject* other);
168 167
169 // What object in the current file ultimately references this? 168 // What object in the current file ultimately references this?
170 LDObjectPtr topLevelParent(); 169 LDObject* topLevelParent();
171 170
172 // Type enumerator of this object 171 // Type enumerator of this object
173 virtual LDObjectType type() const = 0; 172 virtual LDObjectType type() const = 0;
174 173
175 // Type name of this object 174 // Type name of this object
180 179
181 // Get type name by enumerator 180 // Get type name by enumerator
182 static QString typeName (LDObjectType type); 181 static QString typeName (LDObjectType type);
183 182
184 // Returns a default-constructed LDObject by the given type 183 // Returns a default-constructed LDObject by the given type
185 static LDObjectPtr getDefault (const LDObjectType type); 184 static LDObject* getDefault (const LDObjectType type);
186 185
187 // TODO: move this to LDDocument? 186 // TODO: move this to LDDocument?
188 static void moveObjects (LDObjectList objs, const bool up); 187 static void moveObjects (LDObjectList objs, const bool up);
189 188
190 // Get a description of a list of LDObjects 189 // Get a description of a list of LDObjects
191 static QString describeObjects (const LDObjectList& objs); 190 static QString describeObjects (const LDObjectList& objs);
192 static LDObjectPtr fromID (int id); 191 static LDObject* fromID (int id);
193 LDPolygon* getPolygon(); 192 LDPolygon* getPolygon();
194 193
195 // TODO: make this private! 194 // TODO: make this private!
196 QListWidgetItem* qObjListEntry; 195 QListWidgetItem* qObjListEntry;
197
198 // This is public because I cannot protect it as the lambda deletor would
199 // have to be the friend. Do not call this! Ever!
200 void finalDelete();
201
202 // Even though we supply a custom deleter to QSharedPointer, the shared
203 // pointer's base class still calls operator delete directly in one of
204 // its methods. The method should never be called but we need to declare
205 // the class making this delete call a friend anyway.
206 //
207 // Do not directly delete LDObjects. Ever.
208 virtual ~LDObject();
209 196
210 private: 197 private:
211 Vertex m_coords[4]; 198 Vertex m_coords[4];
212 199
213 void chooseID(); 200 void chooseID();
287 } 274 }
288 275
289 void setPosition (const Vertex& a); 276 void setPosition (const Vertex& a);
290 }; 277 };
291 278
292 using LDMatrixObjectPtr = QSharedPointer<LDMatrixObject>;
293 using LDMatrixObjectWeakPtr = QWeakPointer<LDMatrixObject>;
294
295 // 279 //
296 // 280 //
297 // Represents a line in the LDraw file that could not be properly parsed. It is 281 // Represents a line in the LDraw file that could not be properly parsed. It is
298 // represented by a (!) ERROR in the code view. It exists for the purpose of 282 // represented by a (!) ERROR in the code view. It exists for the purpose of
299 // allowing garbage lines be debugged and corrected within LDForge. 283 // allowing garbage lines be debugged and corrected within LDForge.
315 LDObject (document), 299 LDObject (document),
316 m_contents (contents), 300 m_contents (contents),
317 m_reason (reason) {} 301 m_reason (reason) {}
318 }; 302 };
319 303
320 using LDErrorPtr = QSharedPointer<LDError>;
321 using LDErrorWeakPtr = QWeakPointer<LDError>;
322
323 // 304 //
324 // 305 //
325 // Represents an empty line in the LDraw code file. 306 // Represents an empty line in the LDraw code file.
326 // 307 //
327 class LDEmpty : public LDObject 308 class LDEmpty : public LDObject
331 LDOBJ_VERTICES (0) 312 LDOBJ_VERTICES (0)
332 LDOBJ_UNCOLORED 313 LDOBJ_UNCOLORED
333 LDOBJ_NON_SCEMANTIC 314 LDOBJ_NON_SCEMANTIC
334 LDOBJ_NO_MATRIX 315 LDOBJ_NO_MATRIX
335 }; 316 };
336
337 using LDEmptyPtr = QSharedPointer<LDEmpty>;
338 using LDEmptyWeakPtr = QWeakPointer<LDEmpty>;
339 317
340 // 318 //
341 // 319 //
342 // Represents a code-0 comment in the LDraw code file. 320 // Represents a code-0 comment in the LDraw code file.
343 // 321 //
354 public: 332 public:
355 LDComment (QString text, LDDocument* document = nullptr) : 333 LDComment (QString text, LDDocument* document = nullptr) :
356 LDObject (document), 334 LDObject (document),
357 m_text (text) {} 335 m_text (text) {}
358 }; 336 };
359
360 using LDCommentPtr = QSharedPointer<LDComment>;
361 using LDCommentWeakPtr = QWeakPointer<LDComment>;
362 337
363 // 338 //
364 // 339 //
365 // Represents a 0 BFC statement in the LDraw code. 340 // Represents a 0 BFC statement in the LDraw code.
366 // 341 //
399 374
400 // Statement strings 375 // Statement strings
401 static const char* StatementStrings[]; 376 static const char* StatementStrings[];
402 }; 377 };
403 378
404 using LDBFCPtr = QSharedPointer<LDBFC>;
405 using LDBFCWeakPtr = QWeakPointer<LDBFC>;
406
407 // 379 //
408 // LDSubfile 380 // LDSubfile
409 // 381 //
410 // Represents a single code-1 subfile reference. 382 // Represents a single code-1 subfile reference.
411 // 383 //
436 QList<LDPolygon> inlinePolygons(); 408 QList<LDPolygon> inlinePolygons();
437 virtual void getVertices (QVector<Vertex>& verts) const override; 409 virtual void getVertices (QVector<Vertex>& verts) const override;
438 }; 410 };
439 411
440 Q_DECLARE_OPERATORS_FOR_FLAGS (LDSubfile::InlineFlags) 412 Q_DECLARE_OPERATORS_FOR_FLAGS (LDSubfile::InlineFlags)
441 using LDSubfilePtr = QSharedPointer<LDSubfile>;
442 using LDSubfileWeakPtr = QWeakPointer<LDSubfile>;
443 413
444 // 414 //
445 // LDLine 415 // LDLine
446 // 416 //
447 // Represents a single code-2 line in the LDraw code file. 417 // Represents a single code-2 line in the LDraw code file.
458 428
459 public: 429 public:
460 LDLine (Vertex v1, Vertex v2, LDDocument* document = nullptr); 430 LDLine (Vertex v1, Vertex v2, LDDocument* document = nullptr);
461 }; 431 };
462 432
463 using LDLinePtr = QSharedPointer<LDLine>;
464 using LDLineWeakPtr = QWeakPointer<LDLine>;
465
466 // 433 //
467 // LDCondLine 434 // LDCondLine
468 // 435 //
469 // Represents a single code-5 conditional line. 436 // Represents a single code-5 conditional line.
470 // 437 //
481 public: 448 public:
482 LDCondLine (const Vertex& v0, const Vertex& v1, const Vertex& v2, const Vertex& v3, LDDocument* document = nullptr); 449 LDCondLine (const Vertex& v0, const Vertex& v1, const Vertex& v2, const Vertex& v3, LDDocument* document = nullptr);
483 LDLinePtr toEdgeLine(); 450 LDLinePtr toEdgeLine();
484 }; 451 };
485 452
486 using LDCondLinePtr = QSharedPointer<LDCondLine>;
487 using LDCondLineWeakPtr = QWeakPointer<LDCondLine>;
488
489 // 453 //
490 // LDTriangle 454 // LDTriangle
491 // 455 //
492 // Represents a single code-3 triangle in the LDraw code file. Vertices v0, v1 456 // Represents a single code-3 triangle in the LDraw code file. Vertices v0, v1
493 // and v2 contain the end-points of this triangle. dColor is the color the 457 // and v2 contain the end-points of this triangle. dColor is the color the
505 469
506 public: 470 public:
507 LDTriangle (Vertex const& v1, Vertex const& v2, Vertex const& v3, LDDocument* document = nullptr); 471 LDTriangle (Vertex const& v1, Vertex const& v2, Vertex const& v3, LDDocument* document = nullptr);
508 }; 472 };
509 473
510 using LDTrianglePtr = QSharedPointer<LDTriangle>;
511 using LDTriangleWeakPtr = QWeakPointer<LDTriangle>;
512
513 // 474 //
514 // LDQuad 475 // LDQuad
515 // 476 //
516 // Represents a single code-4 quadrilateral. v0, v1, v2 and v3 are the end points 477 // Represents a single code-4 quadrilateral. v0, v1, v2 and v3 are the end points
517 // of the quad, dColor is the color used for the quad. 478 // of the quad, dColor is the color used for the quad.
531 492
532 // Split this quad into two triangles 493 // Split this quad into two triangles
533 QList<LDTrianglePtr> splitToTriangles(); 494 QList<LDTrianglePtr> splitToTriangles();
534 }; 495 };
535 496
536 using LDQuadPtr = QSharedPointer<LDQuad>;
537 using LDQuadWeakPtr = QWeakPointer<LDQuad>;
538
539 // 497 //
540 // LDVertex 498 // LDVertex
541 // 499 //
542 // The vertex is an LDForce-specific extension which represents a single 500 // The vertex is an LDForce-specific extension which represents a single
543 // vertex which can be used as a parameter to tools or to store coordinates 501 // vertex which can be used as a parameter to tools or to store coordinates
557 public: 515 public:
558 Vertex pos; 516 Vertex pos;
559 virtual void getVertices (QVector<Vertex>& verts) const override; 517 virtual void getVertices (QVector<Vertex>& verts) const override;
560 }; 518 };
561 519
562 using LDVertexPtr = QSharedPointer<LDVertex>;
563 using LDVertexWeakPtr = QWeakPointer<LDVertex>;
564
565 // 520 //
566 // LDOverlay 521 // LDOverlay
567 // 522 //
568 // Overlay image meta, stored in the header of parts so as to preserve overlay 523 // Overlay image meta, stored in the header of parts so as to preserve overlay
569 // information. 524 // information.
582 PROPERTY (public, int, width, setWidth, STOCK_WRITE) 537 PROPERTY (public, int, width, setWidth, STOCK_WRITE)
583 PROPERTY (public, int, height, setHeight, STOCK_WRITE) 538 PROPERTY (public, int, height, setHeight, STOCK_WRITE)
584 PROPERTY (public, QString, fileName, setFileName, STOCK_WRITE) 539 PROPERTY (public, QString, fileName, setFileName, STOCK_WRITE)
585 }; 540 };
586 541
587 using LDOverlayPtr = QSharedPointer<LDOverlay>;
588 using LDOverlayWeakPtr = QWeakPointer<LDOverlay>;
589
590 // Other common LDraw stuff 542 // Other common LDraw stuff
591 static const QString CALicenseText ("!LICENSE Redistributable under CCAL version 2.0 : " 543 static const QString CALicenseText ("!LICENSE Redistributable under CCAL version 2.0 : "
592 "see CAreadme.txt"); 544 "see CAreadme.txt");
593 545
594 enum 546 enum
598 }; 550 };
599 551
600 QString PreferredLicenseText(); 552 QString PreferredLicenseText();
601 553
602 template<typename T> 554 template<typename T>
603 inline void DynamicExecute (LDObjectPtr obj, std::function<void (QSharedPointer<T> const&)> func) 555 inline void DynamicExecute (LDObject* obj, std::function<void (QSharedPointer<T> const&)> func)
604 { 556 {
605 static_assert (std::is_base_of<LDObject, T>::value, 557 static_assert (std::is_base_of<LDObject, T>::value,
606 "DynamicExecute may only be used with LDObject-derivatives"); 558 "DynamicExecute may only be used with LDObject-derivatives");
607 559
608 if (obj->type() == T::SubclassType) 560 if (obj->type() == T::SubclassType)
609 func (obj.staticCast<T>()); 561 func (static_cast<T*> (obj));
610 } 562 }
611 563
612 struct LDIterationBreakage {}; 564 struct LDIterationBreakage {};
613 565
614 template<typename T> 566 template<typename T>
618 static_assert (std::is_base_of<LDObject, T>::value, 570 static_assert (std::is_base_of<LDObject, T>::value,
619 "LDIterate may only be used with LDObject-derivatives"); 571 "LDIterate may only be used with LDObject-derivatives");
620 572
621 try 573 try
622 { 574 {
623 for (LDObjectPtr const& obj : objs) 575 for (LDObject* const& obj : objs)
624 DynamicExecute<T> (obj, func); 576 DynamicExecute<T> (obj, func);
625 } 577 }
626 catch (LDIterationBreakage) {} 578 catch (LDIterationBreakage) {}
627 } 579 }
628 580

mercurial