src/primitives.cpp

changeset 1055
c28d07a583ac
parent 1054
1f4c8a369e7c
child 1062
4119185b56ca
equal deleted inserted replaced
1054:1f4c8a369e7c 1055:c28d07a583ac
568 { 568 {
569 return m_name; 569 return m_name;
570 } 570 }
571 571
572 572
573 // 573 /*
574 // --------------------------------------------------------------------------------------------------------------------- 574 * PrimitiveScanner :: PrimitiveScanner
575 // 575 *
576 576 * Constructs a primitive scanner.
577 577 */
578 PrimitiveScanner::PrimitiveScanner (PrimitiveManager* parent) : 578 PrimitiveScanner::PrimitiveScanner(PrimitiveManager* parent) :
579 QObject(parent), 579 QObject(parent),
580 HierarchyElement(parent), 580 HierarchyElement(parent),
581 m_manager(parent), 581 m_manager(parent),
582 m_iterator(LDPaths::primitivesDir(), QDirIterator::Subdirectories) 582 m_iterator(LDPaths::primitivesDir(), QDirIterator::Subdirectories)
583 { 583 {
584 m_basePathLength = LDPaths::primitivesDir().absolutePath().length(); 584 m_basePathLength = LDPaths::primitivesDir().absolutePath().length();
585 print("Scanning primitives..."); 585 print("Scanning primitives...");
586 } 586 }
587 587
588 588 /*
589 const QList<Primitive>& PrimitiveScanner::scannedPrimitives() const 589 * PrimitiveScanner :: scannedPrimitives
590 { 590 *
591 return m_prims; 591 * Returns a vector containing all the primitives found.
592 } 592 */
593 593 const QVector<Primitive> &PrimitiveScanner::scannedPrimitives() const
594 594 {
595 return m_scannedPrimitives;
596 }
597
598 /*
599 * PrimitiveScanner :: work
600 *
601 * Does one step of work, processes up to 100 primitives.
602 * If the scanner does not finish work by this function call, it will ask the event loop to call this method again.
603 */
595 void PrimitiveScanner::work() 604 void PrimitiveScanner::work()
596 { 605 {
597 for (int i = 0; m_iterator.hasNext() and i < 100; ++i) 606 for (int i = 0; m_iterator.hasNext() and i < 100; ++i)
598 { 607 {
599 QString filename = m_iterator.next(); 608 QString filename = m_iterator.next();
600 QFile file (filename); 609 QFile file = {filename};
601 610
602 if (not file.open (QIODevice::ReadOnly)) 611 if (file.open (QIODevice::ReadOnly))
603 continue; 612 {
604 613 Primitive primitive;
605 Primitive primitive; 614 primitive.name = filename.mid(m_basePathLength + 1); // make full path relative
606 primitive.name = filename.mid (m_basePathLength + 1); // make full path relative 615 primitive.name.replace('/', '\\'); // use DOS backslashes, they're expected
607 primitive.name.replace ('/', '\\'); // use DOS backslashes, they're expected 616 primitive.category = nullptr;
608 primitive.category = nullptr; 617 primitive.title = QString::fromUtf8(file.readLine().simplified());
609 QByteArray titledata = file.readLine(); 618
610 619 if (primitive.title[0] == '0')
611 if (titledata != QByteArray()) 620 {
612 primitive.title = QString::fromUtf8 (titledata); 621 primitive.title.remove(0, 1); // remove 0
613 622 primitive.title = primitive.title.simplified();
614 primitive.title = primitive.title.simplified(); 623 }
615 624
616 if (primitive.title[0] == '0') 625 m_scannedPrimitives << primitive;
617 { 626 }
618 primitive.title.remove (0, 1); // remove 0
619 primitive.title = primitive.title.simplified();
620 }
621
622 m_prims << primitive;
623 } 627 }
624 628
625 if (not m_iterator.hasNext()) 629 if (not m_iterator.hasNext())
626 { 630 {
627 // Done with primitives, now save to a config file 631 // If there are no more primitives to iterate, we're done. Now save this information into a cache file.
628 QString path = m_manager->getPrimitivesCfgPath(); 632 QString path = m_manager->getPrimitivesCfgPath();
629 QFile configFile (path); 633 QFile configFile = {path};
630 634
631 if (configFile.open (QIODevice::WriteOnly | QIODevice::Text)) 635 if (configFile.open(QIODevice::WriteOnly | QIODevice::Text))
632 { 636 {
633 for (Primitive& info : m_prims) 637 for (Primitive& primitive : m_scannedPrimitives)
634 fprint (configFile, "%1 %2\r\n", info.name, info.title); 638 fprint(configFile, "%1 %2\r\n", primitive.name, primitive.title);
635 639
636 configFile.close(); 640 configFile.close();
637 } 641 }
638 else 642 else
639 { 643 {
642 646
643 emit workDone(); 647 emit workDone();
644 } 648 }
645 else 649 else
646 { 650 {
647 // Defer to event loop, pick up the work later 651 // Otherwise, defer to event loop, pick up the work later.
648 QMetaObject::invokeMethod (this, "work", Qt::QueuedConnection); 652 QMetaObject::invokeMethod (this, "work", Qt::QueuedConnection);
649 } 653 }
650 } 654 }
651 655
652 656

mercurial