src/ldDocument.cpp

changeset 1017
fc1c13db9618
parent 1015
92c6ec099075
child 1031
55c0d3beea0d
equal deleted inserted replaced
1016:3b279b5e57d3 1017:fc1c13db9618
36 36
37 LDDocument::LDDocument (DocumentManager* parent) : 37 LDDocument::LDDocument (DocumentManager* parent) :
38 QObject (parent), 38 QObject (parent),
39 HierarchyElement (parent), 39 HierarchyElement (parent),
40 m_history (new EditHistory (this)), 40 m_history (new EditHistory (this)),
41 m_isCache (true), 41 m_flags(IsCache | VerticesOutdated | NeedsVertexMerge | NeedsRecache),
42 m_verticesOutdated (true),
43 m_needVertexMerge (true),
44 m_needsReCache(true),
45 m_beingDestroyed (false),
46 m_needRecount(false),
47 m_savePosition(-1), 42 m_savePosition(-1),
48 m_tabIndex(-1), 43 m_tabIndex(-1),
49 m_triangleCount(0), 44 m_triangleCount(0),
50 m_gldata (new LDGLData), 45 m_gldata (new LDGLData),
51 m_manager (parent) {} 46 m_manager (parent) {}
52 47
53 LDDocument::~LDDocument() 48 LDDocument::~LDDocument()
54 { 49 {
55 m_beingDestroyed = true; 50 m_flags |= IsBeingDestroyed;
56 51
57 for (int i = 0; i < m_objects.size(); ++i) 52 for (int i = 0; i < m_objects.size(); ++i)
58 m_objects[i]->destroy(); 53 m_objects[i]->destroy();
59 54
60 delete m_history; 55 delete m_history;
126 m_defaultName = value; 121 m_defaultName = value;
127 } 122 }
128 123
129 int LDDocument::triangleCount() 124 int LDDocument::triangleCount()
130 { 125 {
131 if (m_needRecount) 126 if (checkFlag(NeedsTriangleRecount))
132 { 127 {
133 m_triangleCount = 0; 128 m_triangleCount = 0;
134 129
135 for (LDObject* obj : m_objects) 130 for (LDObject* obj : m_objects)
136 m_triangleCount += obj->triangleCount(); 131 m_triangleCount += obj->triangleCount();
137 132
138 m_needRecount = false; 133 unsetFlag(NeedsTriangleRecount);
139 } 134 }
140 135
141 return m_triangleCount; 136 return m_triangleCount;
142 } 137 }
143 138
144 void LDDocument::openForEditing() 139 void LDDocument::openForEditing()
145 { 140 {
146 if (m_isCache) 141 if (isCache())
147 { 142 {
148 m_isCache = false; 143 m_flags &= ~IsCache;
149 print ("Opened %1", name()); 144 print ("Opened %1", name());
150 145
151 // Cache files are not compiled by the GL renderer. Now that this file is open for editing, it needs to be 146 // Cache files are not compiled by the GL renderer. Now that this file is open for editing, it needs to be
152 // compiled. 147 // compiled.
153 m_window->renderer()->compiler()->compileDocument (this); 148 m_window->renderer()->compiler()->compileDocument (this);
155 } 150 }
156 } 151 }
157 152
158 bool LDDocument::isCache() const 153 bool LDDocument::isCache() const
159 { 154 {
160 return m_isCache; 155 return !!(m_flags & IsCache);
161 } 156 }
162 157
163 void LDDocument::addHistoryStep() 158 void LDDocument::addHistoryStep()
164 { 159 {
165 history()->addStep(); 160 history()->addStep();
187 182
188 void LDDocument::close() 183 void LDDocument::close()
189 { 184 {
190 if (not isCache()) 185 if (not isCache())
191 { 186 {
192 m_isCache = true; 187 m_flags |= IsCache;
193 print ("Closed %1", name()); 188 print ("Closed %1", name());
194 m_window->updateDocumentList(); 189 m_window->updateDocumentList();
195 190
196 // If the current document just became implicit (i.e. user closed it), we need to get a new one to show. 191 // If the current document just became implicit (i.e. user closed it), we need to get a new one to show.
197 if (currentDocument() == this) 192 if (currentDocument() == this)
598 // of loading errors. Circumstances may be different now. 593 // of loading errors. Circumstances may be different now.
599 if (obj->type() == OBJ_Error) 594 if (obj->type() == OBJ_Error)
600 obj->replace (ParseLine (static_cast<LDError*> (obj)->contents())); 595 obj->replace (ParseLine (static_cast<LDError*> (obj)->contents()));
601 } 596 }
602 597
603 m_needsReCache = true; 598 m_flags |= NeedsRecache;
604 599
605 if (this == m_window->currentDocument()) 600 if (this == m_window->currentDocument())
606 m_window->buildObjectList(); 601 m_window->buildObjectList();
607 } 602 }
608 603
612 { 607 {
613 history()->add (new AddHistoryEntry (objects().size(), obj)); 608 history()->add (new AddHistoryEntry (objects().size(), obj));
614 m_objects << obj; 609 m_objects << obj;
615 addKnownVertices (obj); 610 addKnownVertices (obj);
616 obj->setDocument (this); 611 obj->setDocument (this);
617 m_needRecount = true; 612 setFlag(NeedsTriangleRecount);
618 m_window->renderer()->compileObject (obj); 613 m_window->renderer()->compileObject (obj);
619 return getObjectCount() - 1; 614 return getObjectCount() - 1;
620 } 615 }
621 616
622 // ============================================================================= 617 // =============================================================================
630 } 625 }
631 } 626 }
632 627
633 // ============================================================================= 628 // =============================================================================
634 // 629 //
635 void LDDocument::insertObj (int pos, LDObject* obj) 630 void LDDocument::insertObject (int pos, LDObject* obj)
636 { 631 {
637 history()->add (new AddHistoryEntry (pos, obj)); 632 history()->add (new AddHistoryEntry (pos, obj));
638 m_objects.insert (pos, obj); 633 m_objects.insert (pos, obj);
639 obj->setDocument (this); 634 obj->setDocument (this);
640 m_needRecount = true; 635 setFlag(NeedsTriangleRecount);
641 m_window->renderer()->compileObject (obj); 636 m_window->renderer()->compileObject (obj);
642 637
643 638
644 #ifdef DEBUG 639 #ifdef DEBUG
645 if (not isCache()) 640 if (not isCache())
670 665
671 if (m_objects[idx] == obj) 666 if (m_objects[idx] == obj)
672 { 667 {
673 obj->deselect(); 668 obj->deselect();
674 669
675 if (not isCache() and not m_beingDestroyed) 670 if (not isCache() and not checkFlag(IsBeingDestroyed))
676 { 671 {
677 history()->add (new DelHistoryEntry (idx, obj)); 672 history()->add (new DelHistoryEntry (idx, obj));
678 m_objectVertices.remove (obj); 673 m_objectVertices.remove (obj);
679 } 674 }
680 675
681 m_objects.removeAt (idx); 676 m_objects.removeAt (idx);
682 m_needRecount = true; 677 setFlag(NeedsTriangleRecount);
683 obj->setDocument (nullptr); 678 obj->setDocument (nullptr);
684 } 679 }
685 } 680 }
686 681
687 // ============================================================================= 682 // =============================================================================
688 // 683 //
689 void LDDocument::setObject (int idx, LDObject* obj) 684 void LDDocument::setObjectAt (int idx, LDObject* obj)
690 { 685 {
691 if (idx < 0 or idx >= m_objects.size()) 686 if (idx < 0 or idx >= m_objects.size())
692 return; 687 return;
693 688
694 // Mark this change to history 689 // Mark this change to history
748 743
749 // ============================================================================= 744 // =============================================================================
750 // 745 //
751 void LDDocument::initializeCachedData() 746 void LDDocument::initializeCachedData()
752 { 747 {
753 if (m_needsReCache) 748 if (checkFlag(NeedsRecache))
754 { 749 {
755 m_vertices.clear(); 750 m_vertices.clear();
756 751
757 for (LDObject* obj : inlineContents (true, true)) 752 for (LDObject* obj : inlineContents (true, true))
758 { 753 {
771 m_polygonData << *data; 766 m_polygonData << *data;
772 delete data; 767 delete data;
773 } 768 }
774 } 769 }
775 770
776 m_needsReCache = false; 771 unsetFlag(NeedsRecache);
777 } 772 }
778 773
779 if (m_verticesOutdated) 774 if (checkFlag(VerticesOutdated))
780 { 775 {
781 m_objectVertices.clear(); 776 m_objectVertices.clear();
782 777
783 for (LDObject* obj : inlineContents (true, false)) 778 for (LDObject* obj : inlineContents (true, false))
784 addKnownVertices (obj); 779 addKnownVertices (obj);
785 780
786 mergeVertices(); 781 mergeVertices();
787 m_verticesOutdated = false; 782 unsetFlag(VerticesOutdated);
788 } 783 }
789 784
790 if (m_needVertexMerge) 785 if (checkFlag(NeedsVertexMerge))
791 mergeVertices(); 786 mergeVertices();
792 } 787 }
793 788
794 // ============================================================================= 789 // =============================================================================
795 // 790 //
799 794
800 for (QVector<Vertex> const& verts : m_objectVertices) 795 for (QVector<Vertex> const& verts : m_objectVertices)
801 m_vertices << verts; 796 m_vertices << verts;
802 797
803 removeDuplicates (m_vertices); 798 removeDuplicates (m_vertices);
804 m_needVertexMerge = false; 799 unsetFlag(NeedsVertexMerge);
805 } 800 }
806 801
807 // ============================================================================= 802 // =============================================================================
808 // 803 //
809 QList<LDPolygon> LDDocument::inlinePolygons() 804 QList<LDPolygon> LDDocument::inlinePolygons()
919 return m_vertices; 914 return m_vertices;
920 } 915 }
921 916
922 void LDDocument::redoVertices() 917 void LDDocument::redoVertices()
923 { 918 {
924 m_verticesOutdated = true; 919 setFlag(VerticesOutdated);
925 } 920 }
926 921
927 void LDDocument::needVertexMerge() 922 void LDDocument::needVertexMerge()
928 { 923 {
929 m_needVertexMerge = true; 924 setFlag(NeedsVertexMerge);
930 } 925 }
931 926
932 void LDDocument::needRecount() 927 void LDDocument::recountTriangles()
933 { 928 {
934 m_needRecount = true; 929 setFlag(NeedsTriangleRecount);
935 } 930 }

mercurial