src/ldtypes.cc

changeset 603
47e7773c7841
parent 600
209e3f1f7b2c
child 604
01bdac75994a
equal deleted inserted replaced
602:ac1744536b33 603:47e7773c7841
1 /* 1 /*
2 * LDForge: LDraw parts authoring CAD 2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013, 2014 Santeri Piippo 3 * Copyright( C) 2013, 2014 Santeri Piippo
4 * 4 *
5 * This program is free software: you can redistribute it and/or modify 5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by 6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or 7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version. 8 * ( at your option) any later version.
9 * 9 *
10 * This program is distributed in the hope that it will be useful, 10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details. 13 * GNU General Public License for more details.
23 #include "gui.h" 23 #include "gui.h"
24 #include "history.h" 24 #include "history.h"
25 #include "gldraw.h" 25 #include "gldraw.h"
26 #include "colors.h" 26 #include "colors.h"
27 27
28 cfg (String, ld_defaultname, ""); 28 cfg( String, ld_defaultname, "");
29 cfg (String, ld_defaultuser, ""); 29 cfg( String, ld_defaultuser, "");
30 cfg (Int, ld_defaultlicense, 0); 30 cfg( Int, ld_defaultlicense, 0);
31 31
32 // List of all LDObjects 32 // List of all LDObjects
33 static QList<LDObject*> g_LDObjects; 33 static QList<LDObject*> g_LDObjects;
34 34
35 // ============================================================================= 35 // =============================================================================
36 // LDObject constructors 36 // LDObject constructors
37 // ----------------------------------------------------------------------------- 37 // -----------------------------------------------------------------------------
38 LDObject::LDObject() : 38 LDObject::LDObject() :
39 m_Hidden (false), 39 m_Hidden( false),
40 m_Selected (false), 40 m_Selected( false),
41 m_Parent (null), 41 m_Parent( null),
42 m_File (null), 42 m_File( null),
43 m_GLInit (false), 43 m_GLInit( false),
44 qObjListEntry (null) 44 qObjListEntry( null)
45 { memset (m_coords, 0, sizeof m_coords); 45 {
46 memset( m_coords, 0, sizeof m_coords);
46 chooseID(); 47 chooseID();
47 g_LDObjects << this; 48 g_LDObjects << this;
48 } 49 }
49 50
50 // ============================================================================= 51 // =============================================================================
51 // ----------------------------------------------------------------------------- 52 // -----------------------------------------------------------------------------
52 void LDObject::chooseID() 53 void LDObject::chooseID()
53 { int32 id = 1; // 0 shalt be null 54 {
54 55 int32 id = 1; // 0 shalt be null
55 for (LDObject* obj : g_LDObjects) 56
56 { assert (obj != this); 57 for( LDObject* obj : g_LDObjects)
57 58 {
58 if (obj->getID() >= id) 59 assert( obj != this);
60
61 if( obj->getID() >= id)
59 id = obj->getID() + 1; 62 id = obj->getID() + 1;
60 } 63 }
61 64
62 setID (id); 65 setID( id);
63 } 66 }
64 67
65 // ============================================================================= 68 // =============================================================================
66 // Default implementations for LDObject's virtual methods. These should never be 69 // Default implementations for LDObject's virtual methods. These should never be
67 // actually called, for a subclass-less LDObject should never come into existance. 70 // actually called, for a subclass-less LDObject should never come into existance.
68 // These exist only to satisfy the linker. 71 // These exist only to satisfy the linker.
69 // ----------------------------------------------------------------------------- 72 // -----------------------------------------------------------------------------
70 LDObject::Type LDObject::getType() const 73 LDObject::Type LDObject::getType() const
71 { return LDObject::Unidentified; 74 {
75 return LDObject::Unidentified;
72 } 76 }
73 77
74 bool LDObject::hasMatrix() const 78 bool LDObject::hasMatrix() const
75 { return false; 79 {
80 return false;
76 } 81 }
77 82
78 bool LDObject::isColored() const 83 bool LDObject::isColored() const
79 { return false; 84 {
85 return false;
80 } 86 }
81 87
82 bool LDObject::isScemantic() const 88 bool LDObject::isScemantic() const
83 { return false; 89 {
90 return false;
84 } 91 }
85 92
86 str LDObject::getTypeName() const 93 str LDObject::getTypeName() const
87 { return ""; 94 {
95 return "";
88 } 96 }
89 97
90 int LDObject::vertices() const 98 int LDObject::vertices() const
91 { return 0; 99 {
92 } 100 return 0;
93 101 }
94 // ============================================================================= 102
95 // ----------------------------------------------------------------------------- 103 // =============================================================================
96 void LDObject::setVertexCoord (int i, Axis ax, double value) 104 // -----------------------------------------------------------------------------
97 { vertex v = getVertex (i); 105 void LDObject::setVertexCoord( int i, Axis ax, double value)
106 {
107 vertex v = getVertex( i);
98 v[ax] = value; 108 v[ax] = value;
99 setVertex (i, v); 109 setVertex( i, v);
100 } 110 }
101 111
102 LDError::LDError() {} 112 LDError::LDError() {}
103 113
104 // ============================================================================= 114 // =============================================================================
105 // ----------------------------------------------------------------------------- 115 // -----------------------------------------------------------------------------
106 str LDComment::raw() const 116 str LDComment::raw() const
107 { return fmt ("0 %1", text); 117 {
118 return fmt( "0 %1", text);
108 } 119 }
109 120
110 // ============================================================================= 121 // =============================================================================
111 // ----------------------------------------------------------------------------- 122 // -----------------------------------------------------------------------------
112 str LDSubfile::raw() const 123 str LDSubfile::raw() const
113 { str val = fmt ("1 %1 %2 ", getColor(), getPosition()); 124 {
125 str val = fmt( "1 %1 %2 ", getColor(), getPosition());
114 val += getTransform().stringRep(); 126 val += getTransform().stringRep();
115 val += ' '; 127 val += ' ';
116 val += getFileInfo()->getName(); 128 val += getFileInfo()->getName();
117 return val; 129 return val;
118 } 130 }
119 131
120 // ============================================================================= 132 // =============================================================================
121 // ----------------------------------------------------------------------------- 133 // -----------------------------------------------------------------------------
122 str LDLine::raw() const 134 str LDLine::raw() const
123 { str val = fmt ("2 %1", getColor()); 135 {
124 136 str val = fmt( "2 %1", getColor());
125 for (int i = 0; i < 2; ++i) 137
126 val += fmt (" %1", getVertex (i)); 138 for( int i = 0; i < 2; ++i)
139 val += fmt( " %1", getVertex( i));
127 140
128 return val; 141 return val;
129 } 142 }
130 143
131 // ============================================================================= 144 // =============================================================================
132 // ----------------------------------------------------------------------------- 145 // -----------------------------------------------------------------------------
133 str LDTriangle::raw() const 146 str LDTriangle::raw() const
134 { str val = fmt ("3 %1", getColor()); 147 {
135 148 str val = fmt( "3 %1", getColor());
136 for (int i = 0; i < 3; ++i) 149
137 val += fmt (" %1", getVertex (i)); 150 for( int i = 0; i < 3; ++i)
151 val += fmt( " %1", getVertex( i));
138 152
139 return val; 153 return val;
140 } 154 }
141 155
142 // ============================================================================= 156 // =============================================================================
143 // ----------------------------------------------------------------------------- 157 // -----------------------------------------------------------------------------
144 str LDQuad::raw() const 158 str LDQuad::raw() const
145 { str val = fmt ("4 %1", getColor()); 159 {
146 160 str val = fmt( "4 %1", getColor());
147 for (int i = 0; i < 4; ++i) 161
148 val += fmt (" %1", getVertex (i)); 162 for( int i = 0; i < 4; ++i)
163 val += fmt( " %1", getVertex( i));
149 164
150 return val; 165 return val;
151 } 166 }
152 167
153 // ============================================================================= 168 // =============================================================================
154 // ----------------------------------------------------------------------------- 169 // -----------------------------------------------------------------------------
155 str LDCondLine::raw() const 170 str LDCondLine::raw() const
156 { str val = fmt ("5 %1", getColor()); 171 {
172 str val = fmt( "5 %1", getColor());
157 173
158 // Add the coordinates 174 // Add the coordinates
159 for (int i = 0; i < 4; ++i) 175 for( int i = 0; i < 4; ++i)
160 val += fmt (" %1", getVertex (i)); 176 val += fmt( " %1", getVertex( i));
161 177
162 return val; 178 return val;
163 } 179 }
164 180
165 // ============================================================================= 181 // =============================================================================
166 // ----------------------------------------------------------------------------- 182 // -----------------------------------------------------------------------------
167 str LDError::raw() const 183 str LDError::raw() const
168 { return contents; 184 {
185 return contents;
169 } 186 }
170 187
171 // ============================================================================= 188 // =============================================================================
172 // ----------------------------------------------------------------------------- 189 // -----------------------------------------------------------------------------
173 str LDVertex::raw() const 190 str LDVertex::raw() const
174 { return fmt ("0 !LDFORGE VERTEX %1 %2", getColor(), pos); 191 {
192 return fmt( "0 !LDFORGE VERTEX %1 %2", getColor(), pos);
175 } 193 }
176 194
177 // ============================================================================= 195 // =============================================================================
178 // ----------------------------------------------------------------------------- 196 // -----------------------------------------------------------------------------
179 str LDEmpty::raw() const 197 str LDEmpty::raw() const
180 { return ""; 198 {
199 return "";
181 } 200 }
182 201
183 // ============================================================================= 202 // =============================================================================
184 // ----------------------------------------------------------------------------- 203 // -----------------------------------------------------------------------------
185 const char* LDBFC::statements[] = 204 const char* LDBFC::statements[] =
186 { "CERTIFY CCW", 205 {
206 "CERTIFY CCW",
187 "CCW", 207 "CCW",
188 "CERTIFY CW", 208 "CERTIFY CW",
189 "CW", 209 "CW",
190 "NOCERTIFY", 210 "NOCERTIFY",
191 "INVERTNEXT", 211 "INVERTNEXT",
194 "CLIP CW", 214 "CLIP CW",
195 "NOCLIP", 215 "NOCLIP",
196 }; 216 };
197 217
198 str LDBFC::raw() const 218 str LDBFC::raw() const
199 { return fmt ("0 BFC %1", LDBFC::statements[type]); 219 {
220 return fmt( "0 BFC %1", LDBFC::statements[type]);
200 } 221 }
201 222
202 // ============================================================================= 223 // =============================================================================
203 // ----------------------------------------------------------------------------- 224 // -----------------------------------------------------------------------------
204 QList<LDTriangle*> LDQuad::splitToTriangles() 225 QList<LDTriangle*> LDQuad::splitToTriangles()
205 { // Create the two triangles based on this quadrilateral: 226 {
227 // Create the two triangles based on this quadrilateral:
206 // 0---3 0---3 3 228 // 0---3 0---3 3
207 // | | | / /| 229 // | | | / /|
208 // | | ==> | / / | 230 // | | ==> | / / |
209 // | | |/ / | 231 // | | |/ / |
210 // 1---2 1 1---2 232 // 1---2 1 1---2
211 LDTriangle* tri1 = new LDTriangle (getVertex (0), getVertex (1), getVertex (3)); 233 LDTriangle* tri1 = new LDTriangle( getVertex( 0), getVertex( 1), getVertex( 3));
212 LDTriangle* tri2 = new LDTriangle (getVertex (1), getVertex (2), getVertex (3)); 234 LDTriangle* tri2 = new LDTriangle( getVertex( 1), getVertex( 2), getVertex( 3));
213 235
214 // The triangles also inherit the quad's color 236 // The triangles also inherit the quad's color
215 tri1->setColor (getColor()); 237 tri1->setColor( getColor());
216 tri2->setColor (getColor()); 238 tri2->setColor( getColor());
217 239
218 QList<LDTriangle*> triangles; 240 QList<LDTriangle*> triangles;
219 triangles << tri1; 241 triangles << tri1;
220 triangles << tri2; 242 triangles << tri2;
221 return triangles; 243 return triangles;
222 } 244 }
223 245
224 // ============================================================================= 246 // =============================================================================
225 // ----------------------------------------------------------------------------- 247 // -----------------------------------------------------------------------------
226 void LDObject::replace (LDObject* other) 248 void LDObject::replace( LDObject* other)
227 { long idx = getIndex(); 249 {
228 assert (idx != -1); 250 long idx = getIndex();
251 assert( idx != -1);
229 252
230 // Replace the instance of the old object with the new object 253 // Replace the instance of the old object with the new object
231 getFile()->setObject (idx, other); 254 getFile()->setObject( idx, other);
232 255
233 // Remove the old object 256 // Remove the old object
234 deleteSelf(); 257 deleteSelf();
235 } 258 }
236 259
237 // ============================================================================= 260 // =============================================================================
238 // ----------------------------------------------------------------------------- 261 // -----------------------------------------------------------------------------
239 void LDObject::swap (LDObject* other) 262 void LDObject::swap( LDObject* other)
240 { assert (getFile() == other->getFile()); 263 {
241 getFile()->swapObjects (this, other); 264 assert( getFile() == other->getFile());
242 } 265 getFile()->swapObjects( this, other);
243 266 }
244 // ============================================================================= 267
245 // ----------------------------------------------------------------------------- 268 // =============================================================================
246 LDLine::LDLine (vertex v1, vertex v2) 269 // -----------------------------------------------------------------------------
247 { setVertex (0, v1); 270 LDLine::LDLine( vertex v1, vertex v2)
248 setVertex (1, v2); 271 {
249 } 272 setVertex( 0, v1);
250 273 setVertex( 1, v2);
251 // ============================================================================= 274 }
252 // ----------------------------------------------------------------------------- 275
253 LDQuad::LDQuad (const vertex& v0, const vertex& v1, const vertex& v2, const vertex& v3) 276 // =============================================================================
254 { setVertex (0, v0); 277 // -----------------------------------------------------------------------------
255 setVertex (1, v1); 278 LDQuad::LDQuad( const vertex& v0, const vertex& v1, const vertex& v2, const vertex& v3)
256 setVertex (2, v2); 279 {
257 setVertex (3, v3); 280 setVertex( 0, v0);
281 setVertex( 1, v1);
282 setVertex( 2, v2);
283 setVertex( 3, v3);
258 } 284 }
259 285
260 // ============================================================================= 286 // =============================================================================
261 // ----------------------------------------------------------------------------- 287 // -----------------------------------------------------------------------------
262 LDObject::~LDObject() {} 288 LDObject::~LDObject() {}
266 LDSubfile::~LDSubfile() {} 292 LDSubfile::~LDSubfile() {}
267 293
268 // ============================================================================= 294 // =============================================================================
269 // ----------------------------------------------------------------------------- 295 // -----------------------------------------------------------------------------
270 void LDObject::deleteSelf() 296 void LDObject::deleteSelf()
271 { // If this object was selected, unselect it now 297 {
272 if (isSelected()) 298 // If this object was selected, unselect it now
299 if( isSelected())
273 unselect(); 300 unselect();
274 301
275 // If this object was associated to a file, remove it off it now 302 // If this object was associated to a file, remove it off it now
276 if (getFile()) 303 if( getFile())
277 getFile()->forgetObject (this); 304 getFile()->forgetObject( this);
278 305
279 // Delete the GL lists 306 // Delete the GL lists
280 GL::deleteLists (this); 307 GL::deleteLists( this);
281 308
282 // Remove this object from the list of LDObjects 309 // Remove this object from the list of LDObjects
283 g_LDObjects.removeOne (this); 310 g_LDObjects.removeOne( this);
284 311
285 delete this; 312 delete this;
286 } 313 }
287 314
288 // ============================================================================= 315 // =============================================================================
289 // ----------------------------------------------------------------------------- 316 // -----------------------------------------------------------------------------
290 static void transformObject (LDObject* obj, matrix transform, vertex pos, int parentcolor) 317 static void transformObject( LDObject* obj, matrix transform, vertex pos, int parentcolor)
291 { switch (obj->getType()) 318 {
292 { case LDObject::Line: 319 switch( obj->getType())
320 {
321 case LDObject::Line:
293 case LDObject::CondLine: 322 case LDObject::CondLine:
294 case LDObject::Triangle: 323 case LDObject::Triangle:
295 case LDObject::Quad: 324 case LDObject::Quad:
296 325
297 for (int i = 0; i < obj->vertices(); ++i) 326 for( int i = 0; i < obj->vertices(); ++i)
298 { vertex v = obj->getVertex (i); 327 {
299 v.transform (transform, pos); 328 vertex v = obj->getVertex( i);
300 obj->setVertex (i, v); 329 v.transform( transform, pos);
330 obj->setVertex( i, v);
301 } 331 }
302 332
303 break; 333 break;
304 334
305 case LDObject::Subfile: 335 case LDObject::Subfile:
306 { LDSubfile* ref = static_cast<LDSubfile*> (obj); 336 {
337 LDSubfile* ref = static_cast<LDSubfile*>( obj);
307 matrix newMatrix = transform * ref->getTransform(); 338 matrix newMatrix = transform * ref->getTransform();
308 vertex newpos = ref->getPosition(); 339 vertex newpos = ref->getPosition();
309 340
310 newpos.transform (transform, pos); 341 newpos.transform( transform, pos);
311 ref->setPosition (newpos); 342 ref->setPosition( newpos);
312 ref->setTransform (newMatrix); 343 ref->setTransform( newMatrix);
313 } 344 }
314 break; 345 break;
315 346
316 default: 347 default:
317 break; 348 break;
318 } 349 }
319 350
320 if (obj->getColor() == maincolor) 351 if( obj->getColor() == maincolor)
321 obj->setColor (parentcolor); 352 obj->setColor( parentcolor);
322 } 353 }
323 354
324 // ============================================================================= 355 // =============================================================================
325 // ----------------------------------------------------------------------------- 356 // -----------------------------------------------------------------------------
326 QList<LDObject*> LDSubfile::inlineContents (InlineFlags flags) 357 QList<LDObject*> LDSubfile::inlineContents( InlineFlags flags)
327 { QList<LDObject*> objs = getFileInfo()->inlineContents (flags); 358 {
359 QList<LDObject*> objs = getFileInfo()->inlineContents( flags);
328 360
329 // Transform the objects 361 // Transform the objects
330 for (LDObject* obj : objs) 362 for( LDObject* obj : objs)
331 { // Set the parent now so we know what inlined the object. 363 {
332 obj->setParent (this); 364 // Set the parent now so we know what inlined the object.
333 transformObject (obj, getTransform(), getPosition(), getColor()); 365 obj->setParent( this);
366 transformObject( obj, getTransform(), getPosition(), getColor());
334 } 367 }
335 368
336 return objs; 369 return objs;
337 } 370 }
338 371
339 // ============================================================================= 372 // =============================================================================
340 // ----------------------------------------------------------------------------- 373 // -----------------------------------------------------------------------------
341 long LDObject::getIndex() const 374 long LDObject::getIndex() const
342 { assert (getFile() != null); 375 {
343 376 assert( getFile() != null);
344 for (int i = 0; i < getFile()->getObjectCount(); ++i) 377
345 if (getFile()->getObject (i) == this) 378 for( int i = 0; i < getFile()->getObjectCount(); ++i)
379 if( getFile()->getObject( i) == this)
346 return i; 380 return i;
347 381
348 return -1; 382 return -1;
349 } 383 }
350 384
351 // ============================================================================= 385 // =============================================================================
352 // ----------------------------------------------------------------------------- 386 // -----------------------------------------------------------------------------
353 void LDObject::moveObjects (QList<LDObject*> objs, const bool up) 387 void LDObject::moveObjects( QList<LDObject*> objs, const bool up)
354 { if (objs.isEmpty()) 388 {
389 if( objs.isEmpty())
355 return; 390 return;
356 391
357 // If we move down, we need to iterate the array in reverse order. 392 // If we move down, we need to iterate the array in reverse order.
358 const long start = up ? 0 : (objs.size() - 1); 393 const long start = up ? 0 :( objs.size() - 1);
359 const long end = up ? objs.size() : -1; 394 const long end = up ? objs.size() : -1;
360 const long incr = up ? 1 : -1; 395 const long incr = up ? 1 : -1;
361 QList<LDObject*> objsToCompile; 396 QList<LDObject*> objsToCompile;
362 LDDocument* file = objs[0]->getFile(); 397 LDDocument* file = objs[0]->getFile();
363 398
364 for (long i = start; i != end; i += incr) 399 for( long i = start; i != end; i += incr)
365 { LDObject* obj = objs[i]; 400 {
401 LDObject* obj = objs[i];
366 402
367 const long idx = obj->getIndex(), 403 const long idx = obj->getIndex(),
368 target = idx + (up ? -1 : 1); 404 target = idx +( up ? -1 : 1);
369 405
370 if ( (up && idx == 0) || (!up && idx == (long) (file->getObjects().size() - 1))) 406 if( ( up && idx == 0) ||( !up && idx ==( long)( file->getObjects().size() - 1)))
371 { // One of the objects hit the extrema. If this happens, this should be the first 407 {
408 // One of the objects hit the extrema. If this happens, this should be the first
372 // object to be iterated on. Thus, nothing has changed yet and it's safe to just 409 // object to be iterated on. Thus, nothing has changed yet and it's safe to just
373 // abort the entire operation. 410 // abort the entire operation.
374 assert (i == start); 411 assert( i == start);
375 return; 412 return;
376 } 413 }
377 414
378 objsToCompile << obj; 415 objsToCompile << obj;
379 objsToCompile << file->getObject (target); 416 objsToCompile << file->getObject( target);
380 417
381 obj->swap (file->getObject (target)); 418 obj->swap( file->getObject( target));
382 } 419 }
383 420
384 removeDuplicates (objsToCompile); 421 removeDuplicates( objsToCompile);
385 422
386 // The objects need to be recompiled, otherwise their pick lists are left with 423 // The objects need to be recompiled, otherwise their pick lists are left with
387 // the wrong index colors which messes up selection. 424 // the wrong index colors which messes up selection.
388 for (LDObject* obj : objsToCompile) 425 for( LDObject* obj : objsToCompile)
389 g_win->R()->compileObject (obj); 426 g_win->R()->compileObject( obj);
390 } 427 }
391 428
392 // ============================================================================= 429 // =============================================================================
393 // ----------------------------------------------------------------------------- 430 // -----------------------------------------------------------------------------
394 str LDObject::typeName (LDObject::Type type) 431 str LDObject::typeName( LDObject::Type type)
395 { LDObject* obj = LDObject::getDefault (type); 432 {
433 LDObject* obj = LDObject::getDefault( type);
396 str name = obj->getTypeName(); 434 str name = obj->getTypeName();
397 obj->deleteSelf(); 435 obj->deleteSelf();
398 return name; 436 return name;
399 } 437 }
400 438
401 // ============================================================================= 439 // =============================================================================
402 // ----------------------------------------------------------------------------- 440 // -----------------------------------------------------------------------------
403 str LDObject::describeObjects (const QList<LDObject*>& objs) 441 str LDObject::describeObjects( const QList<LDObject*>& objs)
404 { bool firstDetails = true; 442 {
443 bool firstDetails = true;
405 str text = ""; 444 str text = "";
406 445
407 if (objs.isEmpty()) 446 if( objs.isEmpty())
408 return "nothing"; // :) 447 return "nothing"; // :)
409 448
410 for (long i = 0; i < LDObject::NumTypes; ++i) 449 for( long i = 0; i < LDObject::NumTypes; ++i)
411 { LDObject::Type objType = (LDObject::Type) i; 450 {
451 LDObject::Type objType =( LDObject::Type) i;
412 int count = 0; 452 int count = 0;
413 453
414 for (LDObject * obj : objs) 454 for( LDObject * obj : objs)
415 if (obj->getType() == objType) 455 if( obj->getType() == objType)
416 count++; 456 count++;
417 457
418 if (count == 0) 458 if( count == 0)
419 continue; 459 continue;
420 460
421 if (!firstDetails) 461 if( !firstDetails)
422 text += ", "; 462 text += ", ";
423 463
424 str noun = fmt ("%1%2", typeName (objType), plural (count)); 464 str noun = fmt( "%1%2", typeName( objType), plural( count));
425 465
426 // Plural of "vertex" is "vertices", correct that 466 // Plural of "vertex" is "vertices", correct that
427 if (objType == LDObject::Vertex && count != 1) 467 if( objType == LDObject::Vertex && count != 1)
428 noun = "vertices"; 468 noun = "vertices";
429 469
430 text += fmt ("%1 %2", count, noun); 470 text += fmt( "%1 %2", count, noun);
431 firstDetails = false; 471 firstDetails = false;
432 } 472 }
433 473
434 return text; 474 return text;
435 } 475 }
436 476
437 // ============================================================================= 477 // =============================================================================
438 // ----------------------------------------------------------------------------- 478 // -----------------------------------------------------------------------------
439 LDObject* LDObject::topLevelParent() 479 LDObject* LDObject::topLevelParent()
440 { if (!getParent()) 480 {
481 if( !getParent())
441 return this; 482 return this;
442 483
443 LDObject* it = this; 484 LDObject* it = this;
444 485
445 while (it->getParent()) 486 while( it->getParent())
446 it = it->getParent(); 487 it = it->getParent();
447 488
448 return it; 489 return it;
449 } 490 }
450 491
451 // ============================================================================= 492 // =============================================================================
452 // ----------------------------------------------------------------------------- 493 // -----------------------------------------------------------------------------
453 LDObject* LDObject::next() const 494 LDObject* LDObject::next() const
454 { long idx = getIndex(); 495 {
455 assert (idx != -1); 496 long idx = getIndex();
456 497 assert( idx != -1);
457 if (idx == (long) getFile()->getObjectCount() - 1) 498
499 if( idx ==( long) getFile()->getObjectCount() - 1)
458 return null; 500 return null;
459 501
460 return getFile()->getObject (idx + 1); 502 return getFile()->getObject( idx + 1);
461 } 503 }
462 504
463 // ============================================================================= 505 // =============================================================================
464 // ----------------------------------------------------------------------------- 506 // -----------------------------------------------------------------------------
465 LDObject* LDObject::prev() const 507 LDObject* LDObject::prev() const
466 { long idx = getIndex(); 508 {
467 assert (idx != -1); 509 long idx = getIndex();
468 510 assert( idx != -1);
469 if (idx == 0) 511
512 if( idx == 0)
470 return null; 513 return null;
471 514
472 return getFile()->getObject (idx - 1); 515 return getFile()->getObject( idx - 1);
473 } 516 }
474 517
475 // ============================================================================= 518 // =============================================================================
476 // ----------------------------------------------------------------------------- 519 // -----------------------------------------------------------------------------
477 void LDObject::move (vertex vect) 520 void LDObject::move( vertex vect)
478 { if (hasMatrix()) 521 {
479 { LDMatrixObject* mo = dynamic_cast<LDMatrixObject*> (this); 522 if( hasMatrix())
480 mo->setPosition (mo->getPosition() + vect); 523 {
481 } 524 LDMatrixObject* mo = dynamic_cast<LDMatrixObject*>( this);
482 elif (getType() == LDObject::Vertex) 525 mo->setPosition( mo->getPosition() + vect);
483 { // ugh 526 }
484 static_cast<LDVertex*> (this)->pos += vect; 527 elif( getType() == LDObject::Vertex)
528 {
529 // ugh
530 static_cast<LDVertex*>( this)->pos += vect;
485 } 531 }
486 else 532 else
487 { for (int i = 0; i < vertices(); ++i) 533 {
488 setVertex (i, getVertex (i) + vect); 534 for( int i = 0; i < vertices(); ++i)
535 setVertex( i, getVertex( i) + vect);
489 } 536 }
490 } 537 }
491 538
492 // ============================================================================= 539 // =============================================================================
493 // ----------------------------------------------------------------------------- 540 // -----------------------------------------------------------------------------
494 #define CHECK_FOR_OBJ(N) \ 541 #define CHECK_FOR_OBJ(N) \
495 if (type == LDObject::N) \ 542 if( type == LDObject::N) \
496 return new LD##N; 543 return new LD##N;
497 544
498 LDObject* LDObject::getDefault (const LDObject::Type type) 545 LDObject* LDObject::getDefault( const LDObject::Type type)
499 { CHECK_FOR_OBJ (Comment) 546 {
500 CHECK_FOR_OBJ (BFC) 547 CHECK_FOR_OBJ( Comment)
501 CHECK_FOR_OBJ (Line) 548 CHECK_FOR_OBJ( BFC)
502 CHECK_FOR_OBJ (CondLine) 549 CHECK_FOR_OBJ( Line)
503 CHECK_FOR_OBJ (Subfile) 550 CHECK_FOR_OBJ( CondLine)
504 CHECK_FOR_OBJ (Triangle) 551 CHECK_FOR_OBJ( Subfile)
505 CHECK_FOR_OBJ (Quad) 552 CHECK_FOR_OBJ( Triangle)
506 CHECK_FOR_OBJ (Empty) 553 CHECK_FOR_OBJ( Quad)
507 CHECK_FOR_OBJ (BFC) 554 CHECK_FOR_OBJ( Empty)
508 CHECK_FOR_OBJ (Error) 555 CHECK_FOR_OBJ( BFC)
509 CHECK_FOR_OBJ (Vertex) 556 CHECK_FOR_OBJ( Error)
510 CHECK_FOR_OBJ (Overlay) 557 CHECK_FOR_OBJ( Vertex)
558 CHECK_FOR_OBJ( Overlay)
511 return null; 559 return null;
512 } 560 }
513 561
514 // ============================================================================= 562 // =============================================================================
515 // ----------------------------------------------------------------------------- 563 // -----------------------------------------------------------------------------
520 void LDError::invert() {} 568 void LDError::invert() {}
521 569
522 // ============================================================================= 570 // =============================================================================
523 // ----------------------------------------------------------------------------- 571 // -----------------------------------------------------------------------------
524 void LDTriangle::invert() 572 void LDTriangle::invert()
525 { // Triangle goes 0 -> 1 -> 2, reversed: 0 -> 2 -> 1. 573 {
574 // Triangle goes 0 -> 1 -> 2, reversed: 0 -> 2 -> 1.
526 // Thus, we swap 1 and 2. 575 // Thus, we swap 1 and 2.
527 vertex tmp = getVertex (1); 576 vertex tmp = getVertex( 1);
528 setVertex (1, getVertex (2)); 577 setVertex( 1, getVertex( 2));
529 setVertex (2, tmp); 578 setVertex( 2, tmp);
530 579
531 return; 580 return;
532 } 581 }
533 582
534 // ============================================================================= 583 // =============================================================================
535 // ----------------------------------------------------------------------------- 584 // -----------------------------------------------------------------------------
536 void LDQuad::invert() 585 void LDQuad::invert()
537 { // Quad: 0 -> 1 -> 2 -> 3 586 {
587 // Quad: 0 -> 1 -> 2 -> 3
538 // rev: 0 -> 3 -> 2 -> 1 588 // rev: 0 -> 3 -> 2 -> 1
539 // Thus, we swap 1 and 3. 589 // Thus, we swap 1 and 3.
540 vertex tmp = getVertex (1); 590 vertex tmp = getVertex( 1);
541 setVertex (1, getVertex (3)); 591 setVertex( 1, getVertex( 3));
542 setVertex (3, tmp); 592 setVertex( 3, tmp);
543 } 593 }
544 594
545 // ============================================================================= 595 // =============================================================================
546 // ----------------------------------------------------------------------------- 596 // -----------------------------------------------------------------------------
547 void LDSubfile::invert() 597 void LDSubfile::invert()
548 { // Subfiles are inverted when they're prefixed with 598 {
599 // Subfiles are inverted when they're prefixed with
549 // a BFC INVERTNEXT statement. Thus we need to toggle this status. 600 // a BFC INVERTNEXT statement. Thus we need to toggle this status.
550 // For flat primitives it's sufficient that the determinant is 601 // For flat primitives it's sufficient that the determinant is
551 // flipped but I don't have a method for checking flatness yet. 602 // flipped but I don't have a method for checking flatness yet.
552 // Food for thought... 603 // Food for thought...
553 604
554 int idx = getIndex(); 605 int idx = getIndex();
555 606
556 if (idx > 0) 607 if( idx > 0)
557 { LDBFC* bfc = dynamic_cast<LDBFC*> (prev()); 608 {
558 609 LDBFC* bfc = dynamic_cast<LDBFC*>( prev());
559 if (bfc && bfc->type == LDBFC::InvertNext) 610
560 { // This is prefixed with an invertnext, thus remove it. 611 if( bfc && bfc->type == LDBFC::InvertNext)
612 {
613 // This is prefixed with an invertnext, thus remove it.
561 bfc->deleteSelf(); 614 bfc->deleteSelf();
562 return; 615 return;
563 } 616 }
564 } 617 }
565 618
566 // Not inverted, thus prefix it with a new invertnext. 619 // Not inverted, thus prefix it with a new invertnext.
567 LDBFC* bfc = new LDBFC (LDBFC::InvertNext); 620 LDBFC* bfc = new LDBFC( LDBFC::InvertNext);
568 getFile()->insertObj (idx, bfc); 621 getFile()->insertObj( idx, bfc);
569 } 622 }
570 623
571 // ============================================================================= 624 // =============================================================================
572 // ----------------------------------------------------------------------------- 625 // -----------------------------------------------------------------------------
573 static void invertLine (LDObject* line) 626 static void invertLine( LDObject* line)
574 { // For lines, we swap the vertices. I don't think that a 627 {
628 // For lines, we swap the vertices. I don't think that a
575 // cond-line's control points need to be swapped, do they? 629 // cond-line's control points need to be swapped, do they?
576 vertex tmp = line->getVertex (0); 630 vertex tmp = line->getVertex( 0);
577 line->setVertex (0, line->getVertex (1)); 631 line->setVertex( 0, line->getVertex( 1));
578 line->setVertex (1, tmp); 632 line->setVertex( 1, tmp);
579 } 633 }
580 634
581 void LDLine::invert() 635 void LDLine::invert()
582 { invertLine (this); 636 {
637 invertLine( this);
583 } 638 }
584 639
585 void LDCondLine::invert() 640 void LDCondLine::invert()
586 { invertLine (this); 641 {
642 invertLine( this);
587 } 643 }
588 644
589 void LDVertex::invert() {} 645 void LDVertex::invert() {}
590 646
591 // ============================================================================= 647 // =============================================================================
592 // ----------------------------------------------------------------------------- 648 // -----------------------------------------------------------------------------
593 LDLine* LDCondLine::demote() 649 LDLine* LDCondLine::demote()
594 { LDLine* repl = new LDLine; 650 {
595 651 LDLine* repl = new LDLine;
596 for (int i = 0; i < repl->vertices(); ++i) 652
597 repl->setVertex (i, getVertex (i)); 653 for( int i = 0; i < repl->vertices(); ++i)
598 654 repl->setVertex( i, getVertex( i));
599 repl->setColor (getColor()); 655
600 656 repl->setColor( getColor());
601 replace (repl); 657
658 replace( repl);
602 return repl; 659 return repl;
603 } 660 }
604 661
605 // ============================================================================= 662 // =============================================================================
606 // ----------------------------------------------------------------------------- 663 // -----------------------------------------------------------------------------
607 LDObject* LDObject::fromID (int id) 664 LDObject* LDObject::fromID( int id)
608 { for (LDObject* obj : g_LDObjects) 665 {
609 if (obj->getID() == id) 666 for( LDObject* obj : g_LDObjects)
667 if( obj->getID() == id)
610 return obj; 668 return obj;
611 669
612 return null; 670 return null;
613 } 671 }
614 672
615 // ============================================================================= 673 // =============================================================================
616 // ----------------------------------------------------------------------------- 674 // -----------------------------------------------------------------------------
617 str LDOverlay::raw() const 675 str LDOverlay::raw() const
618 { return fmt ("0 !LDFORGE OVERLAY %1 %2 %3 %4 %5 %6", 676 {
677 return fmt( "0 !LDFORGE OVERLAY %1 %2 %3 %4 %5 %6",
619 getFileName(), getCamera(), getX(), getY(), getWidth(), getHeight()); 678 getFileName(), getCamera(), getX(), getY(), getWidth(), getHeight());
620 } 679 }
621 680
622 void LDOverlay::invert() {} 681 void LDOverlay::invert() {}
623 682
624 // ============================================================================= 683 // =============================================================================
625 // Hook the set accessors of certain properties to this changeProperty function. 684 // Hook the set accessors of certain properties to this changeProperty function.
626 // It takes care of history management so we can capture low-level changes, this 685 // It takes care of history management so we can capture low-level changes, this
627 // makes history stuff work out of the box. 686 // makes history stuff work out of the box.
628 // ----------------------------------------------------------------------------- 687 // -----------------------------------------------------------------------------
629 template<class T> static void changeProperty (LDObject* obj, T* ptr, const T& val) 688 template<class T> static void changeProperty( LDObject* obj, T* ptr, const T& val)
630 { long idx; 689 {
631 690 long idx;
632 if (*ptr == val) 691
692 if( *ptr == val)
633 return; 693 return;
634 694
635 if (obj->getFile() && (idx = obj->getIndex()) != -1) 695 if( obj->getFile() &&( idx = obj->getIndex()) != -1)
636 { str before = obj->raw(); 696 {
697 str before = obj->raw();
637 *ptr = val; 698 *ptr = val;
638 str after = obj->raw(); 699 str after = obj->raw();
639 700
640 if (before != after) 701 if( before != after)
641 obj->getFile()->addToHistory (new EditHistory (idx, before, after)); 702 obj->getFile()->addToHistory( new EditHistory( idx, before, after));
642 } 703 }
643 else 704 else
644 *ptr = val; 705 *ptr = val;
645 } 706 }
646 707
647 // ============================================================================= 708 // =============================================================================
648 // ----------------------------------------------------------------------------- 709 // -----------------------------------------------------------------------------
649 void LDObject::setColor (const int& val) 710 void LDObject::setColor( const int& val)
650 { changeProperty (this, &m_Color, val); 711 {
651 } 712 changeProperty( this, &m_Color, val);
652 713 }
653 // ============================================================================= 714
654 // ----------------------------------------------------------------------------- 715 // =============================================================================
655 const vertex& LDObject::getVertex (int i) const 716 // -----------------------------------------------------------------------------
656 { return m_coords[i]->data(); 717 const vertex& LDObject::getVertex( int i) const
657 } 718 {
658 719 return m_coords[i]->data();
659 // ============================================================================= 720 }
660 // ----------------------------------------------------------------------------- 721
661 void LDObject::setVertex (int i, const vertex& vert) 722 // =============================================================================
662 { changeProperty (this, &m_coords[i], LDSharedVertex::getSharedVertex (vert)); 723 // -----------------------------------------------------------------------------
663 } 724 void LDObject::setVertex( int i, const vertex& vert)
664 725 {
665 // ============================================================================= 726 changeProperty( this, &m_coords[i], LDSharedVertex::getSharedVertex( vert));
666 // ----------------------------------------------------------------------------- 727 }
667 void LDMatrixObject::setPosition (const vertex& a) 728
668 { changeProperty (getLinkPointer(), &m_Position, LDSharedVertex::getSharedVertex (a)); 729 // =============================================================================
669 } 730 // -----------------------------------------------------------------------------
670 731 void LDMatrixObject::setPosition( const vertex& a)
671 // ============================================================================= 732 {
672 // ----------------------------------------------------------------------------- 733 changeProperty( getLinkPointer(), &m_Position, LDSharedVertex::getSharedVertex( a));
673 void LDMatrixObject::setTransform (const matrix& val) 734 }
674 { changeProperty (getLinkPointer(), &m_Transform, val); 735
736 // =============================================================================
737 // -----------------------------------------------------------------------------
738 void LDMatrixObject::setTransform( const matrix& val)
739 {
740 changeProperty( getLinkPointer(), &m_Transform, val);
675 } 741 }
676 742
677 // ============================================================================= 743 // =============================================================================
678 // ----------------------------------------------------------------------------- 744 // -----------------------------------------------------------------------------
679 static QMap<vertex, LDSharedVertex*> g_sharedVerts; 745 static QMap<vertex, LDSharedVertex*> g_sharedVerts;
680 746
681 LDSharedVertex* LDSharedVertex::getSharedVertex (const vertex& a) 747 LDSharedVertex* LDSharedVertex::getSharedVertex( const vertex& a)
682 { auto it = g_sharedVerts.find (a); 748 {
683 749 auto it = g_sharedVerts.find( a);
684 if (it == g_sharedVerts.end()) 750
685 { LDSharedVertex* v = new LDSharedVertex (a); 751 if( it == g_sharedVerts.end())
752 {
753 LDSharedVertex* v = new LDSharedVertex( a);
686 g_sharedVerts[a] = v; 754 g_sharedVerts[a] = v;
687 return v; 755 return v;
688 } 756 }
689 757
690 return *it; 758 return *it;
691 } 759 }
692 760
693 // ============================================================================= 761 // =============================================================================
694 // ----------------------------------------------------------------------------- 762 // -----------------------------------------------------------------------------
695 void LDSharedVertex::addRef (LDObject* a) 763 void LDSharedVertex::addRef( LDObject* a)
696 { m_refs << a; 764 {
697 } 765 m_refs << a;
698 766 }
699 // ============================================================================= 767
700 // ----------------------------------------------------------------------------- 768 // =============================================================================
701 void LDSharedVertex::delRef (LDObject* a) 769 // -----------------------------------------------------------------------------
702 { m_refs.removeOne (a); 770 void LDSharedVertex::delRef( LDObject* a)
703 771 {
704 if (m_refs.empty()) 772 m_refs.removeOne( a);
705 { g_sharedVerts.remove (m_data); 773
774 if( m_refs.empty())
775 {
776 g_sharedVerts.remove( m_data);
706 delete this; 777 delete this;
707 } 778 }
708 } 779 }
709 780
710 // ============================================================================= 781 // =============================================================================
711 // ----------------------------------------------------------------------------- 782 // -----------------------------------------------------------------------------
712 void LDObject::select() 783 void LDObject::select()
713 { if (!getFile()) 784 {
714 { log ("Warning: Object #%1 cannot be selected as it is not assigned a file!\n", getID()); 785 if( !getFile())
786 {
787 log( "Warning: Object #%1 cannot be selected as it is not assigned a file!\n", getID());
715 return; 788 return;
716 } 789 }
717 790
718 getFile()->addToSelection (this); 791 getFile()->addToSelection( this);
719 } 792 }
720 793
721 // ============================================================================= 794 // =============================================================================
722 // ----------------------------------------------------------------------------- 795 // -----------------------------------------------------------------------------
723 void LDObject::unselect() 796 void LDObject::unselect()
724 { if (!getFile()) 797 {
725 { log ("Warning: Object #%1 cannot be unselected as it is not assigned a file!\n", getID()); 798 if( !getFile())
799 {
800 log( "Warning: Object #%1 cannot be unselected as it is not assigned a file!\n", getID());
726 return; 801 return;
727 } 802 }
728 803
729 getFile()->removeFromSelection (this); 804 getFile()->removeFromSelection( this);
730 } 805 }
731 806
732 // ============================================================================= 807 // =============================================================================
733 // ----------------------------------------------------------------------------- 808 // -----------------------------------------------------------------------------
734 str getLicenseText (int id) 809 str getLicenseText( int id)
735 { switch (id) 810 {
736 { case 0: 811 switch( id)
812 {
813 case 0:
737 return CALicense; 814 return CALicense;
738 815
739 case 1: 816 case 1:
740 return NonCALicense; 817 return NonCALicense;
741 818
742 case 2: 819 case 2:
743 return ""; 820 return "";
744 } 821 }
745 822
746 assert (false); 823 assert( false);
747 return ""; 824 return "";
748 } 825 }
749 826
750 // ============================================================================= 827 // =============================================================================
751 // ----------------------------------------------------------------------------- 828 // -----------------------------------------------------------------------------
752 LDObject* LDObject::createCopy() const 829 LDObject* LDObject::createCopy() const
753 { /* 830 {
831 /*
754 LDObject* copy = clone(); 832 LDObject* copy = clone();
755 copy->setFile (null); 833 copy->setFile( null);
756 copy->setGLInit (false); 834 copy->setGLInit( false);
757 copy->chooseID(); 835 copy->chooseID();
758 copy->setSelected (false); 836 copy->setSelected( false);
759 */ 837 */
760 838
761 /* 839 /*
762 LDObject* copy = getDefault (getType()); 840 LDObject* copy = getDefault( getType());
763 copy->setColor (getColor()); 841 copy->setColor( getColor());
764 842
765 if (hasMatrix()) 843 if( hasMatrix())
766 { LDMatrixObject* copyMo = static_cast<LDMatrixObject*> (copy); 844 {
767 const LDMatrixObject* mo = static_cast<const LDMatrixObject*> (this); 845 LDMatrixObject* copyMo = static_cast<LDMatrixObject*>( copy);
768 copyMo->setPosition (mo->getPosition()); 846 const LDMatrixObject* mo = static_cast<const LDMatrixObject*>( this);
769 copyMo->setTransform (mo->getTransform()); 847 copyMo->setPosition( mo->getPosition());
848 copyMo->setTransform( mo->getTransform());
770 } 849 }
771 else 850 else
772 { for (int i = 0; i < vertices(); ++i) 851 {
773 copy->setVertex (getVertex (i)); 852 for( int i = 0; i < vertices(); ++i)
774 } 853 copy->setVertex( getVertex( i));
775 854 }
776 switch (getType()) 855
777 { case Subfile: 856 switch( getType())
778 { LDSubfile* copyRef = static_cast<LDSubfile*> (copy); 857 {
779 const LDSubfile* ref = static_cast<const LDSubfile*> (this); 858 case Subfile:
780 859 {
781 copyRef->setFileInfo (ref->getFileInfo()); 860 LDSubfile* copyRef = static_cast<LDSubfile*>( copy);
861 const LDSubfile* ref = static_cast<const LDSubfile*>( this);
862
863 copyRef->setFileInfo( ref->getFileInfo());
782 } 864 }
783 } 865 }
784 */ 866 */
785 867
786 LDObject* copy = parseLine (raw()); 868 LDObject* copy = parseLine( raw());
787 return copy; 869 return copy;
788 } 870 }

mercurial