2010-07-08 14:00:33 +02:00
|
|
|
#include "boundingrecthighlighter.h"
|
2010-09-14 13:39:32 +02:00
|
|
|
#include "qdeclarativeviewobserver.h"
|
2010-09-16 16:43:22 +02:00
|
|
|
#include "qmlobserverconstants.h"
|
2010-07-08 14:00:33 +02:00
|
|
|
|
|
|
|
#include <QGraphicsPolygonItem>
|
|
|
|
#include <QTimer>
|
2010-08-04 18:53:01 +02:00
|
|
|
#include <QObject>
|
2010-07-08 14:00:33 +02:00
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
2010-09-16 15:59:52 +02:00
|
|
|
namespace QmlObserver {
|
2010-07-08 14:00:33 +02:00
|
|
|
|
|
|
|
const qreal AnimDelta = 0.025f;
|
|
|
|
const int AnimInterval = 30;
|
|
|
|
const int AnimFrames = 10;
|
|
|
|
|
2010-08-04 18:53:01 +02:00
|
|
|
BoundingBox::BoundingBox(QGraphicsObject *itemToHighlight, QGraphicsItem *parentItem, QObject *parent)
|
|
|
|
: 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;
|
|
|
|
}
|
|
|
|
|
2010-09-14 13:39:32 +02:00
|
|
|
BoundingRectHighlighter::BoundingRectHighlighter(QDeclarativeViewObserver *view) :
|
|
|
|
LayerItem(view->declarativeView()->scene()),
|
2010-07-08 14:00:33 +02:00
|
|
|
m_view(view),
|
|
|
|
m_animFrame(0)
|
|
|
|
{
|
|
|
|
m_animTimer = new QTimer(this);
|
|
|
|
m_animTimer->setInterval(AnimInterval);
|
|
|
|
connect(m_animTimer, SIGNAL(timeout()), SLOT(animTimeout()));
|
|
|
|
}
|
|
|
|
|
2010-08-04 18:53:01 +02:00
|
|
|
BoundingRectHighlighter::~BoundingRectHighlighter()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-07-08 14:00:33 +02:00
|
|
|
void BoundingRectHighlighter::animTimeout()
|
|
|
|
{
|
|
|
|
++m_animFrame;
|
|
|
|
if (m_animFrame == AnimFrames) {
|
|
|
|
m_animTimer->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal alpha = m_animFrame / float(AnimFrames);
|
2010-07-13 12:03:21 +02:00
|
|
|
|
|
|
|
foreach(BoundingBox *box, m_boxes) {
|
|
|
|
box->highlightPolygonEdge->setOpacity(alpha);
|
|
|
|
}
|
2010-07-08 14:00:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BoundingRectHighlighter::clear()
|
|
|
|
{
|
2010-07-13 12:03:21 +02:00
|
|
|
if (m_boxes.length()) {
|
2010-07-08 14:00:33 +02:00
|
|
|
m_animTimer->stop();
|
|
|
|
|
2010-08-03 12:32:56 +02:00
|
|
|
foreach(BoundingBox *box, m_boxes) {
|
2010-08-04 18:53:01 +02:00
|
|
|
freeBoundingBox(box);
|
2010-08-03 12:32:56 +02:00
|
|
|
}
|
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
|
|
|
{
|
2010-07-13 12:03:21 +02:00
|
|
|
foreach(BoundingBox *box, m_boxes) {
|
|
|
|
if (box->highlightedObject.data() == item) {
|
|
|
|
return box;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoundingRectHighlighter::highlight(QList<QGraphicsObject*> items)
|
|
|
|
{
|
|
|
|
if (items.isEmpty())
|
2010-07-08 14:00:33 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
bool animate = false;
|
2010-07-13 12:03:21 +02:00
|
|
|
|
2010-08-04 18:53:01 +02:00
|
|
|
QList<BoundingBox *> newBoxes;
|
2010-07-13 12:03:21 +02:00
|
|
|
foreach(QGraphicsObject *itemToHighlight, items) {
|
|
|
|
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
|
|
|
animate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2010-07-13 12:03:21 +02:00
|
|
|
highlightAll(animate);
|
|
|
|
}
|
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
|
|
|
bool animate = false;
|
2010-07-08 14:00:33 +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;
|
|
|
|
animate = true;
|
|
|
|
qSort(m_boxes);
|
|
|
|
}
|
2010-07-08 14:00:33 +02:00
|
|
|
|
2010-07-13 12:03:21 +02:00
|
|
|
highlightAll(animate);
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
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
|
|
|
|
2010-08-04 18:53:01 +02:00
|
|
|
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();
|
2010-07-13 12:03:21 +02:00
|
|
|
QRectF itemAndChildRect = item->boundingRect() | item->childrenBoundingRect();
|
|
|
|
|
|
|
|
QPolygonF boundingRectInSceneSpace(item->mapToScene(itemAndChildRect));
|
|
|
|
QPolygonF boundingRectInLayerItemSpace = mapFromScene(boundingRectInSceneSpace);
|
2010-07-27 13:13:12 +02:00
|
|
|
QRectF bboxRect = m_view->adjustToScreenBoundaries(boundingRectInLayerItemSpace.boundingRect());
|
|
|
|
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));
|
|
|
|
|
|
|
|
if (animate)
|
|
|
|
box->highlightPolygonEdge->setOpacity(0);
|
2010-07-08 14:00:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (animate) {
|
|
|
|
m_animFrame = 0;
|
|
|
|
m_animTimer->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoundingRectHighlighter::refresh()
|
|
|
|
{
|
2010-07-13 12:03:21 +02:00
|
|
|
if (!m_boxes.isEmpty())
|
|
|
|
highlightAll(true);
|
2010-07-08 14:00:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-16 15:59:52 +02:00
|
|
|
} // namespace QmlObserver
|