src/Primitives.cc

changeset 644
93dcd1a0e4bd
parent 642
751a8df42842
equal deleted inserted replaced
643:a79277000830 644:93dcd1a0e4bd
57 PrimitiveCategory::loadCategories(); 57 PrimitiveCategory::loadCategories();
58 58
59 // Try to load prims.cfg 59 // Try to load prims.cfg
60 QFile conf (Config::filepath ("prims.cfg")); 60 QFile conf (Config::filepath ("prims.cfg"));
61 61
62 if (!conf.open (QIODevice::ReadOnly)) 62 if (conf.open (QIODevice::ReadOnly) == false)
63 { 63 {
64 // No prims.cfg, build it 64 // No prims.cfg, build it
65 PrimitiveScanner::start(); 65 PrimitiveScanner::start();
66 } 66 }
67 else 67 else
83 info.title = line.mid (space + 1); 83 info.title = line.mid (space + 1);
84 g_primitives << info; 84 g_primitives << info;
85 } 85 }
86 86
87 PrimitiveCategory::populateCategories(); 87 PrimitiveCategory::populateCategories();
88 log ("%1 primitives loaded.\n", g_primitives.size()); 88 print ("%1 primitives loaded.\n", g_primitives.size());
89 } 89 }
90 } 90 }
91 91
92 // ============================================================================= 92 // =============================================================================
93 // 93 //
114 QDir dir (LDPaths::prims()); 114 QDir dir (LDPaths::prims());
115 assert (dir.exists()); 115 assert (dir.exists());
116 m_baselen = dir.absolutePath().length(); 116 m_baselen = dir.absolutePath().length();
117 recursiveGetFilenames (dir, m_files); 117 recursiveGetFilenames (dir, m_files);
118 emit starting (m_files.size()); 118 emit starting (m_files.size());
119 log ("Scanning primitives..."); 119 print ("Scanning primitives...");
120 } 120 }
121 121
122 // ============================================================================= 122 // =============================================================================
123 // 123 //
124 PrimitiveScanner::~PrimitiveScanner() 124 PrimitiveScanner::~PrimitiveScanner()
165 // Done with primitives, now save to a config file 165 // Done with primitives, now save to a config file
166 QString path = Config::filepath ("prims.cfg"); 166 QString path = Config::filepath ("prims.cfg");
167 QFile conf (path); 167 QFile conf (path);
168 168
169 if (!conf.open (QIODevice::WriteOnly | QIODevice::Text)) 169 if (!conf.open (QIODevice::WriteOnly | QIODevice::Text))
170 critical (fmt ("Couldn't write primitive list %1: %2", 170 critical (format ("Couldn't write primitive list %1: %2",
171 path, conf.errorString())); 171 path, conf.errorString()));
172 else 172 else
173 { 173 {
174 for (Primitive& info : m_prims) 174 for (Primitive& info : m_prims)
175 fprint (conf, "%1 %2\r\n", info.name, info.title); 175 fprint (conf, "%1 %2\r\n", info.name, info.title);
177 conf.close(); 177 conf.close();
178 } 178 }
179 179
180 g_primitives = m_prims; 180 g_primitives = m_prims;
181 PrimitiveCategory::populateCategories(); 181 PrimitiveCategory::populateCategories();
182 log ("%1 primitives scanned", g_primitives.size()); 182 print ("%1 primitives scanned", g_primitives.size());
183 g_activeScanner = null; 183 g_activeScanner = null;
184 emit workDone(); 184 emit workDone();
185 deleteLater(); 185 deleteLater();
186 } 186 }
187 else 187 else
279 279
280 QFile f (path); 280 QFile f (path);
281 281
282 if (!f.open (QIODevice::ReadOnly)) 282 if (!f.open (QIODevice::ReadOnly))
283 { 283 {
284 critical (fmt (QObject::tr ("Failed to open primitive categories: %1"), f.errorString())); 284 critical (format (QObject::tr ("Failed to open primitive categories: %1"), f.errorString()));
285 return; 285 return;
286 } 286 }
287 287
288 PrimitiveCategory* cat = null; 288 PrimitiveCategory* cat = null;
289 289
314 type = EFilenameRegex; 314 type = EFilenameRegex;
315 elif (cmd == "t") 315 elif (cmd == "t")
316 type = ETitleRegex; 316 type = ETitleRegex;
317 else 317 else
318 { 318 {
319 log (tr ("Warning: unknown command \"%1\" on line \"%2\""), cmd, line); 319 print (tr ("Warning: unknown command \"%1\" on line \"%2\""), cmd, line);
320 continue; 320 continue;
321 } 321 }
322 322
323 QRegExp regex (line.mid (colon + 1)); 323 QRegExp regex (line.mid (colon + 1));
324 RegexEntry entry = { regex, type }; 324 RegexEntry entry = { regex, type };
325 cat->regexes << entry; 325 cat->regexes << entry;
326 } 326 }
327 else 327 else
328 log ("Warning: Rules given before the first category name"); 328 print ("Warning: Rules given before the first category name");
329 } 329 }
330 330
331 if (cat->isValidToInclude()) 331 if (cat->isValidToInclude())
332 g_PrimitiveCategories << cat; 332 g_PrimitiveCategories << cat;
333 333
341 341
342 // ============================================================================= 342 // =============================================================================
343 // 343 //
344 bool PrimitiveCategory::isValidToInclude() 344 bool PrimitiveCategory::isValidToInclude()
345 { 345 {
346 if (regexes.size() == 0) 346 if (regexes.isEmpty())
347 { 347 {
348 log (tr ("Warning: category \"%1\" left without patterns"), name()); 348 print (tr ("Warning: category \"%1\" left without patterns"), name());
349 deleteLater(); 349 deleteLater();
350 return false; 350 return false;
351 } 351 }
352 352
353 return true; 353 return true;
563 numer *= factor; 563 numer *= factor;
564 denom *= factor; 564 denom *= factor;
565 } 565 }
566 566
567 // Compose some general information: prefix, fraction, root, ring number 567 // Compose some general information: prefix, fraction, root, ring number
568 QString prefix = (divs == g_lores) ? "" : fmt ("%1/", divs); 568 QString prefix = (divs == g_lores) ? "" : format ("%1/", divs);
569 QString frac = fmt ("%1-%2", numer, denom); 569 QString frac = format ("%1-%2", numer, denom);
570 QString root = g_radialNameRoots[type]; 570 QString root = g_radialNameRoots[type];
571 QString numstr = (type == Ring || type == Cone) ? fmt ("%1", num) : ""; 571 QString numstr = (type == Ring || type == Cone) ? format ("%1", num) : "";
572 572
573 // Truncate the root if necessary (7-16rin4.dat for instance). 573 // Truncate the root if necessary (7-16rin4.dat for instance).
574 // However, always keep the root at least 2 characters. 574 // However, always keep the root at least 2 characters.
575 int extra = (frac.length() + numstr.length() + root.length()) - 8; 575 int extra = (frac.length() + numstr.length() + root.length()) - 8;
576 root.chop (clamp (extra, 0, 2)); 576 root.chop (clamp (extra, 0, 2));
596 { 596 {
597 QString spacing = 597 QString spacing =
598 (num < 10) ? " " : 598 (num < 10) ? " " :
599 (num < 100) ? " " : ""; 599 (num < 100) ? " " : "";
600 600
601 descr = fmt ("%1 %2%3 x %4", primitiveTypeName (type), spacing, num, frac); 601 descr = format ("%1 %2%3 x %4", primitiveTypeName (type), spacing, num, frac);
602 } 602 }
603 else 603 else
604 descr = fmt ("%1 %2", primitiveTypeName (type), frac); 604 descr = format ("%1 %2", primitiveTypeName (type), frac);
605 605
606 // Prepend "Hi-Res" if 48/ primitive. 606 // Prepend "Hi-Res" if 48/ primitive.
607 if (divs == g_hires) 607 if (divs == g_hires)
608 descr.insert (0, "Hi-Res "); 608 descr.insert (0, "Hi-Res ");
609 609
614 QString license = ""; 614 QString license = "";
615 615
616 if (ld_defaultname.isEmpty() == false) 616 if (ld_defaultname.isEmpty() == false)
617 { 617 {
618 license = getLicenseText (ld_defaultlicense); 618 license = getLicenseText (ld_defaultlicense);
619 author = fmt ("%1 [%2]", ld_defaultname, ld_defaultuser); 619 author = format ("%1 [%2]", ld_defaultname, ld_defaultuser);
620 } 620 }
621 621
622 f->addObjects ( 622 f->addObjects (
623 { 623 {
624 new LDComment (descr), 624 new LDComment (descr),
625 new LDComment (fmt ("Name: %1", name)), 625 new LDComment (format ("Name: %1", name)),
626 new LDComment (fmt ("Author: %1", author)), 626 new LDComment (format ("Author: %1", author)),
627 new LDComment (fmt ("!LDRAW_ORG Unofficial_%1Primitive", divs == g_hires ? "48_" : "")), 627 new LDComment (format ("!LDRAW_ORG Unofficial_%1Primitive", divs == g_hires ? "48_" : "")),
628 new LDComment (license), 628 new LDComment (license),
629 new LDEmpty, 629 new LDEmpty,
630 new LDBFC (LDBFC::CertifyCCW), 630 new LDBFC (LDBFC::CertifyCCW),
631 new LDEmpty, 631 new LDEmpty,
632 }); 632 });

mercurial