StatusIndicatorStyle.qml 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Quick Extras module of the Qt Toolkit.
  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.2
  40. import QtGraphicalEffects 1.0
  41. import QtQuick.Controls.Private 1.0
  42. import QtQuick.Extras 1.4
  43. /*!
  44. \qmltype StatusIndicatorStyle
  45. \inqmlmodule QtQuick.Controls.Styles
  46. \since 5.5
  47. \ingroup controlsstyling
  48. \brief Provides custom styling for StatusIndicatorStyle.
  49. You can create a custom status indicator by defining the \l indicator
  50. component.
  51. */
  52. Style {
  53. id: pieMenuStyle
  54. /*!
  55. The \l StatusIndicator that this style is attached to.
  56. */
  57. readonly property StatusIndicator control: __control
  58. /*!
  59. The color that instances of
  60. \l [QtQuickExtras]{StatusIndicator} will have.
  61. The \l [QtQuickExtras]{StatusIndicator::}{color}
  62. property in \l [QtQuickExtras]{StatusIndicator}
  63. will override this property when set.
  64. */
  65. property color color: "red"
  66. /*!
  67. This defines the indicator in both its on and off status.
  68. */
  69. property Component indicator: Item {
  70. readonly property real shineStep: 0.05
  71. readonly property real smallestAxis: Math.min(control.width, control.height)
  72. readonly property real outerRecessPercentage: 0.11
  73. readonly property color offColor: Qt.rgba(0.13, 0.13, 0.13)
  74. readonly property color baseColor: control.active ? control.color : offColor
  75. implicitWidth: TextSingleton.implicitHeight * 2
  76. implicitHeight: implicitWidth
  77. Canvas {
  78. id: backgroundCanvas
  79. width: Math.min(parent.width, parent.height)
  80. // height: width --- QTBUG-42878
  81. height: Math.min(parent.width, parent.height)
  82. anchors.centerIn: parent
  83. Connections {
  84. target: control
  85. function onActiveChanged() { backgroundCanvas.requestPaint() }
  86. function onColorChanged() { backgroundCanvas.requestPaint() }
  87. }
  88. onPaint: {
  89. var ctx = getContext("2d");
  90. ctx.reset();
  91. // Draw the semi-transparent background.
  92. ctx.beginPath();
  93. var gradient = ctx.createLinearGradient(width / 2, 0, width / 2, height * 0.75);
  94. gradient.addColorStop(0.0, Qt.rgba(0, 0, 0, control.active ? 0.1 : 0.25));
  95. gradient.addColorStop(1.0, control.active ? Qt.rgba(0, 0, 0, 0.1) : Qt.rgba(0.74, 0.74, 0.74, 0.25));
  96. ctx.fillStyle = gradient;
  97. ctx.ellipse(0, 0, width, height);
  98. ctx.fill();
  99. }
  100. }
  101. Item {
  102. id: shadowGuard
  103. anchors.fill: backgroundCanvas
  104. anchors.margins: -shadow.radius
  105. Canvas {
  106. id: colorCanvas
  107. anchors.fill: parent
  108. anchors.margins: shadow.radius
  109. Connections {
  110. target: control
  111. function onActiveChanged() { colorCanvas.requestPaint() }
  112. function onColorChanged() { colorCanvas.requestPaint() }
  113. }
  114. onPaint: {
  115. var ctx = getContext("2d");
  116. ctx.reset();
  117. // Draw the actual color within the circle.
  118. ctx.beginPath();
  119. ctx.fillStyle = baseColor;
  120. var recess = smallestAxis * outerRecessPercentage;
  121. ctx.ellipse(recess, recess, width - recess * 2, height - recess * 2);
  122. ctx.fill();
  123. }
  124. }
  125. }
  126. DropShadow {
  127. id: shadow
  128. source: shadowGuard
  129. color: control.color
  130. cached: true
  131. anchors.fill: shadowGuard
  132. visible: control.active
  133. }
  134. Canvas {
  135. id: foregroundCanvas
  136. anchors.fill: backgroundCanvas
  137. Connections {
  138. target: control
  139. function onActiveChanged() { foregroundCanvas.requestPaint() }
  140. function onColorChanged() { foregroundCanvas.requestPaint() }
  141. }
  142. onPaint: {
  143. var ctx = getContext("2d");
  144. ctx.reset();
  145. // Draw the first shine.
  146. ctx.beginPath();
  147. ctx.fillStyle = Qt.rgba(1, 1, 1, 0.03);
  148. var recessPercentage = outerRecessPercentage + shineStep * 0.65;
  149. var recess = smallestAxis * recessPercentage;
  150. ctx.ellipse(recess, recess, width - recess * 2, height - recess * 2);
  151. ctx.fill();
  152. // Draw the second, inner shine.
  153. ctx.beginPath();
  154. ctx.fillStyle = Qt.rgba(1, 1, 1, 0.06);
  155. recessPercentage += shineStep;
  156. recess = smallestAxis * recessPercentage;
  157. ctx.ellipse(recess, recess, width - recess * 2, height - recess * 2);
  158. ctx.fill();
  159. // Now draw the final arced shine that goes over the first and second shines.
  160. // First, clip the entire shine area.
  161. ctx.beginPath();
  162. recessPercentage -= shineStep;
  163. recess = smallestAxis * recessPercentage;
  164. ctx.ellipse(recess, recess, width - recess * 2, height - recess * 2);
  165. ctx.clip();
  166. if (!control.active) {
  167. // Then, clip the bottom area out of the shine.
  168. ctx.ellipse(recess, height * 0.425, width - recess * 2, height - recess * 2);
  169. ctx.clip();
  170. }
  171. ctx.beginPath();
  172. var gradient;
  173. if (!control.active) {
  174. // Draw the shine arc.
  175. gradient = ctx.createLinearGradient(width / 2, height * 0.2, width / 2, height * 0.65);
  176. gradient.addColorStop(0.0, Qt.rgba(1, 1, 1, 0.05));
  177. gradient.addColorStop(1.0, "transparent");
  178. } else {
  179. // Draw the radial shine.
  180. gradient = ctx.createRadialGradient(width / 2, height / 2, 0, width / 2, height / 2, width * 0.5 /* (same as height) */);
  181. gradient.addColorStop(0.0, Qt.lighter(baseColor, 1.4));
  182. gradient.addColorStop(1.0, "transparent");
  183. }
  184. ctx.fillStyle = gradient;
  185. ctx.ellipse(recess, recess, width - recess * 2, height - recess * 2);
  186. ctx.fill();
  187. }
  188. }
  189. }
  190. /*! \internal */
  191. property Component panel: Item {
  192. implicitWidth: indicatorLoader.implicitWidth
  193. implicitHeight: indicatorLoader.implicitHeight
  194. Loader {
  195. id: indicatorLoader
  196. width: Math.max(1, parent.width)
  197. height: Math.max(1, parent.height)
  198. anchors.centerIn: parent
  199. sourceComponent: indicator
  200. }
  201. }
  202. }