1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "highlight.h"
44 #include <QtCore/QTimer>
45 #include <QtGui/QPainter>
46 #include <QtGui/QStaticText>
47 #include <QtQuick/QQuickWindow>
49 namespace QmlJSDebugger {
52 Highlight::Highlight(QQuickItem *parent) : QQuickPaintedItem(parent)
57 Highlight::Highlight(QQuickItem *item, QQuickItem *parent)
58 : QQuickPaintedItem(parent)
64 void Highlight::initRenderDetails()
66 setRenderTarget(QQuickPaintedItem::FramebufferObject);
67 setPerformanceHint(QQuickPaintedItem::FastFBOResizing, true);
70 void Highlight::setItem(QQuickItem *item)
73 m_item->disconnect(this);
76 connect(item, SIGNAL(xChanged()), SLOT(adjust()));
77 connect(item, SIGNAL(yChanged()), SLOT(adjust()));
78 connect(item, SIGNAL(widthChanged()), SLOT(adjust()));
79 connect(item, SIGNAL(heightChanged()), SLOT(adjust()));
80 connect(item, SIGNAL(rotationChanged()), SLOT(adjust()));
81 connect(item, SIGNAL(transformOriginChanged(TransformOrigin)),
84 QQuickWindow *view = item->window();
85 QQuickItem * rootItem = view->rootItem();
87 connect(rootItem, SIGNAL(xChanged()), SLOT(adjust()));
88 connect(rootItem, SIGNAL(yChanged()), SLOT(adjust()));
89 connect(rootItem, SIGNAL(widthChanged()), SLOT(adjust()));
90 connect(rootItem, SIGNAL(heightChanged()), SLOT(adjust()));
91 connect(rootItem, SIGNAL(rotationChanged()), SLOT(adjust()));
92 connect(rootItem, SIGNAL(transformOriginChanged(TransformOrigin)),
96 setContentsSize(view->size());
100 void Highlight::adjust()
105 bool success = false;
106 m_transform = m_item->itemTransform(0, &success);
108 m_transform = QTransform();
110 setSize(QSizeF(m_item->width(), m_item->height()));
111 qreal scaleFactor = 1;
112 QPointF originOffset = QPointF(0,0);
113 QQuickWindow *view = m_item->window();
114 if (view->rootItem()) {
115 scaleFactor = view->rootItem()->scale();
116 originOffset -= view->rootItem()->pos();
118 // The scale transform for the overlay needs to be cancelled
119 // as the Item's transform which will be applied to the painter
121 parentItem()->setScale(1/scaleFactor);
122 setPos(originOffset);
127 void HoverHighlight::paint(QPainter *painter)
133 painter->setTransform(transform());
134 painter->setPen(QColor(108, 141, 221));
135 painter->drawRect(QRect(0, 0, item()->width() - 1, item()->height() - 1));
140 SelectionHighlight::SelectionHighlight(const QString &name, QQuickItem *item, QQuickItem *parent)
141 : Highlight(item, parent),
143 m_nameDisplayActive(false)
147 void SelectionHighlight::paint(QPainter *painter)
152 painter->fillRect(QRectF(0,0,contentsSize().width(), contentsSize().height()),
154 painter->setTransform(transform());
155 // Setting the composition mode such that the transparency will
156 // be erased as per the selected item.
157 painter->setCompositionMode(QPainter::CompositionMode_Clear);
158 painter->fillRect(0, 0, item()->width(), item()->height(), Qt::black);
161 // Use the painter with the original transform and not with the
162 // item's transform for display of name.
163 if (!m_nameDisplayActive)
166 // Paint the text in gray background if display name is active..
167 QRect textRect = painter->boundingRect(QRect(10, contentsSize().height() - 10 ,
168 contentsSize().width() - 20, contentsSize().height()),
169 Qt::AlignCenter | Qt::ElideRight, m_name);
171 qreal xPosition = m_displayPoint.x();
172 if (xPosition + textRect.width() > contentsSize().width())
173 xPosition = contentsSize().width() - textRect.width();
176 textRect.setWidth(contentsSize().width());
178 qreal yPosition = m_displayPoint.y() - textRect.height() - 20;
182 painter->fillRect(QRectF(xPosition - 5, yPosition - 5,
183 textRect.width() + 10, textRect.height() + 10), Qt::gray);
184 painter->drawRect(QRectF(xPosition - 5, yPosition - 5,
185 textRect.width() + 10, textRect.height() + 10));
187 painter->drawStaticText(xPosition, yPosition, QStaticText(m_name));
190 void SelectionHighlight::showName(const QPointF &displayPoint)
192 m_displayPoint = displayPoint;
193 m_nameDisplayActive = true;
194 QTimer::singleShot(1500, this, SLOT(disableNameDisplay()));
198 void SelectionHighlight::disableNameDisplay()
200 m_nameDisplayActive = false;
204 } // namespace QtQuick2
205 } // namespace QmlJSDebugger