src/primitives.cc

changeset 606
3dd6f343ec06
parent 604
01bdac75994a
child 609
a8dc74a809c6
equal deleted inserted replaced
605:2983f7c7e7c9 606:3dd6f343ec06
34 34
35 extern_cfg (String, ld_defaultname); 35 extern_cfg (String, ld_defaultname);
36 extern_cfg (String, ld_defaultuser); 36 extern_cfg (String, ld_defaultuser);
37 extern_cfg (Int, ld_defaultlicense); 37 extern_cfg (Int, ld_defaultlicense);
38 38
39 static const str g_radialNameRoots[] = 39 static const QStringList g_radialNameRoots =
40 { 40 {
41 "edge", 41 "edge",
42 "cyli", 42 "cyli",
43 "disc", 43 "disc",
44 "ndis", 44 "ndis",
67 PrimitiveLister::start(); 67 PrimitiveLister::start();
68 } 68 }
69 else 69 else
70 { 70 {
71 // Read primitives from prims.cfg 71 // Read primitives from prims.cfg
72 for (str line : conf) 72 for (QString line : conf)
73 { 73 {
74 int space = line.indexOf (" "); 74 int space = line.indexOf (" ");
75 75
76 if (space == -1) 76 if (space == -1)
77 continue; 77 continue;
86 } 86 }
87 } 87 }
88 88
89 // ============================================================================= 89 // =============================================================================
90 // ----------------------------------------------------------------------------- 90 // -----------------------------------------------------------------------------
91 static void recursiveGetFilenames (QDir dir, QList<str>& fnames) 91 static void recursiveGetFilenames (QDir dir, QList<QString>& fnames)
92 { 92 {
93 QFileInfoList flist = dir.entryInfoList(); 93 QFileInfoList flist = dir.entryInfoList();
94 94
95 for (const QFileInfo & info : flist) 95 for (const QFileInfo & info : flist)
96 { 96 {
132 int j = min (m_i + 300, m_files.size()); 132 int j = min (m_i + 300, m_files.size());
133 log ("PrimitiveLister::work: %1 -> %2\n", m_i, j); 133 log ("PrimitiveLister::work: %1 -> %2\n", m_i, j);
134 134
135 for (; m_i < j; ++m_i) 135 for (; m_i < j; ++m_i)
136 { 136 {
137 str fname = m_files[m_i]; 137 QString fname = m_files[m_i];
138 File f (fname, File::Read); 138 File f (fname, File::Read);
139 Primitive info; 139 Primitive info;
140 info.name = fname.mid (m_baselen + 1); // make full path relative 140 info.name = fname.mid (m_baselen + 1); // make full path relative
141 info.name.replace ('/', '\\'); // use DOS backslashes, they're expected 141 info.name.replace ('/', '\\'); // use DOS backslashes, they're expected
142 info.cat = null; 142 info.cat = null;
196 lister->work(); 196 lister->work();
197 } 197 }
198 198
199 // ============================================================================= 199 // =============================================================================
200 // ----------------------------------------------------------------------------- 200 // -----------------------------------------------------------------------------
201 PrimitiveCategory::PrimitiveCategory (str name, QObject* parent) : 201 PrimitiveCategory::PrimitiveCategory (QString name, QObject* parent) :
202 QObject (parent), 202 QObject (parent),
203 m_Name (name) {} 203 m_Name (name) {}
204 204
205 // ============================================================================= 205 // =============================================================================
206 // ----------------------------------------------------------------------------- 206 // -----------------------------------------------------------------------------
278 278
279 if (f) 279 if (f)
280 { 280 {
281 PrimitiveCategory* cat = null; 281 PrimitiveCategory* cat = null;
282 282
283 for (str line : f) 283 for (QString line : f)
284 { 284 {
285 int colon; 285 int colon;
286 286
287 if (line.length() == 0 || line[0] == '#') 287 if (line.length() == 0 || line[0] == '#')
288 continue; 288 continue;
294 294
295 cat = new PrimitiveCategory (line); 295 cat = new PrimitiveCategory (line);
296 } 296 }
297 elif (cat != null) 297 elif (cat != null)
298 { 298 {
299 str cmd = line.left (colon); 299 QString cmd = line.left (colon);
300 ERegexType type = EFilenameRegex; 300 ERegexType type = EFilenameRegex;
301 301
302 if (cmd == "f") 302 if (cmd == "f")
303 type = EFilenameRegex; 303 type = EFilenameRegex;
304 elif (cmd == "t") 304 elif (cmd == "t")
524 return objs; 524 return objs;
525 } 525 }
526 526
527 // ============================================================================= 527 // =============================================================================
528 // ----------------------------------------------------------------------------- 528 // -----------------------------------------------------------------------------
529 static str primitiveTypeName (PrimitiveType type) 529 static QString primitiveTypeName (PrimitiveType type)
530 { 530 {
531 // Not translated as primitives are in English. 531 // Not translated as primitives are in English.
532 return type == Circle ? "Circle" : 532 return type == Circle ? "Circle" :
533 type == Cylinder ? "Cylinder" : 533 type == Cylinder ? "Cylinder" :
534 type == Disc ? "Disc" : 534 type == Disc ? "Disc" :
536 type == Ring ? "Ring" : "Cone"; 536 type == Ring ? "Ring" : "Cone";
537 } 537 }
538 538
539 // ============================================================================= 539 // =============================================================================
540 // ----------------------------------------------------------------------------- 540 // -----------------------------------------------------------------------------
541 str radialFileName (PrimitiveType type, int segs, int divs, int num) 541 QString radialFileName (PrimitiveType type, int segs, int divs, int num)
542 { 542 {
543 int numer = segs, 543 int numer = segs,
544 denom = divs; 544 denom = divs;
545 545
546 // Simplify the fractional part, but the denominator must be at least 4. 546 // Simplify the fractional part, but the denominator must be at least 4.
552 numer *= factor; 552 numer *= factor;
553 denom *= factor; 553 denom *= factor;
554 } 554 }
555 555
556 // Compose some general information: prefix, fraction, root, ring number 556 // Compose some general information: prefix, fraction, root, ring number
557 str prefix = (divs == lores) ? "" : fmt ("%1/", divs); 557 QString prefix = (divs == lores) ? "" : fmt ("%1/", divs);
558 str frac = fmt ("%1-%2", numer, denom); 558 QString frac = fmt ("%1-%2", numer, denom);
559 str root = g_radialNameRoots[type]; 559 QString root = g_radialNameRoots[type];
560 str numstr = (type == Ring || type == Cone) ? fmt ("%1", num) : ""; 560 QString numstr = (type == Ring || type == Cone) ? fmt ("%1", num) : "";
561 561
562 // Truncate the root if necessary (7-16rin4.dat for instance). 562 // Truncate the root if necessary (7-16rin4.dat for instance).
563 // However, always keep the root at least 2 characters. 563 // However, always keep the root at least 2 characters.
564 int extra = (frac.length() + numstr.length() + root.length()) - 8; 564 int extra = (frac.length() + numstr.length() + root.length()) - 8;
565 root.chop (clamp (extra, 0, 2)); 565 root.chop (clamp (extra, 0, 2));
571 // ============================================================================= 571 // =============================================================================
572 // ----------------------------------------------------------------------------- 572 // -----------------------------------------------------------------------------
573 LDDocument* generatePrimitive (PrimitiveType type, int segs, int divs, int num) 573 LDDocument* generatePrimitive (PrimitiveType type, int segs, int divs, int num)
574 { 574 {
575 // Make the description 575 // Make the description
576 str frac = str::number ((float) segs / divs); 576 QString frac = QString::number ((float) segs / divs);
577 str name = radialFileName (type, segs, divs, num); 577 QString name = radialFileName (type, segs, divs, num);
578 str descr; 578 QString descr;
579 579
580 // Ensure that there's decimals, even if they're 0. 580 // Ensure that there's decimals, even if they're 0.
581 if (frac.indexOf (".") == -1) 581 if (frac.indexOf (".") == -1)
582 frac += ".0"; 582 frac += ".0";
583 583
584 if (type == Ring || type == Cone) 584 if (type == Ring || type == Cone)
585 { 585 {
586 str spacing = 586 QString spacing =
587 (num < 10) ? " " : 587 (num < 10) ? " " :
588 (num < 100) ? " " : ""; 588 (num < 100) ? " " : "";
589 589
590 descr = fmt ("%1 %2%3 x %4", primitiveTypeName (type), spacing, num, frac); 590 descr = fmt ("%1 %2%3 x %4", primitiveTypeName (type), spacing, num, frac);
591 } 591 }
597 descr.insert (0, "Hi-Res "); 597 descr.insert (0, "Hi-Res ");
598 598
599 LDDocument* f = new LDDocument; 599 LDDocument* f = new LDDocument;
600 f->setDefaultName (name); 600 f->setDefaultName (name);
601 601
602 str author = APPNAME; 602 QString author = APPNAME;
603 str license = ""; 603 QString license = "";
604 604
605 if (ld_defaultname.isEmpty() == false) 605 if (ld_defaultname.isEmpty() == false)
606 { 606 {
607 license = getLicenseText (ld_defaultlicense); 607 license = getLicenseText (ld_defaultlicense);
608 author = fmt ("%1 [%2]", ld_defaultname, ld_defaultuser); 608 author = fmt ("%1 [%2]", ld_defaultname, ld_defaultuser);
626 626
627 // ============================================================================= 627 // =============================================================================
628 // ----------------------------------------------------------------------------- 628 // -----------------------------------------------------------------------------
629 LDDocument* getPrimitive (PrimitiveType type, int segs, int divs, int num) 629 LDDocument* getPrimitive (PrimitiveType type, int segs, int divs, int num)
630 { 630 {
631 str name = radialFileName (type, segs, divs, num); 631 QString name = radialFileName (type, segs, divs, num);
632 LDDocument* f = getDocument (name); 632 LDDocument* f = getDocument (name);
633 633
634 if (f != null) 634 if (f != null)
635 return f; 635 return f;
636 636

mercurial