~nik90/ubuntu-clock-app/sync-clock-backend

« back to all changes in this revision

Viewing changes to timer/AnalogTouchHand.qml

  • Committer: Nekhelesh Ramananthan
  • Date: 2013-09-27 08:38:15 UTC
  • mfrom: (200.1.4 trunk)
  • Revision ID: krnekhelesh@gmail.com-20130927083815-mu1n8e5wlp2to930
merged trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2013 Canonical Ltd
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License version 3 as
6
 
 * published by the Free Software Foundation.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful,
9
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
 * GNU General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License
14
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 *
16
 
 * Authored by: Nekhelesh Ramananthan <krnekhelesh@gmail.com>
17
 
 */
18
 
 
19
 
import QtQuick 2.0
20
 
import Ubuntu.Components 0.1
21
 
import "../common"
22
 
 
23
 
// Qml item to enable touch input to the AnalogClockHand component
24
 
Item {
25
 
    id: touchHand;
26
 
 
27
 
    z: parent.z;
28
 
    width: parent.width; height: width;
29
 
    anchors.centerIn: parent;
30
 
 
31
 
    /*!
32
 
      Enable/Disable rotation animation. Disable it while dragging the clock hand to avoid
33
 
      receiving inaccurate input values.
34
 
      */
35
 
    property alias animateFlag: hand.handRotationAnimation
36
 
 
37
 
    /*!
38
 
      Set if the dial should be rotated by the seconds, minutes or hours. This generally needs to be
39
 
      multiplied by 6 to convert it into degrees per second/hour/minute.
40
 
      */
41
 
    property alias rotationValue: hand.rotation;
42
 
 
43
 
    // Set the touch/mouse grab area height and top margin
44
 
    property alias grabHeight: grabArea.height;
45
 
    property int grabMargin: units.gu(0);
46
 
 
47
 
    // Height and width of the clock hand
48
 
    property alias handHeight: hand.handHeight;
49
 
    property alias handWidth: hand.handWidth;
50
 
 
51
 
    // internal properties required for the functioning of the AnalogTouchHand
52
 
    property int timerValue: 0
53
 
    property real centerX : width / 2
54
 
    property real centerY : height / 2
55
 
 
56
 
    // Clock hand which lies above the rotating dial. This is stationary w.r.t to the rotating dial.
57
 
    AnalogClockHand {
58
 
        id: hand
59
 
 
60
 
        handHeight: units.gu(0); handWidth: units.gu(0);
61
 
 
62
 
        /*!
63
 
              Element to define the grab area of the clock hand. This allows us to define small clock hand
64
 
              which are still easier to grab.
65
 
              */
66
 
        Rectangle {
67
 
            id: grabArea
68
 
            color: "transparent"
69
 
            width: units.gu(5); height: width;
70
 
            anchors { top: hand.top; topMargin: touchHand.grabMargin; horizontalCenter: hand.horizontalCenter }
71
 
        }
72
 
 
73
 
        MouseArea{
74
 
            anchors.fill: grabArea;
75
 
            preventStealing: true;
76
 
            enabled: true;
77
 
            onPositionChanged:  {
78
 
                var point =  mapToItem (touchHand, mouse.x, mouse.y);
79
 
                var diffX = (point.x - touchHand.centerX);
80
 
                var diffY = -1 * (point.y - touchHand.centerY);
81
 
                var rad = Math.atan (diffY / diffX);
82
 
                var deg = (rad * 180 / Math.PI);
83
 
 
84
 
                if (diffX > 0 && diffY > 0) {
85
 
                    hand.rotation = 90 - Math.abs (deg);
86
 
                }
87
 
                else if (diffX > 0 && diffY < 0) {
88
 
                    hand.rotation = 90 + Math.abs (deg);
89
 
                }
90
 
                else if (diffX < 0 && diffY > 0) {
91
 
                    hand.rotation = 270 + Math.abs (deg);
92
 
                }
93
 
                else if (diffX < 0 && diffY < 0) {
94
 
                    hand.rotation = 270 - Math.abs (deg);
95
 
                }
96
 
 
97
 
                if (Math.round(hand.rotation/6) === 60) {
98
 
                    touchHand.timerValue = 0
99
 
                } else {
100
 
                    touchHand.timerValue = Math.round(hand.rotation/6);
101
 
                }
102
 
            }
103
 
        }
104
 
    }
105
 
}