1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "inspecttool.h" |
5 | #include "highlight.h" |
6 | #include "qquickwindowinspector.h" |
7 | #include "globalinspector.h" |
8 | |
9 | #include <QtCore/QLineF> |
10 | |
11 | #include <QtGui/QMouseEvent> |
12 | #include <QtGui/QTouchEvent> |
13 | #include <QtGui/QKeyEvent> |
14 | #include <QtGui/QGuiApplication> |
15 | #include <QtGui/QStyleHints> |
16 | |
17 | #include <QtQuick/QQuickView> |
18 | #include <QtQuick/QQuickItem> |
19 | |
20 | QT_BEGIN_NAMESPACE |
21 | |
22 | namespace QmlJSDebugger { |
23 | |
24 | InspectTool::InspectTool(QQuickWindowInspector *inspector, QQuickWindow *view) : |
25 | QObject(inspector), |
26 | m_contentItem(view->contentItem()), |
27 | m_touchTimestamp(0), |
28 | m_hoverHighlight(new HoverHighlight(inspector->overlay())), |
29 | m_lastItem(nullptr), |
30 | m_lastClickedItem(nullptr) |
31 | { |
32 | //Timer to display selected item's name |
33 | m_nameDisplayTimer.setSingleShot(true); |
34 | m_nameDisplayTimer.setInterval(QGuiApplication::styleHints()->mouseDoubleClickInterval()); |
35 | connect(sender: &m_nameDisplayTimer, signal: &QTimer::timeout, context: this, slot: &InspectTool::showItemName); |
36 | } |
37 | |
38 | void InspectTool::enterEvent(QEnterEvent *) |
39 | { |
40 | m_hoverHighlight->setVisible(true); |
41 | } |
42 | |
43 | void InspectTool::leaveEvent(QEvent *) |
44 | { |
45 | m_hoverHighlight->setVisible(false); |
46 | } |
47 | |
48 | void InspectTool::mousePressEvent(QMouseEvent *event) |
49 | { |
50 | m_mousePosition = event->position(); |
51 | if (event->button() == Qt::LeftButton) { |
52 | selectItem(); |
53 | m_hoverHighlight->setVisible(false); |
54 | } |
55 | } |
56 | |
57 | void InspectTool::mouseDoubleClickEvent(QMouseEvent *event) |
58 | { |
59 | m_mousePosition = event->position(); |
60 | if (event->button() == Qt::LeftButton) { |
61 | selectNextItem(); |
62 | m_hoverHighlight->setVisible(false); |
63 | } |
64 | } |
65 | |
66 | void InspectTool::mouseMoveEvent(QMouseEvent *event) |
67 | { |
68 | hoverMoveEvent(event); |
69 | } |
70 | |
71 | void InspectTool::hoverMoveEvent(QMouseEvent *event) |
72 | { |
73 | m_mousePosition = event->position(); |
74 | QQuickItem *item = inspector()->topVisibleItemAt(pos: event->position().toPoint()); |
75 | if (!item || item == m_lastClickedItem) { |
76 | m_hoverHighlight->setVisible(false); |
77 | } else { |
78 | m_hoverHighlight->setItem(item); |
79 | m_hoverHighlight->setVisible(true); |
80 | } |
81 | } |
82 | |
83 | void InspectTool::touchEvent(QTouchEvent *event) |
84 | { |
85 | const auto &touchPoints = event->points(); |
86 | |
87 | switch (event->type()) { |
88 | case QEvent::TouchBegin: |
89 | if (touchPoints.size() == 1 && (event->touchPointStates() & QEventPoint::State::Pressed)) { |
90 | m_mousePosition = touchPoints.first().position(); |
91 | m_tapEvent = true; |
92 | } else { |
93 | m_tapEvent = false; |
94 | } |
95 | break; |
96 | case QEvent::TouchUpdate: { |
97 | if (touchPoints.size() > 1) |
98 | m_tapEvent = false; |
99 | else if ((touchPoints.size() == 1) && (event->touchPointStates() & QEventPoint::State::Updated)) |
100 | m_mousePosition = touchPoints.first().position(); |
101 | break; |
102 | } |
103 | case QEvent::TouchEnd: { |
104 | if (touchPoints.size() == 1 && m_tapEvent) { |
105 | m_tapEvent = false; |
106 | bool doubleTap = event->timestamp() - m_touchTimestamp |
107 | < static_cast<ulong>(QGuiApplication::styleHints()->mouseDoubleClickInterval()); |
108 | if (doubleTap) { |
109 | m_nameDisplayTimer.stop(); |
110 | selectNextItem(); |
111 | } else { |
112 | selectItem(); |
113 | } |
114 | m_touchTimestamp = event->timestamp(); |
115 | } |
116 | break; |
117 | } |
118 | default: |
119 | break; |
120 | } |
121 | } |
122 | |
123 | void InspectTool::selectNextItem() |
124 | { |
125 | if (m_lastClickedItem != inspector()->topVisibleItemAt(pos: m_mousePosition)) |
126 | return; |
127 | QList<QQuickItem*> items = inspector()->itemsAt(pos: m_mousePosition); |
128 | for (int i = 0; i < items.size(); i++) { |
129 | if (m_lastItem == items[i]) { |
130 | if (i + 1 < items.size()) |
131 | m_lastItem = items[i+1]; |
132 | else |
133 | m_lastItem = items[0]; |
134 | globalInspector()->setSelectedItems(QList<QQuickItem*>() << m_lastItem); |
135 | showItemName(); |
136 | break; |
137 | } |
138 | } |
139 | } |
140 | |
141 | void InspectTool::selectItem() |
142 | { |
143 | if (!inspector()->topVisibleItemAt(pos: m_mousePosition)) |
144 | return; |
145 | m_lastClickedItem = inspector()->topVisibleItemAt(pos: m_mousePosition); |
146 | m_lastItem = m_lastClickedItem; |
147 | globalInspector()->setSelectedItems(QList<QQuickItem*>() << m_lastClickedItem); |
148 | if (m_lastClickedItem == inspector()->topVisibleItemAt(pos: m_mousePosition)) { |
149 | m_nameDisplayTimer.start(); |
150 | } else { |
151 | showItemName(); |
152 | } |
153 | } |
154 | |
155 | void InspectTool::showItemName() |
156 | { |
157 | globalInspector()->showSelectedItemName(item: m_lastItem, point: m_mousePosition); |
158 | } |
159 | |
160 | QQuickWindowInspector *InspectTool::inspector() const |
161 | { |
162 | return static_cast<QQuickWindowInspector *>(parent()); |
163 | } |
164 | |
165 | GlobalInspector *InspectTool::globalInspector() const |
166 | { |
167 | return static_cast<GlobalInspector *>(parent()->parent()); |
168 | } |
169 | |
170 | } // namespace QmlJSDebugger |
171 | |
172 | QT_END_NAMESPACE |
173 | |
174 | #include "moc_inspecttool.cpp" |
175 | |