ElevationEffect.qml 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. import QtQuick.Controls.Material
  38. import QtQuick.Controls.Material.impl
  39. /*
  40. An effect for standard Material Design elevation shadows. Useful for using as \c layer.effect.
  41. */
  42. Item {
  43. id: effect
  44. /*
  45. The source the effect is applied to.
  46. */
  47. property var source
  48. /*
  49. The elevation of the \l source Item.
  50. */
  51. property int elevation: 0
  52. /*
  53. Set to \c true if the \l source Item is the same width as its parent and the shadow
  54. should be full width instead of rounding around the corner of the Item.
  55. \sa fullHeight
  56. */
  57. property bool fullWidth: false
  58. /*
  59. Set to \c true if the \l source Item is the same height as its parent and the shadow
  60. should be full height instead of rounding around the corner of the Item.
  61. \sa fullWidth
  62. */
  63. property bool fullHeight: false
  64. /*
  65. \internal
  66. The actual source Item the effect is applied to.
  67. */
  68. readonly property Item sourceItem: source.sourceItem
  69. /*
  70. * The following shadow values are taken from Angular Material
  71. *
  72. * The MIT License (MIT)
  73. *
  74. * Copyright (c) 2014-2016 Google, Inc. http://angularjs.org
  75. *
  76. * Permission is hereby granted, free of charge, to any person obtaining a copy
  77. * of this software and associated documentation files (the "Software"), to deal
  78. * in the Software without restriction, including without limitation the rights
  79. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  80. * copies of the Software, and to permit persons to whom the Software is
  81. * furnished to do so, subject to the following conditions:
  82. *
  83. * The above copyright notice and this permission notice shall be included in all
  84. * copies or substantial portions of the Software.
  85. *
  86. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  87. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  88. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  89. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  90. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  91. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  92. * SOFTWARE.
  93. */
  94. /*
  95. \internal
  96. The shadows to use for each possible elevation. There are three shadows that when combined
  97. make up the elevation.
  98. */
  99. readonly property var _shadows: [
  100. [{offset: 0, blur: 0, spread: 0},
  101. {offset: 0, blur: 0, spread: 0},
  102. {offset: 0, blur: 0, spread: 0}],
  103. [{offset: 1, blur: 3, spread: 0},
  104. {offset: 1, blur: 1, spread: 0},
  105. {offset: 2, blur: 1, spread: -1}],
  106. [{offset: 1, blur: 5, spread: 0},
  107. {offset: 2, blur: 2, spread: 0},
  108. {offset: 3, blur: 1, spread: -2}],
  109. [{offset: 1, blur: 8, spread: 0},
  110. {offset: 3, blur: 4, spread: 0},
  111. {offset: 3, blur: 3, spread: -2}],
  112. [{offset: 2, blur: 4, spread: -1},
  113. {offset: 4, blur: 5, spread: 0},
  114. {offset: 1, blur: 10, spread: 0}],
  115. [{offset: 3, blur: 5, spread: -1},
  116. {offset: 5, blur: 8, spread: 0},
  117. {offset: 1, blur: 14, spread: 0}],
  118. [{offset: 3, blur: 5, spread: -1},
  119. {offset: 6, blur: 10, spread: 0},
  120. {offset: 1, blur: 18, spread: 0}],
  121. [{offset: 4, blur: 5, spread: -2},
  122. {offset: 7, blur: 10, spread: 1},
  123. {offset: 2, blur: 16, spread: 1}],
  124. [{offset: 5, blur: 5, spread: -3},
  125. {offset: 8, blur: 10, spread: 1},
  126. {offset: 3, blur: 14, spread: 2}],
  127. [{offset: 5, blur: 6, spread: -3},
  128. {offset: 9, blur: 12, spread: 1},
  129. {offset: 3, blur: 16, spread: 2}],
  130. [{offset: 6, blur: 6, spread: -3},
  131. {offset: 10, blur: 14, spread: 1},
  132. {offset: 4, blur: 18, spread: 3}],
  133. [{offset: 6, blur: 7, spread: -4},
  134. {offset: 11, blur: 15, spread: 1},
  135. {offset: 4, blur: 20, spread: 3}],
  136. [{offset: 7, blur: 8, spread: -4},
  137. {offset: 12, blur: 17, spread: 2},
  138. {offset: 5, blur: 22, spread: 4}],
  139. [{offset: 7, blur: 8, spread: -4},
  140. {offset: 13, blur: 19, spread: 2},
  141. {offset: 5, blur: 24, spread: 4}],
  142. [{offset: 7, blur: 9, spread: -4},
  143. {offset: 14, blur: 21, spread: 2},
  144. {offset: 5, blur: 26, spread: 4}],
  145. [{offset: 8, blur: 9, spread: -5},
  146. {offset: 15, blur: 22, spread: 2},
  147. {offset: 6, blur: 28, spread: 5}],
  148. [{offset: 8, blur: 10, spread: -5},
  149. {offset: 16, blur: 24, spread: 2},
  150. {offset: 6, blur: 30, spread: 5}],
  151. [{offset: 8, blur: 11, spread: -5},
  152. {offset: 17, blur: 26, spread: 2},
  153. {offset: 6, blur: 32, spread: 5}],
  154. [{offset: 9, blur: 11, spread: -5},
  155. {offset: 18, blur: 28, spread: 2},
  156. {offset: 7, blur: 34, spread: 6}],
  157. [{offset: 9, blur: 12, spread: -6},
  158. {offset: 19, blur: 29, spread: 2},
  159. {offset: 7, blur: 36, spread: 6}],
  160. [{offset: 10, blur: 13, spread: -6},
  161. {offset: 20, blur: 31, spread: 3},
  162. {offset: 8, blur: 38, spread: 7}],
  163. [{offset: 10, blur: 13, spread: -6},
  164. {offset: 21, blur: 33, spread: 3},
  165. {offset: 8, blur: 40, spread: 7}],
  166. [{offset: 10, blur: 14, spread: -6},
  167. {offset: 22, blur: 35, spread: 3},
  168. {offset: 8, blur: 42, spread: 7}],
  169. [{offset: 11, blur: 14, spread: -7},
  170. {offset: 23, blur: 36, spread: 3},
  171. {offset: 9, blur: 44, spread: 8}],
  172. [{offset: 11, blur: 15, spread: -7},
  173. {offset: 24, blur: 38, spread: 3},
  174. {offset: 9, blur: 46, spread: 8}]
  175. ]
  176. /*
  177. \internal
  178. The current shadow based on the elevation.
  179. */
  180. readonly property var _shadow: _shadows[Math.max(0, Math.min(elevation, _shadows.length - 1))]
  181. // Nest the shadows and source view in two items rendered as a layer
  182. // so the shadow is not clipped by the bounds of the source view
  183. Item {
  184. property int margin: -100
  185. x: margin
  186. y: margin
  187. width: parent.width - 2 * margin
  188. height: parent.height - 2 * margin
  189. // By rendering as a layer, the shadow will never show through the source item,
  190. // even when the source item's opacity is less than 1
  191. layer.enabled: true
  192. // The box shadows automatically pick up the size of the source Item and not
  193. // the size of the parent, so we don't need to worry about the extra padding
  194. // in the parent Item
  195. BoxShadow {
  196. offsetY: effect._shadow[0].offset
  197. blurRadius: effect._shadow[0].blur
  198. spreadRadius: effect._shadow[0].spread
  199. color: Qt.rgba(0,0,0, 0.2)
  200. fullWidth: effect.fullWidth
  201. fullHeight: effect.fullHeight
  202. source: effect.sourceItem
  203. }
  204. BoxShadow {
  205. offsetY: effect._shadow[1].offset
  206. blurRadius: effect._shadow[1].blur
  207. spreadRadius: effect._shadow[1].spread
  208. color: Qt.rgba(0,0,0, 0.14)
  209. fullWidth: effect.fullWidth
  210. fullHeight: effect.fullHeight
  211. source: effect.sourceItem
  212. }
  213. BoxShadow {
  214. offsetY: effect._shadow[2].offset
  215. blurRadius: effect._shadow[2].blur
  216. spreadRadius: effect._shadow[2].spread
  217. color: Qt.rgba(0,0,0, 0.12)
  218. fullWidth: effect.fullWidth
  219. fullHeight: effect.fullHeight
  220. source: effect.sourceItem
  221. }
  222. ShaderEffect {
  223. property alias source: effect.source
  224. x: (parent.width - width)/2
  225. y: (parent.height - height)/2
  226. width: effect.sourceItem.width
  227. height: effect.sourceItem.height
  228. }
  229. }
  230. }