LibraryBrowserScripts.dox 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*!
  2. \page LibraryBrowserScripts Library Browser Scripts
  3. The new QCAD Part Library Browser can not only contain static part library items
  4. but also dynamic items that are created based on parameters, mathematical
  5. formulas, user input, SQL data bases, XML files or other data sources.
  6. A dynamic item is rendered when the item is being inserted.
  7. Script items may display a custom user interface component for the user to
  8. enter script parameters. For example, an item that shows the top view of a
  9. dining table might allow the user to input the length and width of the table.
  10. The part library included with QCAD contains such a sample script item
  11. called "DiningTable.js" located under "library/misc".
  12. \section new_script Creating a Dynamic Part Library Item
  13. In this tutorial we will create a new dynamic item for the library browser.
  14. The item should generate a cut-out template for a cube as shown here:
  15. \image html library_browser/cube_template.png
  16. The dynamic item has two parameters which can be specified by the user when the
  17. script item is being inserted into the drawing:
  18. - The length of the cube's edge (in drawing units).
  19. - If the glue flaps should be generated or not (true or false).
  20. First, we create a new script file that will do the work behind our new dynamic
  21. part library item:
  22. -# Create a new directory for your script file(s). This directory can be created
  23. inside the part library directory of your QCAD installation or anywhere else
  24. on your hard drive.
  25. -# Start you favorite text editor and create a new text file.
  26. -# Save the file in the newly created directory as "CubeCuttingOut.js".
  27. \n Note that the extension has to be ".js" since we're about to write an
  28. ECMAScript (JavaScript) file.
  29. -# If you have added the directory and script to the library folder of your
  30. QCAD installation, you can skip the following step.
  31. -# Add the newly created directory to the library browser sources:
  32. -# Open the QCAD Library Browser.
  33. -# Open the application preferences of the Library Browser.
  34. -# In section <em>Library Sources</em> click \a Add and choose the
  35. directory you have created in step 1.
  36. -# Restart the QCAD Library Browser for the changes to take effect.
  37. -# The library browser now has an additional source for library browser items.
  38. \section script_skeleton The Script Structure
  39. Copy / paste the following code into your script file 'CubeCuttingOut.js':
  40. \code
  41. function CubeCuttingOut() {
  42. }
  43. CubeCuttingOut.init = function(formWidget) {
  44. };
  45. CubeCuttingOut.generate = function(documentInterface, file) {
  46. };
  47. CubeCuttingOut.generatePreview = function(documentInterface, iconSize) {
  48. };
  49. \endcode
  50. This adds a class named \c CubeCuttingOut to the script file. Note that the
  51. class must have the same name as the script file (case sensitive).
  52. Besides the constructor, three functions are required to make this a
  53. valid, functional script item:
  54. - \c init(formWidget)
  55. \n The function \c init() is called whenever the preview icon (see below)
  56. is generated or when the script item is being inserted into a drawing.
  57. \n The parameter \c formWidget is the widget that displays the script item's
  58. parameters (if applicable).
  59. - \c generate(documentInterface, file)
  60. \n The function \c generate() is called when the user is about to insert
  61. the script item.
  62. \n The parameter \c documentInterface is a valid document interface
  63. (RDocumentInterface) that is used to create the library item.
  64. This is \i not the document the user is editing. The part library script has
  65. no direct access to the drawing of the user.
  66. \n The parameter \c file is the name of the current script file (String).
  67. \n This function is expected to return an object of type RAddObjectsOperation.
  68. - \c generatePreview(documentInterface, iconSize)
  69. \n The function \c generatePreview() is called to create an icon for
  70. the library browser preview. Note that at the moment the preview icon is
  71. generated, no user input is available. Usually an icon with default parameters
  72. is generated.
  73. \n The parameter \c documentInterface is a valid document interface
  74. (RDocumentInterface).
  75. \n The parameter \c iconSize is the user configurable size of the icon (integer).
  76. \n This function is expected to return an object of type RAddObjectsOperation.
  77. The script item is now valid. Locate your new library folder in the 'File System'
  78. tab of the QCAD Library Browser. Right-click on the folder and click
  79. 'Regenerate Icons'. Your item should show up with an empty icon that is decorated
  80. with a small cog wheel to indicate that this is a dynamic library item backed by
  81. a script file.
  82. \section fill_skeleton Implementation
  83. In most cases, the functions \c generate() and \c generatePreview() do almost
  84. the same.
  85. The main difference between \c generate() and \c generatePreview() is that
  86. generate() works with user input to define the script parameters, while
  87. generatePreview() has no user input and should generate the item with default
  88. parameters to create a recognizable icon.
  89. It is usually recommendable to write a helper function that
  90. creates the RAddObjectsOperation object that is returned by both functions based
  91. on the script parameters.
  92. For this example, we name that function \c createCuttingOut():
  93. \code
  94. CubeCuttingOut.createCuttingOut = function(documentInterface) {
  95. CubeCuttingOut.size = 10;
  96. var va = new Array(
  97. new RVector(0, 0),
  98. new RVector(0, CubeCuttingOut.size),
  99. new RVector(CubeCuttingOut.size, CubeCuttingOut.size),
  100. new RVector(CubeCuttingOut.size, 0)
  101. );
  102. var addOperation = new RAddObjectsOperation(false);
  103. for ( var i = 0; i < va.length; ++i) {
  104. var lineData = new RLineData(va[i], va[(i + 1) % va.length]);
  105. var line = new RLineEntity(documentInterface.getDocument(), lineData);
  106. addOperation.addObject(line);
  107. }
  108. return addOperation;
  109. };
  110. \endcode
  111. For now, the size of the cube is fixed to 10 drawing units
  112. (variable \c CubeCuttingOut.size).
  113. We will later use \c CubeCuttingOut.size as an input parameter for our helper function.
  114. Based on the cube size, the helper function generates the CAD entities
  115. that represent a square. These entities are added to the operation that
  116. will be applied to the document that represents the item.
  117. In function \c generate(), we simply call our helper function:
  118. \code
  119. CubeCuttingOut.generate = function(documentInterface, file) {
  120. return CubeCuttingOut.createCuttingOut(documentInterface);
  121. };
  122. \endcode
  123. The script is now functional but does not display an icon and only creates one
  124. single square with a fixed size.
  125. Save the modified script file and then drag-n-drop the script item from the
  126. library browser into the drawing area.
  127. When moving the mouse cursor inside the drawing area, a square with a size of
  128. 10 drawing units is shown. Left-click to place the square somewhere in your drawing.
  129. Note that you may also specify a scale factor and rotation angle or flip the item
  130. in the options tool bar. These are standard operations available for all items
  131. that are being inserted, including dynamic items.
  132. \image html library_browser/lb01.png
  133. We now modify the script to create the full cut-out template for the cube:
  134. \snippet CubeCuttingOut.jsd init
  135. \snippet CubeCuttingOut.jsd createCuttingOut
  136. \snippet CubeCuttingOut.jsd createSquare
  137. \section script_parameters Providing Script Parameters
  138. For the user to enter script parameters, we need to define a user interface
  139. component (widget). We use Qt Designer to design the user interface
  140. for this widget. Qt Designer is available for free as part of the Qt SDK
  141. or Qt Creator: http://qt.nokia.com/downloads
  142. The UI file must have the same name as the script file but with the extension
  143. <tt>.ui</tt>.
  144. -# Start the Qt Designer.
  145. -# Create a new file and choose \a Widget as template.
  146. -# Add an element of type \a QLineEdit, set its object name to "CubeSize"
  147. and its value to "10".
  148. -# Add an element of type \a QCheckBox, set its object name to "DrawPlates"
  149. and set its \a checked flag.
  150. You may also want to add some labels to indicate to the user what is being
  151. defined where. In the end, the widget may for example look like this:
  152. \image html library_browser/widget.png
  153. Save the UI file as "CubeCuttingOut.ui". If you don't have Qt Designer, you can
  154. find the file source at the bottom of this page.
  155. The library browser will now display that user interface component whenever this
  156. script item is being inserted.
  157. All the script item implementation has to do is to get the script parameters
  158. from the user interface component:
  159. \snippet CubeCuttingOut.jsd include
  160. \snippet CubeCuttingOut.jsd init
  161. \snippet CubeCuttingOut.jsd generate
  162. Save the script file again, and insert the script item from the library browser into
  163. your drawing. The cut-out template is still drawn with a size of 10.
  164. As soon as you enter a different value in the user interface component,
  165. the cut-out template is drawn with that size.
  166. The script item parameter <em>Draw plates</em> can be handled in the same way:
  167. \snippet CubeCuttingOut.jsd generate
  168. \section script_preview Providing a Script Preview
  169. Finally, we provide a script preview that is shown as icon in the library browser.
  170. For that, we simply set some appropriate default values as script item parameters
  171. and call our helper function.
  172. \snippet CubeCuttingOut.jsd generatePreview
  173. The library browser icons are updated on every start of QCAD.
  174. You can also right-click on a directory in the 'File System' tab and choose
  175. <em>Regenerate Icons</em> to rebuild the icons in that directory.
  176. \image html library_browser/lb02.png
  177. \section cube_cutting_out_js The Complete Script and User Interface Sources
  178. \include CubeCuttingOut.jsd
  179. \include CubeCuttingOut.ui
  180. <p>&nbsp;</p>
  181. */