123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- /****************************************************************************
- **
- ** Copyright (C) 2016 The Qt Company Ltd.
- ** Contact: https://www.qt.io/licensing/
- **
- ** This file is part of the Qt Quick Extras module of the Qt Toolkit.
- **
- ** $QT_BEGIN_LICENSE:LGPL$
- ** Commercial License Usage
- ** Licensees holding valid commercial Qt licenses may use this file in
- ** accordance with the commercial license agreement provided with the
- ** Software or, alternatively, in accordance with the terms contained in
- ** a written agreement between you and The Qt Company. For licensing terms
- ** and conditions see https://www.qt.io/terms-conditions. For further
- ** information use the contact form at https://www.qt.io/contact-us.
- **
- ** GNU Lesser General Public License Usage
- ** Alternatively, this file may be used under the terms of the GNU Lesser
- ** General Public License version 3 as published by the Free Software
- ** Foundation and appearing in the file LICENSE.LGPL3 included in the
- ** packaging of this file. Please review the following information to
- ** ensure the GNU Lesser General Public License version 3 requirements
- ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
- **
- ** GNU General Public License Usage
- ** Alternatively, this file may be used under the terms of the GNU
- ** General Public License version 2.0 or (at your option) the GNU General
- ** Public license version 3 or any later version approved by the KDE Free
- ** Qt Foundation. The licenses are as published by the Free Software
- ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
- ** included in the packaging of this file. Please review the following
- ** information to ensure the GNU General Public License requirements will
- ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
- ** https://www.gnu.org/licenses/gpl-3.0.html.
- **
- ** $QT_END_LICENSE$
- **
- ****************************************************************************/
- import QtQuick 2.2
- import QtQuick.Controls.Private 1.0
- import QtQuick.Extras.Private 1.0
- import QtQuick.Extras.Private.CppUtils 1.0
- Style {
- id: circularTickmarkLabelStyle
- /*!
- The distance from the center of the control to the outer edge.
- */
- readonly property real outerRadius: Math.min(control.width, control.height) * 0.5
- property QtObject __protectedScope: QtObject {
- /*!
- Converts a value expressed as a percentage of \l outerRadius to
- a pixel value.
- */
- function toPixels(percentageOfOuterRadius) {
- return percentageOfOuterRadius * outerRadius;
- }
- }
- /*!
- This component defines each individual tickmark. The position of each
- tickmark is already set; only the size needs to be specified.
- */
- property Component tickmark: Rectangle {
- width: outerRadius * 0.02
- antialiasing: true
- height: outerRadius * 0.06
- color: "#c8c8c8"
- }
- /*!
- This component defines each individual minor tickmark. The position of
- each minor tickmark is already set; only the size needs to be specified.
- */
- property Component minorTickmark: Rectangle {
- width: outerRadius * 0.01
- antialiasing: true
- height: outerRadius * 0.03
- color: "#c8c8c8"
- }
- /*!
- This defines the text of each tickmark label on the gauge.
- */
- property Component tickmarkLabel: Text {
- font.pixelSize: Math.max(6, __protectedScope.toPixels(0.12))
- text: styleData.value
- color: "#c8c8c8"
- antialiasing: true
- horizontalAlignment: Text.AlignHCenter
- verticalAlignment: Text.AlignVCenter
- }
- /*! \internal */
- property Component panel: Item {
- id: panelItem
- implicitWidth: 250
- implicitHeight: 250
- function rangeUsed(count, stepSize) {
- return (((count - 1) * stepSize) / (control.maximumValue - control.minimumValue)) * control.angleRange;
- }
- readonly property real tickmarkSectionSize: rangeUsed(control.tickmarkCount, control.tickmarkStepSize) / (control.tickmarkCount - 1)
- readonly property real tickmarkSectionValue: (control.maximumValue - control.minimumValue) / (control.tickmarkCount - 1)
- readonly property real minorTickmarkSectionSize: tickmarkSectionSize / (control.minorTickmarkCount + 1)
- readonly property real minorTickmarkSectionValue: tickmarkSectionValue / (control.minorTickmarkCount + 1)
- readonly property int totalMinorTickmarkCount: {
- // The size of each section within two major tickmarks, expressed as a percentage.
- var minorSectionPercentage = 1 / (control.minorTickmarkCount + 1);
- // The amount of major tickmarks not able to be displayed; will be 0 if they all fit.
- var tickmarksNotDisplayed = control.__tickmarkCount - control.tickmarkCount;
- var count = control.minorTickmarkCount * (control.tickmarkCount - 1);
- // We'll try to display as many minor tickmarks as we can to fill up the space.
- count + tickmarksNotDisplayed / minorSectionPercentage;
- }
- readonly property real labelSectionSize: rangeUsed(control.labelCount, control.labelStepSize) / (control.labelCount - 1)
- function toPixels(percentageOfOuterRadius) {
- return percentageOfOuterRadius * outerRadius;
- }
- /*!
- Returns the angle of \a marker (in the range 0 ... n - 1, where n
- is the amount of markers) on the gauge where sections are of size
- tickmarkSectionSize.
- */
- function tickmarkAngleFromIndex(tickmarkIndex) {
- return tickmarkIndex * tickmarkSectionSize + control.minimumValueAngle;
- }
- function labelAngleFromIndex(labelIndex) {
- return labelIndex * labelSectionSize + control.minimumValueAngle;
- }
- function labelPosFromIndex(index, labelWidth, labelHeight) {
- return MathUtils.centerAlongCircle(outerRadius, outerRadius, labelWidth, labelHeight,
- MathUtils.degToRadOffset(labelAngleFromIndex(index)),
- outerRadius - control.labelInset)
- }
- function minorTickmarkAngleFromIndex(minorTickmarkIndex) {
- var baseAngle = tickmarkAngleFromIndex(Math.floor(minorTickmarkIndex / control.minorTickmarkCount));
- // + minorTickmarkSectionSize because we don't want the first minor tickmark to start on top of its "parent" tickmark.
- var relativeMinorAngle = (minorTickmarkIndex % control.minorTickmarkCount * minorTickmarkSectionSize) + minorTickmarkSectionSize;
- return baseAngle + relativeMinorAngle;
- }
- function tickmarkValueFromIndex(majorIndex) {
- return (majorIndex * tickmarkSectionValue) + control.minimumValue;
- }
- function tickmarkValueFromMinorIndex(minorIndex) {
- var majorIndex = Math.floor(minorIndex / control.minorTickmarkCount);
- var relativeMinorIndex = minorIndex % control.minorTickmarkCount;
- return tickmarkValueFromIndex(majorIndex) + ((relativeMinorIndex * minorTickmarkSectionValue) + minorTickmarkSectionValue);
- }
- Loader {
- active: control.tickmarksVisible && tickmark != null
- width: outerRadius * 2
- height: outerRadius * 2
- anchors.centerIn: parent
- sourceComponent: Repeater {
- id: tickmarkRepeater
- model: control.tickmarkCount
- delegate: Loader {
- id: tickmarkLoader
- objectName: "tickmark" + styleData.index
- x: tickmarkRepeater.width / 2
- y: tickmarkRepeater.height / 2
- transform: [
- Translate {
- y: -outerRadius + control.tickmarkInset
- },
- Rotation {
- angle: panelItem.tickmarkAngleFromIndex(styleData.index) - __tickmarkWidthAsAngle / 2
- }
- ]
- sourceComponent: tickmark
- property int __index: index
- property QtObject styleData: QtObject {
- readonly property alias index: tickmarkLoader.__index
- readonly property real value: tickmarkValueFromIndex(index)
- }
- readonly property real __tickmarkWidthAsAngle: MathUtils.radToDeg((width / (MathUtils.pi2 * outerRadius)) * MathUtils.pi2)
- }
- }
- }
- Loader {
- active: control.tickmarksVisible && minorTickmark != null
- width: outerRadius * 2
- height: outerRadius * 2
- anchors.centerIn: parent
- sourceComponent: Repeater {
- id: minorRepeater
- anchors.fill: parent
- model: totalMinorTickmarkCount
- delegate: Loader {
- id: minorTickmarkLoader
- objectName: "minorTickmark" + styleData.index
- x: minorRepeater.width / 2
- y: minorRepeater.height / 2
- transform: [
- Translate {
- y: -outerRadius + control.minorTickmarkInset
- },
- Rotation {
- angle: panelItem.minorTickmarkAngleFromIndex(styleData.index) - __minorTickmarkWidthAsAngle / 2
- }
- ]
- sourceComponent: minorTickmark
- property int __index: index
- property QtObject styleData: QtObject {
- readonly property alias index: minorTickmarkLoader.__index
- readonly property real value: tickmarkValueFromMinorIndex(index)
- }
- readonly property real __minorTickmarkWidthAsAngle: MathUtils.radToDeg((width / (MathUtils.pi2 * outerRadius)) * MathUtils.pi2)
- }
- }
- }
- Loader {
- id: labelLoader
- active: control.tickmarksVisible && tickmarkLabel != null
- width: outerRadius * 2
- height: outerRadius * 2
- anchors.centerIn: parent
- sourceComponent: Item {
- id: labelItem
- width: outerRadius * 2
- height: outerRadius * 2
- anchors.centerIn: parent
- Connections {
- target: control
- function onMinimumValueChanged() { valueTextModel.update() }
- function onMaximumValueChanged() { valueTextModel.update() }
- function onTickmarkStepSizeChanged() { valueTextModel.update() }
- function onLabelStepSizeChanged() { valueTextModel.update() }
- }
- Repeater {
- id: labelItemRepeater
- Component.onCompleted: valueTextModel.update();
- model: ListModel {
- id: valueTextModel
- function update() {
- if (control.labelStepSize === 0) {
- return;
- }
- // Make bigger if it's too small and vice versa.
- // +1 because we want to show 11 values, with, for example: 0, 10, 20... 100.
- var difference = control.labelCount - count;
- if (difference > 0) {
- for (; difference > 0; --difference) {
- append({ value: 0 });
- }
- } else if (difference < 0) {
- for (; difference < 0; ++difference) {
- remove(count - 1);
- }
- }
- var index = 0;
- for (var value = control.minimumValue;
- value <= control.maximumValue && index < count;
- value += control.labelStepSize, ++index) {
- setProperty(index, "value", value);
- }
- }
- }
- delegate: Loader {
- id: tickmarkLabelDelegateLoader
- objectName: "labelDelegateLoader" + index
- sourceComponent: tickmarkLabel
- x: pos.x
- y: pos.y
- readonly property point pos: panelItem.labelPosFromIndex(index, width, height);
- readonly property int __index: index
- readonly property real __value: value
- property QtObject styleData: QtObject {
- readonly property var value: index != -1 ? tickmarkLabelDelegateLoader.__value : 0
- readonly property alias index: tickmarkLabelDelegateLoader.__index
- }
- }
- }
- }
- }
- }
- }
|