src/ldDocument.cc

changeset 834
3e697ba996e8
parent 831
3cd913bf0c48
child 835
268413885cb1
equal deleted inserted replaced
833:ad681d97d341 834:3e697ba996e8
652 int idx = rfiles.indexOf (path); 652 int idx = rfiles.indexOf (path);
653 653
654 // If this file already is in the list, pop it out. 654 // If this file already is in the list, pop it out.
655 if (idx != -1) 655 if (idx != -1)
656 { 656 {
657 if (rfiles.size() == 1) 657 if (idx == rfiles.size() - 1)
658 return; // only recent file - abort and do nothing 658 return; // first recent file - abort and do nothing
659 659
660 // Pop it out.
661 rfiles.removeAt (idx); 660 rfiles.removeAt (idx);
662 } 661 }
663 662
664 // If there's too many recent files, drop one out. 663 // If there's too many recent files, drop one out.
665 while (rfiles.size() > (g_maxRecentFiles - 1)) 664 while (rfiles.size() > (g_maxRecentFiles - 1))
736 g_loadingMainFile = false; 735 g_loadingMainFile = false;
737 } 736 }
738 737
739 // ============================================================================= 738 // =============================================================================
740 // 739 //
741 bool LDDocument::save (QString savepath) 740 bool LDDocument::save (QString path, int64* sizeptr)
742 { 741 {
743 if (isImplicit()) 742 if (isImplicit())
744 return false; 743 return false;
745 744
746 if (not savepath.length()) 745 if (not path.length())
747 savepath = fullPath(); 746 path = fullPath();
748 747
749 // If the second object in the list holds the file name, update that now. 748 // If the second object in the list holds the file name, update that now.
750 LDObjectPtr nameObject = getObject (1); 749 LDObjectPtr nameObject = getObject (1);
751 750
752 if (nameObject != null && nameObject->type() == OBJ_Comment) 751 if (nameObject != null && nameObject->type() == OBJ_Comment)
753 { 752 {
754 LDCommentPtr nameComment = nameObject.staticCast<LDComment>(); 753 LDCommentPtr nameComment = nameObject.staticCast<LDComment>();
755 754
756 if (nameComment->text().left (6) == "Name: ") 755 if (nameComment->text().left (6) == "Name: ")
757 { 756 {
758 QString newname = shortenName (savepath); 757 QString newname = shortenName (path);
759 nameComment->setText (format ("Name: %1", newname)); 758 nameComment->setText (format ("Name: %1", newname));
760 g_win->buildObjList(); 759 g_win->buildObjList();
761 } 760 }
762 } 761 }
763 762
764 QByteArray data; 763 QByteArray data;
764
765 if (sizeptr != null)
766 *sizeptr = 0;
765 767
766 // File is open, now save the model to it. Note that LDraw requires files to 768 // File is open, now save the model to it. Note that LDraw requires files to
767 // have DOS line endings, so we terminate the lines with \r\n. 769 // have DOS line endings, so we terminate the lines with \r\n.
768 for (LDObjectPtr obj : objects()) 770 for (LDObjectPtr obj : objects())
769 data.append ((obj->asText() + "\r\n").toUtf8()); 771 {
770 772 QByteArray subdata ((obj->asText() + "\r\n").toUtf8());
771 QFile f (savepath); 773 data.append (subdata);
774
775 if (sizeptr != null)
776 *sizeptr += subdata.size();
777 }
778
779 QFile f (path);
772 780
773 if (not f.open (QIODevice::WriteOnly)) 781 if (not f.open (QIODevice::WriteOnly))
774 return false; 782 return false;
775 783
776 f.write (data); 784 f.write (data);
777 f.close(); 785 f.close();
778 786
779 // We have successfully saved, update the save position now. 787 // We have successfully saved, update the save position now.
780 setSavePosition (history()->position()); 788 setSavePosition (history()->position());
781 setFullPath (savepath); 789 setFullPath (path);
782 setName (shortenName (savepath)); 790 setName (shortenName (path));
783 791
784 g_win->updateDocumentListItem (self().toStrongRef()); 792 g_win->updateDocumentListItem (self().toStrongRef());
785 g_win->updateTitle(); 793 g_win->updateTitle();
786 return true; 794 return true;
787 } 795 }

mercurial