CreatingNewTool.dox 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*!
  2. \page CreatingNewTool Creating a New Tool
  3. A good point to start QCAD script development is to create your own simple
  4. CAD tool that adds a menu and / or tool button to the QCAD user interface
  5. and handles the user interaction to do something.
  6. In this example, we create a drawing tool which creates three point entities
  7. with one click. We call our tool "ExThreePoints" (Ex for example).
  8. Quite often, we can start by copying a similar tool that exists already.
  9. However, in this case, we start from scratch.
  10. Create a directory for our new tool. For this example, we create
  11. a directory with the name "ExThreePoints". We place our tool inside the
  12. DrawExamples directory "scripts/Misc/Examples/DrawExamples", so that the path for
  13. our new tool becomes: "scripts/Misc/Examples/DrawExamples/ExThreePoints" (relative
  14. to the QCAD installation directory).
  15. Inside this directory, create an empty text file called "ExThreePoints.js".
  16. You can also create an icon for the tool in SVG format and call it
  17. "ExThreePoints.svg".
  18. <b>Each tool in QCAD is bundled this way inside its own directory.</b>
  19. The tool directory contains the tool implementation ("ExThreePoints.js"),
  20. the tool icon ("ExThreePoints.svg"), an optional Qt project file which
  21. is mainly used to handle translations ("ExThreePoints.pro") and all other
  22. resources that are exclusively used by this tool (user interface definitions,
  23. icons, script files, etc.).
  24. Inside the file "ExThreePoints.js", define a class with the same name as
  25. the tool: "ExThreePoints".
  26. <b>The name of the main class of a tool always has to match the tools directory
  27. name (in this case "ExThreePoints").</b>
  28. A tool class is usually derived from the class
  29. that is defined in its parent directory or from EAction, defined in scripts/EAction.js.
  30. For that reason, we have to include the script file of EAction.
  31. In this case "scripts/EAction.js":
  32. \snippet ../../DrawExamples/ExThreePoints/ExThreePoints.jsd include
  33. Then, we define the class constructor, which calls the base class constructor:
  34. \snippet ../../DrawExamples/ExThreePoints/ExThreePoints.jsd constructor
  35. Finally, we derive our class from the base class, in this case "DrawExamples":
  36. \snippet ../../DrawExamples/ExThreePoints/ExThreePoints.jsd inheritance
  37. When QCAD finds the tool directory "ExThreePoints", it looks inside for a file
  38. called "ExThreePoints.js" and inside that script file for the definition of a
  39. class called "ExThreePoints". This class is instantiated when the tool is
  40. started by the user.
  41. Create a static "init" method at the bottom of the file "ExThreePoints.js"
  42. as follows:
  43. \snippet ../../DrawExamples/ExThreePoints/ExThreePoints.jsd init
  44. <b>The static "init" method is called during the startup of QCAD.</b>
  45. The "init" method usually creates a menu or tool button to launch the tool or
  46. performs other necessary initialization.
  47. You can now start QCAD to test if the new tool is correctly shown under
  48. menu "Examples - Drawing". To make sure that QCAD scans the scripts directory
  49. for new script tools, start QCAD with the parameter -rescan:
  50. <tt>./qcad -rescan</tt>
  51. You also might want to enable the script debugger to get a meaningful
  52. error message if there is a typo in your script file:
  53. <tt>./qcad -rescan -enable-script-debugger</tt>
  54. <b>Note that the script debugger should only be enabled for debugging script
  55. files and never during production use of QCAD. Enabling the script debugger
  56. can change the behavior of scripts and lead to unexpected results or even
  57. crashes.</b>
  58. At this point, our new tool does not appear to do anything. It only
  59. waits until Escape is pressed or the right mouse button is clicked
  60. and then terminates.
  61. We can now add functionality by implementing some methods. We start with
  62. the method "beginEvent", which is called immediately when the user starts
  63. the tool. Some tools only use "beginEvent" and terminate themselves
  64. at the end of it (for example the auto zoom tool). This is the case
  65. if there is no user interaction required and the tool does
  66. something and then terminates when it's done.
  67. We start by calling the "beginEvent" implementation of the parent class
  68. (good practice), then write some debugging output to the console
  69. and terminate our tool:
  70. \code{.js}
  71. ExThreePoints.prototype.beginEvent = function() {
  72. EAction.prototype.beginEvent.call(this);
  73. qDebug("ExThreePoints.prototype.beginEvent was called.");
  74. this.terminate();
  75. };
  76. \endcode
  77. Note that you don't have to restart QCAD after editing the tool script
  78. file. If you start the tool again after adding the "beginEvent" method as
  79. described above, the tool should write the debugging output to the console
  80. and then terminate itself.
  81. In the next step, we add some interaction to our tool. Our tool should
  82. wait for the user to click a coordinate in the drawing, draw three points
  83. at and beside that coordinate and then termiante.
  84. If a tool is interactive (i.e. waits for the user to do something),
  85. we have to keep track of its progress. One way to do this is by adding
  86. a state to it. Even if our tool can only be in one state, it's good
  87. practice to define this single state:
  88. \snippet ../../DrawExamples/ExThreePoints/ExThreePoints.jsd State
  89. We can now implement "setState", which is called whenever the tools state
  90. changes:
  91. \snippet ../../DrawExamples/ExThreePoints/ExThreePoints.jsd setState
  92. In our "beginEvent", we set the initial state of the tool to our
  93. one and only state. We also remove the tool termination:
  94. \snippet ../../DrawExamples/ExThreePoints/ExThreePoints.jsd beginEvent
  95. Now we can implement "coordinateEvent", which is called when the user
  96. clicks or enters a coordinate. Our implementation draws three points
  97. beside each other.
  98. \snippet ../../DrawExamples/ExThreePoints/ExThreePoints.jsd coordinateEvent
  99. Here's the complete code listing of file ExThreePoints.js:
  100. \snippet ../../DrawExamples/ExThreePoints/ExThreePoints.jsd main
  101. */