BitmapExport.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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("BitmapExportWorker.js");
  21. include("scripts/Tools/arguments.js");
  22. function BitmapExport(guiAction) {
  23. File.call(this, guiAction);
  24. }
  25. BitmapExport.prototype = new File();
  26. BitmapExport.includeBasePath = includeBasePath;
  27. BitmapExport.prototype.beginEvent = function() {
  28. File.prototype.beginEvent.call(this);
  29. var bmpFileName = undefined;
  30. var properties = [];
  31. if (!isNull(this.guiAction)) {
  32. // get output name from command line:
  33. bmpFileName = getArgument(this.guiAction.getArguments(), "-o", "", undefined);
  34. properties = [];
  35. properties["width"] = getIntArgument(this.guiAction.getArguments(), "-x", "", 640);
  36. properties["height"] = getIntArgument(this.guiAction.getArguments(), "-y", "", 480);
  37. properties["margin"] = 10;
  38. properties["antialiasing"] = false;
  39. }
  40. if (isNull(bmpFileName)) {
  41. // get file name / properties from dialogs:
  42. bmpFileName = this.getFilename();
  43. if (isNull(bmpFileName)) {
  44. // file dialog cancelled:
  45. this.terminate();
  46. return;
  47. }
  48. properties = this.getProperties();
  49. if (isNull(properties)) {
  50. // properties dialog cancelled:
  51. this.terminate();
  52. return;
  53. }
  54. }
  55. if (isNull(bmpFileName)) {
  56. this.terminate();
  57. return;
  58. }
  59. var res = this.exportBitmap(this.getDocumentInterface(), bmpFileName, properties);
  60. var appWin = EAction.getMainWindow();
  61. if (!res[0]) {
  62. print("Error: cannot save file: ", bmpFileName);
  63. print("Error: ", res[1]);
  64. appWin.handleUserWarning(
  65. qsTr("Error while generating bitmap file \"%1\": %2")
  66. .arg(bmpFileName).arg(res[1]));
  67. }
  68. else {
  69. appWin.handleUserMessage(
  70. qsTr("Bitmap file has been exported to \"%1\"").arg(bmpFileName));
  71. }
  72. this.terminate();
  73. };
  74. BitmapExport.prototype.getFilename = function() {
  75. var fi;
  76. var drawingFileName = this.getDocument().getFileName();
  77. var lastUsedExtension = RSettings.getStringValue("BitmapExport/Extension", "bmp");
  78. var initialPath = File.getInitialSaveAsPath(drawingFileName, lastUsedExtension);
  79. var formats = QImageWriter.supportedImageFormats();
  80. var filters = [];
  81. var filterAllImages = "";
  82. for (var i=0; i<formats.length; ++i) {
  83. var format = formats[i];
  84. var formatAlt = "";
  85. if (format==="jpg" || format==="tif") {
  86. continue;
  87. }
  88. if (format==="jpeg") {
  89. formatAlt = "jpg";
  90. }
  91. else if (format==="tiff") {
  92. formatAlt = "tif";
  93. }
  94. //if (filters.length!==0) {
  95. // filters += ";;";
  96. //}
  97. var filter;
  98. if (RSettings.getQtVersion()>=0x060000) {
  99. filter = format.toUpperCase();
  100. }
  101. else {
  102. filter = format.toUpper();
  103. }
  104. filter += " " + qsTr("Files") + " (";
  105. if (formatAlt.length!==0) {
  106. filter += "*." + formatAlt + " ";
  107. }
  108. filter += "*." + format;
  109. filter += ")";
  110. filters.push(filter);
  111. }
  112. var appWin = EAction.getMainWindow();
  113. var ret = File.getSaveFileName(appWin, qsTr("Export as Bitmap"), initialPath, filters);
  114. if (isNull(ret)) {
  115. return undefined;
  116. }
  117. var fileName = ret[0];
  118. if (isNull(fileName) || fileName.length===0) {
  119. return undefined;
  120. }
  121. RSettings.setValue("BitmapExport/Extension", new QFileInfo(fileName).suffix());
  122. return fileName;
  123. };
  124. BitmapExport.prototype.getProperties = function() {
  125. var appWin = EAction.getMainWindow();
  126. this.dialog = WidgetFactory.createDialog(BitmapExport.includeBasePath, "BitmapExportDialog.ui", appWin);
  127. WidgetFactory.restoreState(this.dialog);
  128. var widthEdit = this.dialog.findChild("Width");
  129. var heightEdit = this.dialog.findChild("Height");
  130. var resolutionCombo = this.dialog.findChild("Resolution");
  131. var marginCombo = this.dialog.findChild("Margin");
  132. var whiteRadio = this.dialog.findChild("WhiteBackground");
  133. var blackRadio = this.dialog.findChild("BlackBackground");
  134. var transparentRadio = this.dialog.findChild("TransparentBackground");
  135. var monoRadio = this.dialog.findChild("ColorMonochrome");
  136. var grayRadio = this.dialog.findChild("ColorGrayscale");
  137. var colorRadio = this.dialog.findChild("ColorFull");
  138. var antiAliasingCheckbox = this.dialog.findChild("AntiAliasing");
  139. var selectionCheckbox = this.dialog.findChild("Selection");
  140. selectionCheckbox.toggled.connect(this, this.selectionChanged);
  141. var weightMarginCheckbox = this.dialog.findChild("WeightMargin");
  142. widthEdit.valueChanged.connect(
  143. function() {
  144. resolutionCombo.index = 0;
  145. });
  146. heightEdit.valueChanged.connect(
  147. function() {
  148. resolutionCombo.index = 0;
  149. });
  150. resolutionCombo.editTextChanged.connect(this, this.resolutionChanged);
  151. this.resolutionChanged(resolutionCombo.currentText);
  152. if (!this.dialog.exec()) {
  153. destr(this.dialog);
  154. EAction.activateMainWindow();
  155. return undefined;
  156. }
  157. WidgetFactory.saveState(this.dialog);
  158. var ret = [];
  159. if (resolutionCombo.currentText!=="auto") {
  160. ret["resolution"] = parseInt(resolutionCombo.currentText);
  161. }
  162. else {
  163. ret["width"] = Math.ceil(widthEdit.getValue());
  164. ret["height"] = Math.ceil(heightEdit.getValue());
  165. }
  166. if (whiteRadio.checked) {
  167. ret["backgroundColor"] = new QColor("white");
  168. }
  169. else if (blackRadio.checked) {
  170. ret["backgroundColor"] = new QColor("black");
  171. }
  172. else {
  173. ret["backgroundColor"] = new QColor("#000000ff");
  174. }
  175. ret["margin"] = RMath.eval(marginCombo.currentText);
  176. ret["antialiasing"] = antiAliasingCheckbox.checked;
  177. if (monoRadio.checked) {
  178. ret["monochrome"] = true;
  179. }
  180. if (grayRadio.checked) {
  181. ret["grayscale"] = true;
  182. }
  183. ret["noWeightMargin"] = !weightMarginCheckbox.checked;
  184. if (selectionCheckbox.checked) {
  185. var doc = this.getDocument();
  186. ret["entityIds"] = doc.querySelectedEntities();
  187. }
  188. destr(this.dialog);
  189. EAction.activateMainWindow();
  190. return ret;
  191. };
  192. BitmapExport.prototype.selectionChanged = function(value) {
  193. this.documentWidth = undefined;
  194. this.documentHeight = undefined;
  195. var resolutionCombo = this.dialog.findChild("Resolution");
  196. this.resolutionChanged(resolutionCombo.currentText);
  197. };
  198. BitmapExport.prototype.resolutionChanged = function(str) {
  199. if (str==="auto") {
  200. return;
  201. }
  202. var res = parseInt(str, 10);
  203. if (isNaN(res)) {
  204. return;
  205. }
  206. var widthEdit = this.dialog.findChild("Width");
  207. var heightEdit = this.dialog.findChild("Height");
  208. var selectionCheckbox = this.dialog.findChild("Selection");
  209. if (isNull(this.documentWidth) || isNull(this.documentHeight)) {
  210. var document = this.getDocument();
  211. var bb;
  212. if (selectionCheckbox.checked) {
  213. bb = document.getSelectionBox();
  214. }
  215. else {
  216. bb = document.getBoundingBox(true, true);
  217. }
  218. this.documentWidth = bb.getWidth();
  219. this.documentHeight = bb.getHeight();
  220. }
  221. widthEdit.setValue(Math.ceil(res * this.documentWidth));
  222. heightEdit.setValue(Math.ceil(res * this.documentHeight));
  223. };
  224. BitmapExport.prototype.exportBitmap = function(di, fileName, properties) {
  225. // make sure nothing is selected:
  226. di.deselectAll();
  227. var view = di.getLastKnownViewWithFocus();
  228. var scene = view.getScene();
  229. var ret = exportBitmap(di.getDocument(), scene, fileName, properties);
  230. // restore selection before export:
  231. if (!isNull(properties["entityids"])) {
  232. di.selectEntities(properties["entityids"]);
  233. }
  234. return ret;
  235. };