124 // ============================================================================= |
124 // ============================================================================= |
125 // |
125 // |
126 LDDocument::LDDocument (LDDocumentPtr* selfptr) : |
126 LDDocument::LDDocument (LDDocumentPtr* selfptr) : |
127 m_isImplicit (true), |
127 m_isImplicit (true), |
128 m_flags (0), |
128 m_flags (0), |
|
129 _verticesOutdated (true), |
|
130 _needVertexMerge (true), |
129 m_gldata (new LDGLData) |
131 m_gldata (new LDGLData) |
130 { |
132 { |
131 *selfptr = LDDocumentPtr (this); |
133 *selfptr = LDDocumentPtr (this); |
132 setSelf (*selfptr); |
134 setSelf (*selfptr); |
133 setSavePosition (-1); |
135 setSavePosition (-1); |
1092 // |
1094 // |
1093 int LDDocument::addObject (LDObjectPtr obj) |
1095 int LDDocument::addObject (LDObjectPtr obj) |
1094 { |
1096 { |
1095 history()->add (new AddHistory (objects().size(), obj)); |
1097 history()->add (new AddHistory (objects().size(), obj)); |
1096 m_objects << obj; |
1098 m_objects << obj; |
1097 addKnownVerticesOf (obj); |
1099 addKnownVertices (obj); |
1098 |
1100 |
1099 #ifdef DEBUG |
1101 #ifdef DEBUG |
1100 if (not isImplicit()) |
1102 if (not isImplicit()) |
1101 dprint ("Added object #%1 (%2)\n", obj->id(), obj->typeName()); |
1103 dprint ("Added object #%1 (%2)\n", obj->id(), obj->typeName()); |
1102 #endif |
1104 #endif |
1106 return getObjectCount() - 1; |
1108 return getObjectCount() - 1; |
1107 } |
1109 } |
1108 |
1110 |
1109 // ============================================================================= |
1111 // ============================================================================= |
1110 // |
1112 // |
1111 void LDDocument::addObjects (const LDObjectList objs) |
1113 void LDDocument::addObjects (const LDObjectList& objs) |
1112 { |
1114 { |
1113 for (LDObjectPtr obj : objs) |
1115 for (LDObjectPtr obj : objs) |
1114 if (obj) |
1116 { |
|
1117 if (obj != null) |
1115 addObject (obj); |
1118 addObject (obj); |
|
1119 } |
1116 } |
1120 } |
1117 |
1121 |
1118 // ============================================================================= |
1122 // ============================================================================= |
1119 // |
1123 // |
1120 void LDDocument::insertObj (int pos, LDObjectPtr obj) |
1124 void LDDocument::insertObj (int pos, LDObjectPtr obj) |
1121 { |
1125 { |
1122 history()->add (new AddHistory (pos, obj)); |
1126 history()->add (new AddHistory (pos, obj)); |
1123 m_objects.insert (pos, obj); |
1127 m_objects.insert (pos, obj); |
1124 obj->setDocument (this); |
1128 obj->setDocument (this); |
1125 g_win->R()->compileObject (obj); |
1129 g_win->R()->compileObject (obj); |
1126 addKnownVerticesOf (obj); |
1130 |
1127 |
1131 |
1128 #ifdef DEBUG |
1132 #ifdef DEBUG |
1129 if (not isImplicit()) |
1133 if (not isImplicit()) |
1130 dprint ("Inserted object #%1 (%2) at %3\n", obj->id(), obj->typeName(), pos); |
1134 dprint ("Inserted object #%1 (%2) at %3\n", obj->id(), obj->typeName(), pos); |
1131 #endif |
1135 #endif |
1132 } |
1136 } |
1133 |
1137 |
1134 // ============================================================================= |
1138 // ============================================================================= |
1135 // |
1139 // |
1136 void LDDocument::addKnownVerticesOf (LDObjectPtr obj) |
1140 void LDDocument::addKnownVertices (LDObjectPtr obj) |
1137 { |
1141 { |
1138 if (isImplicit()) |
1142 auto it = _objectVertices.find (obj); |
1139 return; |
1143 |
1140 |
1144 if (it == _objectVertices.end()) |
1141 if (obj->type() == OBJ_Subfile) |
1145 it = _objectVertices.insert (obj, QVector<Vertex>()); |
1142 { |
|
1143 LDSubfilePtr ref = obj.staticCast<LDSubfile>(); |
|
1144 |
|
1145 for (Vertex vrt : ref->fileInfo()->inlineVertices()) |
|
1146 { |
|
1147 vrt.transform (ref->transform(), ref->position()); |
|
1148 addKnownVertexReference (vrt); |
|
1149 } |
|
1150 } |
|
1151 else |
1146 else |
1152 { |
1147 it->clear(); |
1153 for (int i = 0; i < obj->numVertices(); ++i) |
1148 |
1154 addKnownVertexReference (obj->vertex (i)); |
1149 obj->getVertices (*it); |
1155 } |
1150 needVertexMerge(); |
1156 } |
|
1157 |
|
1158 // ============================================================================= |
|
1159 // |
|
1160 void LDDocument::removeKnownVerticesOf (LDObjectPtr obj) |
|
1161 { |
|
1162 if (isImplicit()) |
|
1163 return; |
|
1164 |
|
1165 if (obj->type() == OBJ_Subfile) |
|
1166 { |
|
1167 LDSubfilePtr ref = obj.staticCast<LDSubfile>(); |
|
1168 |
|
1169 for (Vertex vrt : ref->fileInfo()->inlineVertices()) |
|
1170 { |
|
1171 vrt.transform (ref->transform(), ref->position()); |
|
1172 removeKnownVertexReference (vrt); |
|
1173 } |
|
1174 } |
|
1175 else |
|
1176 { |
|
1177 for (int i = 0; i < obj->numVertices(); ++i) |
|
1178 removeKnownVertexReference (obj->vertex (i)); |
|
1179 } |
|
1180 } |
1151 } |
1181 |
1152 |
1182 // ============================================================================= |
1153 // ============================================================================= |
1183 // |
1154 // |
1184 void LDDocument::forgetObject (LDObjectPtr obj) |
1155 void LDDocument::forgetObject (LDObjectPtr obj) |
1188 assert (m_objects[idx] == obj); |
1159 assert (m_objects[idx] == obj); |
1189 |
1160 |
1190 if (not isImplicit() && not (flags() & DOCF_IsBeingDestroyed)) |
1161 if (not isImplicit() && not (flags() & DOCF_IsBeingDestroyed)) |
1191 { |
1162 { |
1192 history()->add (new DelHistory (idx, obj)); |
1163 history()->add (new DelHistory (idx, obj)); |
1193 removeKnownVerticesOf (obj); |
1164 _objectVertices.remove (obj); |
1194 } |
1165 } |
1195 |
1166 |
1196 m_objects.removeAt (idx); |
1167 m_objects.removeAt (idx); |
1197 obj->setDocument (LDDocumentPtr()); |
1168 obj->setDocument (LDDocumentPtr()); |
1198 } |
|
1199 |
|
1200 // ============================================================================= |
|
1201 // |
|
1202 void LDDocument::vertexChanged (const Vertex& a, const Vertex& b) |
|
1203 { |
|
1204 removeKnownVertexReference (a); |
|
1205 addKnownVertexReference (b); |
|
1206 } |
|
1207 |
|
1208 // ============================================================================= |
|
1209 // |
|
1210 void LDDocument::addKnownVertexReference (const Vertex& a) |
|
1211 { |
|
1212 if (isImplicit()) |
|
1213 return; |
|
1214 |
|
1215 auto it = m_vertices.find (a); |
|
1216 |
|
1217 if (it == m_vertices.end()) |
|
1218 m_vertices[a] = 1; |
|
1219 else |
|
1220 ++it.value(); |
|
1221 } |
|
1222 |
|
1223 // ============================================================================= |
|
1224 // |
|
1225 void LDDocument::removeKnownVertexReference (const Vertex& a) |
|
1226 { |
|
1227 if (isImplicit()) |
|
1228 return; |
|
1229 |
|
1230 auto it = m_vertices.find (a); |
|
1231 assert (it != m_vertices.end()); |
|
1232 |
|
1233 // If there's no more references to a given vertex, it is to be removed. |
|
1234 if (--it.value() == 0) |
|
1235 m_vertices.erase (it); |
|
1236 } |
1169 } |
1237 |
1170 |
1238 // ============================================================================= |
1171 // ============================================================================= |
1239 // |
1172 // |
1240 bool safeToCloseAll() |
1173 bool safeToCloseAll() |
1260 QString oldcode = getObject (idx)->asText(); |
1193 QString oldcode = getObject (idx)->asText(); |
1261 QString newcode = obj->asText(); |
1194 QString newcode = obj->asText(); |
1262 *m_history << new EditHistory (idx, oldcode, newcode); |
1195 *m_history << new EditHistory (idx, oldcode, newcode); |
1263 } |
1196 } |
1264 |
1197 |
1265 removeKnownVerticesOf (m_objects[idx]); |
1198 _objectVertices.remove (m_objects[idx]); |
1266 m_objects[idx]->deselect(); |
1199 m_objects[idx]->deselect(); |
1267 m_objects[idx]->setDocument (LDDocumentPtr()); |
1200 m_objects[idx]->setDocument (LDDocumentPtr()); |
1268 obj->setDocument (this); |
1201 obj->setDocument (this); |
1269 addKnownVerticesOf (obj); |
1202 addKnownVertices (obj); |
1270 g_win->R()->compileObject (obj); |
1203 g_win->R()->compileObject (obj); |
1271 m_objects[idx] = obj; |
1204 m_objects[idx] = obj; |
1272 } |
1205 needVertexMerge(); |
1273 |
1206 } |
1274 // ============================================================================= |
|
1275 // |
|
1276 // Close all documents we don't need anymore |
|
1277 // |
|
1278 void LDDocument::closeUnused() {} |
|
1279 |
1207 |
1280 // ============================================================================= |
1208 // ============================================================================= |
1281 // |
1209 // |
1282 LDObjectPtr LDDocument::getObject (int pos) const |
1210 LDObjectPtr LDDocument::getObject (int pos) const |
1283 { |
1211 { |
1316 |
1244 |
1317 // ============================================================================= |
1245 // ============================================================================= |
1318 // |
1246 // |
1319 void LDDocument::initializeCachedData() |
1247 void LDDocument::initializeCachedData() |
1320 { |
1248 { |
1321 if (not m_needsReCache) |
1249 if (m_needsReCache) |
1322 return; |
1250 { |
1323 |
1251 _vertices.clear(); |
1324 m_storedVertices.clear(); |
1252 |
1325 |
1253 for (LDObjectPtr obj : inlineContents (true, true)) |
1326 for (LDObjectPtr obj : inlineContents (true, true)) |
1254 { |
1327 { |
1255 assert (obj->type() != OBJ_Subfile); |
1328 assert (obj->type() != OBJ_Subfile); |
1256 LDPolygon* data = obj->getPolygon(); |
1329 LDPolygon* data = obj->getPolygon(); |
1257 |
1330 |
1258 if (data != null) |
1331 if (data != null) |
1259 { |
1332 { |
1260 m_polygonData << *data; |
1333 m_polygonData << *data; |
1261 delete data; |
1334 delete data; |
1262 } |
1335 } |
1263 } |
1336 |
1264 |
1337 for (int i = 0; i < obj->numVertices(); ++i) |
1265 m_needsReCache = false; |
1338 m_storedVertices << obj->vertex (i); |
1266 } |
1339 } |
1267 |
1340 |
1268 if (_verticesOutdated) |
1341 removeDuplicates (m_storedVertices); |
1269 { |
1342 m_needsReCache = false; |
1270 _objectVertices.clear(); |
|
1271 |
|
1272 for (LDObjectPtr obj : inlineContents (true, false)) |
|
1273 addKnownVertices (obj); |
|
1274 |
|
1275 mergeVertices(); |
|
1276 _verticesOutdated = false; |
|
1277 } |
|
1278 |
|
1279 if (_needVertexMerge) |
|
1280 mergeVertices(); |
|
1281 } |
|
1282 |
|
1283 // ============================================================================= |
|
1284 // |
|
1285 void LDDocument::mergeVertices() |
|
1286 { |
|
1287 _vertices.clear(); |
|
1288 |
|
1289 for (QVector<Vertex> const& verts : _objectVertices) |
|
1290 _vertices << verts; |
|
1291 |
|
1292 removeDuplicates (_vertices); |
|
1293 _needVertexMerge = false; |
1343 } |
1294 } |
1344 |
1295 |
1345 // ============================================================================= |
1296 // ============================================================================= |
1346 // |
1297 // |
1347 QList<LDPolygon> LDDocument::inlinePolygons() |
1298 QList<LDPolygon> LDDocument::inlinePolygons() |