File.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. /**
  20. * \defgroup ecma_file File Tools
  21. * \ingroup ecma_scripts
  22. *
  23. * \brief This module contains ECMAScript implementations of the
  24. * tools in the file menu.
  25. */
  26. include("scripts/EAction.js");
  27. /**
  28. * \class File
  29. * \brief Base class for all file menu tools.
  30. * \ingroup ecma_file
  31. */
  32. function File(guiAction) {
  33. EAction.call(this, guiAction);
  34. }
  35. File.prototype = new EAction();
  36. File.includeBasePath = includeBasePath;
  37. File.prototype.beginEvent = function() {
  38. EAction.prototype.beginEvent.call(this);
  39. if (!isNull(this.getGuiAction()) && this.getGuiAction().objectName==="FileToolsPanelAction") {
  40. EAction.showCadToolBarPanel("FileToolsPanel");
  41. this.terminate();
  42. }
  43. };
  44. File.getMenu = function() {
  45. var menu = EAction.getMenu(File.getTitle(), "FileMenu");
  46. menu.setProperty("scriptFile", File.includeBasePath + "/File.js");
  47. return menu;
  48. };
  49. File.getToolBar = function() {
  50. // var tb = EAction.getToolBar(File.getTitle(), "FileToolBar");
  51. // return tb;
  52. };
  53. File.getCadToolBarPanel = function() {
  54. var mtb = EAction.getMainCadToolBarPanel();
  55. var actionName = "FileToolsPanelAction";
  56. if (!isNull(mtb) && mtb.findChild(actionName)==undefined) {
  57. var action = new RGuiAction(qsTr("File Tools"), mtb);
  58. action.setScriptFile(File.includeBasePath + "/File.js");
  59. action.objectName = actionName;
  60. action.setRequiresDocument(false);
  61. action.setIcon(File.includeBasePath + "/File.svg");
  62. //action.setStatusTip(qsTr("Show file tools"));
  63. action.setNoState();
  64. action.setDefaultCommands(["filemenu"]);
  65. action.setGroupSortOrder(10);
  66. action.setSortOrder(100);
  67. action.setWidgetNames(["!MainToolsPanel"]);
  68. }
  69. var tb = EAction.getCadToolBarPanel(
  70. File.getTitle(),
  71. "FileToolsPanel",
  72. true
  73. );
  74. return tb;
  75. };
  76. File.getToolMatrixPanel = function() {
  77. RSettings.setValue("File/VisibleInMatrixPanel", false, false);
  78. return EAction.getToolMatrixPanel(File.getTitle(), "FileMatrixPanel", 10);
  79. };
  80. File.getTitle = function() {
  81. return qsTr("项目");
  82. // return qsTr("&File");
  83. };
  84. File.prototype.getTitle = function() {
  85. return File.getTitle();
  86. };
  87. File.init = function() {
  88. File.getMenu();
  89. File.getToolBar();
  90. File.getCadToolBarPanel();
  91. File.getToolMatrixPanel();
  92. };
  93. /**
  94. * \return Suggestion for initial file path for saving the given file or
  95. * a file related to the given file.
  96. *
  97. * \param filePath Typically the path and file name of the drawing file.
  98. * \param extension File extension to use for exported file (bmp, pdf, dxf, etc).
  99. */
  100. File.getInitialSaveAsPath = function(filePath, extension) {
  101. var ret = "";
  102. var fi;
  103. if (isNull(filePath) || filePath.length === 0) {
  104. var mdiChild = EAction.getMdiChild();
  105. var fn;
  106. if (!isNull(mdiChild)) {
  107. fn = stripDirtyFlag(EAction.getMdiChild().windowTitle)
  108. }
  109. else {
  110. fn = "File";
  111. }
  112. fi = new QFileInfo(QDir.homePath());
  113. ret = fi.absoluteFilePath() + QDir.separator + fn + "." + extension;
  114. } else {
  115. fi = new QFileInfo(filePath);
  116. ret = fi.path() + QDir.separator + fi.completeBaseName() + "." + extension;
  117. }
  118. return ret;
  119. };
  120. /**
  121. * Advanced file save as dialog with extension completion.
  122. * This is what QFileDialog.getSaveFileName should be.
  123. *
  124. * \param parentWidget Parent widget or null
  125. * \param caption Dialog caption
  126. * \param path Initial path with file name of the dialog
  127. * \param fileName Initial file name to suggest to user
  128. * \param filterStrings Array of filter strings in the format 'My File Type (*.mft *.mftype)'
  129. *
  130. * \return Array with complete file path and selected name filter or undefined.
  131. */
  132. File.getSaveFileName = function(parentWidget, caption, path, filterStrings, defaultNameFilter) {
  133. var fileDialog = new QFileDialog(parentWidget);
  134. // use native dialog:
  135. fileDialog.setOption(QFileDialog.DontUseNativeDialog, getDontUseNativeDialog());
  136. // overwrite is handled after extension has been added:
  137. fileDialog.setOption(QFileDialog.DontConfirmOverwrite, true);
  138. if (!isNull(QFileDialog.DontUseCustomDirectoryIcons)) {
  139. fileDialog.setOption(QFileDialog.DontUseCustomDirectoryIcons, true);
  140. }
  141. var fiDir = new QFileInfo(path);
  142. fileDialog.setDirectory(fiDir.absolutePath());
  143. fileDialog.fileMode = QFileDialog.AnyFile;
  144. fileDialog.acceptMode = QFileDialog.AcceptSave;
  145. filterStrings = translateFilterStrings(filterStrings);
  146. fileDialog.setNameFilters(filterStrings);
  147. // preselect configured name filter:
  148. if (isNull(defaultNameFilter) || defaultNameFilter.length===0) {
  149. // preselect default name filter DXF R27:
  150. for (var i=0; i<filterStrings.length; ++i) {
  151. if (filterStrings[i].contains("R27") && filterStrings[i].contains("*.dxf")) {
  152. fileDialog.selectNameFilter(filterStrings[i]);
  153. break;
  154. }
  155. }
  156. }
  157. else {
  158. fileDialog.selectNameFilter(defaultNameFilter);
  159. }
  160. fileDialog.selectFile(fiDir.completeBaseName());
  161. if (fiDir.suffix().length!==0) {
  162. // preselect first name filter that matches current extension:
  163. for (var i=0; i<filterStrings.length; ++i) {
  164. if (filterStrings[i].contains("*." + fiDir.suffix().toLowerCase())) {
  165. fileDialog.selectNameFilter(filterStrings[i]);
  166. break;
  167. }
  168. }
  169. }
  170. fileDialog.setLabelText(QFileDialog.FileType, qsTr("Format:"));
  171. var done = false;
  172. while (!done) {
  173. done = true;
  174. if (!fileDialog.exec()) {
  175. destr(fileDialog);
  176. EAction.activateMainWindow();
  177. return undefined;
  178. }
  179. var fileToSave = fileDialog.selectedFiles()[0];
  180. var selectedNameFilter = fileDialog.selectedNameFilter();
  181. // file, possibly without suffix:
  182. var fi = new QFileInfo(fileToSave);
  183. if (fi.suffix().length===0) {
  184. var suffix = "";
  185. var regex = /\(\*(\.[^ )]*)/i;
  186. if (regex.test(selectedNameFilter)) {
  187. var matches = selectedNameFilter.match(regex);
  188. if (matches.length==2) {
  189. suffix = selectedNameFilter.match(regex)[1];
  190. }
  191. }
  192. if (suffix.length!==0) {
  193. fileToSave += suffix;
  194. }
  195. }
  196. // file with suffix:
  197. fi = new QFileInfo(fileToSave);
  198. if (fi.exists()) {
  199. var buttons = makeQMessageBoxStandardButtons(QMessageBox.Yes, QMessageBox.No);
  200. var ret = QMessageBox.warning(parentWidget,
  201. qsTr("Overwrite File?"),
  202. qsTr("The file \"%1\" already exists. Do you wish to overwrite it?").arg(fileToSave),
  203. buttons);
  204. if (ret!=QMessageBox.Yes) {
  205. done = false;
  206. }
  207. }
  208. }
  209. return [ fileToSave, selectedNameFilter ];
  210. };
  211. /**
  212. * Show file dialog and let user choose a file to open.
  213. *
  214. * \return Array with [path and file name chosen by the user] and [filter chosen by user]
  215. */
  216. File.getOpenFileName = function(parentWidget, caption, dir, filterStrings, noAllFiles) {
  217. if (isNull(filterStrings)) {
  218. filterStrings = RFileImporterRegistry.getFilterStrings();
  219. filterStrings = translateFilterStrings(filterStrings);
  220. }
  221. if (isNull(noAllFiles)) {
  222. noAllFiles = false;
  223. }
  224. if (!noAllFiles) {
  225. filterStrings = new Array(qsTr("All Files") + " (*)").concat(filterStrings);
  226. }
  227. var fileDialog = new QFileDialog(parentWidget, caption, dir, "");
  228. var allFilter = filterStrings[0];
  229. fileDialog.setNameFilters(filterStrings);
  230. fileDialog.selectNameFilter(allFilter);
  231. fileDialog.setOption(QFileDialog.DontUseNativeDialog, getDontUseNativeDialog());
  232. if (!isNull(QFileDialog.DontUseCustomDirectoryIcons)) {
  233. fileDialog.setOption(QFileDialog.DontUseCustomDirectoryIcons, true);
  234. }
  235. fileDialog.fileMode = QFileDialog.ExistingFiles;
  236. fileDialog.setLabelText(QFileDialog.FileType, qsTr("Format:"));
  237. if (!fileDialog.exec()) {
  238. destr(fileDialog);
  239. EAction.activateMainWindow();
  240. return undefined;
  241. }
  242. var fileNames = fileDialog.selectedFiles();
  243. if (fileNames.length===0) {
  244. destr(fileDialog);
  245. EAction.activateMainWindow();
  246. return undefined;
  247. }
  248. var fileName = fileNames[0];
  249. var suffix = new QFileInfo(fileName).suffix();
  250. suffix = suffix.toUpperCase();
  251. var nameFilter = fileDialog.selectedNameFilter();
  252. destr(fileDialog);
  253. EAction.activateMainWindow();
  254. return [ fileNames[0], nameFilter ];
  255. };
  256. /**
  257. * \return True if the file version is recommendable (supports 24bit colors).
  258. * \param version String from the file version drop down in the save as dialog.
  259. */
  260. File.isRecommendedVersion = function(version) {
  261. if (version.indexOf("R2.5")!==-1) {
  262. return false;
  263. }
  264. else if (version.indexOf("R2.6")!==-1) {
  265. return false;
  266. }
  267. else if (version.indexOf("R9")!==-1) {
  268. return false;
  269. }
  270. else if (version.indexOf("R10")!==-1) {
  271. return false;
  272. }
  273. else if (version.indexOf("R12")!==-1) {
  274. return false;
  275. }
  276. else if (version.indexOf("R13")!==-1) {
  277. return false;
  278. }
  279. else if (version.indexOf("R14")!==-1 && version.indexOf("Beta")!==-1) {
  280. return false;
  281. }
  282. else if (version.indexOf("R14")!==-1) {
  283. return false;
  284. }
  285. else if (version.indexOf("R15")!==-1) {
  286. return false;
  287. }
  288. else if (version.indexOf("R18")!==-1) {
  289. return true;
  290. }
  291. else if (version.indexOf("R21")!==-1) {
  292. return true;
  293. }
  294. else if (version.indexOf("R24")!==-1) {
  295. return true;
  296. }
  297. else if (version.indexOf("R27")!==-1) {
  298. return true;
  299. }
  300. else if (version.indexOf("R32")!==-1) {
  301. return true;
  302. }
  303. else {
  304. return true;
  305. }
  306. };