#include "../qdeclarativeviewobserver_p_p.h"
#include "../qmlobserverconstants_p.h"
-#include <QtCore/QDebug>
-
-#include <QtGui/QGraphicsPolygonItem>
+#include <QtGui/QGraphicsRectItem>
#include <QtGui/QGraphicsObject>
#include <QtGui/QGraphicsScene>
#include <QtGui/QPen>
-#include <cmath>
-
QT_BEGIN_NAMESPACE
-LiveSelectionIndicator::LiveSelectionIndicator(QDeclarativeViewObserver *editorView,
- QGraphicsObject *layerItem)
- : m_layerItem(layerItem), m_view(editorView)
+LiveSelectionIndicator::LiveSelectionIndicator(QDeclarativeViewObserver *viewObserver,
+ QGraphicsObject *layerItem)
+ : m_layerItem(layerItem)
+ , m_view(viewObserver)
{
}
void LiveSelectionIndicator::show()
{
- foreach (QGraphicsPolygonItem *item, m_indicatorShapeHash.values())
+ foreach (QGraphicsRectItem *item, m_indicatorShapeHash)
item->show();
}
void LiveSelectionIndicator::hide()
{
- foreach (QGraphicsPolygonItem *item, m_indicatorShapeHash.values())
+ foreach (QGraphicsRectItem *item, m_indicatorShapeHash)
item->hide();
}
void LiveSelectionIndicator::clear()
{
if (!m_layerItem.isNull()) {
- QHashIterator<QGraphicsItem*, QGraphicsPolygonItem *> iter(m_indicatorShapeHash);
- while (iter.hasNext()) {
- iter.next();
- m_layerItem.data()->scene()->removeItem(iter.value());
- delete iter.value();
+ QGraphicsScene *scene = m_layerItem.data()->scene();
+ foreach (QGraphicsRectItem *item, m_indicatorShapeHash) {
+ scene->removeItem(item);
+ delete item;
}
}
}
-QPolygonF LiveSelectionIndicator::addBoundingRectToPolygon(QGraphicsItem *item, QPolygonF &polygon)
-{
- // ### remove this if statement when QTBUG-12172 gets fixed
- if (item->boundingRect() != QRectF(0,0,0,0)) {
- QPolygonF bounding = item->mapToScene(item->boundingRect());
- if (bounding.isClosed()) //avoid crashes if there is an infinite scale.
- polygon = polygon.united(bounding);
- }
-
- foreach (QGraphicsItem *child, item->childItems()) {
- if (!QDeclarativeViewObserverPrivate::get(m_view)->isEditorItem(child))
- addBoundingRectToPolygon(child, polygon);
- }
- return polygon;
-}
-
void LiveSelectionIndicator::setItems(const QList<QWeakPointer<QGraphicsObject> > &itemList)
{
clear();
- // set selections to also all children if they are not editor items
-
foreach (const QWeakPointer<QGraphicsObject> &object, itemList) {
if (object.isNull())
continue;
QGraphicsItem *item = object.data();
- QGraphicsPolygonItem *newSelectionIndicatorGraphicsItem
- = new QGraphicsPolygonItem(m_layerItem.data());
if (!m_indicatorShapeHash.contains(item)) {
- m_indicatorShapeHash.insert(item, newSelectionIndicatorGraphicsItem);
-
- QPolygonF boundingShapeInSceneSpace;
- addBoundingRectToPolygon(item, boundingShapeInSceneSpace);
-
- QRectF boundingRect
- = m_view->adjustToScreenBoundaries(boundingShapeInSceneSpace.boundingRect());
- QPolygonF boundingRectInLayerItemSpace = m_layerItem.data()->mapFromScene(boundingRect);
-
- QPen pen;
- pen.setColor(QColor(108, 141, 221));
- newSelectionIndicatorGraphicsItem->setData(Constants::EditorItemDataKey,
- QVariant(true));
- newSelectionIndicatorGraphicsItem->setFlag(QGraphicsItem::ItemIsSelectable, false);
- newSelectionIndicatorGraphicsItem->setPolygon(boundingRectInLayerItemSpace);
- newSelectionIndicatorGraphicsItem->setPen(pen);
+ QGraphicsRectItem *selectionIndicator = new QGraphicsRectItem(m_layerItem.data());
+ m_indicatorShapeHash.insert(item, selectionIndicator);
+
+ const QRectF boundingRect = m_view->adjustToScreenBoundaries(item->mapRectToScene(item->boundingRect()));
+ const QRectF boundingRectInLayerItemSpace = m_layerItem.data()->mapRectFromScene(boundingRect);
+
+ selectionIndicator->setData(Constants::EditorItemDataKey, true);
+ selectionIndicator->setFlag(QGraphicsItem::ItemIsSelectable, false);
+ selectionIndicator->setRect(boundingRectInLayerItemSpace);
+ selectionIndicator->setPen(QColor(108, 141, 221));
}
}
}
QT_END_NAMESPACE
-
return result;
}
+static QPolygonF regionToPolygon(const QRegion ®ion)
+{
+ QPainterPath path;
+ foreach (const QRect &rect, region.rects())
+ path.addRect(rect);
+ return path.toFillPolygon();
+}
void SubcomponentMaskLayerItem::setCurrentItem(QGraphicsItem *item)
{
if (!m_currentItem)
return;
- QPolygonF viewPoly(QRectF(m_observer->declarativeView()->rect()));
- viewPoly = m_observer->declarativeView()->mapToScene(viewPoly.toPolygon());
+ QRect viewRect = m_observer->declarativeView()->rect();
+ viewRect = m_observer->declarativeView()->mapToScene(viewRect).boundingRect().toRect();
QRectF itemRect = item->boundingRect() | item->childrenBoundingRect();
- QPolygonF itemPoly(itemRect);
- itemPoly = item->mapToScene(itemPoly);
+ itemRect = item->mapRectToScene(itemRect);
// if updating the same item as before, resize the rectangle only bigger, not smaller.
if (prevItem == item && prevItem != 0) {
- m_itemPolyRect = resizeRect(itemPoly.boundingRect(), m_itemPolyRect);
+ m_itemPolyRect = resizeRect(itemRect, m_itemPolyRect);
} else {
- m_itemPolyRect = itemPoly.boundingRect();
+ m_itemPolyRect = itemRect;
}
QRectF borderRect = m_itemPolyRect;
borderRect.adjust(-1, -1, 1, 1);
m_borderRect->setRect(borderRect);
- itemPoly = viewPoly.subtracted(QPolygonF(m_itemPolyRect));
- setPolygon(itemPoly);
+ const QRegion externalRegion = QRegion(viewRect).subtracted(m_itemPolyRect.toRect());
+ setPolygon(regionToPolygon(externalRegion));
}
QGraphicsItem *SubcomponentMaskLayerItem::currentItem() const