summaryrefslogtreecommitdiff
path: root/src/extras/Styles/Flat/PieMenuStyle.qml
blob: 8ac971a5898c111db5a4ec9436123cd02fdb054a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/****************************************************************************
**
** Copyright (C) 2021 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:COMM$
**
** 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.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

import QtQuick 2.3
import QtQuick.Controls.Styles 1.4 as Base
import QtQuick.Controls.Styles.Flat 1.0
import QtQuick.Extras.Private 1.0

Base.PieMenuStyle {
    id: pieMenuStyle
    shadowRadius: 0
    shadowSpread: 0
    shadowColor: "transparent"
    backgroundColor: FlatStyle.styleColor
    selectionColor: FlatStyle.styleColor
    startAngle: -78
    endAngle: 78
    cancelRadius: radius * 0.4352

    __implicitWidth: Math.round(85 * 2 * FlatStyle.scaleFactor)
    __implicitHeight: __implicitWidth

    title: null

    menuItem: Item {
        readonly property var __styleData: styleData

        Canvas {
            id: actionCanvas
            antialiasing: true
            anchors.fill: parent
            anchors.margins: -1

            Connections {
                target: pieMenuStyle
                function onStartAngleChanged() { actionCanvas.requestPaint() }
                function onEndAngleChanged() { actionCanvas.requestPaint() }
            }

            Connections {
                target: __styleData
                function onPressedChanged() { actionCanvas.requestPaint() }
            }

            readonly property bool stroke: !__styleData.hovered
            readonly property real spacingThickness: Math.max(4 * FlatStyle.scaleFactor, radius * 0.05)
            // It's actually one, but stroking always goes 50/50 on the inside/outside, so this is
            // an easy way of making it one pixel without having to mess around with more drawing code.
            // This will work as long as the fill and stroke colors are both completely opaque.
            readonly property real lineWidth: FlatStyle.twoPixels
            // The following properties are all for either side of a radius/circumference, so the total is actually double.
            readonly property real outerMenuItemSpacing: Math.asin(spacingThickness / (2 * radius))
            readonly property real innerMenuItemSpacing: Math.asin(spacingThickness / (2 * cancelRadius))
            // The total angle to subtract from the circumference of the inner radius arc
            readonly property real innerRadiusStrokeCircumferenceAdjustment: Math.asin(lineWidth / (2 * cancelRadius))
            readonly property real outerRadiusStrokeCircumferenceAdjustment: Math.asin(lineWidth / (2 * radius))

            onStrokeChanged: actionCanvas.requestPaint()

            function drawRingSection(ctx, x, y, section, r, ringWidth, ringColor) {
                if (stroke) {
                    ctx.strokeStyle = ringColor;
                    ctx.fillStyle = "white";
                } else {
                    ctx.fillStyle = ringColor;
                }

                ctx.beginPath();

                if (stroke)
                    ctx.lineWidth = lineWidth;
                var start = control.__protectedScope.sectionStartAngle(section);
                var end = control.__protectedScope.sectionEndAngle(section);
                // Adjust the radius to account for stroke being 50% inside/50% outside.
                var radius = !stroke ? r : r - ctx.lineWidth / 2;
                // Add spacing between the items, while still accounting for reversed menus.
                if (start > end) {
                    start -= outerMenuItemSpacing;
                    end += outerMenuItemSpacing;
                    if (stroke) {
                        start -= outerRadiusStrokeCircumferenceAdjustment;
                        end += outerRadiusStrokeCircumferenceAdjustment;
                    }
                } else {
                    start += outerMenuItemSpacing;
                    end -= outerMenuItemSpacing;
                    if (stroke) {
                        start += outerRadiusStrokeCircumferenceAdjustment;
                        end -= outerRadiusStrokeCircumferenceAdjustment;
                    }
                }

                ctx.arc(x, y, radius, start, end, start > end);

                start = control.__protectedScope.sectionStartAngle(section);
                end = control.__protectedScope.sectionEndAngle(section);
                radius = !stroke ? ringWidth : ringWidth + ctx.lineWidth / 2;
                if (start > end) {
                    start -= innerMenuItemSpacing;
                    end += innerMenuItemSpacing;
                    if (stroke) {
                        start -= innerRadiusStrokeCircumferenceAdjustment;
                        end += innerRadiusStrokeCircumferenceAdjustment;
                    }
                } else {
                    start += innerMenuItemSpacing;
                    end -= innerMenuItemSpacing;
                    if (stroke) {
                        start += innerRadiusStrokeCircumferenceAdjustment;
                        end -= innerRadiusStrokeCircumferenceAdjustment;
                    }
                }

                ctx.arc(x, y, radius, end, start, start <= end);
                ctx.closePath();

                if (stroke)
                    ctx.stroke();

                ctx.fill();
            }

            onPaint: {
                var ctx = getContext("2d");
                ctx.reset();
                drawRingSection(ctx, width / 2, height / 2, styleData.index, radius, cancelRadius,
                    __styleData.pressed ? FlatStyle.pressedColorAlt : (stroke ? backgroundColor : selectionColor));
            }
        }

        PieMenuIcon {
            control: pieMenuStyle.control
            styleData: __styleData
        }
    }
}