2010-11-01 17:03:46 +01:00
|
|
|
/**************************************************************************
|
|
|
|
**
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2010-11-01 17:03:46 +01:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Contact: Nokia Corporation (info@qt.nokia.com)
|
2010-11-01 17:03:46 +01:00
|
|
|
**
|
|
|
|
**
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2010-11-01 17:03:46 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-07-05 18:26:01 +02:00
|
|
|
** Nokia at info@qt.nokia.com.
|
2010-11-01 17:03:46 +01:00
|
|
|
**
|
|
|
|
**************************************************************************/
|
|
|
|
|
2010-07-08 14:00:33 +02:00
|
|
|
#include "boundingrecthighlighter.h"
|
2011-05-30 16:32:25 +02:00
|
|
|
#include "qdeclarativeviewinspector.h"
|
|
|
|
#include "qmlinspectorconstants.h"
|
2010-07-08 14:00:33 +02:00
|
|
|
|
2011-01-24 12:30:21 +01:00
|
|
|
#include <QtGui/QGraphicsPolygonItem>
|
2010-07-08 14:00:33 +02:00
|
|
|
|
2011-01-24 12:30:21 +01:00
|
|
|
#include <QtCore/QTimer>
|
|
|
|
#include <QtCore/QObject>
|
|
|
|
#include <QtCore/QDebug>
|
2010-07-08 14:00:33 +02:00
|
|
|
|
2010-09-22 09:59:18 +02:00
|
|
|
namespace QmlJSDebugger {
|
2010-07-08 14:00:33 +02:00
|
|
|
|
2010-12-01 11:46:49 +01:00
|
|
|
BoundingBox::BoundingBox(QGraphicsObject *itemToHighlight, QGraphicsItem *parentItem,
|
|
|
|
QObject *parent)
|
2010-08-04 18:53:01 +02:00
|
|
|
: QObject(parent),
|
|
|
|
highlightedObject(itemToHighlight),
|
2010-07-13 12:03:21 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2010-07-08 14:00:33 +02:00
|
|
|
BoundingBoxPolygonItem::BoundingBoxPolygonItem(QGraphicsItem *item) : QGraphicsPolygonItem(item)
|
|
|
|
{
|
|
|
|
QPen pen;
|
|
|
|
pen.setColor(QColor(108, 141, 221));
|
2010-08-05 16:23:28 +02:00
|
|
|
pen.setWidth(1);
|
2010-07-08 14:00:33 +02:00
|
|
|
setPen(pen);
|
|
|
|
}
|
|
|
|
|
|
|
|
int BoundingBoxPolygonItem::type() const
|
|
|
|
{
|
|
|
|
return Constants::EditorItemType;
|
|
|
|
}
|
|
|
|
|
2011-05-30 16:32:25 +02:00
|
|
|
BoundingRectHighlighter::BoundingRectHighlighter(QDeclarativeViewInspector *view) :
|
2011-02-09 16:59:43 +01:00
|
|
|
LiveLayerItem(view->declarativeView()->scene()),
|
2011-05-26 20:23:16 +02:00
|
|
|
m_view(view)
|
2010-07-08 14:00:33 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-08-04 18:53:01 +02:00
|
|
|
BoundingRectHighlighter::~BoundingRectHighlighter()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-07-08 14:00:33 +02:00
|
|
|
void BoundingRectHighlighter::clear()
|
|
|
|
{
|
2011-05-26 20:23:16 +02:00
|
|
|
foreach (BoundingBox *box, m_boxes)
|
|
|
|
freeBoundingBox(box);
|
2010-07-08 14:00:33 +02:00
|
|
|
}
|
|
|
|
|
2010-07-13 12:03:21 +02:00
|
|
|
BoundingBox *BoundingRectHighlighter::boxFor(QGraphicsObject *item) const
|
2010-07-08 14:00:33 +02:00
|
|
|
{
|
2011-02-22 14:23:57 +01:00
|
|
|
foreach (BoundingBox *box, m_boxes) {
|
2011-05-26 20:23:16 +02:00
|
|
|
if (box->highlightedObject.data() == item)
|
2010-07-13 12:03:21 +02:00
|
|
|
return box;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoundingRectHighlighter::highlight(QList<QGraphicsObject*> items)
|
|
|
|
{
|
|
|
|
if (items.isEmpty())
|
2010-07-08 14:00:33 +02:00
|
|
|
return;
|
|
|
|
|
2010-08-04 18:53:01 +02:00
|
|
|
QList<BoundingBox *> newBoxes;
|
2011-02-22 14:23:57 +01:00
|
|
|
foreach (QGraphicsObject *itemToHighlight, items) {
|
2010-07-13 12:03:21 +02:00
|
|
|
BoundingBox *box = boxFor(itemToHighlight);
|
2011-05-26 20:23:16 +02:00
|
|
|
if (!box)
|
2010-08-03 12:32:56 +02:00
|
|
|
box = createBoundingBox(itemToHighlight);
|
2010-07-13 12:03:21 +02:00
|
|
|
|
|
|
|
newBoxes << box;
|
2010-07-08 14:00:33 +02:00
|
|
|
}
|
2010-07-13 12:03:21 +02:00
|
|
|
qSort(newBoxes);
|
2010-07-08 14:00:33 +02:00
|
|
|
|
2010-07-13 12:03:21 +02:00
|
|
|
if (newBoxes != m_boxes) {
|
|
|
|
clear();
|
|
|
|
m_boxes << newBoxes;
|
|
|
|
}
|
2010-07-08 14:00:33 +02:00
|
|
|
|
2011-05-26 20:23:16 +02:00
|
|
|
highlightAll();
|
2010-07-13 12:03:21 +02:00
|
|
|
}
|
2010-07-09 16:33:09 +02:00
|
|
|
|
2010-07-13 12:03:21 +02:00
|
|
|
void BoundingRectHighlighter::highlight(QGraphicsObject* itemToHighlight)
|
|
|
|
{
|
|
|
|
if (!itemToHighlight)
|
|
|
|
return;
|
2010-07-09 16:33:09 +02:00
|
|
|
|
2010-07-13 12:03:21 +02:00
|
|
|
BoundingBox *box = boxFor(itemToHighlight);
|
|
|
|
if (!box) {
|
2010-08-03 12:32:56 +02:00
|
|
|
box = createBoundingBox(itemToHighlight);
|
2010-07-13 12:03:21 +02:00
|
|
|
m_boxes << box;
|
|
|
|
qSort(m_boxes);
|
|
|
|
}
|
2010-07-08 14:00:33 +02:00
|
|
|
|
2011-05-26 20:23:16 +02:00
|
|
|
highlightAll();
|
2010-07-13 12:03:21 +02:00
|
|
|
}
|
2010-07-08 14:00:33 +02:00
|
|
|
|
2010-08-03 12:32:56 +02:00
|
|
|
BoundingBox *BoundingRectHighlighter::createBoundingBox(QGraphicsObject *itemToHighlight)
|
|
|
|
{
|
2010-08-04 18:53:01 +02:00
|
|
|
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);
|
2010-08-03 12:32:56 +02:00
|
|
|
|
|
|
|
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()));
|
2010-08-04 18:53:01 +02:00
|
|
|
connect(itemToHighlight, SIGNAL(destroyed(QObject*)), this, SLOT(itemDestroyed(QObject*)));
|
2010-08-03 12:32:56 +02:00
|
|
|
|
|
|
|
return box;
|
|
|
|
}
|
|
|
|
|
2010-08-04 18:53:01 +02:00
|
|
|
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)
|
2010-07-13 12:03:21 +02:00
|
|
|
{
|
2011-02-22 14:23:57 +01:00
|
|
|
foreach (BoundingBox *box, m_boxes) {
|
2010-08-04 18:53:01 +02:00
|
|
|
if (box->highlightedObject.data() == obj) {
|
|
|
|
freeBoundingBox(box);
|
|
|
|
break;
|
2010-07-13 12:03:21 +02:00
|
|
|
}
|
2010-08-04 18:53:01 +02:00
|
|
|
}
|
|
|
|
}
|
2010-07-13 12:03:21 +02:00
|
|
|
|
2011-05-26 20:23:16 +02:00
|
|
|
void BoundingRectHighlighter::highlightAll()
|
2010-08-04 18:53:01 +02:00
|
|
|
{
|
2011-02-22 14:23:57 +01:00
|
|
|
foreach (BoundingBox *box, m_boxes) {
|
2010-08-04 18:53:01 +02:00
|
|
|
if (box && box->highlightedObject.isNull()) {
|
|
|
|
// clear all highlights
|
|
|
|
clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QGraphicsObject *item = box->highlightedObject.data();
|
2010-07-13 12:03:21 +02:00
|
|
|
|
2011-05-24 14:08:09 +02:00
|
|
|
QRectF boundingRectInSceneSpace(item->mapToScene(item->boundingRect()).boundingRect());
|
|
|
|
QRectF boundingRectInLayerItemSpace = mapRectFromScene(boundingRectInSceneSpace);
|
|
|
|
QRectF bboxRect = m_view->adjustToScreenBoundaries(boundingRectInLayerItemSpace);
|
2010-07-27 13:13:12 +02:00
|
|
|
QRectF edgeRect = bboxRect;
|
2010-07-13 12:03:21 +02:00
|
|
|
edgeRect.adjust(-1, -1, 1, 1);
|
|
|
|
|
|
|
|
box->highlightPolygon->setPolygon(QPolygonF(bboxRect));
|
|
|
|
box->highlightPolygonEdge->setPolygon(QPolygonF(edgeRect));
|
2010-07-08 14:00:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoundingRectHighlighter::refresh()
|
|
|
|
{
|
2010-07-13 12:03:21 +02:00
|
|
|
if (!m_boxes.isEmpty())
|
2011-05-26 20:23:16 +02:00
|
|
|
highlightAll();
|
2010-07-08 14:00:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-22 09:59:18 +02:00
|
|
|
} // namespace QmlJSDebugger
|