src/ldObject.cc

changeset 770
b04c1e6ca1fb
parent 769
8bb3bed44570
child 771
0f04e34bec54
equal deleted inserted replaced
769:8bb3bed44570 770:b04c1e6ca1fb
32 CFGENTRY (Int, defaultLicense, 0); 32 CFGENTRY (Int, defaultLicense, 0);
33 33
34 // List of all LDObjects 34 // List of all LDObjects
35 static QMap<long, LDObjectWeakPtr> g_allObjects; 35 static QMap<long, LDObjectWeakPtr> g_allObjects;
36 36
37 // Temporary resource 37 #define LDOBJ_DEFAULT_CTOR(T,BASE) \
38 LDObjectPtr g_temporaryLDObject; 38 T :: T (LDObjectPtr* selfptr) : \
39 BASE (selfptr) {}
39 40
40 // ============================================================================= 41 // =============================================================================
41 // LDObject constructors 42 // LDObject constructors
42 // 43 //
43 LDObject::LDObject() : 44 LDObject::LDObject (LDObjectPtr* selfptr) :
44 m_isHidden (false), 45 m_isHidden (false),
45 m_isSelected (false), 46 m_isSelected (false),
46 m_isDestructed (false), 47 m_isDestructed (false),
47 m_document (null), 48 m_document (null),
48 qObjListEntry (null) 49 qObjListEntry (null)
49 { 50 {
50 LDObjectPtr selfptr (this, [](LDObject* obj){ obj->finalDelete(); }); 51 *selfptr = LDObjectPtr (this, [](LDObject* obj){ obj->finalDelete(); });
51 memset (m_coords, 0, sizeof m_coords); 52 memset (m_coords, 0, sizeof m_coords);
52 m_self = selfptr; 53 m_self = selfptr->toWeakRef();
53 chooseID(); 54 chooseID();
54 g_allObjects[id()] = self(); 55 g_allObjects[id()] = self();
55 setRandomColor (QColor::fromHsv (rand() % 360, rand() % 256, rand() % 96 + 128)); 56 setRandomColor (QColor::fromHsv (rand() % 360, rand() % 256, rand() % 96 + 128));
56 57 }
57 // This prevents the object from being prematurely deleted just because 58
58 // selfptr falls out of scope before it's caught by spawn() 59 LDSubfile::LDSubfile (LDObjectPtr* selfptr) :
59 g_temporaryLDObject = selfptr; 60 LDObject (selfptr)
60 } 61 {
62 setLinkPointer (self());
63 }
64
65 LDOBJ_DEFAULT_CTOR (LDEmpty, LDObject)
66 LDOBJ_DEFAULT_CTOR (LDError, LDObject)
67 LDOBJ_DEFAULT_CTOR (LDLine, LDObject)
68 LDOBJ_DEFAULT_CTOR (LDTriangle, LDObject)
69 LDOBJ_DEFAULT_CTOR (LDCondLine, LDLine)
70 LDOBJ_DEFAULT_CTOR (LDQuad, LDObject)
71 LDOBJ_DEFAULT_CTOR (LDVertex, LDObject)
72 LDOBJ_DEFAULT_CTOR (LDOverlay, LDObject)
73 LDOBJ_DEFAULT_CTOR (LDBFC, LDObject)
74 LDOBJ_DEFAULT_CTOR (LDComment, LDObject)
61 75
62 // ============================================================================= 76 // =============================================================================
63 // 77 //
64 void LDObject::chooseID() 78 void LDObject::chooseID()
65 { 79 {
66 int32 id = 1; // 0 shalt be null 80 int32 id = 1; // 0 shalt be null
67 81
68 for (auto it = g_allObjects.begin(); it != g_allObjects.end(); ++it) 82 for (auto it = g_allObjects.begin(); it != g_allObjects.end(); ++it)
69 { 83 {
70 LDObjectPtr obj = it->toStrongRef(); 84 LDObjectPtr obj = it->toStrongRef();
71
72 if (not obj)
73 fprint (stdout, "obj #%1 wasn't removed properly\n", it.key());
74 85
75 assert (obj != this); 86 assert (obj != this);
76 assert (obj != null); 87 assert (obj != null);
77 88
78 if (obj->id() >= id) 89 if (obj->id() >= id)
88 { 99 {
89 Vertex v = vertex (i); 100 Vertex v = vertex (i);
90 v.setCoordinate (ax, value); 101 v.setCoordinate (ax, value);
91 setVertex (i, v); 102 setVertex (i, v);
92 } 103 }
93
94 LDError::LDError() {}
95 104
96 // ============================================================================= 105 // =============================================================================
97 // 106 //
98 String LDComment::asText() const 107 String LDComment::asText() const
99 { 108 {
244 document()->swapObjects (self(), other); 253 document()->swapObjects (self(), other);
245 } 254 }
246 255
247 // ============================================================================= 256 // =============================================================================
248 // 257 //
249 LDLine::LDLine (Vertex v1, Vertex v2) 258 LDLine::LDLine (LDObjectPtr* selfptr, Vertex v1, Vertex v2) :
259 LDObject (selfptr)
250 { 260 {
251 setVertex (0, v1); 261 setVertex (0, v1);
252 setVertex (1, v2); 262 setVertex (1, v2);
253 } 263 }
254 264
255 // ============================================================================= 265 // =============================================================================
256 // 266 //
257 LDQuad::LDQuad (const Vertex& v0, const Vertex& v1, const Vertex& v2, const Vertex& v3) 267 LDQuad::LDQuad (LDObjectPtr* selfptr, const Vertex& v0, const Vertex& v1,
268 const Vertex& v2, const Vertex& v3) :
269 LDObject (selfptr)
258 { 270 {
259 setVertex (0, v0); 271 setVertex (0, v0);
260 setVertex (1, v1); 272 setVertex (1, v1);
261 setVertex (2, v2); 273 setVertex (2, v2);
262 setVertex (3, v3); 274 setVertex (3, v3);
263 } 275 }
264 276
265 // ============================================================================= 277 // =============================================================================
266 // 278 //
267 LDCondLine::LDCondLine (const Vertex& v0, const Vertex& v1, const Vertex& v2, const Vertex& v3) 279 LDCondLine::LDCondLine (LDObjectPtr* selfptr, const Vertex& v0, const Vertex& v1,
280 const Vertex& v2, const Vertex& v3) :
281 LDLine (selfptr)
268 { 282 {
269 setVertex (0, v0); 283 setVertex (0, v0);
270 setVertex (1, v1); 284 setVertex (1, v1);
271 setVertex (2, v2); 285 setVertex (2, v2);
272 setVertex (3, v3); 286 setVertex (3, v3);

mercurial