boundingrecthighlighter.cpp 6.71 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8
9
10
11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12
13
14
15
16
17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23
24
25
26
27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
30
31
32
**
**************************************************************************/

33
#include "boundingrecthighlighter.h"
34
35
#include "qdeclarativeviewinspector.h"
#include "qmlinspectorconstants.h"
36

37
#include <QGraphicsPolygonItem>
38

39
40
41
#include <QTimer>
#include <QObject>
#include <QDebug>
42

43
namespace QmlJSDebugger {
44

Kai Koehne's avatar
Kai Koehne committed
45
46
BoundingBox::BoundingBox(QGraphicsObject *itemToHighlight, QGraphicsItem *parentItem,
                         QObject *parent)
47
48
    : QObject(parent),
      highlightedObject(itemToHighlight),
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
      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();
}

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

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

80
BoundingRectHighlighter::BoundingRectHighlighter(QDeclarativeViewInspector *view) :
81
    LiveLayerItem(view->declarativeView()->scene()),
82
    m_view(view)
83
84
85
{
}

86
87
88
89
90
BoundingRectHighlighter::~BoundingRectHighlighter()
{

}

91
92
void BoundingRectHighlighter::clear()
{
93
94
    foreach (BoundingBox *box, m_boxes)
        freeBoundingBox(box);
95
96
}

97
BoundingBox *BoundingRectHighlighter::boxFor(QGraphicsObject *item) const
98
{
99
    foreach (BoundingBox *box, m_boxes) {
100
        if (box->highlightedObject.data() == item)
101
102
103
104
105
106
107
108
            return box;
    }
    return 0;
}

void BoundingRectHighlighter::highlight(QList<QGraphicsObject*> items)
{
    if (items.isEmpty())
109
110
        return;

111
    QList<BoundingBox *> newBoxes;
112
    foreach (QGraphicsObject *itemToHighlight, items) {
113
        BoundingBox *box = boxFor(itemToHighlight);
114
        if (!box)
115
            box = createBoundingBox(itemToHighlight);
116
117

        newBoxes << box;
118
    }
119
    qSort(newBoxes);
120

121
122
123
124
    if (newBoxes != m_boxes) {
        clear();
        m_boxes << newBoxes;
    }
125

126
    highlightAll();
127
}
128

129
130
131
132
void BoundingRectHighlighter::highlight(QGraphicsObject* itemToHighlight)
{
    if (!itemToHighlight)
        return;
133

134
135
    BoundingBox *box = boxFor(itemToHighlight);
    if (!box) {
136
        box = createBoundingBox(itemToHighlight);
137
138
139
        m_boxes << box;
        qSort(m_boxes);
    }
140

141
    highlightAll();
142
}
143

144
145
BoundingBox *BoundingRectHighlighter::createBoundingBox(QGraphicsObject *itemToHighlight)
{
146
147
148
149
150
151
152
153
154
155
156
157
    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);
158
159
160
161
162
163

    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()));
164
    connect(itemToHighlight, SIGNAL(destroyed(QObject*)), this, SLOT(itemDestroyed(QObject*)));
165
166
167
168

    return box;
}

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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)
193
{
194
    foreach (BoundingBox *box, m_boxes) {
195
196
197
        if (box->highlightedObject.data() == obj) {
            freeBoundingBox(box);
            break;
198
        }
199
200
    }
}
201

202
void BoundingRectHighlighter::highlightAll()
203
{
204
    foreach (BoundingBox *box, m_boxes) {
205
206
207
208
209
210
        if (box && box->highlightedObject.isNull()) {
            // clear all highlights
            clear();
            return;
        }
        QGraphicsObject *item = box->highlightedObject.data();
211

212
213
214
        QRectF boundingRectInSceneSpace(item->mapToScene(item->boundingRect()).boundingRect());
        QRectF boundingRectInLayerItemSpace = mapRectFromScene(boundingRectInSceneSpace);
        QRectF bboxRect = m_view->adjustToScreenBoundaries(boundingRectInLayerItemSpace);
215
        QRectF edgeRect = bboxRect;
216
217
218
219
        edgeRect.adjust(-1, -1, 1, 1);

        box->highlightPolygon->setPolygon(QPolygonF(bboxRect));
        box->highlightPolygonEdge->setPolygon(QPolygonF(edgeRect));
220
221
222
223
224
    }
}

void BoundingRectHighlighter::refresh()
{
225
    if (!m_boxes.isEmpty())
226
        highlightAll();
227
228
229
}


230
} // namespace QmlJSDebugger