|
|
@ -9,7 +9,7 @@ |
|
|
|
#include "core/memcheck.h" |
|
|
|
#include "core/memcheck.h" |
|
|
|
|
|
|
|
|
|
|
|
UBGraphicsGroupContainerItem::UBGraphicsGroupContainerItem(QGraphicsItem *parent) |
|
|
|
UBGraphicsGroupContainerItem::UBGraphicsGroupContainerItem(QGraphicsItem *parent) |
|
|
|
: QGraphicsItemGroup(parent) |
|
|
|
: QGraphicsItem(parent) |
|
|
|
{ |
|
|
|
{ |
|
|
|
setData(UBGraphicsItemData::ItemLayerType, UBItemLayerType::Object); |
|
|
|
setData(UBGraphicsItemData::ItemLayerType, UBItemLayerType::Object); |
|
|
|
|
|
|
|
|
|
|
@ -23,8 +23,123 @@ UBGraphicsGroupContainerItem::UBGraphicsGroupContainerItem(QGraphicsItem *parent |
|
|
|
UBGraphicsGroupContainerItem::setAcceptHoverEvents(true); |
|
|
|
UBGraphicsGroupContainerItem::setAcceptHoverEvents(true); |
|
|
|
|
|
|
|
|
|
|
|
setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::ObjectItem)); //Necessary to set if we want z value to be assigned correctly
|
|
|
|
setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::ObjectItem)); //Necessary to set if we want z value to be assigned correctly
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void UBGraphicsGroupContainerItem::addToGroup(QGraphicsItem *item) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (!item) { |
|
|
|
|
|
|
|
qWarning("UBGraphicsGroupContainerItem::addToGroup: cannot add null item"); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (item == this) { |
|
|
|
|
|
|
|
qWarning("UBGraphicsGroupContainerItem::addToGroup: cannot add a group to itself"); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// COMBINE
|
|
|
|
|
|
|
|
bool ok; |
|
|
|
|
|
|
|
QTransform itemTransform = item->itemTransform(this, &ok); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!ok) { |
|
|
|
|
|
|
|
qWarning("UBGraphicsGroupContainerItem::addToGroup: could not find a valid transformation from item to group coordinates"); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QTransform newItemTransform(itemTransform); |
|
|
|
|
|
|
|
item->setPos(mapFromItem(item, 0, 0)); |
|
|
|
|
|
|
|
item->setParentItem(this); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// removing position from translation component of the new transform
|
|
|
|
|
|
|
|
if (!item->pos().isNull()) |
|
|
|
|
|
|
|
newItemTransform *= QTransform::fromTranslate(-item->x(), -item->y()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// removing additional transformations properties applied with itemTransform()
|
|
|
|
|
|
|
|
QPointF origin = item->transformOriginPoint(); |
|
|
|
|
|
|
|
QMatrix4x4 m; |
|
|
|
|
|
|
|
QList<QGraphicsTransform*> transformList = item->transformations(); |
|
|
|
|
|
|
|
for (int i = 0; i < transformList.size(); ++i) |
|
|
|
|
|
|
|
transformList.at(i)->applyTo(&m); |
|
|
|
|
|
|
|
newItemTransform *= m.toTransform().inverted(); |
|
|
|
|
|
|
|
newItemTransform.translate(origin.x(), origin.y()); |
|
|
|
|
|
|
|
newItemTransform.rotate(-item->rotation()); |
|
|
|
|
|
|
|
newItemTransform.scale(1/item->scale(), 1/item->scale()); |
|
|
|
|
|
|
|
newItemTransform.translate(-origin.x(), -origin.y()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ### Expensive, we could maybe use dirtySceneTransform bit for optimization
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
item->setTransform(newItemTransform); |
|
|
|
|
|
|
|
// item->d_func()->setIsMemberOfGroup(true);
|
|
|
|
|
|
|
|
prepareGeometryChange(); |
|
|
|
|
|
|
|
itemsBoundingRect |= itemTransform.mapRect(item->boundingRect() | item->childrenBoundingRect()); |
|
|
|
|
|
|
|
update(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
void UBGraphicsGroupContainerItem::removeFromGroup(QGraphicsItem *item) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (!item) { |
|
|
|
|
|
|
|
qWarning("QGraphicsItemGroup::removeFromGroup: cannot remove null item"); |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QGraphicsItem *newParent = parentItem(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// COMBINE
|
|
|
|
|
|
|
|
bool ok; |
|
|
|
|
|
|
|
QTransform itemTransform; |
|
|
|
|
|
|
|
if (newParent) |
|
|
|
|
|
|
|
itemTransform = item->itemTransform(newParent, &ok); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
itemTransform = item->sceneTransform(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QPointF oldPos = item->mapToItem(newParent, 0, 0); |
|
|
|
|
|
|
|
item->setParentItem(newParent); |
|
|
|
|
|
|
|
item->setPos(oldPos); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// removing position from translation component of the new transform
|
|
|
|
|
|
|
|
if (!item->pos().isNull()) |
|
|
|
|
|
|
|
itemTransform *= QTransform::fromTranslate(-item->x(), -item->y()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// removing additional transformations properties applied
|
|
|
|
|
|
|
|
// with itemTransform() or sceneTransform()
|
|
|
|
|
|
|
|
QPointF origin = item->transformOriginPoint(); |
|
|
|
|
|
|
|
QMatrix4x4 m; |
|
|
|
|
|
|
|
QList<QGraphicsTransform*> transformList = item->transformations(); |
|
|
|
|
|
|
|
for (int i = 0; i < transformList.size(); ++i) |
|
|
|
|
|
|
|
transformList.at(i)->applyTo(&m); |
|
|
|
|
|
|
|
itemTransform *= m.toTransform().inverted(); |
|
|
|
|
|
|
|
itemTransform.translate(origin.x(), origin.y()); |
|
|
|
|
|
|
|
itemTransform.rotate(-item->rotation()); |
|
|
|
|
|
|
|
itemTransform.scale(1 / item->scale(), 1 / item->scale()); |
|
|
|
|
|
|
|
itemTransform.translate(-origin.x(), -origin.y()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ### Expensive, we could maybe use dirtySceneTransform bit for optimization
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
item->setTransform(itemTransform); |
|
|
|
|
|
|
|
// item->d_func()->setIsMemberOfGroup(item->group() != 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ### Quite expensive. But removeFromGroup() isn't called very often.
|
|
|
|
|
|
|
|
prepareGeometryChange(); |
|
|
|
|
|
|
|
itemsBoundingRect = childrenBoundingRect(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QRectF UBGraphicsGroupContainerItem::boundingRect() const |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return itemsBoundingRect; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
void UBGraphicsGroupContainerItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, |
|
|
|
|
|
|
|
QWidget *widget) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Q_UNUSED(widget); |
|
|
|
|
|
|
|
if (option->state & QStyle::State_Selected) { |
|
|
|
|
|
|
|
painter->setBrush(Qt::NoBrush); |
|
|
|
|
|
|
|
QPen tmpPen; |
|
|
|
|
|
|
|
qreal tmpPenWidth = 1.0; |
|
|
|
|
|
|
|
tmpPen.setWidth(tmpPenWidth); |
|
|
|
|
|
|
|
tmpPen.setColor(Qt::lightGray); |
|
|
|
|
|
|
|
painter->setPen(tmpPen); |
|
|
|
|
|
|
|
painter->drawRect(itemsBoundingRect.adjusted(tmpPenWidth / 2, tmpPenWidth / 2, -tmpPenWidth / 2, -tmpPenWidth / 2)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
UBGraphicsScene *UBGraphicsGroupContainerItem::scene() |
|
|
|
UBGraphicsScene *UBGraphicsGroupContainerItem::scene() |
|
|
|
{ |
|
|
|
{ |
|
|
@ -60,23 +175,18 @@ void UBGraphicsGroupContainerItem::destroy() { |
|
|
|
foreach (QGraphicsItem *item, childItems()) { |
|
|
|
foreach (QGraphicsItem *item, childItems()) { |
|
|
|
removeFromGroup(item); |
|
|
|
removeFromGroup(item); |
|
|
|
item->setFlag(QGraphicsItem::ItemIsSelectable, true); |
|
|
|
item->setFlag(QGraphicsItem::ItemIsSelectable, true); |
|
|
|
|
|
|
|
item->setFlag(QGraphicsItem::ItemIsFocusable, true); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
mDelegate->remove(true); |
|
|
|
mDelegate->remove(true); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void UBGraphicsGroupContainerItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
qDebug() << "hover move group"; |
|
|
|
|
|
|
|
QGraphicsItemGroup::hoverMoveEvent(event); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void UBGraphicsGroupContainerItem::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
|
|
void UBGraphicsGroupContainerItem::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (mDelegate->mousePressEvent(event)) { |
|
|
|
if (mDelegate->mousePressEvent(event)) { |
|
|
|
//NOOP
|
|
|
|
//NOOP
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
QGraphicsItemGroup::mousePressEvent(event); |
|
|
|
QGraphicsItem::mousePressEvent(event); |
|
|
|
setSelected(true); |
|
|
|
setSelected(true); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -86,18 +196,18 @@ void UBGraphicsGroupContainerItem::mouseMoveEvent(QGraphicsSceneMouseEvent *even |
|
|
|
if (mDelegate->mouseMoveEvent(event)) { |
|
|
|
if (mDelegate->mouseMoveEvent(event)) { |
|
|
|
// NOOP;
|
|
|
|
// NOOP;
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
QGraphicsItemGroup::mouseMoveEvent(event); |
|
|
|
QGraphicsItem::mouseMoveEvent(event); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void UBGraphicsGroupContainerItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) |
|
|
|
void UBGraphicsGroupContainerItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) |
|
|
|
{ |
|
|
|
{ |
|
|
|
mDelegate->mouseReleaseEvent(event); |
|
|
|
mDelegate->mouseReleaseEvent(event); |
|
|
|
QGraphicsItemGroup::mouseReleaseEvent(event); |
|
|
|
QGraphicsItem::mouseReleaseEvent(event); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
QVariant UBGraphicsGroupContainerItem::itemChange(GraphicsItemChange change, const QVariant &value) |
|
|
|
QVariant UBGraphicsGroupContainerItem::itemChange(GraphicsItemChange change, const QVariant &value) |
|
|
|
{ |
|
|
|
{ |
|
|
|
QVariant newValue = mDelegate->itemChange(change, value); |
|
|
|
QVariant newValue = mDelegate->itemChange(change, value); |
|
|
|
return QGraphicsItemGroup::itemChange(change, newValue); |
|
|
|
return QGraphicsItem::itemChange(change, newValue); |
|
|
|
} |
|
|
|
} |
|
|
|