From 0fc1a4df929201a966bc3a18dffd2bf879ad17e8 Mon Sep 17 00:00:00 2001 From: Anatoly Mihalchenko Date: Fri, 24 Aug 2012 18:29:23 +0300 Subject: [PATCH] Eraser: functionality improvement --- src/domain/UBGraphicsPolygonItem.cpp | 22 ++------- src/domain/UBGraphicsPolygonItem.h | 2 - src/domain/UBGraphicsScene.cpp | 73 +++++++++++++++++++++++++--- 3 files changed, 69 insertions(+), 28 deletions(-) diff --git a/src/domain/UBGraphicsPolygonItem.cpp b/src/domain/UBGraphicsPolygonItem.cpp index f00bf42b..b21f2760 100644 --- a/src/domain/UBGraphicsPolygonItem.cpp +++ b/src/domain/UBGraphicsPolygonItem.cpp @@ -143,7 +143,10 @@ QColor UBGraphicsPolygonItem::color() const UBItem* UBGraphicsPolygonItem::deepCopy() const { - UBGraphicsPolygonItem* copy = deepCopy(this->polygon()); + UBGraphicsPolygonItem* copy = new UBGraphicsPolygonItem(polygon(), parentItem()); + + copyItemParameters(copy); + copy->mOriginalLine = this->mOriginalLine; copy->mOriginalWidth = this->mOriginalWidth; copy->mIsNominalLine = this->mIsNominalLine; @@ -152,23 +155,6 @@ UBItem* UBGraphicsPolygonItem::deepCopy() const } -UBGraphicsPolygonItem* UBGraphicsPolygonItem::deepCopy(const QPolygonF& pol) const -{ - QPolygonF p(pol); - if (parentItem()!=NULL) - { - p = mapToItem(parentItem(), p); - } - UBGraphicsPolygonItem* copy = new UBGraphicsPolygonItem(p, parentItem()); - - copyItemParameters(copy); - - // TODO UB 4.7 ... complete all members ? - - return copy; - -} - void UBGraphicsPolygonItem::copyItemParameters(UBItem *copy) const { UBGraphicsPolygonItem *cp = dynamic_cast(copy); diff --git a/src/domain/UBGraphicsPolygonItem.h b/src/domain/UBGraphicsPolygonItem.h index 1917e731..7b961e52 100644 --- a/src/domain/UBGraphicsPolygonItem.h +++ b/src/domain/UBGraphicsPolygonItem.h @@ -87,8 +87,6 @@ class UBGraphicsPolygonItem : public QGraphicsPolygonItem, public UBItem virtual UBItem* deepCopy() const; - // optimisation (eraser) - UBGraphicsPolygonItem* deepCopy(const QPolygonF& pol) const; virtual void copyItemParameters(UBItem *copy) const; QLineF originalLine() { return mOriginalLine;} diff --git a/src/domain/UBGraphicsScene.cpp b/src/domain/UBGraphicsScene.cpp index 9e88e124..fbac6ef1 100644 --- a/src/domain/UBGraphicsScene.cpp +++ b/src/domain/UBGraphicsScene.cpp @@ -740,6 +740,63 @@ void UBGraphicsScene::drawLineTo(const QPointF &pEndPoint, const qreal &pWidth, } } +void UBGraphicsScene::eraseLineTo(const QPointF &pEndPoint, const qreal &pWidth) +{ + const QLineF line(mPreviousPoint, pEndPoint); + mPreviousPoint = pEndPoint; + + const QPolygonF eraserPolygon = UBGeometryUtils::lineToPolygon(line, pWidth); + const QRectF eraserBoundingRect = eraserPolygon.boundingRect(); + + QPainterPath eraserPath; + eraserPath.addPolygon(eraserPolygon); + + // Get all the items that are intersecting with the eraser path + QList collidItems = items(eraserBoundingRect, Qt::IntersectsItemBoundingRect); + + QList intersectedItems; + QList intersectedPolygons; + + #pragma omp parallel for + for(int i=0; i(collidItems[i]); + if(pi == NULL) + continue; + + QPainterPath itemPainterPath; + itemPainterPath.addPolygon(pi->sceneTransform().map(pi->polygon())); + if (eraserPath.contains(itemPainterPath)) + { + // Compele remove item + intersectedItems << pi; + intersectedPolygons << QPolygonF(); + } + else if (eraserPath.intersects(itemPainterPath)) + { + QPainterPath newPath = itemPainterPath.subtracted(eraserPath); + intersectedItems << pi; + intersectedPolygons << newPath.simplified().toFillPolygon(pi->sceneTransform().inverted()); + } + } + + for(int i=0; isetPolygon(intersectedPolygons[i]); + } + } + + if (!intersectedItems.empty()) + setModified(true); +} + +/* void UBGraphicsScene::eraseLineTo(const QPointF &pEndPoint, const qreal &pWidth) { const QLineF line(mPreviousPoint, pEndPoint); @@ -810,7 +867,6 @@ void UBGraphicsScene::eraseLineTo(const QPointF &pEndPoint, const qreal &pWidth) collidingPath.addPolygon(collidingPolygon); // Then we substract the eraser path to the polygon and we simplify it - /**/ QTransform polyTransform = collidingPolygonItem->sceneTransform().inverted(); QPointF mTrPrevPoint = polyTransform.map(mPreviousPoint); QPointF mTrEndPoint = polyTransform.map(pEndPoint); @@ -821,18 +877,18 @@ void UBGraphicsScene::eraseLineTo(const QPointF &pEndPoint, const qreal &pWidth) QPainterPath trEraser; trEraser.addPolygon(trEraserPolygon); QPainterPath croppedPath = collidingPath.subtracted(trEraser); - /**/ // Original //QPainterPath croppedPath = collidingPath.subtracted(eraserPath); QPainterPath croppedPathSimplified = croppedPath.simplified(); - /*if (croppedPath == collidingPath) - { - // NOOP - toBeRemovedItems << collidingPolygonItem; - } - else */if (croppedPathSimplified.isEmpty()) + //if (croppedPath == collidingPath) + //{ + // // NOOP + // toBeRemovedItems << collidingPolygonItem; + //} + //else + if (croppedPathSimplified.isEmpty()) { #pragma omp critical // Put the entire polygon into the remove list if the eraser removes all its visible content @@ -948,6 +1004,7 @@ void UBGraphicsScene::eraseLineTo(const QPointF &pEndPoint, const qreal &pWidth) mPreviousPoint = pEndPoint; } +*/ void UBGraphicsScene::drawArcTo(const QPointF& pCenterPoint, qreal pSpanAngle) {