RectangularGlow.qml 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2017 The Qt Company Ltd.
  4. ** Contact: http://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL3$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see http://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at http://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPLv3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or later as published by the Free
  28. ** Software Foundation and appearing in the file LICENSE.GPL included in
  29. ** the packaging of this file. Please review the following information to
  30. ** ensure the GNU General Public License version 2.0 requirements will be
  31. ** met: http://www.gnu.org/licenses/gpl-2.0.html.
  32. **
  33. ** $QT_END_LICENSE$
  34. **
  35. ****************************************************************************/
  36. import QtQuick
  37. /*
  38. A cross-graphics API implementation of QtGraphicalEffects' RectangularGlow.
  39. */
  40. Item {
  41. id: rootItem
  42. /*
  43. This property defines how many pixels outside the item area are reached
  44. by the glow.
  45. The value ranges from 0.0 (no glow) to inf (infinite glow). By default,
  46. the property is set to \c 0.0.
  47. \table
  48. \header
  49. \li Output examples with different glowRadius values
  50. \li
  51. \li
  52. \row
  53. \li \image RectangularGlow_glowRadius1.png
  54. \li \image RectangularGlow_glowRadius2.png
  55. \li \image RectangularGlow_glowRadius3.png
  56. \row
  57. \li \b { glowRadius: 10 }
  58. \li \b { glowRadius: 20 }
  59. \li \b { glowRadius: 40 }
  60. \row
  61. \li \l spread: 0
  62. \li \l spread: 0
  63. \li \l spread: 0
  64. \row
  65. \li \l color: #ffffff
  66. \li \l color: #ffffff
  67. \li \l color: #ffffff
  68. \row
  69. \li \l cornerRadius: 25
  70. \li \l cornerRadius: 25
  71. \li \l cornerRadius: 25
  72. \endtable
  73. */
  74. property real glowRadius: 0.0
  75. /*
  76. This property defines how large part of the glow color is strenghtened
  77. near the source edges.
  78. The value ranges from 0.0 (no strenght increase) to 1.0 (maximum
  79. strenght increase). By default, the property is set to \c 0.0.
  80. \table
  81. \header
  82. \li Output examples with different spread values
  83. \li
  84. \li
  85. \row
  86. \li \image RectangularGlow_spread1.png
  87. \li \image RectangularGlow_spread2.png
  88. \li \image RectangularGlow_spread3.png
  89. \row
  90. \li \b { spread: 0.0 }
  91. \li \b { spread: 0.5 }
  92. \li \b { spread: 1.0 }
  93. \row
  94. \li \l glowRadius: 20
  95. \li \l glowRadius: 20
  96. \li \l glowRadius: 20
  97. \row
  98. \li \l color: #ffffff
  99. \li \l color: #ffffff
  100. \li \l color: #ffffff
  101. \row
  102. \li \l cornerRadius: 25
  103. \li \l cornerRadius: 25
  104. \li \l cornerRadius: 25
  105. \endtable
  106. */
  107. property real spread: 0.0
  108. /*
  109. This property defines the RGBA color value which is used for the glow.
  110. By default, the property is set to \c "white".
  111. \table
  112. \header
  113. \li Output examples with different color values
  114. \li
  115. \li
  116. \row
  117. \li \image RectangularGlow_color1.png
  118. \li \image RectangularGlow_color2.png
  119. \li \image RectangularGlow_color3.png
  120. \row
  121. \li \b { color: #ffffff }
  122. \li \b { color: #55ff55 }
  123. \li \b { color: #5555ff }
  124. \row
  125. \li \l glowRadius: 20
  126. \li \l glowRadius: 20
  127. \li \l glowRadius: 20
  128. \row
  129. \li \l spread: 0
  130. \li \l spread: 0
  131. \li \l spread: 0
  132. \row
  133. \li \l cornerRadius: 25
  134. \li \l cornerRadius: 25
  135. \li \l cornerRadius: 25
  136. \endtable
  137. */
  138. property color color: "white"
  139. /*
  140. This property defines the corner radius that is used to draw a glow with
  141. rounded corners.
  142. The value ranges from 0.0 to half of the effective width or height of
  143. the glow, whichever is smaller. This can be calculated with: \c{
  144. min(width, height) / 2.0 + glowRadius}
  145. By default, the property is bound to glowRadius property. The glow
  146. behaves as if the rectangle was blurred when adjusting the glowRadius
  147. property.
  148. \table
  149. \header
  150. \li Output examples with different cornerRadius values
  151. \li
  152. \li
  153. \row
  154. \li \image RectangularGlow_cornerRadius1.png
  155. \li \image RectangularGlow_cornerRadius2.png
  156. \li \image RectangularGlow_cornerRadius3.png
  157. \row
  158. \li \b { cornerRadius: 0 }
  159. \li \b { cornerRadius: 25 }
  160. \li \b { cornerRadius: 50 }
  161. \row
  162. \li \l glowRadius: 20
  163. \li \l glowRadius: 20
  164. \li \l glowRadius: 20
  165. \row
  166. \li \l spread: 0
  167. \li \l spread: 0
  168. \li \l spread: 0
  169. \row
  170. \li \l color: #ffffff
  171. \li \l color: #ffffff
  172. \li \l color: #ffffff
  173. \endtable
  174. */
  175. property real cornerRadius: glowRadius
  176. /*
  177. This property allows the effect output pixels to be cached in order to
  178. improve the rendering performance.
  179. Every time the source or effect properties are changed, the pixels in
  180. the cache must be updated. Memory consumption is increased, because an
  181. extra buffer of memory is required for storing the effect output.
  182. It is recommended to disable the cache when the source or the effect
  183. properties are animated.
  184. By default, the property is set to \c false.
  185. */
  186. property bool cached: false
  187. ShaderEffectSource {
  188. id: cacheItem
  189. anchors.fill: shaderItem
  190. visible: rootItem.cached
  191. smooth: true
  192. sourceItem: shaderItem
  193. live: true
  194. hideSource: visible
  195. }
  196. ShaderEffect {
  197. id: shaderItem
  198. x: (parent.width - width) / 2.0
  199. y: (parent.height - height) / 2.0
  200. width: parent.width + rootItem.glowRadius * 2 + cornerRadius * 2
  201. height: parent.height + rootItem.glowRadius * 2 + cornerRadius * 2
  202. function clampedCornerRadius() {
  203. var maxCornerRadius = Math.min(rootItem.width, rootItem.height) / 2 + rootItem.glowRadius;
  204. return Math.max(0, Math.min(rootItem.cornerRadius, maxCornerRadius))
  205. }
  206. property color color: rootItem.color
  207. property real inverseSpread: 1.0 - rootItem.spread
  208. property real relativeSizeX: ((inverseSpread * inverseSpread) * rootItem.glowRadius + cornerRadius * 2.0) / width
  209. property real relativeSizeY: relativeSizeX * (width / height)
  210. property real spread: rootItem.spread / 2.0
  211. property real cornerRadius: clampedCornerRadius()
  212. fragmentShader: "qrc:/qt-project.org/imports/QtQuick/Controls/Material/shaders/RectangularGlow.frag"
  213. }
  214. }