boundingrecthighlighter.cpp 7.78 KB
Newer Older
1
2
3
4
5
6
7
8
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11
12
13
14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
15
16
17
18
19
20
21
22
23
24
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
con's avatar
con committed
25
26
27
28
29
30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
31
32
33
**
**************************************************************************/

34
#include "boundingrecthighlighter.h"
35
#include "qdeclarativeviewobserver.h"
36
#include "qmlobserverconstants.h"
37
38
39

#include <QGraphicsPolygonItem>
#include <QTimer>
40
#include <QObject>
41
42
43

#include <QDebug>

44
namespace QmlJSDebugger {
45
46
47
48
49

const qreal AnimDelta = 0.025f;
const int AnimInterval = 30;
const int AnimFrames = 10;

50
51
52
BoundingBox::BoundingBox(QGraphicsObject *itemToHighlight, QGraphicsItem *parentItem, QObject *parent)
    : QObject(parent),
      highlightedObject(itemToHighlight),
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
      highlightPolygon(0),
      highlightPolygonEdge(0)
{
    highlightPolygon = new BoundingBoxPolygonItem(parentItem);
    highlightPolygonEdge = new BoundingBoxPolygonItem(parentItem);

    highlightPolygon->setPen(QPen(QColor(0, 22, 159)));
    highlightPolygonEdge->setPen(QPen(QColor(158, 199, 255)));

    highlightPolygon->setFlag(QGraphicsItem::ItemIsSelectable, false);
    highlightPolygonEdge->setFlag(QGraphicsItem::ItemIsSelectable, false);
}

BoundingBox::~BoundingBox()
{
    highlightedObject.clear();
}

71
72
73
74
BoundingBoxPolygonItem::BoundingBoxPolygonItem(QGraphicsItem *item) : QGraphicsPolygonItem(item)
{
    QPen pen;
    pen.setColor(QColor(108, 141, 221));
75
    pen.setWidth(1);
76
77
78
79
80
81
82
83
    setPen(pen);
}

int BoundingBoxPolygonItem::type() const
{
    return Constants::EditorItemType;
}

84
85
BoundingRectHighlighter::BoundingRectHighlighter(QDeclarativeViewObserver *view) :
    LayerItem(view->declarativeView()->scene()),
86
87
88
89
90
91
92
93
    m_view(view),
    m_animFrame(0)
{
    m_animTimer = new QTimer(this);
    m_animTimer->setInterval(AnimInterval);
    connect(m_animTimer, SIGNAL(timeout()), SLOT(animTimeout()));
}

94
95
96
97
98
BoundingRectHighlighter::~BoundingRectHighlighter()
{

}

99
100
101
102
103
104
105
106
void BoundingRectHighlighter::animTimeout()
{
    ++m_animFrame;
    if (m_animFrame == AnimFrames) {
        m_animTimer->stop();
    }

    qreal alpha = m_animFrame / float(AnimFrames);
107
108
109
110

    foreach(BoundingBox *box, m_boxes) {
        box->highlightPolygonEdge->setOpacity(alpha);
    }
111
112
113
114
}

void BoundingRectHighlighter::clear()
{
115
    if (m_boxes.length()) {
116
117
        m_animTimer->stop();

118
        foreach(BoundingBox *box, m_boxes) {
119
            freeBoundingBox(box);
120
        }
121
122
123
    }
}

124
BoundingBox *BoundingRectHighlighter::boxFor(QGraphicsObject *item) const
125
{
126
127
128
129
130
131
132
133
134
135
136
    foreach(BoundingBox *box, m_boxes) {
        if (box->highlightedObject.data() == item) {
            return box;
        }
    }
    return 0;
}

void BoundingRectHighlighter::highlight(QList<QGraphicsObject*> items)
{
    if (items.isEmpty())
137
138
139
        return;

    bool animate = false;
140

141
    QList<BoundingBox *> newBoxes;
142
143
144
    foreach(QGraphicsObject *itemToHighlight, items) {
        BoundingBox *box = boxFor(itemToHighlight);
        if (!box) {
145
            box = createBoundingBox(itemToHighlight);
146
147
148
149
            animate = true;
        }

        newBoxes << box;
150
    }
151
    qSort(newBoxes);
152

153
154
155
156
    if (newBoxes != m_boxes) {
        clear();
        m_boxes << newBoxes;
    }
157

158
159
    highlightAll(animate);
}
160

161
162
163
164
void BoundingRectHighlighter::highlight(QGraphicsObject* itemToHighlight)
{
    if (!itemToHighlight)
        return;
165

166
    bool animate = false;
167

168
169
    BoundingBox *box = boxFor(itemToHighlight);
    if (!box) {
170
        box = createBoundingBox(itemToHighlight);
171
172
173
174
        m_boxes << box;
        animate = true;
        qSort(m_boxes);
    }
175

176
177
    highlightAll(animate);
}
178

179
180
BoundingBox *BoundingRectHighlighter::createBoundingBox(QGraphicsObject *itemToHighlight)
{
181
182
183
184
185
186
187
188
189
190
191
192
    if (!m_freeBoxes.isEmpty()) {
        BoundingBox *box = m_freeBoxes.last();
        if (box->highlightedObject.isNull()) {
            box->highlightedObject = itemToHighlight;
            box->highlightPolygon->show();
            box->highlightPolygonEdge->show();
            m_freeBoxes.removeLast();
            return box;
        }
    }

    BoundingBox *box = new BoundingBox(itemToHighlight, this, this);
193
194
195
196
197
198

    connect(itemToHighlight, SIGNAL(xChanged()), this, SLOT(refresh()));
    connect(itemToHighlight, SIGNAL(yChanged()), this, SLOT(refresh()));
    connect(itemToHighlight, SIGNAL(widthChanged()), this, SLOT(refresh()));
    connect(itemToHighlight, SIGNAL(heightChanged()), this, SLOT(refresh()));
    connect(itemToHighlight, SIGNAL(rotationChanged()), this, SLOT(refresh()));
199
    connect(itemToHighlight, SIGNAL(destroyed(QObject*)), this, SLOT(itemDestroyed(QObject*)));
200
201
202
203

    return box;
}

204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
void BoundingRectHighlighter::removeBoundingBox(BoundingBox *box)
{
    delete box;
    box = 0;
}

void BoundingRectHighlighter::freeBoundingBox(BoundingBox *box)
{
    if (!box->highlightedObject.isNull()) {
        disconnect(box->highlightedObject.data(), SIGNAL(xChanged()), this, SLOT(refresh()));
        disconnect(box->highlightedObject.data(), SIGNAL(yChanged()), this, SLOT(refresh()));
        disconnect(box->highlightedObject.data(), SIGNAL(widthChanged()), this, SLOT(refresh()));
        disconnect(box->highlightedObject.data(), SIGNAL(heightChanged()), this, SLOT(refresh()));
        disconnect(box->highlightedObject.data(), SIGNAL(rotationChanged()), this, SLOT(refresh()));
    }

    box->highlightedObject.clear();
    box->highlightPolygon->hide();
    box->highlightPolygonEdge->hide();
    m_boxes.removeOne(box);
    m_freeBoxes << box;
}

void BoundingRectHighlighter::itemDestroyed(QObject *obj)
228
229
{
    foreach(BoundingBox *box, m_boxes) {
230
231
232
        if (box->highlightedObject.data() == obj) {
            freeBoundingBox(box);
            break;
233
        }
234
235
    }
}
236

237
238
239
240
241
242
243
244
245
void BoundingRectHighlighter::highlightAll(bool animate)
{
    foreach(BoundingBox *box, m_boxes) {
        if (box && box->highlightedObject.isNull()) {
            // clear all highlights
            clear();
            return;
        }
        QGraphicsObject *item = box->highlightedObject.data();
246
247
248
249
        QRectF itemAndChildRect = item->boundingRect() | item->childrenBoundingRect();

        QPolygonF boundingRectInSceneSpace(item->mapToScene(itemAndChildRect));
        QPolygonF boundingRectInLayerItemSpace = mapFromScene(boundingRectInSceneSpace);
250
251
        QRectF bboxRect = m_view->adjustToScreenBoundaries(boundingRectInLayerItemSpace.boundingRect());
        QRectF edgeRect = bboxRect;
252
253
254
255
256
257
258
        edgeRect.adjust(-1, -1, 1, 1);

        box->highlightPolygon->setPolygon(QPolygonF(bboxRect));
        box->highlightPolygonEdge->setPolygon(QPolygonF(edgeRect));

        if (animate)
            box->highlightPolygonEdge->setOpacity(0);
259
260
261
262
263
264
265
266
267
268
    }

    if (animate) {
        m_animFrame = 0;
        m_animTimer->start();
    }
}

void BoundingRectHighlighter::refresh()
{
269
270
    if (!m_boxes.isEmpty())
        highlightAll(true);
271
272
273
}


274
} // namespace QmlJSDebugger