Image.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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_draw_image Image Drawing Tool
  21. * \ingroup ecma_draw
  22. *
  23. * \brief This module contains the ECMAScript implementation of the image
  24. * drawing tool.
  25. */
  26. include("scripts/Draw/Draw.js");
  27. /**
  28. * \class Image
  29. * \brief Inserts an image (bitmap) into the drawing.
  30. * \ingroup ecma_draw_image
  31. */
  32. function Image(guiAction) {
  33. Draw.call(this, guiAction);
  34. this.fileName = undefined;
  35. this.image = undefined;
  36. this.width = undefined;
  37. this.height = undefined;
  38. this.angle = undefined;
  39. this.pos = undefined;
  40. if (!isNull(guiAction)) {
  41. var url = this.guiAction.data();
  42. if (!isNull(url) && isFunction(url.isLocalFile) && url.isLocalFile()) {
  43. this.fileName = url.toLocalFile();
  44. }
  45. }
  46. this.setUiOptions(Image.includeBasePath + "/Image.ui");
  47. }
  48. Image.State = {
  49. SettingPosition : 0
  50. };
  51. Image.prototype = new Draw();
  52. Image.includeBasePath = includeBasePath;
  53. Image.prototype.beginEvent = function() {
  54. Draw.prototype.beginEvent.call(this);
  55. if (isNull(this.fileName)) {
  56. this.fileName = this.getFileName();
  57. }
  58. if (isNull(this.fileName)) {
  59. this.terminate();
  60. return;
  61. }
  62. this.image = new RImageEntity(
  63. this.getDocument(),
  64. new RImageData(this.fileName,
  65. new RVector(0,0),
  66. new RVector(1,0),
  67. new RVector(0,1),
  68. 50, 50, 0)
  69. );
  70. // initial size is image size in pixels (i.e. one pixel -> one unit):
  71. this.width = this.image.getPixelWidth();
  72. this.height = this.image.getPixelHeight();
  73. var optionsToolBar = EAction.getOptionsToolBar();
  74. var widthEdit = optionsToolBar.findChild("Width");
  75. var heightEdit = optionsToolBar.findChild("Height");
  76. widthEdit.setValue(this.width);
  77. heightEdit.setValue(this.height);
  78. this.setState(Image.State.SettingPosition);
  79. };
  80. Image.prototype.finishEvent = function() {
  81. Draw.prototype.finishEvent.call(this);
  82. if (!isNull(this.image)) {
  83. destr(this.image);
  84. }
  85. };
  86. Image.isSupportedBitmapFile = function(filePath) {
  87. var formats = Image.getSupportedFormats(false);
  88. var fi = new QFileInfo(filePath);
  89. return formats.indexOf(fi.suffix().toLowerCase())!==-1;
  90. };
  91. Image.getSupportedFormats = function(noAlias) {
  92. var ret = [];
  93. var formats = QImageReader.supportedImageFormats();
  94. for (var i=0; i<formats.length; ++i) {
  95. var format = formats[i].toString();
  96. //var formatAlt = "";
  97. // ignore format aliases:
  98. if (noAlias && (format==="jpg" || format==="tif")) {
  99. continue;
  100. }
  101. // ignore unsupported formats:
  102. if (format==="ico" || format==="mng" ||
  103. format==="pbm" || format==="pgm" || format==="ppm" ||
  104. format==="svg" || format==="svgz" ||
  105. format==="xbm" || format==="xpm") {
  106. continue;
  107. }
  108. // if (format==="jpeg") {
  109. // formatAlt = "jpg";
  110. // }
  111. // else if (format==="tiff") {
  112. // formatAlt = "tif";
  113. // }
  114. ret.push(format);
  115. }
  116. return ret;
  117. };
  118. Image.prototype.getFileName = function() {
  119. var lastOpenFileDir = RSettings.getStringValue("Image/Path", RSettings.getDocumentsLocation());
  120. var formats = Image.getSupportedFormats(true);
  121. var filters = [];
  122. var filterAllImages = "";
  123. for (var i=0; i<formats.length; ++i) {
  124. var format = formats[i];
  125. var formatAlt = "";
  126. // ignore format aliases:
  127. // if (format=="jpg" ||
  128. // format=="tif") {
  129. // continue;
  130. // }
  131. // ignore unsupported formats:
  132. // if (format=="ico" || format=="mng" ||
  133. // format=="pbm" || format=="pgm" || format=="ppm" ||
  134. // format=="svg" || format=="svgz" ||
  135. // format=="xbm" || format=="xpm") {
  136. // continue;
  137. // }
  138. if (format==="jpeg") {
  139. formatAlt = "jpg";
  140. }
  141. else if (format==="tiff") {
  142. formatAlt = "tif";
  143. }
  144. // if (filters.length!==0) {
  145. // filters += ";;";
  146. // }
  147. var filter = format.toUpperCase();
  148. filter += " " + qsTr("Files") + " (";
  149. filter += "*." + format;
  150. if (filterAllImages.length!==0) {
  151. filterAllImages += " ";
  152. }
  153. filterAllImages += "*." + format;
  154. if (formatAlt.length!==0) {
  155. filter += " *." + formatAlt;
  156. filterAllImages += " *." + formatAlt;
  157. }
  158. filter += ")";
  159. filters.push(filter);
  160. }
  161. // default filter (first) is for all image files:
  162. var allFilter = qsTr("All Image Files (%1)").arg(filterAllImages);
  163. filters.push(allFilter);
  164. filters.push(qsTr("All Files") + " (*)");
  165. // Shorter, but crashes under XUbuntu:
  166. //var fileName = QFileDialog.getOpenFileName(this, qsTr("Import Bitmap"),
  167. // initialPath, filters);
  168. var appWin = EAction.getMainWindow();
  169. var fileDialog = new QFileDialog(appWin, qsTr("Import Bitmap"), lastOpenFileDir, "");
  170. fileDialog.setNameFilters(filters);
  171. fileDialog.selectNameFilter(allFilter);
  172. fileDialog.setOption(QFileDialog.DontUseNativeDialog, getDontUseNativeDialog());
  173. if (!isNull(QFileDialog.DontUseCustomDirectoryIcons)) {
  174. fileDialog.setOption(QFileDialog.DontUseCustomDirectoryIcons, true);
  175. }
  176. fileDialog.fileMode = QFileDialog.ExistingFiles;
  177. fileDialog.setLabelText(QFileDialog.FileType, qsTr("Format:"));
  178. if (!fileDialog.exec()) {
  179. destr(fileDialog);
  180. EAction.activateMainWindow();
  181. return undefined;
  182. }
  183. var files = fileDialog.selectedFiles();
  184. if (files.length===0) {
  185. destr(fileDialog);
  186. EAction.activateMainWindow();
  187. return undefined;
  188. }
  189. RSettings.setValue("Image/Path", fileDialog.directory().absolutePath());
  190. return files[0];
  191. };
  192. Image.prototype.setState = function(state) {
  193. Draw.prototype.setState.call(this, state);
  194. this.setCrosshairCursor();
  195. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  196. var appWin = RMainWindowQt.getMainWindow();
  197. var tr = qsTr("Position");
  198. this.setLeftMouseTip(tr);
  199. this.setCommandPrompt(tr);
  200. this.setRightMouseTip(EAction.trCancel);
  201. EAction.showSnapTools();
  202. };
  203. Image.prototype.pickCoordinate = function(event, preview) {
  204. this.pos = event.getModelPosition();
  205. var op = this.getOperation();
  206. if (!isNull(op)) {
  207. if (preview) {
  208. this.updatePreview();
  209. }
  210. else {
  211. this.getDocumentInterface().setRelativeZero(this.pos);
  212. this.applyOperation();
  213. }
  214. }
  215. };
  216. Image.prototype.getOperation = function(preview) {
  217. if (isNull(this.image)) {
  218. return undefined;
  219. }
  220. if (isNull(this.width) || isNull(this.height) || isNull(this.angle) || isNull(this.pos)) {
  221. return undefined;
  222. }
  223. this.image.setInsertionPoint(this.pos);
  224. this.image.setWidth(this.width);
  225. this.image.setHeight(this.height);
  226. this.image.setAngle(this.angle);
  227. return new RAddObjectOperation(this.image.clone(), this.getToolTitle());
  228. };
  229. Image.prototype.slotKeepProportionsChanged = function(value) {
  230. var optionsToolBar = EAction.getOptionsToolBar();
  231. var widthEdit = optionsToolBar.findChild("Width");
  232. var heightEdit = optionsToolBar.findChild("Height");
  233. //var heightLabel = optionsToolBar.findChild("HeightLabel");
  234. var keepProportionsSwitch = optionsToolBar.findChild("KeepProportions");
  235. if (value===true) {
  236. //heightEdit.enabled = false;
  237. //heightLabel.enabled = false;
  238. keepProportionsSwitch.icon = new QIcon(Image.includeBasePath + "/KeepProportionsOn.svg");
  239. // adjust height to width:
  240. var w = this.image.getPixelWidth();
  241. var f = widthEdit.getValue() / w;
  242. var h = this.image.getPixelHeight();
  243. heightEdit.setValue(h * f);
  244. }
  245. else {
  246. //heightEdit.enabled = true;
  247. //heightLabel.enabled = true;
  248. keepProportionsSwitch.icon = new QIcon(Image.includeBasePath + "/KeepProportionsOff.svg");
  249. }
  250. this.updatePreview(true);
  251. }
  252. Image.prototype.slotWidthChanged = function(value) {
  253. if (isNaN(value)) {
  254. return;
  255. }
  256. var optionsToolBar = EAction.getOptionsToolBar();
  257. var heightEdit = optionsToolBar.findChild("Height");
  258. var keepProportionsSwitch = optionsToolBar.findChild("KeepProportions");
  259. var keepProportions = keepProportionsSwitch.checked;
  260. this.width = value;
  261. if (!isNull(this.image)) {
  262. this.image.setWidth(value, keepProportions);
  263. this.height = this.image.getHeight();
  264. //qDebug("kp: ", keepProportions);
  265. //qDebug("height: ", this.height);
  266. heightEdit.blockSignals(true);
  267. heightEdit.setValue(this.height);
  268. heightEdit.blockSignals(false);
  269. }
  270. this.updatePreview(true);
  271. };
  272. Image.prototype.slotHeightChanged = function(value) {
  273. if (isNaN(value)) {
  274. return;
  275. }
  276. var optionsToolBar = EAction.getOptionsToolBar();
  277. var widthEdit = optionsToolBar.findChild("Width");
  278. var keepProportionsSwitch = optionsToolBar.findChild("KeepProportions");
  279. var keepProportions = keepProportionsSwitch.checked;
  280. this.height = value;
  281. if (!isNull(this.image)) {
  282. this.image.setHeight(value, keepProportions);
  283. this.width = this.image.getWidth();
  284. widthEdit.blockSignals(true);
  285. widthEdit.setValue(this.width);
  286. widthEdit.blockSignals(false);
  287. }
  288. this.updatePreview(true);
  289. };
  290. Image.prototype.slotAngleChanged = function(value) {
  291. this.angle = value;
  292. if (!isNull(this.image)) {
  293. this.image.setAngle(value);
  294. }
  295. this.updatePreview(true);
  296. };