src/ldDocument.cc

changeset 717
fdc285e5952f
parent 714
b4a990f59a5e
child 719
f2cc5964f52d
equal deleted inserted replaced
716:639a900999bc 717:fdc285e5952f
122 } 122 }
123 123
124 // ============================================================================= 124 // =============================================================================
125 // 125 //
126 LDDocument::LDDocument() : 126 LDDocument::LDDocument() :
127 m_flags (0),
127 m_gldata (new LDGLData) 128 m_gldata (new LDGLData)
128 { 129 {
129 setImplicit (true); 130 setImplicit (true);
130 setSavePosition (-1); 131 setSavePosition (-1);
131 setTabIndex (-1); 132 setTabIndex (-1);
139 LDDocument::~LDDocument() 140 LDDocument::~LDDocument()
140 { 141 {
141 // Remove this file from the list of files. This MUST be done FIRST, otherwise 142 // Remove this file from the list of files. This MUST be done FIRST, otherwise
142 // a ton of other functions will think this file is still valid when it is not! 143 // a ton of other functions will think this file is still valid when it is not!
143 g_loadedFiles.removeOne (this); 144 g_loadedFiles.removeOne (this);
144 145 m_flags |= DOCF_IsBeingDestroyed;
145 m_history->setIgnoring (true); 146 m_history->setIgnoring (true);
146 147
147 // Clear everything from the model 148 // Clear everything from the model
148 for (LDObject* obj : objects()) 149 for (LDObject* obj : objects())
149 obj->destroy(); 150 obj->destroy();
469 return objs; 470 return objs;
470 } 471 }
471 472
472 // ============================================================================= 473 // =============================================================================
473 // 474 //
474 LDDocument* openDocument (QString path, bool search) 475 LDDocument* openDocument (QString path, bool search, bool implicit)
475 { 476 {
476 // Convert the file name to lowercase since some parts contain uppercase 477 // Convert the file name to lowercase since some parts contain uppercase
477 // file names. I'll assume here that the library will always use lowercase 478 // file names. I'll assume here that the library will always use lowercase
478 // file names for the actual parts.. 479 // file names for the actual parts..
479 QFile* fp; 480 QFile* fp;
495 496
496 if (not fp) 497 if (not fp)
497 return null; 498 return null;
498 499
499 LDDocument* load = new LDDocument; 500 LDDocument* load = new LDDocument;
501 load->setImplicit (implicit);
500 load->setFullPath (fullpath); 502 load->setFullPath (fullpath);
501 load->setName (LDDocument::shortenName (load->fullPath())); 503 load->setName (LDDocument::shortenName (load->fullPath()));
502 dprint ("name: %1 (%2)", load->name(), load->fullPath()); 504 dprint ("name: %1 (%2)", load->name(), load->fullPath());
503 g_loadedFiles << load; 505 g_loadedFiles << load;
504 506
669 { 671 {
670 g_loadingMainFile = false; 672 g_loadingMainFile = false;
671 return; 673 return;
672 } 674 }
673 675
674 LDDocument* file = openDocument (path, false); 676 LDDocument* file = openDocument (path, false, false);
675 677
676 if (not file) 678 if (not file)
677 { 679 {
678 // Loading failed, thus drop down to a new file since we 680 // Loading failed, thus drop down to a new file since we
679 // closed everything prior. 681 // closed everything prior.
1022 // Try find the file in the list of loaded files 1024 // Try find the file in the list of loaded files
1023 LDDocument* doc = findDocument (filename); 1025 LDDocument* doc = findDocument (filename);
1024 1026
1025 // If it's not loaded, try open it 1027 // If it's not loaded, try open it
1026 if (not doc) 1028 if (not doc)
1027 doc = openDocument (filename, true); 1029 doc = openDocument (filename, true, true);
1028 1030
1029 return doc; 1031 return doc;
1030 } 1032 }
1031 1033
1032 // ============================================================================= 1034 // =============================================================================
1064 // 1066 //
1065 int LDDocument::addObject (LDObject* obj) 1067 int LDDocument::addObject (LDObject* obj)
1066 { 1068 {
1067 history()->add (new AddHistory (objects().size(), obj)); 1069 history()->add (new AddHistory (objects().size(), obj));
1068 m_objects << obj; 1070 m_objects << obj;
1069 1071 addKnownVerticesOf (obj);
1070 if (obj->type() == LDObject::EVertex)
1071 m_vertices << obj;
1072 1072
1073 #ifdef DEBUG 1073 #ifdef DEBUG
1074 if (not isImplicit()) 1074 if (not isImplicit())
1075 dprint ("Added object #%1 (%2)\n", obj->id(), obj->typeName()); 1075 dprint ("Added object #%1 (%2)\n", obj->id(), obj->typeName());
1076 #endif 1076 #endif
1095 { 1095 {
1096 history()->add (new AddHistory (pos, obj)); 1096 history()->add (new AddHistory (pos, obj));
1097 m_objects.insert (pos, obj); 1097 m_objects.insert (pos, obj);
1098 obj->setDocument (this); 1098 obj->setDocument (this);
1099 g_win->R()->compileObject (obj); 1099 g_win->R()->compileObject (obj);
1100 addKnownVerticesOf (obj);
1100 1101
1101 #ifdef DEBUG 1102 #ifdef DEBUG
1102 if (not isImplicit()) 1103 if (not isImplicit())
1103 dprint ("Inserted object #%1 (%2) at %3\n", obj->id(), obj->typeName(), pos); 1104 dprint ("Inserted object #%1 (%2) at %3\n", obj->id(), obj->typeName(), pos);
1104 #endif 1105 #endif
1105 } 1106 }
1106 1107
1107 // ============================================================================= 1108 // =============================================================================
1108 // 1109 //
1110 void LDDocument::addKnownVerticesOf (LDObject* obj)
1111 {
1112 if (isImplicit())
1113 return;
1114
1115 if (obj->type() == LDObject::ESubfile)
1116 {
1117 for (LDObject* sub : static_cast<LDSubfile*> (obj)->inlineContents (true, false))
1118 {
1119 addKnownVerticesOf (sub);
1120 sub->destroy();
1121 }
1122 }
1123 else
1124 {
1125 for (int i = 0; i < obj->vertices(); ++i)
1126 addKnownVertexReference (obj->vertex (i));
1127 }
1128 }
1129
1130 // =============================================================================
1131 //
1132 void LDDocument::removeKnownVerticesOf (LDObject* obj)
1133 {
1134 if (isImplicit())
1135 return;
1136
1137 if (obj->type() == LDObject::ESubfile)
1138 {
1139 for (LDObject* sub : static_cast<LDSubfile*> (obj)->inlineContents (true, false))
1140 {
1141 removeKnownVerticesOf (sub);
1142 sub->destroy();
1143 }
1144 }
1145 else
1146 {
1147 for (int i = 0; i < obj->vertices(); ++i)
1148 removeKnownVertexReference (obj->vertex (i));
1149 }
1150 }
1151
1152 // =============================================================================
1153 //
1109 void LDDocument::forgetObject (LDObject* obj) 1154 void LDDocument::forgetObject (LDObject* obj)
1110 { 1155 {
1111 int idx = obj->lineNumber(); 1156 int idx = obj->lineNumber();
1112 obj->unselect(); 1157 obj->unselect();
1113 assert (m_objects[idx] == obj); 1158 assert (m_objects[idx] == obj);
1114 1159
1115 if (not history()->isIgnoring()) 1160 if (not isImplicit() && not (flags() & DOCF_IsBeingDestroyed))
1161 {
1116 history()->add (new DelHistory (idx, obj)); 1162 history()->add (new DelHistory (idx, obj));
1163 removeKnownVerticesOf (obj);
1164 }
1117 1165
1118 m_objects.removeAt (idx); 1166 m_objects.removeAt (idx);
1119 obj->setDocument (null); 1167 obj->setDocument (null);
1168 }
1169
1170 // =============================================================================
1171 //
1172 void LDDocument::vertexChanged (const Vertex& a, const Vertex& b)
1173 {
1174 removeKnownVertexReference (a);
1175 addKnownVertexReference (b);
1176 }
1177
1178 // =============================================================================
1179 //
1180 void LDDocument::addKnownVertexReference (const Vertex& a)
1181 {
1182 if (isImplicit())
1183 return;
1184
1185 auto it = m_vertices.find (a);
1186
1187 if (it == m_vertices.end())
1188 m_vertices[a] = 1;
1189 else
1190 ++it.value();
1191 }
1192
1193 // =============================================================================
1194 //
1195 void LDDocument::removeKnownVertexReference (const Vertex& a)
1196 {
1197 if (isImplicit())
1198 return;
1199
1200 auto it = m_vertices.find (a);
1201 assert (it != m_vertices.end());
1202
1203 // If there's no more references to a given vertex, it is to be removed.
1204 if (--it.value() == 0)
1205 m_vertices.erase (it);
1120 } 1206 }
1121 1207
1122 // ============================================================================= 1208 // =============================================================================
1123 // 1209 //
1124 bool safeToCloseAll() 1210 bool safeToCloseAll()
1142 QString oldcode = getObject (idx)->asText(); 1228 QString oldcode = getObject (idx)->asText();
1143 QString newcode = obj->asText(); 1229 QString newcode = obj->asText();
1144 *m_history << new EditHistory (idx, oldcode, newcode); 1230 *m_history << new EditHistory (idx, oldcode, newcode);
1145 } 1231 }
1146 1232
1233 removeKnownVerticesOf (m_objects[idx]);
1147 m_objects[idx]->unselect(); 1234 m_objects[idx]->unselect();
1148 m_objects[idx]->setDocument (null); 1235 m_objects[idx]->setDocument (null);
1149 obj->setDocument (this); 1236 obj->setDocument (this);
1237 addKnownVerticesOf (obj);
1150 g_win->R()->compileObject (obj); 1238 g_win->R()->compileObject (obj);
1151 m_objects[idx] = obj; 1239 m_objects[idx] = obj;
1152 } 1240 }
1153 1241
1154 // ============================================================================= 1242 // =============================================================================
1352 return; 1440 return;
1353 1441
1354 delete g_logoedStud; 1442 delete g_logoedStud;
1355 delete g_logoedStud2; 1443 delete g_logoedStud2;
1356 1444
1357 g_logoedStud = openDocument ("stud-logo.dat", true); 1445 g_logoedStud = openDocument ("stud-logo.dat", true, true);
1358 g_logoedStud2 = openDocument ("stud2-logo.dat", true); 1446 g_logoedStud2 = openDocument ("stud2-logo.dat", true, true);
1359 1447
1360 print (LDDocument::tr ("Logoed studs loaded.\n")); 1448 print (LDDocument::tr ("Logoed studs loaded.\n"));
1361 } 1449 }
1362 1450
1363 // ============================================================================= 1451 // =============================================================================

mercurial