~feng-kylin/youker-assistant/youker-assistant

« back to all changes in this revision

Viewing changes to qml/func/common/Dial.qml

  • Committer: kobe
  • Date: 2015-02-13 07:37:10 UTC
  • Revision ID: xiangli@ubuntukylin.com-20150213073710-0jyp02ilyi5njj10
Qt Version

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
**
3
 
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4
 
** All rights reserved.
5
 
** Contact: Nokia Corporation (qt-info@nokia.com)
6
 
**
7
 
** This file is part of the Qt Components project on Qt Labs.
8
 
**
9
 
** No Commercial Usage
10
 
** This file contains pre-release code and may not be distributed.
11
 
** You may use this file in accordance with the terms and conditions contained
12
 
** in the Technology Preview License Agreement accompanying this package.
13
 
**
14
 
** GNU Lesser General Public License Usage
15
 
** Alternatively, this file may be used under the terms of the GNU Lesser
16
 
** General Public License version 2.1 as published by the Free Software
17
 
** Foundation and appearing in the file LICENSE.LGPL included in the
18
 
** packaging of this file.  Please review the following information to
19
 
** ensure the GNU Lesser General Public License version 2.1 requirements
20
 
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
21
 
**
22
 
** If you have questions regarding the use of this file, please contact
23
 
** Nokia at qt-info@nokia.com.
24
 
**
25
 
****************************************************************************/
26
 
 
27
 
import QtQuick 1.1
28
 
import StyleItemType 0.1
29
 
import WheelAreaType 0.1
30
 
// jens: ContainsMouse breaks drag functionality
31
 
 
32
 
Item {
33
 
    id: dial
34
 
 
35
 
    width: 100
36
 
    height: 100
37
 
 
38
 
    property alias maximumValue: range.maximumValue
39
 
    property alias minimumValue: range.minimumValue
40
 
    property alias containsMouse: mouseArea.containsMouse
41
 
    property alias value: range.value
42
 
    property alias stepSize: range.stepSize
43
 
 
44
 
    property bool wrapping: false
45
 
    property bool tickmarksEnabled: false
46
 
    property bool activeFocusOnPress: false
47
 
 
48
 
    RangeModel {
49
 
        id: range
50
 
        minimumValue: 0.0
51
 
        maximumValue: 1.0
52
 
        stepSize: 0.0
53
 
        value: 0
54
 
    }
55
 
 
56
 
    MouseArea {
57
 
        id: mouseArea
58
 
        anchors.fill:parent
59
 
        property bool inDrag
60
 
        hoverEnabled:true
61
 
 
62
 
        onPositionChanged: {
63
 
            if (pressed) {
64
 
                value = valueFromPoint(mouseX, mouseY)
65
 
                inDrag = true
66
 
            }
67
 
        }
68
 
        onPressed: {
69
 
            value = valueFromPoint(mouseX, mouseY)
70
 
             if (activeFocusOnPress) dial.focus = true
71
 
        }
72
 
 
73
 
        onReleased:inDrag = false;
74
 
        function bound(val) { return Math.max(minimumValue, Math.min(maximumValue, val)); }
75
 
 
76
 
        function valueFromPoint(x, y)
77
 
        {
78
 
            var yy = height/2.0 - y;
79
 
            var xx = x - width/2.0;
80
 
            var a = (xx || yy) ? Math.atan2(yy, xx) : 0;
81
 
 
82
 
            if (a < Math.PI/ -2)
83
 
                a = a + Math.PI * 2;
84
 
 
85
 
            var dist = 0;
86
 
            var minv = minimumValue*100, maxv = maximumValue*100;
87
 
 
88
 
            if (minimumValue < 0) {
89
 
                dist = -minimumValue;
90
 
                minv = 0;
91
 
                maxv = maximumValue + dist;
92
 
            }
93
 
 
94
 
            var r = maxv - minv;
95
 
            var v;
96
 
            if (wrapping)
97
 
                v =  (0.5 + minv + r * (Math.PI * 3 / 2 - a) / (2 * Math.PI));
98
 
            else
99
 
                v =  (0.5 + minv + r* (Math.PI * 4 / 3 - a) / (Math.PI * 10 / 6));
100
 
 
101
 
            if (dist > 0)
102
 
                v -= dist;
103
 
            return maximumValue - bound(v/100)
104
 
        }
105
 
    }
106
 
    StyleItem {
107
 
        anchors.fill: parent
108
 
        elementType: "dial"
109
 
        hasFocus: dial.focus
110
 
        sunken: mouseArea.pressed
111
 
        maximum: range.maximumValue * 100
112
 
        minimum: range.minimumValue * 100
113
 
        value: visualPos * 100
114
 
        enabled: dial.enabled
115
 
        step: range.stepSize * 100
116
 
        activeControl: tickmarksEnabled ? "tick" : ""
117
 
        property double visualPos : range.value
118
 
 
119
 
        Behavior on visualPos {
120
 
            enabled: !mouseArea.inDrag
121
 
            NumberAnimation {
122
 
                duration: 300
123
 
                easing.type: Easing.OutSine
124
 
            }
125
 
        }
126
 
    }
127
 
    WheelArea {
128
 
        id: wheelarea
129
 
        anchors.fill: parent
130
 
        horizontalMinimumValue: dial.minimumValue
131
 
        horizontalMaximumValue: dial.maximumValue
132
 
        verticalMinimumValue: dial.minimumValue
133
 
        verticalMaximumValue: dial.maximumValue
134
 
        property double step: (dial.maximumValue - dial.minimumValue)/100
135
 
 
136
 
        onVerticalWheelMoved: {
137
 
            value += verticalDelta/4*step
138
 
        }
139
 
 
140
 
        onHorizontalWheelMoved: {
141
 
            value += horizontalDelta/4*step
142
 
        }
143
 
    }
144
 
}