src/primitives.cpp

changeset 1283
3c3a5eb965f7
parent 1261
5d2c9d36da9d
child 1284
7da74697b7d2
equal deleted inserted replaced
1282:c6bc95cbf4a3 1283:3c3a5eb965f7
31 #include "linetypes/empty.h" 31 #include "linetypes/empty.h"
32 #include "linetypes/quadrilateral.h" 32 #include "linetypes/quadrilateral.h"
33 #include "linetypes/triangle.h" 33 #include "linetypes/triangle.h"
34 34
35 PrimitiveManager::PrimitiveManager(QObject* parent) : 35 PrimitiveManager::PrimitiveManager(QObject* parent) :
36 QObject(parent), 36 QAbstractItemModel {parent},
37 HierarchyElement(parent), 37 HierarchyElement {parent},
38 m_activeScanner(nullptr), 38 m_activeScanner {nullptr},
39 m_unmatched(nullptr) {} 39 m_unmatched {nullptr} {}
40 40
41 41
42 PrimitiveScanner* PrimitiveManager::activeScanner() 42 PrimitiveScanner* PrimitiveManager::activeScanner()
43 { 43 {
44 return m_activeScanner; 44 return m_activeScanner;
93 connect(m_activeScanner, &PrimitiveScanner::workDone, this, [&]() 93 connect(m_activeScanner, &PrimitiveScanner::workDone, this, [&]()
94 { 94 {
95 if (m_activeScanner) 95 if (m_activeScanner)
96 { 96 {
97 m_primitives = m_activeScanner->scannedPrimitives(); 97 m_primitives = m_activeScanner->scannedPrimitives();
98 emit layoutAboutToBeChanged();
98 populateCategories(); 99 populateCategories();
99 print(tr("%1 primitives scanned"), countof(m_primitives)); 100 print(tr("%1 primitives scanned"), countof(m_primitives));
100 delete m_activeScanner; 101 delete m_activeScanner;
101 m_activeScanner = nullptr; 102 m_activeScanner = nullptr;
103 emit layoutChanged();
102 } 104 }
103 }); 105 });
104 } 106 }
105 } 107 }
106 108
512 514
513 return document; 515 return document;
514 } 516 }
515 517
516 /* 518 /*
517 * PrimitiveManager :: populateTreeWidget 519 * Returns the amount of columns in the primitives tree (1)
518 * 520 */
519 * Fills in a tree widget with all known primitives. 521 int PrimitiveManager::columnCount(const QModelIndex&) const
520 */ 522 {
521 void PrimitiveManager::populateTreeWidget(QTreeWidget* tree, const QString& selectByDefault) 523 return 1;
522 { 524 }
523 tree->clear(); 525
524 526 /*
525 for (PrimitiveCategory* category : m_categories) 527 * For an index that points to a primitive, returns the category that contains it
526 { 528 */
527 PrimitiveTreeItem* parentItem = new PrimitiveTreeItem {tree, nullptr}; 529 static PrimitiveCategory* categoryForPrimitiveIndex(const QModelIndex& primitiveIndex)
528 parentItem->setText(0, category->name()); 530 {
529 //QList<QTreeWidgetItem*> subfileItems; 531 return static_cast<PrimitiveCategory*>(primitiveIndex.internalPointer());
530 532 }
531 for (Primitive& primitive : category->primitives) 533
532 { 534 /*
533 PrimitiveTreeItem* item = new PrimitiveTreeItem {parentItem, &primitive}; 535 * Returns data from the tree model.
534 item->setText(0, format("%1 - %2", primitive.name, primitive.title)); 536 */
535 //subfileItems.append(item); 537 QVariant PrimitiveManager::data(const QModelIndex& index, int role) const
536 538 {
537 // If this primitive is the one the current object points to, 539 if (index.isValid())
538 // select it by default 540 {
539 if (selectByDefault == primitive.name) 541 if (categoryForPrimitiveIndex(index) != nullptr)
540 tree->setCurrentItem(item); 542 {
541 } 543 // Index points to a primitive, return primitive information.
542 544 Primitive& primitive = categoryForPrimitiveIndex(index)->primitives[index.row()];
543 tree->addTopLevelItem(parentItem); 545
544 } 546 switch(role)
545 } 547 {
546 548 case Qt::DisplayRole:
549 return format("%1 - %2", primitive.name, primitive.title);
550 case Qt::DecorationRole:
551 return MainWindow::getIcon("subfilereference");
552 default:
553 return {};
554 }
555 }
556 else
557 {
558 // Index points to a category, return category information.
559 PrimitiveCategory* category = this->m_categories[index.row()];
560
561 switch (role)
562 {
563 case Qt::DisplayRole:
564 return category->name();
565 case Qt::DecorationRole:
566 return MainWindow::getIcon("folder");
567 default:
568 return {};
569 }
570 }
571 }
572 else
573 {
574 // Index is invalid.
575 return {};
576 }
577 }
578
579 /*
580 * For a row and parent index, returns a child index.
581 */
582 QModelIndex PrimitiveManager::index(int row, int, const QModelIndex& parent) const
583 {
584 if (parent.isValid())
585 {
586 if (categoryForPrimitiveIndex(parent))
587 {
588 // Parent is a primitive index. Primitives cannot have children so return an
589 // invalid index.
590 return {};
591 }
592 else
593 {
594 // Parent is a category, return an index to a primitive
595 PrimitiveCategory* category = m_categories[parent.row()];
596
597 // Create an index inside the category
598 if (row >= 0 and row < category->primitives.size())
599 return this->createIndex(row, 0, category);
600 else
601 return {};
602 }
603 }
604 else
605 {
606 // Create a top-level index pointing to a category
607 if (row >= 0 and row < this->m_categories.size())
608 return this->createIndex(row, 0, nullptr);
609 else
610 return {};
611 }
612 }
613
614 /*
615 * For a primitive index, find the category index that contains it.
616 */
617 QModelIndex PrimitiveManager::parent(const QModelIndex &index) const
618 {
619 int row = this->m_categories.indexOf(categoryForPrimitiveIndex(index));
620
621 if (row != -1)
622 return this->createIndex(row, 0, nullptr);
623 else
624 return {};
625 }
626
627 /*
628 * Returns the amount of rows contained inside the given index.
629 */
630 int PrimitiveManager::rowCount(const QModelIndex& parent) const
631 {
632 if (parent.isValid())
633 {
634 if (categoryForPrimitiveIndex(parent))
635 {
636 // Primitives don't have child nodes, so return 0.
637 return 0;
638 }
639 else
640 {
641 // For categories, return the amount of primitives contained.
642 return this->m_categories[parent.row()]->primitives.size();
643 }
644 }
645 else
646 {
647 // For top-level, return the amount of categories.
648 return this->m_categories.size();
649 }
650 }
651
652 /*
653 * Returns a static "Primitives" text for the header.
654 */
655 QVariant PrimitiveManager::headerData(int section, Qt::Orientation, int role) const
656 {
657 if (section == 0 and role == Qt::DisplayRole)
658 return tr("Primitives");
659 else
660 return {};
661 }
547 662
548 // 663 //
549 // --------------------------------------------------------------------------------------------------------------------- 664 // ---------------------------------------------------------------------------------------------------------------------
550 // 665 //
551 666
624 } 739 }
625 740
626 if (not m_iterator.hasNext()) 741 if (not m_iterator.hasNext())
627 { 742 {
628 // If there are no more primitives to iterate, we're done. Now save this information into a cache file. 743 // If there are no more primitives to iterate, we're done. Now save this information into a cache file.
744 std::sort(
745 m_scannedPrimitives.begin(),
746 m_scannedPrimitives.end(),
747 [](const Primitive& one, const Primitive& other) -> bool
748 {
749 return one.title < other.title;
750 }
751 );
629 QString path = m_manager->getPrimitivesCfgPath(); 752 QString path = m_manager->getPrimitivesCfgPath();
630 QFile configFile = {path}; 753 QFile configFile = {path};
631 754
632 if (configFile.open(QIODevice::WriteOnly | QIODevice::Text)) 755 if (configFile.open(QIODevice::WriteOnly | QIODevice::Text))
633 { 756 {

mercurial