simple.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * Simple API, mainly designed for use in the ECMAScript console.
  3. */
  4. /**
  5. * \defgroup ecma_simple QCAD Simple API
  6. * \ingroup ecma_scripts
  7. *
  8. * \brief This module defines the QCAD Simple API in ECMAScript.
  9. * The QCAD Simple API is meant to simplify common tasks such as adding entities.
  10. * To use the QCAD Simple API in your scripts, include the file simple.js in your
  11. * script:
  12. *
  13. * \code
  14. * include("simple.js");
  15. * \endcode
  16. */
  17. include("library.js");
  18. include("input.js");
  19. // internal:
  20. __simpleUseOp = false;
  21. __simpleOp = undefined;
  22. __simpleDoc = undefined;
  23. __simpleDi = undefined;
  24. include("simple_create.js");
  25. include("simple_info.js");
  26. include("simple_input.js");
  27. include("simple_modify.js");
  28. include("simple_transaction.js");
  29. include("simple_view.js");
  30. /**
  31. * Returns a pointer to the main application window (RMainWindowQt).
  32. * \ingroup ecma_simple
  33. */
  34. function getMainWindow() {
  35. return RMainWindowQt.getMainWindow();
  36. }
  37. /**
  38. * Returns the current RDocumentInterface or undefined.
  39. * \ingroup ecma_simple
  40. */
  41. function getDocumentInterface() {
  42. var appWin = getMainWindow();
  43. if (isNull(appWin)) {
  44. qWarning("QCAD SIMPLE API: no main window");
  45. return undefined;
  46. }
  47. // EAction.handleUserMessage("GetDocumentInterface sucessful");
  48. return appWin.getDocumentInterface();
  49. }
  50. /**
  51. * Returns the Assign RDocumentInterface or undefined.
  52. * \ingroup ecma_simple
  53. */
  54. function getAssignDocumentInterface(nfileType) {
  55. var appWin = getMainWindow();
  56. if (isNull(appWin)) {
  57. qWarning("QCAD SIMPLE API: no main window");
  58. return undefined;
  59. }
  60. // EAction.handleUserMessage("GetAssignDocumentInterface sucessful");
  61. return appWin.getAssignDocumentInterface(nfileType);
  62. }
  63. /**
  64. * Returns the current RDocument or undefined.
  65. * \ingroup ecma_simple
  66. */
  67. function getDocument() {
  68. var di = getDocumentInterface();
  69. if (isNull(di)) {
  70. qWarning("QCAD SIMPLE API: no document");
  71. return undefined;
  72. }
  73. return di.getDocument();
  74. }
  75. function getAssignDocument(fileType) {
  76. var di = getAssignDocumentInterface(fileType);
  77. if (isNull(di)) {
  78. qWarning("QCAD SIMPLE API: no document");
  79. return undefined;
  80. }
  81. return di.getDocument();
  82. }
  83. /**
  84. * Returns the current or last active RGraphicsView.
  85. * \ingroup ecma_simple
  86. */
  87. function getGraphicsView() {
  88. var di = getDocumentInterface();
  89. if (isNull(di)) {
  90. return undefined;
  91. }
  92. return di.getLastKnownViewWithFocus();
  93. }
  94. /**
  95. * \deprecated
  96. */
  97. function createOffScreenDocument() {
  98. return createDocument();
  99. }
  100. /**
  101. * Creates an (off-screen) document. Such a document can be filled with
  102. * objects and saved to disk, pasted into the current document, etc.
  103. * \ingroup ecma_simple
  104. *
  105. * \return new empty RDocument with default settings
  106. */
  107. function createDocument() {
  108. return new RDocument(new RMemoryStorage(), new RSpatialIndexSimple());
  109. }
  110. /**
  111. * Creates an (off-screen) document with a document interface that can be used
  112. * to import / export to / from files or graphics scenes and views.
  113. * \ingroup ecma_simple
  114. *
  115. * \return new empty RDocumentInterface with default settings
  116. */
  117. function createDocumentInterface() {
  118. return new RDocumentInterface(new RDocument(new RMemoryStorage(), createSpatialIndex()));
  119. }
  120. /**
  121. * Disables the main application window to prevent user input.
  122. * \ingroup ecma_simple
  123. */
  124. function disableInput() {
  125. getMainWindow().enabled = false;
  126. return getMainWindow().enabled;
  127. }
  128. /**
  129. * Enables the main application window to allow user input.
  130. * \ingroup ecma_simple
  131. */
  132. function enableInput() {
  133. getMainWindow().enabled = true;
  134. return getMainWindow().enabled;
  135. }
  136. /**
  137. * Returns true if user input is enabled.
  138. * \ingroup ecma_simple
  139. */
  140. function isInputEnabled() {
  141. return getMainWindow().enabled;
  142. }
  143. /**
  144. * Prints a warning to stdout.
  145. * \ingroup ecma_simple
  146. */
  147. function warning(msg) {
  148. if (isFunction(warning.handler)) {
  149. return warning.handler(msg);
  150. }
  151. print("WARNING: " + msg);
  152. }
  153. /**
  154. * Keeps the user interface up to date during long operations.
  155. * User input must be disabled using disableInput before calling update.
  156. * \ingroup ecma_simple
  157. */
  158. function update() {
  159. if (isInputEnabled()) {
  160. warning("User input must be disabled before calling update");
  161. return false;
  162. }
  163. var ret = true;
  164. try {
  165. QCoreApplication.processEvents(QEventLoop.ExcludeSocketNotifiers, 100);
  166. } catch (e) {
  167. qWarning("error: " + e);
  168. ret = false;
  169. }
  170. return ret;
  171. }
  172. /**
  173. * Sleeps for the indicated time in milliseconds.
  174. * \ingroup ecma_simple
  175. */
  176. function sleep(d) {
  177. if (isInputEnabled()) {
  178. warning("User input must be disabled before calling sleep");
  179. return false;
  180. }
  181. var t = new QTime();
  182. t.start();
  183. while (t.elapsed() < d) {
  184. if (!update()) {
  185. return false;
  186. }
  187. }
  188. return true;
  189. }