InsertScriptItem.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/Block/Block.js");
  20. include("scripts/Block/InsertBlockItem/InsertBlockItem.js");
  21. /**
  22. * \class InsertScriptItem
  23. * \brief Called when a script is inserted from the part library.
  24. * \ingroup ecma_block
  25. */
  26. function InsertScriptItem(guiAction) {
  27. InsertBlockItem.call(this, guiAction);
  28. if (!isNull(guiAction)) {
  29. this.setUiOptions("../InsertBlockItem/InsertBlockItem.ui");
  30. }
  31. this.diItem = undefined;
  32. this.docItem = undefined;
  33. this.file = undefined;
  34. //this.offset = undefined;
  35. }
  36. InsertScriptItem.prototype = new InsertBlockItem();
  37. InsertScriptItem.prototype.beginEvent = function() {
  38. var ms = new RMemoryStorage();
  39. var si = createSpatialIndex();
  40. this.docItem = new RDocument(ms, si);
  41. // avoid unit conversion:
  42. this.docItem.setUnit(RS.None);
  43. this.diItem = new RDocumentInterface(this.docItem);
  44. // this document does never update the layer list, block list, etc:
  45. this.diItem.setNotifyListeners(false);
  46. Block.prototype.beginEvent.call(this);
  47. if (isNull(this.url) || !this.url.isLocalFile()) {
  48. this.terminate();
  49. return;
  50. }
  51. this.file = this.url.toLocalFile();
  52. // sanity checks:
  53. if (!InsertScriptItem.check(this.file)) {
  54. this.terminate();
  55. return;
  56. }
  57. include(this.file);
  58. InsertScriptItem.evalInit(this.file);
  59. this.generate();
  60. this.setState(InsertBlockItem.State.SettingPosition);
  61. };
  62. /**
  63. * Sanity check before including a script.
  64. * \return true if this is very likely a part library script item.
  65. */
  66. InsertScriptItem.check = function(fileName) {
  67. var baseName = new QFileInfo(fileName).baseName();
  68. var content = readTextFile(fileName);
  69. if (isNull(content)) {
  70. EAction.handleUserWarning(qsTr("Cannot read file:") + " " + fileName);
  71. return false;
  72. }
  73. if (!content.contains("function " + baseName)) {
  74. EAction.handleUserWarning(qsTr("No constructor found in file:") + " " + fileName);
  75. return false;
  76. }
  77. if (!content.contains(baseName + ".init")) {
  78. EAction.handleUserWarning(qsTr("No 'init' function found in file:") + " " + fileName);
  79. return false;
  80. }
  81. if (!content.contains(baseName + ".generate")) {
  82. EAction.handleUserWarning(qsTr("No 'generate' function found in file:") + " " + fileName);
  83. return false;
  84. }
  85. if (!content.contains(baseName + ".generatePreview")) {
  86. EAction.handleUserWarning(qsTr("No 'generatePreview' function found in file:") + " " + this.file);
  87. return false;
  88. }
  89. return true;
  90. };
  91. InsertScriptItem.prototype.finishEvent = function() {
  92. var dock = objectFromPath("MainWindow::" + InsertScriptItem.getObjectName(this.file));
  93. if (!isNull(dock)) {
  94. dock.hide();
  95. destr(dock);
  96. }
  97. if (!isNull(this.diItem)) {
  98. destr(this.diItem);
  99. }
  100. InsertBlockItem.prototype.finishEvent.call(this);
  101. };
  102. /**
  103. * Generates the script item into a temporary document for insertion.
  104. */
  105. InsertScriptItem.prototype.generate = function() {
  106. this.diItem.clear();
  107. var operation = InsertScriptItem.evalGenerate(this.diItem, this.file);
  108. if (!isNull(operation)) {
  109. this.diItem.applyOperation(operation);
  110. }
  111. };
  112. InsertScriptItem.isScriptFile = function(file) {
  113. return new QFileInfo(file).suffix().toLowerCase() == "js";
  114. };
  115. InsertScriptItem.hasUiFile = function(file) {
  116. var uiFile = InsertScriptItem.getUiFilePath(file);
  117. return new QFileInfo(uiFile).isFile();
  118. };
  119. InsertScriptItem.getUiFilePath = function(file) {
  120. var fi = new QFileInfo(file);
  121. return fi.absolutePath() + QDir.separator + fi.completeBaseName() + ".ui";
  122. };
  123. InsertScriptItem.getWidget = function(file) {
  124. return objectFromPath("MainWindow::" + InsertScriptItem.getObjectName(file)).widget();
  125. };
  126. InsertScriptItem.getObjectName = function(file) {
  127. var str = "";
  128. var sa = file.split(QDir.separator);
  129. for ( var i = 0; i < sa.length; ++i) {
  130. var s = sa[i];
  131. if (s.length === 0) {
  132. continue;
  133. }
  134. var first = s.slice(0, 1).toUpperCase();
  135. var rest = s.slice(1, s.length);
  136. str += first + rest;
  137. }
  138. var re = /\.js$/;
  139. return str.replace(re, "");
  140. };
  141. InsertScriptItem.getFileTitle = function(file) {
  142. return new QFileInfo(file).completeBaseName();
  143. };
  144. /**
  145. * Called when a script item is about to be inserted to initialize the
  146. * user interface component that displays script parameters and call
  147. * the script init function.
  148. */
  149. InsertScriptItem.evalInit = function(file) {
  150. var formWidget;
  151. var className = new QFileInfo(file).completeBaseName();
  152. if (InsertScriptItem.hasUiFile(file)) {
  153. var dock = objectFromPath("MainWindow::" + InsertScriptItem.getObjectName(file));
  154. if (isNull(dock)) {
  155. // create script item UI as a dock widget:
  156. var basePath = new QFileInfo(file).absolutePath();
  157. var uiFile = new QFileInfo(InsertScriptItem.getUiFilePath(file)).fileName();
  158. formWidget = WidgetFactory.createWidget(basePath, uiFile);
  159. var appWin = RMainWindowQt.getMainWindow();
  160. dock = new QDockWidget(InsertScriptItem.getFileTitle(file), appWin);
  161. dock.objectName = InsertScriptItem.getObjectName(file);
  162. dock.setWidget(formWidget);
  163. appWin.addDockWidget(Qt.RightDockWidgetArea, dock);
  164. } else {
  165. // show previously used and hidden script item UI again:
  166. if (!RSettings.getOriginalArguments().contains("-no-show")) {
  167. dock.visible = true;
  168. }
  169. formWidget = dock.widget();
  170. }
  171. }
  172. // call init()
  173. if (!isNull(global[className]) && isFunction(global[className].init)) {
  174. global[className].init(formWidget);
  175. }
  176. };
  177. InsertScriptItem.evalGenerate = function(di, file) {
  178. var operation = undefined;
  179. var className = new QFileInfo(file).completeBaseName();
  180. if (!isNull(global[className]) && isFunction(global[className].generate)) {
  181. operation = global[className].generate(di, file);
  182. }
  183. return operation;
  184. };