Handle MouseArea.enabled = false after mouse is pressed.
[profile/ivi/qtdeclarative.git] / examples / qml / touchinteraction / mousearea / mousearea-example.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 **     the names of its contributors may be used to endorse or promote
22 **     products derived from this software without specific prior written
23 **     permission.
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 import QtQuick 2.0
42
43 Rectangle {
44     id: box
45     width: 400; height: 300
46
47     Rectangle {
48         id: redSquare
49         width: 120; height: 120
50         anchors.top: parent.top; anchors.left: parent.left; anchors.margins: 10
51         color: "red"
52
53         Text { text: "Click"; font.pixelSize: 16; anchors.centerIn: parent }
54
55         MouseArea {
56             anchors.fill: parent
57             hoverEnabled: true
58             property string buttonID
59
60             acceptedButtons: Qt.AllButtons
61             // Value 'All.Buttons' is eqivalent to:
62             // 'Qt::LeftButton | Qt::RightButton | Qt::MiddleButton  .... | Qt::ExtraButton24'
63
64             onEntered: info.text = 'Entered'
65             onExited: info.text = 'Exited (pressed=' + pressed + ')'
66
67             onPressed: {
68                 if (mouse.button == Qt.LeftButton)
69                     buttonID = 'LeftButton'
70                 else if (mouse.button == Qt.RightButton)
71                     buttonID = 'RightButton'
72                 else if (mouse.button == Qt.MidButton)
73                     buttonID = 'MiddleButton'
74                 else if (mouse.button == Qt.BackButton)
75                     buttonID = 'BackButton'
76                 else if (mouse.button == Qt.ForwardButton)
77                     buttonID = 'ForwardButton'
78                 else if (mouse.button == Qt.TaskButton)
79                     buttonID = 'TaskButton'
80                 else if (mouse.button == Qt.ExtraButton4)
81                     buttonID = 'ExtraButton4'
82                 else if (mouse.button == Qt.ExtraButton5)
83                     buttonID = 'ExtraButton5'
84                 else if (mouse.button == Qt.ExtraButton6)
85                     buttonID = 'ExtraButton6'
86                 else if (mouse.button == Qt.ExtraButton7)
87                     buttonID = 'ExtraButton7'
88                 else if (mouse.button == Qt.ExtraButton8)
89                     buttonID = 'ExtraButton8'
90                 else if (mouse.button == Qt.ExtraButton9)
91                     buttonID = 'ExtraButton9'
92                 else if (mouse.button == Qt.ExtraButton10)
93                     buttonID = 'ExtraButton10'
94                 else if (mouse.button == Qt.ExtraButton11)
95                     buttonID = 'ExtraButton11'
96                 else if (mouse.button == Qt.ExtraButton12)
97                     buttonID = 'ExtraButton12'
98                 else if (mouse.button == Qt.ExtraButton13)
99                     buttonID = 'ExtraButton13'
100                 else if (mouse.button == Qt.ExtraButton14)
101                     buttonID = 'ExtraButton14'
102                 else if (mouse.button == Qt.ExtraButton15)
103                     buttonID = 'ExtraButton15'
104                 else if (mouse.button == Qt.ExtraButton16)
105                     buttonID = 'ExtraButton16'
106                 else if (mouse.button == Qt.ExtraButton17)
107                     buttonID = 'ExtraButton17'
108                 else if (mouse.button == Qt.ExtraButton18)
109                     buttonID = 'ExtraButton18'
110                 else if (mouse.button == Qt.ExtraButton19)
111                     buttonID = 'ExtraButton19'
112                 else if (mouse.button == Qt.ExtraButton20)
113                     buttonID = 'ExtraButton20'
114                 else if (mouse.button == Qt.ExtraButton21)
115                     buttonID = 'ExtraButton21'
116                 else if (mouse.button == Qt.ExtraButton22)
117                     buttonID = 'ExtraButton22'
118                 else if (mouse.button == Qt.ExtraButton23)
119                     buttonID = 'ExtraButton23'
120                 else if (mouse.button == Qt.ExtraButton24)
121                     buttonID = 'ExtraButton24'
122
123                 info.text = 'Pressed (' + buttonID + ' shift='
124                     + (mouse.modifiers & Qt.ShiftModifier ? 'true' : 'false') + ')'
125                 var posInBox = redSquare.mapToItem(box, mouse.x, mouse.y)
126                 posInfo.text = + mouse.x + ',' + mouse.y + ' in square'
127                         + ' (' + posInBox.x + ',' + posInBox.y + ' in window)'
128             }
129
130             onReleased: {
131                 btn.text = 'Released (isClick=' + mouse.isClick + ' wasHeld=' + mouse.wasHeld + ')'
132                 posInfo.text = ''
133             }
134
135             onPressAndHold: btn.text = 'Press and hold'
136             onClicked: btn.text = 'Clicked (wasHeld=' + mouse.wasHeld + ')'
137             onDoubleClicked: btn.text = 'Double clicked'
138         }
139     }
140
141     Rectangle {
142         id: blueSquare
143         width: 120; height: 120
144         x: box.width - width - 10; y: 10    // making this item draggable, so don't use anchors
145         color: "blue"
146
147         Text { text: "Drag"; font.pixelSize: 16; color: "white"; anchors.centerIn: parent }
148
149         MouseArea {
150             anchors.fill: parent
151             drag.target: blueSquare
152             drag.axis: Drag.XandYAxis
153             drag.minimumX: 0
154             drag.maximumX: box.width - parent.width
155             drag.minimumY: 0
156             drag.maximumY: box.height - parent.width
157         }
158     }
159
160     Text {
161         id: info
162         anchors.bottom: btn.top; anchors.horizontalCenter: parent.horizontalCenter; anchors.margins: 20
163
164         onTextChanged: console.log(text)
165     }
166
167     Text {
168         id: btn
169         anchors.bottom: posInfo.top; anchors.horizontalCenter: parent.horizontalCenter; anchors.margins: 20
170     }
171
172     Text {
173         id: posInfo
174         anchors.bottom: parent.bottom; anchors.horizontalCenter: parent.horizontalCenter; anchors.margins: 20
175     }
176 }