Save.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
  3. *
  4. * This file is part of the QCAD project.
  5. *
  6. * QCAD is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * QCAD is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with QCAD.
  18. */
  19. include("scripts/File/File.js");
  20. include("scripts/File/AutoSave/AutoSave.js");
  21. /**
  22. * This action handles all user interaction to save the current drawing. If the current
  23. * drawing has no file name (has never been saved before), action SaveAs is triggered
  24. * instead.
  25. */
  26. function Save(guiAction) {
  27. File.call(this, guiAction);
  28. }
  29. Save.prototype = new File();
  30. Save.includeBasePath = includeBasePath;
  31. Save.prototype.beginEvent = function() {
  32. File.prototype.beginEvent.call(this);
  33. var fileName = this.getDocument().getFileName();
  34. var fileVersion = this.getDocument().getFileVersion();
  35. // not yet saved or
  36. // loaded a backup file or
  37. // save failed:
  38. // launch save as instead:
  39. if (fileName === "" || new QFileInfo(fileName).completeBaseName().startsWith("~") ||
  40. !this.save(fileName, fileVersion, false)) {
  41. include(Save.includeBasePath + "/../SaveAs/SaveAs.js");
  42. var saveAsAction = new SaveAs();
  43. this.getDocumentInterface().queueAction(saveAsAction);
  44. this.terminate();
  45. return;
  46. }
  47. this.terminate();
  48. };
  49. Save.prototype.save = function(fileName, fileVersion, overwriteWarning) {
  50. if (isNull(overwriteWarning)) {
  51. overwriteWarning = true;
  52. }
  53. var appWin = EAction.getMainWindow();
  54. var di = EAction.getDocumentInterface();
  55. var buttons, ret;
  56. // var editingWorkingSet = di.getDocument().isEditingWorkingSet();
  57. // if (editingWorkingSet) {
  58. // buttons = new QMessageBox.StandardButtons(QMessageBox.Yes, QMessageBox.Cancel);
  59. // ret = QMessageBox.warning(
  60. // appWin,
  61. // qsTr("Block editing in progress"),
  62. // qsTr("A block is currently exploded and being edited. Please save the block before saving your drawing.") + " " +
  63. // qsTr("Saving will save the block contents exploded.") + " " +
  64. // qsTr("Save anyway?"), buttons);
  65. // if (ret===QMessageBox.Cancel) {
  66. // return true;
  67. // }
  68. // }
  69. fileVersion = isNull(fileVersion) ? "" : fileVersion;
  70. fileName = di.getCorrectedFileName(fileName, fileVersion);
  71. var saveAs = false;
  72. if (overwriteWarning) {
  73. if (new QFileInfo(fileName).exists()) {
  74. buttons = makeQMessageBoxStandardButtons(QMessageBox.Yes, QMessageBox.No);
  75. ret = QMessageBox.warning(
  76. appWin,
  77. qsTr("Overwrite File?"),
  78. qsTr("The file \"%1\" already exists. Do you wish to overwrite it?").arg(fileName), buttons);
  79. if (ret!=QMessageBox.Yes) {
  80. saveAs = true;
  81. }
  82. }
  83. }
  84. var colorWarning = RSettings.getBoolValue("SaveAs/ColorWarning", true);
  85. var noFullColor = !File.isRecommendedVersion(fileVersion);
  86. if (!saveAs && noFullColor) {
  87. if (colorWarning) {
  88. // dxflib has no full color support:
  89. if (!fileVersion.contains("dxflib")) {
  90. buttons = makeQMessageBoxStandardButtons(QMessageBox.Yes, QMessageBox.No);
  91. ret = QMessageBox.warning(
  92. appWin,
  93. qsTr("File Format Version Not Recommended"),
  94. qsTr("The file format version you are using is not recommended: \"%1\".").arg(fileVersion) + "\n"
  95. + qsTr("Custom properties will not be saved.") + "\n"
  96. + qsTr("All black entities and layers will be saved as white.") + "\n"
  97. + qsTr("All custom colors will be adjusted to the nearest fixed color.") + " " + qsTr("Proceed?"), buttons);
  98. if (ret!=QMessageBox.Yes) {
  99. saveAs = true;
  100. }
  101. }
  102. }
  103. }
  104. if (saveAs) {
  105. include("scripts/File/SaveAs/SaveAs.js");
  106. var saveAsAction = new SaveAs();
  107. this.getDocumentInterface().queueAction(saveAsAction);
  108. this.terminate();
  109. return true;
  110. }
  111. appWin.setProgressText(qsTr("Saving..."));
  112. if (noFullColor) {
  113. EAction.handleUserWarning(qsTr("Saving to file format version \"%1\".").arg(fileVersion));
  114. EAction.handleUserWarning(qsTr("Black entities and layers are saved as white."));
  115. EAction.handleUserWarning(qsTr("Custom colors are adjusted to the nearest fixed color."));
  116. }
  117. var bakFileName = AutoSave.getAutoSaveFileNameCurrent();
  118. if (!di.exportFile(fileName, fileVersion)) {
  119. var text = qsTr("File %1 has not been saved.").arg(fileName);
  120. appWin.handleUserWarning(text, true);
  121. appWin.setProgressText();
  122. return false;
  123. }
  124. AutoSave.cleanUp(bakFileName);
  125. // file name might have been changed by filter (extension added):
  126. var doc = di.getDocument();
  127. fileName = doc.getFileName();
  128. fileVersion = doc.getFileVersion()
  129. var mdiChild = EAction.getMdiChild();
  130. if (!isNull(mdiChild)) {
  131. var title = new QFileInfo(fileName).fileName();
  132. title = title.replace(/&/g, "&&");
  133. mdiChild.setWindowTitle(addDirtyFlag(title));
  134. }
  135. RSettings.addRecentFile(fileName);
  136. appWin.handleUserMessage(qsTr("Saved file:") + " " + fileName);
  137. if (fileVersion.length!==0) {
  138. appWin.handleUserMessage(qsTr("Format:") + " " + fileVersion);
  139. }
  140. var tabBar = appWin.getTabBar();
  141. if (!isNull(tabBar)) {
  142. tabBar.setTabToolTip(tabBar.currentIndex, fileName);
  143. }
  144. appWin.setProgressText();
  145. return true;
  146. };