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