1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Copyright (C) 2016 Rick Stockton <rickstockton@reno-computerhelp.com>
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** BSD License Usage
19** Alternatively, you may use this file under the terms of the BSD license
20** as follows:
21**
22** "Redistribution and use in source and binary forms, with or without
23** modification, are permitted provided that the following conditions are
24** met:
25** * Redistributions of source code must retain the above copyright
26** notice, this list of conditions and the following disclaimer.
27** * Redistributions in binary form must reproduce the above copyright
28** notice, this list of conditions and the following disclaimer in
29** the documentation and/or other materials provided with the
30** distribution.
31** * Neither the name of The Qt Company Ltd nor the names of its
32** contributors may be used to endorse or promote products derived
33** from this software without specific prior written permission.
34**
35**
36** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
39** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
40** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
43** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
44** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
45** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
46** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
47**
48** $QT_END_LICENSE$
49**
50****************************************************************************/
51
52#include "buttontester.h"
53
54#include <QDebug>
55
56void ButtonTester::mousePressEvent(QMouseEvent *e)
57{
58 int j = ButtonTester::buttonByNumber (button: e->button());
59 QString result = "Mouse Press: raw button=" + QString::number(j)
60 + " Qt=" + enumNameFromValue(button: e->button());
61 QString buttonsString = ButtonTester::enumNamesFromMouseButtons(buttons: e->buttons());
62 result += "\n heldbuttons " + buttonsString;
63 qDebug() << result;
64 this->setText(result);
65 this->repaint();
66}
67
68void ButtonTester::mouseReleaseEvent(QMouseEvent *e)
69{
70 int j = ButtonTester::buttonByNumber (button: e->button());
71 QString result = "Mouse Release: raw button=" + QString::number(j)
72 + " Qt=" + enumNameFromValue(button: e->button());
73 QString buttonsString = ButtonTester::enumNamesFromMouseButtons(buttons: e->buttons());
74 result += "\n heldbuttons " + buttonsString;
75 qDebug() << result;
76 this->setText(result);
77 this->repaint();
78
79}
80
81void ButtonTester::mouseDoubleClickEvent(QMouseEvent *e)
82{
83 int j = ButtonTester::buttonByNumber (button: e->button());
84 QString result = "Mouse DoubleClick: raw button=" + QString::number(j)
85 + " Qt=" + enumNameFromValue(button: e->button());
86 QString buttonsString = ButtonTester::enumNamesFromMouseButtons(buttons: e->buttons());
87 result += "\n heldbuttons" + buttonsString;
88 qDebug() << result;
89 this->setText(result);
90}
91
92#if QT_CONFIG(wheelevent)
93void ButtonTester::wheelEvent (QWheelEvent *e)
94{
95 QString result;
96 const bool vertical = qAbs(t: e->angleDelta().y()) >= qAbs(t: e->angleDelta().x());
97 const int delta = vertical ? e->angleDelta().y() : e->angleDelta().x();
98 if (delta > 0) {
99 if (vertical) {
100 result = "Mouse Wheel Event: UP";
101 } else {
102 result = "Mouse Wheel Event: LEFT";
103 }
104 } else if (delta < 0) {
105 if (vertical) {
106 result = "Mouse Wheel Event: DOWN";
107 } else {
108 result = "Mouse Wheel Event: RIGHT";
109 }
110 }
111 qDebug() << result;
112 this->setText(result);
113}
114#endif
115
116int ButtonTester::buttonByNumber(const Qt::MouseButton button)
117{
118 if (button == Qt::NoButton) return 0;
119 if (button == Qt::LeftButton) return 1;
120 if (button == Qt::RightButton) return 2;
121 if (button == Qt::MiddleButton) return 3;
122
123/* Please note that Qt Button #4 corresponds to button #8 on all
124 * platforms which EMULATE wheel events by creating button events
125 * (Button #4 = Scroll Up; Button #5 = Scroll Down; Button #6 = Scroll
126 * Left; and Button #7 = Scroll Right.) This includes X11, with both
127 * Xlib and XCB. So, the "raw button" for "Qt::BackButton" is
128 * usually described as "Button #8".
129
130 * If your platform supports "smooth scrolling", then, for the cases of
131 * Qt::BackButton and higher, this program will show "raw button" with a
132 * value which is too large. Subtract 4 to get the correct button ID for
133 * your platform.
134 */
135
136 if (button == Qt::BackButton) return 8;
137 if (button == Qt::ForwardButton) return 9;
138 if (button == Qt::TaskButton) return 10;
139 if (button == Qt::ExtraButton4) return 11;
140 if (button == Qt::ExtraButton5) return 12;
141 if (button == Qt::ExtraButton6) return 13;
142 if (button == Qt::ExtraButton7) return 14;
143 if (button == Qt::ExtraButton8) return 15;
144 if (button == Qt::ExtraButton9) return 16;
145 if (button == Qt::ExtraButton10) return 17;
146 if (button == Qt::ExtraButton11) return 18;
147 if (button == Qt::ExtraButton12) return 19;
148 if (button == Qt::ExtraButton13) return 20;
149 if (button == Qt::ExtraButton14) return 21;
150 if (button == Qt::ExtraButton15) return 22;
151 if (button == Qt::ExtraButton16) return 23;
152 if (button == Qt::ExtraButton17) return 24;
153 if (button == Qt::ExtraButton18) return 25;
154 if (button == Qt::ExtraButton19) return 26;
155 if (button == Qt::ExtraButton20) return 27;
156 if (button == Qt::ExtraButton21) return 28;
157 if (button == Qt::ExtraButton22) return 29;
158 if (button == Qt::ExtraButton23) return 30;
159 if (button == Qt::ExtraButton24) return 31;
160 qDebug(msg: "QMouseShortcutEntry::addShortcut contained Invalid Qt::MouseButton value");
161 return 0;
162}
163
164QString ButtonTester::enumNameFromValue(const Qt::MouseButton button)
165{
166 if (button == Qt::NoButton) return "NoButton";
167 if (button == Qt::LeftButton) return "LeftButton";
168 if (button == Qt::RightButton) return "RightButton";
169 if (button == Qt::MiddleButton) return "MiddleButton";
170 if (button == Qt::BackButton) return "BackButton";
171 if (button == Qt::ForwardButton) return "ForwardButton";
172 if (button == Qt::TaskButton) return "TaskButton";
173 if (button == Qt::ExtraButton4) return "ExtraButton4";
174 if (button == Qt::ExtraButton5) return "ExtraButton5";
175 if (button == Qt::ExtraButton6) return "ExtraButton6";
176 if (button == Qt::ExtraButton7) return "ExtraButton7";
177 if (button == Qt::ExtraButton8) return "ExtraButton8";
178 if (button == Qt::ExtraButton9) return "ExtraButton9";
179 if (button == Qt::ExtraButton10) return "ExtraButton10";
180 if (button == Qt::ExtraButton11) return "ExtraButton11";
181 if (button == Qt::ExtraButton12) return "ExtraButton12";
182 if (button == Qt::ExtraButton13) return "ExtraButton13";
183 if (button == Qt::ExtraButton14) return "ExtraButton14";
184 if (button == Qt::ExtraButton15) return "ExtraButton15";
185 if (button == Qt::ExtraButton16) return "ExtraButton16";
186 if (button == Qt::ExtraButton17) return "ExtraButton17";
187 if (button == Qt::ExtraButton18) return "ExtraButton18";
188 if (button == Qt::ExtraButton19) return "ExtraButton19";
189 if (button == Qt::ExtraButton20) return "ExtraButton20";
190 if (button == Qt::ExtraButton21) return "ExtraButton21";
191 if (button == Qt::ExtraButton22) return "ExtraButton22";
192 if (button == Qt::ExtraButton23) return "ExtraButton23";
193 if (button == Qt::ExtraButton24) return "ExtraButton24";
194 qDebug(msg: "QMouseShortcutEntry::addShortcut contained Invalid Qt::MouseButton value");
195 return "NoButton";
196}
197
198QString ButtonTester::enumNamesFromMouseButtons(const Qt::MouseButtons buttons)
199{
200 QString returnText = "";
201 if (buttons == Qt::NoButton) return "NoButton";
202 if (buttons & Qt::LeftButton) returnText += "LeftButton ";
203 if (buttons & Qt::RightButton) returnText += "RightButton ";
204 if (buttons & Qt::MiddleButton) returnText += "MiddleButton ";
205 if (buttons & Qt::BackButton) returnText += "BackButton ";
206 if (buttons & Qt::ForwardButton) returnText += "ForwardButton ";
207 if (buttons & Qt::TaskButton) returnText += "TaskButton ";
208 if (buttons & Qt::ExtraButton4) returnText += "ExtraButton4 ";
209 if (buttons & Qt::ExtraButton5) returnText += "ExtraButton5 ";
210 if (buttons & Qt::ExtraButton6) returnText += "ExtraButton6 ";
211 if (buttons & Qt::ExtraButton7) returnText += "ExtraButton7 ";
212 if (buttons & Qt::ExtraButton8) returnText += "ExtraButton8 ";
213 if (buttons & Qt::ExtraButton9) returnText += "ExtraButton9 ";
214 if (buttons & Qt::ExtraButton10) returnText += "ExtraButton10 ";
215 if (buttons & Qt::ExtraButton11) returnText += "ExtraButton11 ";
216 if (buttons & Qt::ExtraButton12) returnText += "ExtraButton12 ";
217 if (buttons & Qt::ExtraButton13) returnText += "ExtraButton13 ";
218 if (buttons & Qt::ExtraButton14) returnText += "ExtraButton14 ";
219 if (buttons & Qt::ExtraButton15) returnText += "ExtraButton15 ";
220 if (buttons & Qt::ExtraButton16) returnText += "ExtraButton16 ";
221 if (buttons & Qt::ExtraButton17) returnText += "ExtraButton17 ";
222 if (buttons & Qt::ExtraButton18) returnText += "ExtraButton18 ";
223 if (buttons & Qt::ExtraButton19) returnText += "ExtraButton19 ";
224 if (buttons & Qt::ExtraButton20) returnText += "ExtraButton20 ";
225 if (buttons & Qt::ExtraButton21) returnText += "ExtraButton21 ";
226 if (buttons & Qt::ExtraButton22) returnText += "ExtraButton22 ";
227 if (buttons & Qt::ExtraButton23) returnText += "ExtraButton23 ";
228 if (buttons & Qt::ExtraButton24) returnText += "ExtraButton24 ";
229 return returnText;
230}
231
232

source code of qtbase/examples/widgets/widgets/mousebuttons/buttontester.cpp