Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#include "highlight.h"
5
6#include <QtCore/QTimer>
7#include <QtGui/QPainter>
8#include <QtGui/QStaticText>
9#include <QtQuick/QQuickWindow>
10
12
13namespace QmlJSDebugger {
14
16{
17 initRenderDetails();
18}
19
21 : QQuickPaintedItem(parent)
22{
23 initRenderDetails();
25}
26
27void Highlight::initRenderDetails()
28{
31}
32
34{
35 if (m_item)
36 m_item->disconnect(this);
37
38 if (item) {
39 connect(item, &QQuickItem::xChanged, this, &Highlight::adjust);
40 connect(item, &QQuickItem::yChanged, this, &Highlight::adjust);
41 connect(item, &QQuickItem::widthChanged, this, &Highlight::adjust);
42 connect(item, &QQuickItem::heightChanged, this, &Highlight::adjust);
43 connect(item, &QQuickItem::rotationChanged, this, &Highlight::adjust);
44 connect(item, &QQuickItem::transformOriginChanged, this, &Highlight::adjust);
45 }
47 QQuickItem * contentItem = view->contentItem();
48 if (contentItem) {
49 connect(contentItem, &QQuickItem::xChanged, this, &Highlight::adjust);
50 connect(contentItem, &QQuickItem::yChanged, this, &Highlight::adjust);
51 connect(contentItem, &QQuickItem::widthChanged, this, &Highlight::adjust);
52 connect(contentItem, &QQuickItem::heightChanged, this, &Highlight::adjust);
53 connect(contentItem, &QQuickItem::rotationChanged, this, &Highlight::adjust);
54 connect(contentItem, &QQuickItem::transformOriginChanged, this, &Highlight::adjust);
55 }
56 m_item = item;
57 setContentsSize(view->size());
58 adjust();
59}
60
61void Highlight::adjust()
62{
63 if (!m_item)
64 return;
65
66 bool success = false;
67 m_transform = m_item->itemTransform(nullptr, &success);
68 if (!success)
69 m_transform = QTransform();
70
71 setSize(QSizeF(m_item->width(), m_item->height()));
72 qreal scaleFactor = 1;
73 QPointF originOffset = QPointF(0,0);
74 QQuickWindow *view = m_item->window();
75 if (view->contentItem()) {
76 scaleFactor = view->contentItem()->scale();
77 originOffset -= view->contentItem()->position();
78 }
79 // The scale transform for the overlay needs to be cancelled
80 // as the Item's transform which will be applied to the painter
81 // takes care of it.
82 parentItem()->setScale(1/scaleFactor);
83 setPosition(originOffset);
84 update();
85}
86
87
89{
90 if (!item())
91 return;
92
93 painter->save();
95 painter->setPen(QColor(108, 141, 221));
96 painter->drawRect(QRect(0, 0, item()->width() - 1, item()->height() - 1));
98}
99
100
102 : Highlight(item, parent),
103 m_name(name),
104 m_nameDisplayActive(false)
105{
106}
107
109{
110 if (!item())
111 return;
112 painter->save();
114 QColor(0,0,0,127));
116 // Setting the composition mode such that the transparency will
117 // be erased as per the selected item.
119 painter->fillRect(0, 0, item()->width(), item()->height(), Qt::black);
120 painter->restore();
121
122 // Use the painter with the original transform and not with the
123 // item's transform for display of name.
124 if (!m_nameDisplayActive)
125 return;
126
127 // Paint the text in gray background if display name is active..
129 contentsSize().width() - 20, contentsSize().height()),
131
132 qreal xPosition = m_displayPoint.x();
133 if (xPosition + textRect.width() > contentsSize().width())
134 xPosition = contentsSize().width() - textRect.width();
135 if (xPosition < 0) {
136 xPosition = 0;
138 }
139 qreal yPosition = m_displayPoint.y() - textRect.height() - 20;
140 if (yPosition < 50 )
141 yPosition = 50;
142
143 painter->fillRect(QRectF(xPosition - 5, yPosition - 5,
144 textRect.width() + 10, textRect.height() + 10), Qt::gray);
145 painter->drawRect(QRectF(xPosition - 5, yPosition - 5,
146 textRect.width() + 10, textRect.height() + 10));
147
148 painter->drawStaticText(xPosition, yPosition, QStaticText(m_name));
149}
150
151void SelectionHighlight::showName(const QPointF &displayPoint)
152{
153 m_displayPoint = displayPoint;
154 m_nameDisplayActive = true;
155 QTimer::singleShot(1500, this, &SelectionHighlight::disableNameDisplay);
156 update();
157}
158
159void SelectionHighlight::disableNameDisplay()
160{
161 m_nameDisplayActive = false;
162 update();
163}
164
165} // namespace QmlJSDebugger
166
168
169#include "moc_highlight.cpp"
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
\threadsafe
Definition qobject.cpp:3236
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
void drawRect(const QRectF &rect)
Draws the current rectangle with the current pen and brush.
Definition qpainter.h:519
void setPen(const QColor &color)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void restore()
Restores the current painter state (pops a saved state off the stack).
void setCompositionMode(CompositionMode mode)
Sets the composition mode to the given mode.
void save()
Saves the current painter state (pushes the state onto a stack).
void drawStaticText(const QPointF &topLeftPosition, const QStaticText &staticText)
@ CompositionMode_Clear
Definition qpainter.h:100
void fillRect(const QRectF &, const QBrush &)
Fills the given rectangle with the brush specified.
void setTransform(const QTransform &transform, bool combine=false)
QRectF boundingRect(const QRectF &rect, int flags, const QString &text)
Returns the bounding rectangle of the text as it will appear when drawn inside the given rectangle wi...
\inmodule QtCore\reentrant
Definition qpoint.h:217
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:343
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:348
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
void xChanged()
void heightChanged()
void setSize(const QSizeF &size)
void rotationChanged()
void setScale(qreal)
void widthChanged()
QQuickWindow * window() const
Returns the window in which this item is rendered.
void yChanged()
qreal width
This property holds the width of this item.
Definition qquickitem.h:75
QQuickItem * parentItem() const
QTransform itemTransform(QQuickItem *, bool *) const
\qmlmethod point QtQuick::Item::mapFromItem(Item item, real x, real y) \qmlmethod point QtQuick::Item...
QPointF position() const
qreal height
This property holds the height of this item.
Definition qquickitem.h:76
qreal scale
\qmlproperty real QtQuick::Item::scale This property holds the scale factor for this item.
Definition qquickitem.h:107
void transformOriginChanged(TransformOrigin)
void update()
Schedules a call to updatePaintNode() for this item.
The QQuickPaintedItem class provides a way to use the QPainter API in the QML Scene Graph.
QSize contentsSize
Obsolete method for setting the contents size.
void setRenderTarget(RenderTarget target)
void setPerformanceHint(PerformanceHint hint, bool enabled=true)
Sets the given performance hint on the item if enabled is true; otherwise clears the performance hint...
void setContentsSize(const QSize &)
\qmltype Window \instantiates QQuickWindow \inqmlmodule QtQuick
QQuickItem * contentItem
\qmlattachedproperty Item Window::contentItem
\inmodule QtCore\reentrant
Definition qrect.h:484
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr int height() const noexcept
Returns the height of the rectangle.
Definition qrect.h:239
constexpr void setWidth(int w) noexcept
Sets the width of the rectangle to the given width.
Definition qrect.h:381
constexpr int width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:236
\inmodule QtCore
Definition qsize.h:208
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:130
The QStaticText class enables optimized drawing of text when the text and its layout is updated rarel...
Definition qstatictext.h:21
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool singleShot
whether the timer is a single-shot timer
Definition qtimer.h:22
The QTransform class specifies 2D transformations of a coordinate system.
Definition qtransform.h:20
void setItem(QQuickItem *item)
Definition highlight.cpp:33
Highlight(QQuickItem *parent)
Definition highlight.cpp:15
QQuickItem * item()
Definition highlight.h:25
QTransform transform()
Definition highlight.h:28
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:88
SelectionHighlight(const QString &name, QQuickItem *item, QQuickItem *parent)
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)
QRect textRect
Combined button and popup list for selecting options.
@ AlignCenter
Definition qnamespace.h:163
@ gray
Definition qnamespace.h:33
@ black
Definition qnamespace.h:30
@ ElideRight
Definition qnamespace.h:190
n void setPosition(void) \n\
GLuint name
double qreal
Definition qtypes.h:187
QGraphicsItem * item
QPainter painter(this)
[7]
QQuickView * view
[0]