QQuickCanvas renames
[profile/ivi/qtdeclarative.git] / src / plugins / qmltooling / qmldbg_qtquick2 / highlight.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "highlight.h"
43
44 #include <QtCore/QTimer>
45 #include <QtGui/QPainter>
46 #include <QtGui/QStaticText>
47 #include <QtQuick/QQuickWindow>
48
49 namespace QmlJSDebugger {
50 namespace QtQuick2 {
51
52 Highlight::Highlight(QQuickItem *parent) : QQuickPaintedItem(parent)
53 {
54     initRenderDetails();
55 }
56
57 Highlight::Highlight(QQuickItem *item, QQuickItem *parent)
58     : QQuickPaintedItem(parent)
59 {
60     initRenderDetails();
61     setItem(item);
62 }
63
64 void Highlight::initRenderDetails()
65 {
66     setRenderTarget(QQuickPaintedItem::FramebufferObject);
67     setPerformanceHint(QQuickPaintedItem::FastFBOResizing, true);
68 }
69
70 void Highlight::setItem(QQuickItem *item)
71 {
72     if (m_item)
73         m_item->disconnect(this);
74
75     if (item) {
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)),
82                 SLOT(adjust()));
83     }
84     QQuickWindow *view = item->window();
85     QQuickItem * rootItem = view->rootItem();
86     if (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)),
93                 SLOT(adjust()));
94     }
95     m_item = item;
96     setContentsSize(view->size());
97     adjust();
98 }
99
100 void Highlight::adjust()
101 {
102     if (!m_item)
103         return;
104
105     bool success = false;
106     m_transform = m_item->itemTransform(0, &success);
107     if (!success)
108         m_transform = QTransform();
109
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();
117     }
118     // The scale transform for the overlay needs to be cancelled
119     // as the Item's transform which will be applied to the painter
120     // takes care of it.
121     parentItem()->setScale(1/scaleFactor);
122     setPos(originOffset);
123     update();
124 }
125
126
127 void HoverHighlight::paint(QPainter *painter)
128 {
129     if (!item())
130         return;
131
132     painter->save();
133     painter->setTransform(transform());
134     painter->setPen(QColor(108, 141, 221));
135     painter->drawRect(QRect(0, 0, item()->width() - 1, item()->height() - 1));
136     painter->restore();
137 }
138
139
140 SelectionHighlight::SelectionHighlight(const QString &name, QQuickItem *item, QQuickItem *parent)
141     : Highlight(item, parent),
142       m_name(name),
143       m_nameDisplayActive(false)
144 {
145 }
146
147 void SelectionHighlight::paint(QPainter *painter)
148 {
149     if (!item())
150         return;
151     painter->save();
152     painter->fillRect(QRectF(0,0,contentsSize().width(), contentsSize().height()),
153                       QColor(0,0,0,127));
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);
159     painter->restore();
160
161     // Use the painter with the original transform and not with the
162     // item's transform for display of name.
163     if (!m_nameDisplayActive)
164         return;
165
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);
170
171     qreal xPosition = m_displayPoint.x();
172     if (xPosition + textRect.width() > contentsSize().width())
173         xPosition = contentsSize().width() - textRect.width();
174     if (xPosition < 0) {
175         xPosition = 0;
176         textRect.setWidth(contentsSize().width());
177     }
178     qreal yPosition = m_displayPoint.y() - textRect.height() - 20;
179     if (yPosition < 50 )
180         yPosition = 50;
181
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));
186
187     painter->drawStaticText(xPosition, yPosition, QStaticText(m_name));
188 }
189
190 void SelectionHighlight::showName(const QPointF &displayPoint)
191 {
192     m_displayPoint = displayPoint;
193     m_nameDisplayActive = true;
194     QTimer::singleShot(1500, this, SLOT(disableNameDisplay()));
195     update();
196 }
197
198 void SelectionHighlight::disableNameDisplay()
199 {
200     m_nameDisplayActive = false;
201     update();
202 }
203
204 } // namespace QtQuick2
205 } // namespace QmlJSDebugger