TraceCanvas.qml 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright (C) 2016 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
  3. import QtQuick
  4. import "TraceUtils.js" as TraceUtils
  5. import QtQuick.VirtualKeyboard
  6. /*!
  7. \qmltype TraceCanvas
  8. \inqmlmodule QtQuick.VirtualKeyboard.Styles
  9. \brief A specialized Canvas type for rendering Trace objects.
  10. \ingroup qmlclass
  11. \ingroup qtvirtualkeyboard-styles-qml
  12. \inherits Canvas
  13. \since QtQuick.VirtualKeyboard 2.0
  14. This type provides capabilities for rendering Trace objects on the screen.
  15. To make the Trace rendering available in the keyboard, this type must be
  16. declared as the \l {KeyboardStyle::traceCanvasDelegate}
  17. {KeyboardStyle.traceCanvasDelegate} component.
  18. \code
  19. traceCanvasDelegate: TraceCanvas {
  20. }
  21. \endcode
  22. Custom drawing attributes can be initialized in the Canvas.available
  23. signal. For example:
  24. \code
  25. onAvailableChanged: {
  26. if (!available)
  27. return;
  28. var ctx = getContext("2d")
  29. ctx.lineWidth = 8 * scaleHint
  30. ctx.lineCap = "round"
  31. ctx.strokeStyle = Qt.rgba(0xFF, 0xFF, 0xFF)
  32. ctx.fillStyle = ctx.strokeStyle
  33. }
  34. \endcode
  35. The type offers built-in options for Trace rendering. Currently
  36. the following rendering options are available:
  37. \list
  38. \li \c renderSmoothedLine Renders smoothed line with round corners (the default)
  39. \endlist
  40. The rendering function can be changed with the renderFunction property.
  41. \code
  42. renderFunction: renderSmoothedLine
  43. \endcode
  44. Custom rendering function is also supported. Consider the following example:
  45. \code
  46. renderFunction: renderCustomLine
  47. function renderCustomLine() {
  48. getContext("2d")
  49. var points = trace.points()
  50. ...
  51. }
  52. \endcode
  53. */
  54. Canvas {
  55. id: canvas
  56. /*! Provides access to \l Trace object.
  57. */
  58. property Trace trace
  59. /*! Enables auto destruction mode.
  60. If enabled, this item will be destroyed when the \c trace object is
  61. destroyed.
  62. The default value is false. In this case the canvas can be reused after
  63. onRecycle signal is triggered.
  64. */
  65. property bool autoDestroy
  66. /*! Specifies the approximate delay in milliseconds, counted from the beginning of the
  67. auto destruction, before the object is to be destroyed or recycled.
  68. This delay makes it possible, for example, to animate the item before destruction.
  69. The default value is 0.
  70. */
  71. property int autoDestroyDelay
  72. /*! This property defines the rendering function.
  73. The default value is \c renderSmoothedLine
  74. */
  75. property var renderFunction: renderSmoothedLine
  76. property int __renderPos
  77. property bool __renderingEnabled
  78. /*! Renders smoothed line with round corners.
  79. This function is incremental and renders only the new part added to the Trace.
  80. This function does not alter any of the canvas attributes (i.e. they can be set elsewhere.)
  81. */
  82. function renderSmoothedLine() {
  83. __renderPos = TraceUtils.renderSmoothedLine(getContext("2d"), trace, __renderPos)
  84. }
  85. /*! Clears screen and resets the rendering.
  86. \since QtQuick.VirtualKeyboard.Styles 6.1
  87. */
  88. function renderClear() {
  89. var ctx = getContext("2d")
  90. ctx.clearRect(0, 0, width, height)
  91. __renderPos = 0
  92. }
  93. /*! Recycles trace canvas by clearing all drawings and resetting the variables.
  94. The function triggers onRecycle signal after completed (before the return).
  95. The function returns true when recycling is successful.
  96. \since QtQuick.VirtualKeyboard.Styles 6.1
  97. */
  98. function recycle() {
  99. if (!available) {
  100. destroy()
  101. return false
  102. }
  103. trace = null
  104. recycleTimer.stop()
  105. opacity = Qt.binding(function() {
  106. return trace ? trace.opacity : 1.0
  107. })
  108. requestAnimationFrame(renderClear)
  109. onRecycle(canvas)
  110. return true
  111. }
  112. /*! Emitted when the \a traceCanvas is recycled.
  113. \since QtQuick.VirtualKeyboard.Styles 6.1
  114. */
  115. signal onRecycle(var traceCanvas)
  116. Timer {
  117. id: recycleTimer
  118. interval: canvas.autoDestroyDelay
  119. onTriggered: canvas.recycle()
  120. }
  121. onTraceChanged: {
  122. if (trace === null) {
  123. if (autoDestroy || !available)
  124. destroy(autoDestroyDelay)
  125. else
  126. recycleTimer.restart()
  127. }
  128. }
  129. onAvailableChanged: {
  130. __renderingEnabled = available
  131. if (__renderingEnabled)
  132. requestAnimationFrame(renderFunction)
  133. }
  134. Connections {
  135. target: canvas.__renderingEnabled && trace ? trace : null
  136. function onLengthChanged() { if (renderFunction) canvas.requestAnimationFrame(renderFunction) }
  137. function onFinalChanged() { if (renderFunction) canvas.requestAnimationFrame(renderFunction) }
  138. }
  139. opacity: trace ? trace.opacity : 1.0
  140. Behavior on opacity {
  141. NumberAnimation {
  142. duration: 1500
  143. easing.type: Easing.InOutQuad
  144. }
  145. }
  146. }