Update MouseArea and KeyInteraction to new guidelines
[profile/ivi/qtdeclarative.git] / examples / quick / mousearea / mousearea.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 /*!
44     \title QtQuick Examples - MouseArea
45     \example quick/mousearea
46     \brief This is a collection of QML Animation examples.
47     \image qml-mousearea-example.png
48
49     This example shows you how to respond to clicks and drags with a MouseArea.
50 */
51
52 Rectangle {
53     id: box
54     width: 320; height: 480
55
56     Rectangle {
57         id: redSquare
58         width: 120; height: 120
59         anchors.top: parent.top; anchors.left: parent.left; anchors.margins: 10
60         color: "red"
61
62         Text { text: "Click"; font.pixelSize: 16; anchors.centerIn: parent }
63
64         MouseArea {
65             anchors.fill: parent
66             hoverEnabled: true
67             property string buttonID
68
69             acceptedButtons: Qt.AllButtons
70             // Value 'All.Buttons' is eqivalent to:
71             // 'Qt::LeftButton | Qt::RightButton | Qt::MiddleButton  .... | Qt::ExtraButton24'
72
73             onEntered: info.text = 'Entered'
74             onExited: info.text = 'Exited (pressed=' + pressed + ')'
75
76             onPressed: {
77                 if (mouse.button == Qt.LeftButton)
78                     buttonID = 'LeftButton'
79                 else if (mouse.button == Qt.RightButton)
80                     buttonID = 'RightButton'
81                 else if (mouse.button == Qt.MidButton)
82                     buttonID = 'MiddleButton'
83                 else if (mouse.button == Qt.BackButton)
84                     buttonID = 'BackButton'
85                 else if (mouse.button == Qt.ForwardButton)
86                     buttonID = 'ForwardButton'
87                 else if (mouse.button == Qt.TaskButton)
88                     buttonID = 'TaskButton'
89                 else if (mouse.button == Qt.ExtraButton4)
90                     buttonID = 'ExtraButton4'
91                 else if (mouse.button == Qt.ExtraButton5)
92                     buttonID = 'ExtraButton5'
93                 else if (mouse.button == Qt.ExtraButton6)
94                     buttonID = 'ExtraButton6'
95                 else if (mouse.button == Qt.ExtraButton7)
96                     buttonID = 'ExtraButton7'
97                 else if (mouse.button == Qt.ExtraButton8)
98                     buttonID = 'ExtraButton8'
99                 else if (mouse.button == Qt.ExtraButton9)
100                     buttonID = 'ExtraButton9'
101                 else if (mouse.button == Qt.ExtraButton10)
102                     buttonID = 'ExtraButton10'
103                 else if (mouse.button == Qt.ExtraButton11)
104                     buttonID = 'ExtraButton11'
105                 else if (mouse.button == Qt.ExtraButton12)
106                     buttonID = 'ExtraButton12'
107                 else if (mouse.button == Qt.ExtraButton13)
108                     buttonID = 'ExtraButton13'
109                 else if (mouse.button == Qt.ExtraButton14)
110                     buttonID = 'ExtraButton14'
111                 else if (mouse.button == Qt.ExtraButton15)
112                     buttonID = 'ExtraButton15'
113                 else if (mouse.button == Qt.ExtraButton16)
114                     buttonID = 'ExtraButton16'
115                 else if (mouse.button == Qt.ExtraButton17)
116                     buttonID = 'ExtraButton17'
117                 else if (mouse.button == Qt.ExtraButton18)
118                     buttonID = 'ExtraButton18'
119                 else if (mouse.button == Qt.ExtraButton19)
120                     buttonID = 'ExtraButton19'
121                 else if (mouse.button == Qt.ExtraButton20)
122                     buttonID = 'ExtraButton20'
123                 else if (mouse.button == Qt.ExtraButton21)
124                     buttonID = 'ExtraButton21'
125                 else if (mouse.button == Qt.ExtraButton22)
126                     buttonID = 'ExtraButton22'
127                 else if (mouse.button == Qt.ExtraButton23)
128                     buttonID = 'ExtraButton23'
129                 else if (mouse.button == Qt.ExtraButton24)
130                     buttonID = 'ExtraButton24'
131
132                 info.text = 'Pressed (' + buttonID + ' shift='
133                     + (mouse.modifiers & Qt.ShiftModifier ? 'true' : 'false') + ')'
134                 var posInBox = redSquare.mapToItem(box, mouse.x, mouse.y)
135                 posInfo.text = + mouse.x + ',' + mouse.y + ' in square'
136                         + ' (' + posInBox.x + ',' + posInBox.y + ' in window)'
137             }
138
139             onReleased: {
140                 btn.text = 'Released (isClick=' + mouse.isClick + ' wasHeld=' + mouse.wasHeld + ')'
141                 posInfo.text = ''
142             }
143
144             onPressAndHold: btn.text = 'Press and hold'
145             onClicked: btn.text = 'Clicked (wasHeld=' + mouse.wasHeld + ')'
146             onDoubleClicked: btn.text = 'Double clicked'
147         }
148     }
149
150     Rectangle {
151         id: blueSquare
152         width: 120; height: 120
153         x: box.width - width - 10; y: 10    // making this item draggable, so don't use anchors
154         color: "blue"
155
156         Text { text: "Drag"; font.pixelSize: 16; color: "white"; anchors.centerIn: parent }
157
158         MouseArea {
159             anchors.fill: parent
160             drag.target: blueSquare
161             drag.axis: Drag.XandYAxis
162             drag.minimumX: 0
163             drag.maximumX: box.width - parent.width
164             drag.minimumY: 0
165             drag.maximumY: box.height - parent.width
166         }
167     }
168
169     Text {
170         id: info
171         anchors.bottom: btn.top; anchors.horizontalCenter: parent.horizontalCenter; anchors.margins: 20
172
173         onTextChanged: console.log(text)
174     }
175
176     Text {
177         id: btn
178         anchors.bottom: posInfo.top; anchors.horizontalCenter: parent.horizontalCenter; anchors.margins: 20
179     }
180
181     Text {
182         id: posInfo
183         anchors.bottom: parent.bottom; anchors.horizontalCenter: parent.horizontalCenter; anchors.margins: 20
184     }
185 }