RadialGradient.qml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 RadialGradient
  43. \inqmlmodule QtGraphicalEffects
  44. \since QtGraphicalEffects 1.0
  45. \inherits QtQuick2::Item
  46. \ingroup qtgraphicaleffects-gradient
  47. \brief Draws a radial gradient.
  48. A gradient is defined by two or more colors, which are blended seamlessly.
  49. The colors start from the middle of the item and end at the borders.
  50. \table
  51. \header
  52. \li Effect applied
  53. \row
  54. \li \image RadialGradient.png
  55. \endtable
  56. \note This effect is available when running with OpenGL.
  57. \section1 Example
  58. The following example shows how to apply the effect.
  59. \snippet RadialGradient-example.qml example
  60. */
  61. Item {
  62. id: rootItem
  63. /*!
  64. This property allows the effect output pixels to be cached in order to
  65. improve the rendering performance.
  66. Every time the source or effect properties are changed, the pixels in
  67. the cache must be updated. Memory consumption is increased, because an
  68. extra buffer of memory is required for storing the effect output.
  69. It is recommended to disable the cache when the source or the effect
  70. properties are animated.
  71. By default, the property is set to \c false.
  72. */
  73. property bool cached: false
  74. /*!
  75. \qmlproperty real RadialGradient::horizontalOffset
  76. \qmlproperty real RadialGradient::verticalOffset
  77. The horizontalOffset and verticalOffset properties define the offset in
  78. pixels for the center point of the gradient compared to the item center.
  79. The values range from -inf to inf. By default, these properties are set
  80. to \c 0.
  81. \table
  82. \header
  83. \li Output examples with different horizontalOffset values
  84. \li
  85. \li
  86. \row
  87. \li \image RadialGradient_horizontalOffset1.png
  88. \li \image RadialGradient_horizontalOffset2.png
  89. \li \image RadialGradient_horizontalOffset3.png
  90. \row
  91. \li \b { horizontalOffset: -150 }
  92. \li \b { horizontalOffset: 0 }
  93. \li \b { horizontalOffset: 150 }
  94. \row
  95. \li \l verticalOffset: 0
  96. \li \l verticalOffset: 0
  97. \li \l verticalOffset: 0
  98. \row
  99. \li \l horizontalRadius: 300
  100. \li \l horizontalRadius: 300
  101. \li \l horizontalRadius: 300
  102. \row
  103. \li \l verticalRadius: 300
  104. \li \l verticalRadius: 300
  105. \li \l verticalRadius: 300
  106. \row
  107. \li \l angle: 0
  108. \li \l angle: 0
  109. \li \l angle: 0
  110. \endtable
  111. */
  112. property real horizontalOffset: 0.0
  113. property real verticalOffset: 0.0
  114. /*!
  115. \qmlproperty real RadialGradient::horizontalRadius
  116. \qmlproperty real RadialGradient::verticalRadius
  117. The horizontalRadius and verticalRadius properties define the shape and
  118. size of the radial gradient. If the radiuses are equal, the shape of the
  119. gradient is a circle. If the horizontal and vertical radiuses differ,
  120. the shape is elliptical. The radiuses are given in pixels.
  121. The value ranges from -inf to inf. By default, horizontalRadius is bound
  122. to width and verticalRadius is bound to height.
  123. \table
  124. \header
  125. \li Output examples with different horizontalRadius values
  126. \li
  127. \li
  128. \row
  129. \li \image RadialGradient_horizontalRadius1.png
  130. \li \image RadialGradient_horizontalRadius2.png
  131. \row
  132. \li \b { horizontalRadius: 300 }
  133. \li \b { horizontalRadius: 100 }
  134. \row
  135. \li \l horizontalOffset: 0
  136. \li \l horizontalOffset: 0
  137. \row
  138. \li \l verticalOffset: 0
  139. \li \l verticalOffset: 0
  140. \row
  141. \li \l verticalRadius: 300
  142. \li \l verticalRadius: 300
  143. \row
  144. \li \l angle: 0
  145. \li \l angle: 0
  146. \row
  147. \li \l gradient: QQuickGradient(0xa05fb10)
  148. \li \l gradient: QQuickGradient(0xa05fb10)
  149. \endtable
  150. */
  151. property real horizontalRadius: width
  152. property real verticalRadius: height
  153. /*!
  154. This property defines the rotation of the gradient around its center
  155. point. The rotation is only visible when the
  156. \l{RadialGradient::horizontalRadius}{horizontalRadius} and
  157. \l{RadialGradient::verticalRadius}{verticalRadius} properties are not
  158. equal. The angle is given in degrees and the default value is \c 0.
  159. \table
  160. \header
  161. \li Output examples with different angle values
  162. \li
  163. \li
  164. \row
  165. \li \image RadialGradient_angle1.png
  166. \li \image RadialGradient_angle2.png
  167. \li \image RadialGradient_angle3.png
  168. \row
  169. \li \b { angle: 0 }
  170. \li \b { angle: 45 }
  171. \li \b { angle: 90 }
  172. \row
  173. \li \l horizontalOffset: 0
  174. \li \l horizontalOffset: 0
  175. \li \l horizontalOffset: 0
  176. \row
  177. \li \l verticalOffset: 0
  178. \li \l verticalOffset: 0
  179. \li \l verticalOffset: 0
  180. \row
  181. \li \l horizontalRadius: 100
  182. \li \l horizontalRadius: 100
  183. \li \l horizontalRadius: 100
  184. \row
  185. \li \l verticalRadius: 300
  186. \li \l verticalRadius: 300
  187. \li \l verticalRadius: 300
  188. \endtable
  189. */
  190. property real angle: 0.0
  191. /*!
  192. This property defines the item that is going to be filled with gradient.
  193. Source item gets rendered into an intermediate pixel buffer and the
  194. alpha values from the result are used to determine the gradient's pixels
  195. visibility in the display. The default value for source is undefined and
  196. in that case whole effect area is filled with gradient.
  197. \table
  198. \header
  199. \li Output examples with different source values
  200. \li
  201. \li
  202. \row
  203. \li \image RadialGradient_maskSource1.png
  204. \li \image RadialGradient_maskSource2.png
  205. \row
  206. \li \b { source: undefined }
  207. \li \b { source: Image { source: images/butterfly.png } }
  208. \row
  209. \li \l horizontalOffset: 0
  210. \li \l horizontalOffset: 0
  211. \row
  212. \li \l verticalOffset: 0
  213. \li \l verticalOffset: 0
  214. \row
  215. \li \l horizontalRadius: 300
  216. \li \l horizontalRadius: 300
  217. \row
  218. \li \l verticalRadius: 300
  219. \li \l verticalRadius: 300
  220. \row
  221. \li \l angle: 0
  222. \li \l angle: 0
  223. \endtable
  224. \note It is not supported to let the effect include itself, for
  225. instance by setting source to the effect's parent.
  226. */
  227. property variant source
  228. /*!
  229. A gradient is defined by two or more colors, which are blended
  230. seamlessly. The colors are specified as a set of GradientStop child
  231. items, each of which defines a position on the gradient from 0.0 to 1.0
  232. and a color. The position of each GradientStop is defined by setting the
  233. position property. The color is defined by setting the color property.
  234. \table
  235. \header
  236. \li Output examples with different gradient values
  237. \li
  238. \li
  239. \row
  240. \li \image RadialGradient_gradient1.png
  241. \li \image RadialGradient_gradient2.png
  242. \li \image RadialGradient_gradient3.png
  243. \row
  244. \li \b {gradient:} \code
  245. Gradient {
  246. GradientStop {
  247. position: 0.000
  248. color: Qt.rgba(1, 0, 0, 1)
  249. }
  250. GradientStop {
  251. position: 0.167
  252. color: Qt.rgba(1, 1, 0, 1)
  253. }
  254. GradientStop {
  255. position: 0.333
  256. color: Qt.rgba(0, 1, 0, 1)
  257. }
  258. GradientStop {
  259. position: 0.500
  260. color: Qt.rgba(0, 1, 1, 1)
  261. }
  262. GradientStop {
  263. position: 0.667
  264. color: Qt.rgba(0, 0, 1, 1)
  265. }
  266. GradientStop {
  267. position: 0.833
  268. color: Qt.rgba(1, 0, 1, 1)
  269. }
  270. GradientStop {
  271. position: 1.000
  272. color: Qt.rgba(1, 0, 0, 1)
  273. }
  274. }
  275. \endcode
  276. \li \b {gradient:} \code
  277. Gradient {
  278. GradientStop {
  279. position: 0.0
  280. color: "#F0F0F0"
  281. }
  282. GradientStop {
  283. position: 0.5
  284. color: "#000000"
  285. }
  286. GradientStop {
  287. position: 1.0
  288. color: "#F0F0F0"
  289. }
  290. }
  291. \endcode
  292. \li \b {gradient:}
  293. \code
  294. Gradient {
  295. GradientStop {
  296. position: 0.0
  297. color: "#00000000"
  298. }
  299. GradientStop {
  300. position: 1.0
  301. color: "#FF000000"
  302. }
  303. }
  304. \endcode
  305. \row
  306. \li \l horizontalOffset: 0
  307. \li \l horizontalOffset: 0
  308. \li \l horizontalOffset: 0
  309. \row
  310. \li \l verticalOffset: 0
  311. \li \l verticalOffset: 0
  312. \li \l verticalOffset: 0
  313. \row
  314. \li \l horizontalRadius: 300
  315. \li \l horizontalRadius: 300
  316. \li \l horizontalRadius: 300
  317. \row
  318. \li \l verticalRadius: 300
  319. \li \l verticalRadius: 300
  320. \li \l verticalRadius: 300
  321. \row
  322. \li \l angle: 0
  323. \li \l angle: 0
  324. \li \l angle: 0
  325. \endtable
  326. */
  327. property Gradient gradient: Gradient {
  328. GradientStop { position: 0.0; color: "white" }
  329. GradientStop { position: 1.0; color: "black" }
  330. }
  331. SourceProxy {
  332. id: maskSourceProxy
  333. input: rootItem.source
  334. }
  335. ShaderEffectSource {
  336. id: gradientSource
  337. sourceItem: Rectangle {
  338. width: 16
  339. height: 256
  340. gradient: rootItem.gradient
  341. smooth: true
  342. }
  343. smooth: true
  344. hideSource: true
  345. visible: false
  346. }
  347. ShaderEffectSource {
  348. id: cacheItem
  349. anchors.fill: parent
  350. visible: rootItem.cached
  351. smooth: true
  352. sourceItem: shaderItem
  353. live: true
  354. hideSource: visible
  355. }
  356. ShaderEffect {
  357. id: shaderItem
  358. property variant gradientImage: gradientSource
  359. property variant maskSource: maskSourceProxy.output
  360. property variant center: Qt.point(0.5 + rootItem.horizontalOffset / width, 0.5 + rootItem.verticalOffset / height)
  361. property real horizontalRatio: rootItem.horizontalRadius > 0 ? width / (2 * rootItem.horizontalRadius) : width * 16384
  362. property real verticalRatio: rootItem.verticalRadius > 0 ? height / (2 * rootItem.verticalRadius) : height * 16384
  363. property real angle: -rootItem.angle / 360 * 2 * Math.PI
  364. property variant matrixData: Qt.point(Math.sin(angle), Math.cos(angle))
  365. anchors.fill: parent
  366. vertexShader: "qrc:/qt-project.org/imports/QtGraphicalEffects/shaders/radialgradient.vert"
  367. fragmentShader: maskSource == undefined ? noMaskShader : maskShader
  368. onFragmentShaderChanged: horizontalRatioChanged()
  369. property string maskShader: "qrc:/qt-project.org/imports/QtGraphicalEffects/shaders/radialgradient_mask.frag"
  370. property string noMaskShader: "qrc:/qt-project.org/imports/QtGraphicalEffects/shaders/radialgradient_nomask.frag"
  371. }
  372. }