87 str parts () { return pathInfo.partsPath; } |
87 str parts () { return pathInfo.partsPath; } |
88 } |
88 } |
89 |
89 |
90 // ============================================================================= |
90 // ============================================================================= |
91 LDOpenFile::LDOpenFile () { |
91 LDOpenFile::LDOpenFile () { |
92 m_implicit = true; |
92 setImplicit (true); |
93 savePos = -1; |
93 setSavePos (-1); |
94 } |
94 } |
95 |
95 |
96 // ============================================================================= |
96 // ============================================================================= |
97 LDOpenFile::~LDOpenFile () { |
97 LDOpenFile::~LDOpenFile () { |
98 // Clear everything from the model |
98 // Clear everything from the model |
99 for (LDObject* obj : m_objs) |
99 for (LDObject* obj : m_objs) |
100 delete obj; |
100 delete obj; |
101 |
101 |
102 // Clear the cache as well |
102 // Clear the cache as well |
103 for (LDObject* obj : m_objCache) |
103 for (LDObject* obj : m_cache) |
104 delete obj; |
104 delete obj; |
105 } |
105 } |
106 |
106 |
107 // ============================================================================= |
107 // ============================================================================= |
108 LDOpenFile* findLoadedFile (str name) { |
108 LDOpenFile* findLoadedFile (str name) { |
109 for (LDOpenFile* file : g_loadedFiles) |
109 for (LDOpenFile* file : g_loadedFiles) |
110 if (file->m_filename == name) |
110 if (file->name () == name) |
111 return file; |
111 return file; |
112 |
112 |
113 return null; |
113 return null; |
114 } |
114 } |
115 |
115 |
145 #ifndef WIN32 |
145 #ifndef WIN32 |
146 relpath.replace ("\\", "/"); |
146 relpath.replace ("\\", "/"); |
147 #endif // WIN32 |
147 #endif // WIN32 |
148 |
148 |
149 if (g_curfile != null) { |
149 if (g_curfile != null) { |
150 str partpath = fmt ("%s" DIRSLASH "%s", dirname (g_curfile->m_filename).c (), relpath.c ()); |
150 str partpath = fmt ("%s" DIRSLASH "%s", dirname (g_curfile->name ()).c (), relpath.c ()); |
151 printf ("try %s\n", partpath.c ()); |
151 printf ("try %s\n", partpath.c ()); |
152 FILE* fp = fopen (partpath, "r"); |
152 FILE* fp = fopen (partpath, "r"); |
153 |
153 |
154 if (fp != null) |
154 if (fp != null) |
155 return fp; |
155 return fp; |
347 // ============================================================================= |
347 // ============================================================================= |
348 bool LDOpenFile::safeToClose () { |
348 bool LDOpenFile::safeToClose () { |
349 setlocale (LC_ALL, "C"); |
349 setlocale (LC_ALL, "C"); |
350 |
350 |
351 // If we have unsaved changes, warn and give the option of saving. |
351 // If we have unsaved changes, warn and give the option of saving. |
352 if (!m_implicit && History::pos () != savePos) { |
352 if (!implicit () && History::pos () != savePos ()) { |
353 switch (QMessageBox::question (g_win, "Unsaved Changes", |
353 switch (QMessageBox::question (g_win, "Unsaved Changes", |
354 fmt ("There are unsaved changes to %s. Should it be saved?", |
354 fmt ("There are unsaved changes to %s. Should it be saved?", |
355 (m_filename.len () > 0) ? m_filename.chars () : "<anonymous>"), |
355 (name ().len () > 0) ? name ().c () : "<anonymous>"), |
356 (QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel), QMessageBox::Cancel)) |
356 (QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel), QMessageBox::Cancel)) |
357 { |
357 { |
358 case QMessageBox::Yes: |
358 case QMessageBox::Yes: |
359 // If we don't have a file path yet, we have to ask the user for one. |
359 // If we don't have a file path yet, we have to ask the user for one. |
360 if (m_filename.len () == 0) { |
360 if (name ().len () == 0) { |
361 str path = QFileDialog::getSaveFileName (g_win, "Save As", |
361 str newpath = QFileDialog::getSaveFileName (g_win, "Save As", |
362 g_curfile->m_filename, "LDraw files (*.dat *.ldr)"); |
362 g_curfile->name (), "LDraw files (*.dat *.ldr)"); |
363 |
363 |
364 if (path.len () == 0) |
364 if (newpath.len () == 0) |
365 return false; |
365 return false; |
366 |
366 |
367 m_filename = path; |
367 setName (newpath); |
368 } |
368 } |
369 |
369 |
370 if (!save ()) { |
370 if (!save ()) { |
371 str errormsg = fmt ("Failed to save %s: %s\nDo you still want to close?", |
371 str errormsg = fmt ("Failed to save %s: %s\nDo you still want to close?", |
372 m_filename.chars (), strerror (lastError)); |
372 name ().c (), strerror (errno)); |
373 |
373 |
374 if (QMessageBox::critical (g_win, "Save Failure", errormsg, |
374 if (QMessageBox::critical (g_win, "Save Failure", errormsg, |
375 (QMessageBox::Yes | QMessageBox::No), QMessageBox::No) == QMessageBox::No) |
375 (QMessageBox::Yes | QMessageBox::No), QMessageBox::No) == QMessageBox::No) |
376 { |
376 { |
377 return false; |
377 return false; |
503 } |
503 } |
504 |
504 |
505 // ============================================================================= |
505 // ============================================================================= |
506 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
506 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
507 // ============================================================================= |
507 // ============================================================================= |
508 bool LDOpenFile::save (str path) { |
508 bool LDOpenFile::save (str savepath) { |
509 if (!~path) |
509 if (!~savepath) |
510 path = m_filename; |
510 savepath = name (); |
511 |
511 |
512 FILE* fp = fopen (path, "w"); |
512 FILE* fp = fopen (savepath, "w"); |
513 |
513 |
514 if (!fp) { |
514 if (!fp) |
515 lastError = errno; |
|
516 return false; |
515 return false; |
517 } |
|
518 |
516 |
519 // If the second object in the list holds the file name, update that now. |
517 // If the second object in the list holds the file name, update that now. |
520 // Only do this if the file is explicitly open. If it's saved into a directory |
518 // Only do this if the file is explicitly open. If it's saved into a directory |
521 // called "s" or "48", prepend that into the name. |
519 // called "s" or "48", prepend that into the name. |
522 LDComment* fpathComment = null; |
520 LDComment* fpathComment = null; |
523 if (m_implicit == false && m_objs.size () >= 2 && object (1)->getType () == LDObject::Comment) { |
521 if (!implicit () && objs ().size () >= 2 && object (1)->getType () == LDObject::Comment) { |
524 fpathComment = static_cast<LDComment*> (object (1)); |
522 fpathComment = static_cast<LDComment*> (object (1)); |
525 |
523 |
526 if (fpathComment->text.substr (0, 6) == "Name: ") { |
524 if (fpathComment->text.substr (0, 6) == "Name: ") { |
527 str newfname; |
525 str newfname; |
528 str dir = basename (dirname (path)); |
526 str dir = basename (dirname (savepath)); |
529 |
527 |
530 if (dir == "s" || dir == "48") |
528 if (dir == "s" || dir == "48") |
531 newfname = fmt ("%s\\", dir.c ()); |
529 newfname = fmt ("%s\\", dir.c ()); |
532 |
530 |
533 newfname += basename (path); |
531 newfname += basename (savepath); |
534 fpathComment->text = fmt ("Name: %s", newfname.c ()); |
532 fpathComment->text = fmt ("Name: %s", newfname.c ()); |
535 g_win->buildObjList (); |
533 g_win->buildObjList (); |
536 } |
534 } |
537 } |
535 } |
538 |
536 |
539 // Write all entries now |
537 // Write all entries now |
540 for (LDObject* obj : m_objs) { |
538 for (LDObject* obj : objs ()) { |
541 // LDraw requires files to have DOS line endings |
539 // LDraw requires files to have DOS line endings |
542 str line = fmt ("%s\r\n", obj->getContents ().chars ()); |
540 str line = fmt ("%s\r\n", obj->getContents ().chars ()); |
543 fwrite (line.chars(), 1, line.len (), fp); |
541 fwrite (line.chars(), 1, line.len (), fp); |
544 } |
542 } |
545 |
543 |
546 fclose (fp); |
544 fclose (fp); |
547 |
545 |
548 // We have successfully saved, update the save position now. |
546 // We have successfully saved, update the save position now. |
549 savePos = History::pos (); |
547 setSavePos (History::pos ()); |
550 m_filename = path; |
548 setName (savepath); |
551 |
549 |
552 g_win->updateTitle (); |
550 g_win->updateTitle (); |
553 return true; |
551 return true; |
554 } |
552 } |
555 |
553 |
794 |
792 |
795 g_loadedFiles.clear (); |
793 g_loadedFiles.clear (); |
796 g_loadedFiles.push_back (g_curfile); |
794 g_loadedFiles.push_back (g_curfile); |
797 |
795 |
798 // Go through all objects in the current file and reload the subfiles |
796 // Go through all objects in the current file and reload the subfiles |
799 for (LDObject* obj : g_curfile->m_objs) { |
797 for (LDObject* obj : g_curfile->objs ()) { |
800 if (obj->getType() == LDObject::Subfile) { |
798 if (obj->getType() == LDObject::Subfile) { |
801 // Note: ref->fileInfo is invalid right now since all subfiles were closed. |
799 // Note: ref->fileInfo is invalid right now since all subfiles were closed. |
802 LDSubfile* ref = static_cast<LDSubfile*> (obj); |
800 LDSubfile* ref = static_cast<LDSubfile*> (obj); |
803 LDOpenFile* fileInfo = loadSubfile (ref->fileName); |
801 LDOpenFile* fileInfo = loadSubfile (ref->fileName); |
804 |
802 |
824 m_objs.push_back (obj); |
822 m_objs.push_back (obj); |
825 |
823 |
826 if (this == g_curfile) |
824 if (this == g_curfile) |
827 g_BBox.calcObject (obj); |
825 g_BBox.calcObject (obj); |
828 |
826 |
829 return m_objs.size() - 1; |
827 return numObjs () - 1; |
830 } |
828 } |
831 |
829 |
832 // ============================================================================= |
830 // ============================================================================= |
833 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
831 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
834 // ============================================================================= |
832 // ============================================================================= |