RectangularGlow.qml 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2017 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Graphical Effects module.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  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 https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://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.LGPL3 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-3.0.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 (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. import QtQuick 2.12
  40. import QtGraphicalEffects.private 1.12
  41. /*!
  42. \qmltype RectangularGlow
  43. \inqmlmodule QtGraphicalEffects
  44. \since QtGraphicalEffects 1.0
  45. \inherits QtQuick2::Item
  46. \ingroup qtgraphicaleffects-glow
  47. \brief Generates a blurred and colorized rectangle, which gives
  48. the impression that the source is glowing.
  49. This effect is intended to have good performance. The shape of the glow is
  50. limited to a rectangle with a custom corner radius. For situations where
  51. custom shapes are required, consider \l {QtGraphicalEffects::Glow} {Glow}
  52. effect.
  53. \table
  54. \header
  55. \li Effect applied
  56. \row
  57. \li \image RectangularGlow_applied.png
  58. \endtable
  59. \note This effect is available when running with OpenGL.
  60. \section1 Example
  61. The following example shows how to apply the effect.
  62. \snippet RectangularGlow-example.qml example
  63. */
  64. Item {
  65. id: rootItem
  66. /*!
  67. This property defines how many pixels outside the item area are reached
  68. by the glow.
  69. The value ranges from 0.0 (no glow) to inf (infinite glow). By default,
  70. the property is set to \c 0.0.
  71. \table
  72. \header
  73. \li Output examples with different glowRadius values
  74. \li
  75. \li
  76. \row
  77. \li \image RectangularGlow_glowRadius1.png
  78. \li \image RectangularGlow_glowRadius2.png
  79. \li \image RectangularGlow_glowRadius3.png
  80. \row
  81. \li \b { glowRadius: 10 }
  82. \li \b { glowRadius: 20 }
  83. \li \b { glowRadius: 40 }
  84. \row
  85. \li \l spread: 0
  86. \li \l spread: 0
  87. \li \l spread: 0
  88. \row
  89. \li \l color: #ffffff
  90. \li \l color: #ffffff
  91. \li \l color: #ffffff
  92. \row
  93. \li \l cornerRadius: 25
  94. \li \l cornerRadius: 25
  95. \li \l cornerRadius: 25
  96. \endtable
  97. */
  98. property real glowRadius: 0.0
  99. /*!
  100. This property defines how large part of the glow color is strengthened
  101. near the source edges.
  102. The value ranges from 0.0 (no strength increase) to 1.0 (maximum
  103. strength increase). By default, the property is set to \c 0.0.
  104. \table
  105. \header
  106. \li Output examples with different spread values
  107. \li
  108. \li
  109. \row
  110. \li \image RectangularGlow_spread1.png
  111. \li \image RectangularGlow_spread2.png
  112. \li \image RectangularGlow_spread3.png
  113. \row
  114. \li \b { spread: 0.0 }
  115. \li \b { spread: 0.5 }
  116. \li \b { spread: 1.0 }
  117. \row
  118. \li \l glowRadius: 20
  119. \li \l glowRadius: 20
  120. \li \l glowRadius: 20
  121. \row
  122. \li \l color: #ffffff
  123. \li \l color: #ffffff
  124. \li \l color: #ffffff
  125. \row
  126. \li \l cornerRadius: 25
  127. \li \l cornerRadius: 25
  128. \li \l cornerRadius: 25
  129. \endtable
  130. */
  131. property real spread: 0.0
  132. /*!
  133. This property defines the RGBA color value which is used for the glow.
  134. By default, the property is set to \c "white".
  135. \table
  136. \header
  137. \li Output examples with different color values
  138. \li
  139. \li
  140. \row
  141. \li \image RectangularGlow_color1.png
  142. \li \image RectangularGlow_color2.png
  143. \li \image RectangularGlow_color3.png
  144. \row
  145. \li \b { color: #ffffff }
  146. \li \b { color: #55ff55 }
  147. \li \b { color: #5555ff }
  148. \row
  149. \li \l glowRadius: 20
  150. \li \l glowRadius: 20
  151. \li \l glowRadius: 20
  152. \row
  153. \li \l spread: 0
  154. \li \l spread: 0
  155. \li \l spread: 0
  156. \row
  157. \li \l cornerRadius: 25
  158. \li \l cornerRadius: 25
  159. \li \l cornerRadius: 25
  160. \endtable
  161. */
  162. property color color: "white"
  163. /*!
  164. This property defines the corner radius that is used to draw a glow with
  165. rounded corners.
  166. The value ranges from 0.0 to half of the effective width or height of
  167. the glow, whichever is smaller. This can be calculated with: \c{
  168. min(width, height) / 2.0 + glowRadius}
  169. By default, the property is bound to glowRadius property. The glow
  170. behaves as if the rectangle was blurred when adjusting the glowRadius
  171. property.
  172. \table
  173. \header
  174. \li Output examples with different cornerRadius values
  175. \li
  176. \li
  177. \row
  178. \li \image RectangularGlow_cornerRadius1.png
  179. \li \image RectangularGlow_cornerRadius2.png
  180. \li \image RectangularGlow_cornerRadius3.png
  181. \row
  182. \li \b { cornerRadius: 0 }
  183. \li \b { cornerRadius: 25 }
  184. \li \b { cornerRadius: 50 }
  185. \row
  186. \li \l glowRadius: 20
  187. \li \l glowRadius: 20
  188. \li \l glowRadius: 20
  189. \row
  190. \li \l spread: 0
  191. \li \l spread: 0
  192. \li \l spread: 0
  193. \row
  194. \li \l color: #ffffff
  195. \li \l color: #ffffff
  196. \li \l color: #ffffff
  197. \endtable
  198. */
  199. property real cornerRadius: glowRadius
  200. /*!
  201. This property allows the effect output pixels to be cached in order to
  202. improve the rendering performance.
  203. Every time the source or effect properties are changed, the pixels in
  204. the cache must be updated. Memory consumption is increased, because an
  205. extra buffer of memory is required for storing the effect output.
  206. It is recommended to disable the cache when the source or the effect
  207. properties are animated.
  208. By default, the property is set to \c false.
  209. */
  210. property bool cached: false
  211. ShaderEffectSource {
  212. id: cacheItem
  213. anchors.fill: shaderItem
  214. visible: rootItem.cached
  215. smooth: true
  216. sourceItem: shaderItem
  217. live: true
  218. hideSource: visible
  219. }
  220. ShaderEffect {
  221. id: shaderItem
  222. x: (parent.width - width) / 2.0
  223. y: (parent.height - height) / 2.0
  224. width: parent.width + rootItem.glowRadius * 2 + cornerRadius * 2
  225. height: parent.height + rootItem.glowRadius * 2 + cornerRadius * 2
  226. function clampedCornerRadius() {
  227. var maxCornerRadius = Math.min(rootItem.width, rootItem.height) / 2 + glowRadius;
  228. return Math.max(0, Math.min(rootItem.cornerRadius, maxCornerRadius))
  229. }
  230. property color color: rootItem.color
  231. property real inverseSpread: 1.0 - rootItem.spread
  232. property real relativeSizeX: ((inverseSpread * inverseSpread) * rootItem.glowRadius + cornerRadius * 2.0) / width
  233. property real relativeSizeY: relativeSizeX * (width / height)
  234. property real spread: rootItem.spread / 2.0
  235. property real cornerRadius: clampedCornerRadius()
  236. fragmentShader: "qrc:/qt-project.org/imports/QtGraphicalEffects/shaders/rectangularglow.frag"
  237. }
  238. }