294 |
294 |
295 for (; i < max && i < (int) lines().size(); ++i) |
295 for (; i < max && i < (int) lines().size(); ++i) |
296 { str line = lines() [i]; |
296 { str line = lines() [i]; |
297 |
297 |
298 // Trim the trailing newline |
298 // Trim the trailing newline |
299 qchar c; |
299 QChar c; |
300 |
300 |
301 while (!line.isEmpty() && ((c = line[line.length() - 1]) == '\n' || c == '\r')) |
301 while (!line.isEmpty() && ((c = line[line.length() - 1]) == '\n' || c == '\r')) |
302 line.chop (1); |
302 line.chop (1); |
303 |
303 |
304 LDObject* obj = parseLine (line); |
304 LDObject* obj = parseLine (line); |
437 return load; |
437 return load; |
438 } |
438 } |
439 |
439 |
440 // ============================================================================= |
440 // ============================================================================= |
441 // ----------------------------------------------------------------------------- |
441 // ----------------------------------------------------------------------------- |
442 bool LDFile::safeToClose() |
442 bool LDFile::isSafeToClose() |
443 { typedef QMessageBox msgbox; |
443 { typedef QMessageBox msgbox; |
444 setlocale (LC_ALL, "C"); |
444 setlocale (LC_ALL, "C"); |
445 |
445 |
446 // If we have unsaved changes, warn and give the option of saving. |
446 // If we have unsaved changes, warn and give the option of saving. |
447 if (hasUnsavedChanges()) |
447 if (hasUnsavedChanges()) |
509 LDFile::setCurrent (f); |
509 LDFile::setCurrent (f); |
510 |
510 |
511 LDFile::closeInitialFile(); |
511 LDFile::closeInitialFile(); |
512 |
512 |
513 g_win->R()->setFile (f); |
513 g_win->R()->setFile (f); |
514 g_win->fullRefresh(); |
514 g_win->doFullRefresh(); |
515 g_win->updateTitle(); |
515 g_win->updateTitle(); |
516 f->history().updateActions(); |
516 f->history().updateActions(); |
517 } |
517 } |
518 |
518 |
519 // ============================================================================= |
519 // ============================================================================= |
570 // (aside of the one we just opened), close it now. |
570 // (aside of the one we just opened), close it now. |
571 LDFile::closeInitialFile(); |
571 LDFile::closeInitialFile(); |
572 |
572 |
573 // Rebuild the object tree view now. |
573 // Rebuild the object tree view now. |
574 LDFile::setCurrent (file); |
574 LDFile::setCurrent (file); |
575 g_win->fullRefresh(); |
575 g_win->doFullRefresh(); |
576 |
576 |
577 // Add it to the recent files list. |
577 // Add it to the recent files list. |
578 addRecentFile (path); |
578 addRecentFile (path); |
579 g_loadingMainFile = false; |
579 g_loadingMainFile = false; |
580 } |
580 } |
592 |
592 |
593 // If the second object in the list holds the file name, update that now. |
593 // If the second object in the list holds the file name, update that now. |
594 // Only do this if the file is explicitly open. If it's saved into a directory |
594 // Only do this if the file is explicitly open. If it's saved into a directory |
595 // called "s" or "48", prepend that into the name. |
595 // called "s" or "48", prepend that into the name. |
596 LDComment* fpathComment = null; |
596 LDComment* fpathComment = null; |
597 LDObject* first = object (1); |
597 LDObject* first = getObject (1); |
598 |
598 |
599 if (!implicit() && first != null && first->getType() == LDObject::Comment) |
599 if (!implicit() && first != null && first->getType() == LDObject::Comment) |
600 { fpathComment = static_cast<LDComment*> (first); |
600 { fpathComment = static_cast<LDComment*> (first); |
601 |
601 |
602 if (fpathComment->text.left (6) == "Name: ") |
602 if (fpathComment->text.left (6) == "Name: ") |
678 while (comm[0] == ' ') |
678 while (comm[0] == ' ') |
679 comm.remove (0, 1); |
679 comm.remove (0, 1); |
680 |
680 |
681 // Handle BFC statements |
681 // Handle BFC statements |
682 if (tokens.size() > 2 && tokens[1] == "BFC") |
682 if (tokens.size() > 2 && tokens[1] == "BFC") |
683 { for (short i = 0; i < LDBFC::NumStatements; ++i) |
683 { for (int i = 0; i < LDBFC::NumStatements; ++i) |
684 if (comm == fmt ("BFC %1", LDBFC::statements [i])) |
684 if (comm == fmt ("BFC %1", LDBFC::statements [i])) |
685 return new LDBFC ( (LDBFC::Type) i); |
685 return new LDBFC ( (LDBFC::Type) i); |
686 |
686 |
687 // MLCAD is notorious for stuffing these statements in parts it |
687 // MLCAD is notorious for stuffing these statements in parts it |
688 // creates. The above block only handles valid statements, so we |
688 // creates. The above block only handles valid statements, so we |
760 obj->setColor (tokens[1].toLong()); |
760 obj->setColor (tokens[1].toLong()); |
761 obj->setPosition (parseVertex (tokens, 2)); // 2 - 4 |
761 obj->setPosition (parseVertex (tokens, 2)); // 2 - 4 |
762 |
762 |
763 matrix transform; |
763 matrix transform; |
764 |
764 |
765 for (short i = 0; i < 9; ++i) |
765 for (int i = 0; i < 9; ++i) |
766 transform[i] = tokens[i + 5].toDouble(); // 5 - 13 |
766 transform[i] = tokens[i + 5].toDouble(); // 5 - 13 |
767 |
767 |
768 obj->setTransform (transform); |
768 obj->setTransform (transform); |
769 obj->setFileInfo (load); |
769 obj->setFileInfo (load); |
770 return obj; |
770 return obj; |
790 |
790 |
791 // Triangle |
791 // Triangle |
792 LDTriangle* obj = new LDTriangle; |
792 LDTriangle* obj = new LDTriangle; |
793 obj->setColor (tokens[1].toLong()); |
793 obj->setColor (tokens[1].toLong()); |
794 |
794 |
795 for (short i = 0; i < 3; ++i) |
795 for (int i = 0; i < 3; ++i) |
796 obj->setVertex (i, parseVertex (tokens, 2 + (i * 3))); // 2 - 10 |
796 obj->setVertex (i, parseVertex (tokens, 2 + (i * 3))); // 2 - 10 |
797 |
797 |
798 return obj; |
798 return obj; |
799 } |
799 } |
800 |
800 |
876 |
876 |
877 if (obj->getType() == LDObject::Vertex) |
877 if (obj->getType() == LDObject::Vertex) |
878 m_vertices << obj; |
878 m_vertices << obj; |
879 |
879 |
880 obj->setFile (this); |
880 obj->setFile (this); |
881 return numObjs() - 1; |
881 return getObjectCount() - 1; |
882 } |
882 } |
883 |
883 |
884 // ============================================================================= |
884 // ============================================================================= |
885 // ----------------------------------------------------------------------------- |
885 // ----------------------------------------------------------------------------- |
886 void LDFile::addObjects (const QList<LDObject*> objs) |
886 void LDFile::addObjects (const QList<LDObject*> objs) |
908 |
908 |
909 // ============================================================================= |
909 // ============================================================================= |
910 // ----------------------------------------------------------------------------- |
910 // ----------------------------------------------------------------------------- |
911 bool safeToCloseAll() |
911 bool safeToCloseAll() |
912 { for (LDFile* f : g_loadedFiles) |
912 { for (LDFile* f : g_loadedFiles) |
913 if (!f->safeToClose()) |
913 if (!f->isSafeToClose()) |
914 return false; |
914 return false; |
915 |
915 |
916 return true; |
916 return true; |
917 } |
917 } |
918 |
918 |
919 // ============================================================================= |
919 // ============================================================================= |
920 // ----------------------------------------------------------------------------- |
920 // ----------------------------------------------------------------------------- |
921 void LDFile::setObject (int idx, LDObject* obj) |
921 void LDFile::setObject (int idx, LDObject* obj) |
922 { assert (idx < numObjs()); |
922 { assert (idx < m_objects.size()); |
923 |
923 |
924 // Mark this change to history |
924 // Mark this change to history |
925 str oldcode = object (idx)->raw(); |
925 str oldcode = getObject (idx)->raw(); |
926 str newcode = obj->raw(); |
926 str newcode = obj->raw(); |
927 m_history << new EditHistory (idx, oldcode, newcode); |
927 m_history << new EditHistory (idx, oldcode, newcode); |
928 |
928 |
929 obj->setFile (this); |
929 obj->setFile (this); |
930 m_objects[idx] = obj; |
930 m_objects[idx] = obj; |
933 // ============================================================================= |
933 // ============================================================================= |
934 // ----------------------------------------------------------------------------- |
934 // ----------------------------------------------------------------------------- |
935 static QList<LDFile*> getFilesUsed (LDFile* node) |
935 static QList<LDFile*> getFilesUsed (LDFile* node) |
936 { QList<LDFile*> filesUsed; |
936 { QList<LDFile*> filesUsed; |
937 |
937 |
938 for (LDObject * obj : node->objects()) |
938 for (LDObject* obj : node->objects()) |
939 { if (obj->getType() != LDObject::Subfile) |
939 { if (obj->getType() != LDObject::Subfile) |
940 continue; |
940 continue; |
941 |
941 |
942 LDSubfile* ref = static_cast<LDSubfile*> (obj); |
942 LDSubfile* ref = static_cast<LDSubfile*> (obj); |
943 filesUsed << ref->fileInfo(); |
943 filesUsed << ref->fileInfo(); |
980 g_loadedFiles << filesUsed; |
980 g_loadedFiles << filesUsed; |
981 } |
981 } |
982 |
982 |
983 // ============================================================================= |
983 // ============================================================================= |
984 // ----------------------------------------------------------------------------- |
984 // ----------------------------------------------------------------------------- |
985 LDObject* LDFile::object (int pos) const |
985 LDObject* LDFile::getObject (int pos) const |
986 { if (m_objects.size() <= pos) |
986 { if (m_objects.size() <= pos) |
987 return null; |
987 return null; |
988 |
988 |
989 return m_objects[pos]; |
989 return m_objects[pos]; |
990 } |
990 } |
991 |
991 |
992 // ============================================================================= |
992 // ============================================================================= |
993 // ----------------------------------------------------------------------------- |
993 // ----------------------------------------------------------------------------- |
994 LDObject* LDFile::obj (int pos) const |
994 int LDFile::getObjectCount() const |
995 { return object (pos); |
|
996 } |
|
997 |
|
998 // ============================================================================= |
|
999 // ----------------------------------------------------------------------------- |
|
1000 int LDFile::numObjs() const |
|
1001 { return objects().size(); |
995 { return objects().size(); |
1002 } |
996 } |
1003 |
997 |
1004 // ============================================================================= |
998 // ============================================================================= |
1005 // ----------------------------------------------------------------------------- |
999 // ----------------------------------------------------------------------------- |