CircularGaugeStyle.qml 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 1.4
  42. import QtQuick.Controls.Styles 1.4
  43. import QtQuick.Controls.Private 1.0
  44. import QtQuick.Extras 1.4
  45. import QtQuick.Extras.Private 1.0
  46. /*!
  47. \qmltype CircularGaugeStyle
  48. \inqmlmodule QtQuick.Controls.Styles
  49. \since 5.5
  50. \ingroup controlsstyling
  51. \brief Provides custom styling for CircularGauge.
  52. You can create a custom circular gauge by replacing the following delegates:
  53. \list
  54. \li \l background
  55. \li \l tickmark
  56. \li \l minorTickmark
  57. \li \l tickmarkLabel
  58. \li \l needle
  59. \li \l foreground
  60. \endlist
  61. Below is an example that changes the needle to a basic orange \l Rectangle:
  62. \code
  63. CircularGauge {
  64. style: CircularGaugeStyle {
  65. needle: Rectangle {
  66. y: outerRadius * 0.15
  67. implicitWidth: outerRadius * 0.03
  68. implicitHeight: outerRadius * 0.9
  69. antialiasing: true
  70. color: Qt.rgba(0.66, 0.3, 0, 1)
  71. }
  72. }
  73. }
  74. \endcode
  75. The result:
  76. \image circulargauge-needle-example-2.png CircularGaugeStyle example
  77. \section2 Direction
  78. \l minimumValueAngle and \l maximumValueAngle determine not only the
  79. position of the tickmarks, labels and needle, but the direction in which
  80. they are displayed around the gauge. For example, if \l minimumValueAngle
  81. is greater than \l maximumValueAngle, the gauge will be anti-clockwise.
  82. Below, there are two gauges: the top gauge has a \l minimumValueAngle of
  83. \c -90 degrees and a \l maximumValueAngle of \c 90 degrees, and the bottom
  84. gauge has the opposite.
  85. \image circulargauge-reversed.png Reversed CircularGauge
  86. \sa {Styling CircularGauge}
  87. */
  88. Style {
  89. id: circularGaugeStyle
  90. /*!
  91. The \l CircularGauge that this style is attached to.
  92. */
  93. readonly property CircularGauge control: __control
  94. /*!
  95. The distance from the center of the gauge to the outer edge of the
  96. gauge.
  97. This property is useful for determining the size of the various
  98. components of the style, in order to ensure that they are scaled
  99. proportionately when the gauge is resized.
  100. */
  101. readonly property real outerRadius: Math.min(control.width, control.height) * 0.5
  102. /*!
  103. This property determines the angle at which the minimum value is
  104. displayed on the gauge.
  105. The angle set affects the following components of the gauge:
  106. \list
  107. \li The angle of the needle
  108. \li The position of the tickmarks and labels
  109. \endlist
  110. The angle origin points north:
  111. \image circulargauge-angles.png
  112. There is no minimum or maximum angle for this property, but the default
  113. style only supports angles whose absolute range is less than or equal
  114. to \c 360 degrees. This is because ranges higher than \c 360 degrees
  115. will cause the tickmarks and labels to overlap each other.
  116. The default value is \c -145.
  117. */
  118. property real minimumValueAngle: -145
  119. /*!
  120. This property determines the angle at which the maximum value is
  121. displayed on the gauge.
  122. The angle set affects the following components of the gauge:
  123. \list
  124. \li The angle of the needle
  125. \li The position of the tickmarks and labels
  126. \endlist
  127. The angle origin points north:
  128. \image circulargauge-angles.png
  129. There is no minimum or maximum angle for this property, but the default
  130. style only supports angles whose absolute range is less than or equal
  131. to \c 360 degrees. This is because ranges higher than \c 360 degrees
  132. will cause the tickmarks and labels to overlap each other.
  133. The default value is \c 145.
  134. */
  135. property real maximumValueAngle: 145
  136. /*!
  137. The range between \l minimumValueAngle and \l maximumValueAngle, in
  138. degrees. This value will always be positive.
  139. */
  140. readonly property real angleRange: control.__panel.circularTickmarkLabel.angleRange
  141. /*!
  142. This property holds the rotation of the needle in degrees.
  143. */
  144. property real needleRotation: {
  145. var percentage = (control.value - control.minimumValue) / (control.maximumValue - control.minimumValue);
  146. minimumValueAngle + percentage * angleRange;
  147. }
  148. /*!
  149. The interval at which tickmarks are displayed.
  150. For example, if this property is set to \c 10 (the default),
  151. control.minimumValue to \c 0, and control.maximumValue to \c 100,
  152. the tickmarks displayed will be 0, 10, 20, etc., to 100,
  153. around the gauge.
  154. */
  155. property real tickmarkStepSize: 10
  156. /*!
  157. The distance in pixels from the outside of the gauge (outerRadius) at
  158. which the outermost point of the tickmark line is drawn.
  159. */
  160. property real tickmarkInset: 0
  161. /*!
  162. The amount of tickmarks displayed by the gauge, calculated from
  163. \l tickmarkStepSize and the control's
  164. \l {CircularGauge::minimumValue}{minimumValue} and
  165. \l {CircularGauge::maximumValue}{maximumValue}.
  166. \sa minorTickmarkCount
  167. */
  168. readonly property int tickmarkCount: control.__panel.circularTickmarkLabel.tickmarkCount
  169. /*!
  170. The amount of minor tickmarks between each tickmark.
  171. The default value is \c 4.
  172. \sa tickmarkCount
  173. */
  174. property int minorTickmarkCount: 4
  175. /*!
  176. The distance in pixels from the outside of the gauge (outerRadius) at
  177. which the outermost point of the minor tickmark line is drawn.
  178. */
  179. property real minorTickmarkInset: 0
  180. /*!
  181. The distance in pixels from the outside of the gauge (outerRadius) at
  182. which the center of the value marker text is drawn.
  183. */
  184. property real labelInset: __protectedScope.toPixels(0.19)
  185. /*!
  186. The interval at which tickmark labels are displayed.
  187. For example, if this property is set to \c 10 (the default),
  188. control.minimumValue to \c 0, and control.maximumValue to \c 100, the
  189. tickmark labels displayed will be 0, 10, 20, etc., to 100,
  190. around the gauge.
  191. */
  192. property real labelStepSize: tickmarkStepSize
  193. /*!
  194. The amount of tickmark labels displayed by the gauge, calculated from
  195. \l labelStepSize and the control's
  196. \l {CircularGauge::minimumValue}{minimumValue} and
  197. \l {CircularGauge::maximumValue}{maximumValue}.
  198. \sa tickmarkCount, minorTickmarkCount
  199. */
  200. readonly property int labelCount: control.__panel.circularTickmarkLabel.labelCount
  201. /*! \qmlmethod real CircularGaugeStyle::valueToAngle(real value)
  202. Returns \a value as an angle in degrees.
  203. This function is useful for custom drawing or positioning of items in
  204. the style's components. For example, it can be used to calculate the
  205. angles at which to draw an arc around the gauge indicating the safe
  206. area for the needle to be within.
  207. For example, if minimumValueAngle is set to \c 270 and
  208. maximumValueAngle is set to \c 90, this function will return \c 270
  209. when passed minimumValue and \c 90 when passed maximumValue.
  210. \sa {Styling CircularGauge#styling-circulargauge-background}{
  211. Styling CircularGauge's background}
  212. */
  213. function valueToAngle(value) {
  214. return control.__panel.circularTickmarkLabel.valueToAngle(value);
  215. }
  216. property QtObject __protectedScope: QtObject {
  217. /*!
  218. Converts a value expressed as a percentage of \l outerRadius to
  219. a pixel value.
  220. */
  221. function toPixels(percentageOfOuterRadius) {
  222. return percentageOfOuterRadius * outerRadius;
  223. }
  224. }
  225. /*!
  226. The background of the gauge.
  227. If set, the background determines the implicit size of the gauge.
  228. By default, there is no background defined.
  229. \sa {Styling CircularGauge#styling-circulargauge-background}{
  230. Styling CircularGauge's background}
  231. */
  232. property Component background
  233. /*!
  234. This component defines each individual tickmark. The position of each
  235. tickmark is already set; only the
  236. \l {Item::implicitWidth}{implicitWidth} and
  237. \l {Item::implicitHeight}{implicitHeight} need to be specified.
  238. Each instance of this component has access to the following properties:
  239. \table
  240. \row \li \c {readonly property int} \b styleData.index
  241. \li The index of this tickmark.
  242. \row \li \c {readonly property real} \b styleData.value
  243. \li The value that this tickmark represents.
  244. \endtable
  245. To illustrate what these properties refer to, we can use the following
  246. example:
  247. \snippet circulargauge-tickmark-indices-values.qml tickmarks
  248. We've replaced the conventional \e line tickmarks with \l Text items
  249. and have hidden the tickmarkLabel component in order to make the
  250. association clearer:
  251. \image circulargauge-tickmark-indices-values.png Tickmarks
  252. The index property can be useful if you have another model that
  253. contains images to display for each index, for example.
  254. The value property is useful for drawing lower and upper limits around
  255. the gauge to indicate the recommended value ranges. For example, speeds
  256. above 200 kilometers an hour in a car's speedometer could be indicated
  257. as dangerous using this property.
  258. \sa {Styling CircularGauge#styling-circulargauge-tickmark}{
  259. Styling CircularGauge's tickmark}
  260. */
  261. property Component tickmark: Rectangle {
  262. implicitWidth: outerRadius * 0.02
  263. antialiasing: true
  264. implicitHeight: outerRadius * 0.06
  265. color: "#c8c8c8"
  266. }
  267. /*!
  268. This component defines each individual minor tickmark. The position of
  269. each minor tickmark is already set; only the
  270. \l {Item::implicitWidth}{implicitWidth} and
  271. \l {Item::implicitHeight}{implicitHeight} need to be specified.
  272. Each instance of this component has access to the following properties:
  273. \table
  274. \row \li \c {readonly property int} \b styleData.index
  275. \li The index of this tickmark.
  276. \row \li \c {readonly property real} \b styleData.value
  277. \li The value that this tickmark represents.
  278. \endtable
  279. \sa {Styling CircularGauge#styling-circulargauge-minorTickmark}{
  280. Styling CircularGauge's minorTickmark}
  281. */
  282. property Component minorTickmark: Rectangle {
  283. implicitWidth: outerRadius * 0.01
  284. antialiasing: true
  285. implicitHeight: outerRadius * 0.03
  286. color: "#c8c8c8"
  287. }
  288. /*!
  289. This defines the text of each tickmark label on the gauge.
  290. Each instance of this component has access to the following properties:
  291. \table
  292. \row \li \c {readonly property int} \b styleData.index
  293. \li The index of this label.
  294. \row \li \c {readonly property real} \b styleData.value
  295. \li The value that this label represents.
  296. \endtable
  297. \sa {Styling CircularGauge#styling-circulargauge-tickmarkLabel}{
  298. Styling CircularGauge's tickmarkLabel}
  299. */
  300. property Component tickmarkLabel: Text {
  301. font.pixelSize: Math.max(6, __protectedScope.toPixels(0.12))
  302. text: styleData.value
  303. color: "#c8c8c8"
  304. antialiasing: true
  305. horizontalAlignment: Text.AlignHCenter
  306. verticalAlignment: Text.AlignVCenter
  307. }
  308. /*!
  309. The needle that points to the gauge's current value.
  310. This component is drawn below the \l foreground component.
  311. The style expects the needle to be pointing up at a rotation of \c 0,
  312. in order for the rotation to be correct. For example:
  313. \image circulargauge-needle.png CircularGauge's needle
  314. When defining your own needle component, the only properties that the
  315. style requires you to set are the
  316. \l {Item::implicitWidth}{implicitWidth} and
  317. \l {Item::implicitHeight}{implicitHeight}.
  318. Optionally, you can set \l {Item::x}{x} and \l {Item::y}{y} to change
  319. the needle's transform origin. Setting the \c x position can be useful
  320. for needle images where the needle is not centered exactly
  321. horizontally. Setting the \c y position allows you to make the base of
  322. the needle hang over the center of the gauge.
  323. \sa {Styling CircularGauge#styling-circulargauge-needle}{
  324. Styling CircularGauge's needle}
  325. */
  326. property Component needle: Item {
  327. implicitWidth: __protectedScope.toPixels(0.08)
  328. implicitHeight: 0.9 * outerRadius
  329. Image {
  330. anchors.fill: parent
  331. source: "images/needle.png"
  332. }
  333. }
  334. /*!
  335. The foreground of the gauge. This component is drawn above all others.
  336. Like \l background, the foreground component fills the entire gauge.
  337. By default, the knob of the gauge is defined here.
  338. \sa {Styling CircularGauge#styling-circulargauge-foreground}{
  339. Styling CircularGauge's foreground}
  340. */
  341. property Component foreground: Item {
  342. Image {
  343. source: "images/knob.png"
  344. anchors.centerIn: parent
  345. scale: {
  346. var idealHeight = __protectedScope.toPixels(0.2);
  347. var originalImageHeight = sourceSize.height;
  348. idealHeight / originalImageHeight;
  349. }
  350. }
  351. }
  352. /*! \internal */
  353. property Component panel: Item {
  354. id: panelItem
  355. implicitWidth: backgroundLoader.item ? backgroundLoader.implicitWidth : TextSingleton.implicitHeight * 16
  356. implicitHeight: backgroundLoader.item ? backgroundLoader.implicitHeight : TextSingleton.implicitHeight * 16
  357. property alias background: backgroundLoader.item
  358. property alias circularTickmarkLabel: circularTickmarkLabel_
  359. Loader {
  360. id: backgroundLoader
  361. sourceComponent: circularGaugeStyle.background
  362. width: outerRadius * 2
  363. height: outerRadius * 2
  364. anchors.centerIn: parent
  365. }
  366. CircularTickmarkLabel {
  367. id: circularTickmarkLabel_
  368. anchors.fill: backgroundLoader
  369. minimumValue: control.minimumValue
  370. maximumValue: control.maximumValue
  371. stepSize: control.stepSize
  372. tickmarksVisible: control.tickmarksVisible
  373. minimumValueAngle: circularGaugeStyle.minimumValueAngle
  374. maximumValueAngle: circularGaugeStyle.maximumValueAngle
  375. tickmarkStepSize: circularGaugeStyle.tickmarkStepSize
  376. tickmarkInset: circularGaugeStyle.tickmarkInset
  377. minorTickmarkCount: circularGaugeStyle.minorTickmarkCount
  378. minorTickmarkInset: circularGaugeStyle.minorTickmarkInset
  379. labelInset: circularGaugeStyle.labelInset
  380. labelStepSize: circularGaugeStyle.labelStepSize
  381. style: CircularTickmarkLabelStyle {
  382. tickmark: circularGaugeStyle.tickmark
  383. minorTickmark: circularGaugeStyle.minorTickmark
  384. tickmarkLabel: circularGaugeStyle.tickmarkLabel
  385. }
  386. }
  387. Loader {
  388. id: needleLoader
  389. sourceComponent: circularGaugeStyle.needle
  390. transform: [
  391. Rotation {
  392. angle: needleRotation
  393. origin.x: needleLoader.width / 2
  394. origin.y: needleLoader.height
  395. },
  396. Translate {
  397. x: panelItem.width / 2 - needleLoader.width / 2
  398. y: panelItem.height / 2 - needleLoader.height
  399. }
  400. ]
  401. }
  402. Loader {
  403. id: foreground
  404. sourceComponent: circularGaugeStyle.foreground
  405. anchors.fill: backgroundLoader
  406. }
  407. }
  408. }