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
highlight.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 "highlight.h"
6
7#include <QtCore/QTimer>
8#include <QtGui/QPainter>
9#include <QtGui/QStaticText>
10#include <QtQuick/QQuickWindow>
11
13
14namespace QmlJSDebugger {
15
16Highlight::Highlight(QQuickItem *parent) : QQuickPaintedItem(parent)
17{
18 initRenderDetails();
19}
20
21Highlight::Highlight(QQuickItem *item, QQuickItem *parent)
23{
24 initRenderDetails();
25 setItem(item);
26}
27
28void Highlight::initRenderDetails()
29{
30 setRenderTarget(QQuickPaintedItem::FramebufferObject);
31 setPerformanceHint(QQuickPaintedItem::FastFBOResizing, true);
32}
33
34void Highlight::setItem(QQuickItem *item)
35{
36 if (m_item)
37 m_item->disconnect(this);
38
39 if (item) {
40 connect(item, &QQuickItem::xChanged, this, &Highlight::adjust);
41 connect(item, &QQuickItem::yChanged, this, &Highlight::adjust);
42 connect(item, &QQuickItem::widthChanged, this, &Highlight::adjust);
43 connect(item, &QQuickItem::heightChanged, this, &Highlight::adjust);
44 connect(item, &QQuickItem::rotationChanged, this, &Highlight::adjust);
45 connect(item, &QQuickItem::transformOriginChanged, this, &Highlight::adjust);
46 }
47 QQuickWindow *view = item->window();
48 QQuickItem * contentItem = view->contentItem();
49 if (contentItem) {
50 connect(contentItem, &QQuickItem::xChanged, this, &Highlight::adjust);
51 connect(contentItem, &QQuickItem::yChanged, this, &Highlight::adjust);
52 connect(contentItem, &QQuickItem::widthChanged, this, &Highlight::adjust);
53 connect(contentItem, &QQuickItem::heightChanged, this, &Highlight::adjust);
54 connect(contentItem, &QQuickItem::rotationChanged, this, &Highlight::adjust);
55 connect(contentItem, &QQuickItem::transformOriginChanged, this, &Highlight::adjust);
56 }
57 m_item = item;
58 setContentsSize(view->size());
59 adjust();
60}
61
62void Highlight::adjust()
63{
64 if (!m_item)
65 return;
66
67 bool success = false;
68 m_transform = m_item->itemTransform(nullptr, &success);
69 if (!success)
70 m_transform = QTransform();
71
72 setSize(QSizeF(m_item->width(), m_item->height()));
73 qreal scaleFactor = 1;
74 QPointF originOffset = QPointF(0,0);
75 QQuickWindow *view = m_item->window();
76 if (view->contentItem()) {
77 scaleFactor = view->contentItem()->scale();
78 originOffset -= view->contentItem()->position();
79 }
80 // The scale transform for the overlay needs to be cancelled
81 // as the Item's transform which will be applied to the painter
82 // takes care of it.
83 parentItem()->setScale(1/scaleFactor);
84 setPosition(originOffset);
85 update();
86}
87
88
89void HoverHighlight::paint(QPainter *painter)
90{
91 if (!item())
92 return;
93
94 painter->save();
95 painter->setTransform(transform());
96 painter->setPen(QColor(108, 141, 221));
97 painter->drawRect(QRect(0, 0, item()->width() - 1, item()->height() - 1));
98 painter->restore();
99}
100
101
102SelectionHighlight::SelectionHighlight(const QString &name, QQuickItem *item, QQuickItem *parent)
103 : Highlight(item, parent),
104 m_name(name),
105 m_nameDisplayActive(false)
106{
107}
108
109void SelectionHighlight::paint(QPainter *painter)
110{
111 if (!item())
112 return;
113 painter->save();
114 painter->fillRect(QRectF(0,0,contentsSize().width(), contentsSize().height()),
115 QColor(0,0,0,127));
116 painter->setTransform(transform());
117 // Setting the composition mode such that the transparency will
118 // be erased as per the selected item.
119 painter->setCompositionMode(QPainter::CompositionMode_Clear);
120 painter->fillRect(0, 0, item()->width(), item()->height(), Qt::black);
121 painter->restore();
122
123 // Use the painter with the original transform and not with the
124 // item's transform for display of name.
125 if (!m_nameDisplayActive)
126 return;
127
128 // Paint the text in gray background if display name is active..
129 QRect textRect = painter->boundingRect(QRect(10, contentsSize().height() - 10 ,
130 contentsSize().width() - 20, contentsSize().height()),
131 Qt::AlignCenter | Qt::ElideRight, m_name);
132
133 qreal xPosition = m_displayPoint.x();
134 if (xPosition + textRect.width() > contentsSize().width())
135 xPosition = contentsSize().width() - textRect.width();
136 if (xPosition < 0) {
137 xPosition = 0;
138 textRect.setWidth(contentsSize().width());
139 }
140 qreal yPosition = m_displayPoint.y() - textRect.height() - 20;
141 if (yPosition < 50 )
142 yPosition = 50;
143
144 painter->fillRect(QRectF(xPosition - 5, yPosition - 5,
145 textRect.width() + 10, textRect.height() + 10), Qt::gray);
146 painter->drawRect(QRectF(xPosition - 5, yPosition - 5,
147 textRect.width() + 10, textRect.height() + 10));
148
149 painter->drawStaticText(xPosition, yPosition, QStaticText(m_name));
150}
151
152void SelectionHighlight::showName(const QPointF &displayPoint)
153{
154 m_displayPoint = displayPoint;
155 m_nameDisplayActive = true;
156 QTimer::singleShot(1500, this, &SelectionHighlight::disableNameDisplay);
157 update();
158}
159
160void SelectionHighlight::disableNameDisplay()
161{
162 m_nameDisplayActive = false;
163 update();
164}
165
166} // namespace QmlJSDebugger
167
168QT_END_NAMESPACE
169
170#include "moc_highlight.cpp"
Highlight(QQuickItem *item, QQuickItem *parent)
Definition highlight.cpp:21
void setItem(QQuickItem *item)
Definition highlight.cpp:34
void paint(QPainter *painter) override
This function, which is usually called by the QML Scene Graph, paints the contents of an item in loca...
Definition highlight.cpp:89
void paint(QPainter *painter) override
This function, which is usually called by the QML Scene Graph, paints the contents of an item in loca...
void showName(const QPointF &displayPoint)
Combined button and popup list for selecting options.