From 51afc034d317d7a7c8065342218f13ce57be2e7d Mon Sep 17 00:00:00 2001 From: shibakaneki Date: Fri, 11 May 2012 09:17:23 +0200 Subject: [PATCH] Enabled a first version of the strokes as object --- src/board/UBDrawingController.cpp | 4 +- src/board/UBDrawingController.h | 2 +- src/domain/UBGraphicsDelegateFrame.cpp | 6 +- src/domain/UBGraphicsItemDelegate.cpp | 20 +++--- src/domain/UBGraphicsScene.cpp | 91 +++++++++++++++++++------- 5 files changed, 84 insertions(+), 39 deletions(-) diff --git a/src/board/UBDrawingController.cpp b/src/board/UBDrawingController.cpp index e03ef408..a9e4b813 100644 --- a/src/board/UBDrawingController.cpp +++ b/src/board/UBDrawingController.cpp @@ -45,7 +45,7 @@ UBDrawingController::UBDrawingController(QObject * parent) , mActiveRuler(NULL) , mStylusTool((UBStylusTool::Enum)-1) , mLatestDrawingTool((UBStylusTool::Enum)-1) - , mDrawingMode(DRAWING_MODE) + , mDrawingMode(/*DRAWING_MODE*/eDrawingMode_Vector) { connect(UBSettings::settings(), SIGNAL(colorContextChanged()), this, SIGNAL(colorPaletteChanged())); @@ -114,7 +114,7 @@ void UBDrawingController::setStylusTool(int tool) UBApplication::mainWindow->actionPen->setChecked(true); else if (mStylusTool == UBStylusTool::Eraser){ UBApplication::mainWindow->actionEraser->setChecked(true); - mDrawingMode = eDrawingMode_Artistic; + //mDrawingMode = eDrawingMode_Artistic; } else if (mStylusTool == UBStylusTool::Marker) UBApplication::mainWindow->actionMarker->setChecked(true); diff --git a/src/board/UBDrawingController.h b/src/board/UBDrawingController.h index 21d4f78d..4c620f1c 100644 --- a/src/board/UBDrawingController.h +++ b/src/board/UBDrawingController.h @@ -27,7 +27,7 @@ typedef enum{ eDrawingMode_Vector }eDrawingMode; -#define DRAWING_MODE eDrawingMode_Artistic +#define DRAWING_MODE eDrawingMode_Vector class UBDrawingController : public QObject { diff --git a/src/domain/UBGraphicsDelegateFrame.cpp b/src/domain/UBGraphicsDelegateFrame.cpp index 54dc4650..027d35da 100644 --- a/src/domain/UBGraphicsDelegateFrame.cpp +++ b/src/domain/UBGraphicsDelegateFrame.cpp @@ -583,9 +583,9 @@ void UBGraphicsDelegateFrame::positionHandles() { QRectF itemRect = delegated()->boundingRect(); - if (mDelegate->getToolBarItem()->isVisibleOnBoard() - && mDelegate->getToolBarItem()->isShifting()) - itemRect.setHeight(itemRect.height() + mDelegate->getToolBarItem()->rect().height() * mDelegate->antiScaleRatio() * 1.1); +// if (mDelegate->getToolBarItem()->isVisibleOnBoard() +// && mDelegate->getToolBarItem()->isShifting()) +// itemRect.setHeight(itemRect.height() + mDelegate->getToolBarItem()->rect().height() * mDelegate->antiScaleRatio() * 1.1); QTransform itemTransform = delegated()->sceneTransform(); QPointF topLeft = itemTransform.map(itemRect.topLeft()); diff --git a/src/domain/UBGraphicsItemDelegate.cpp b/src/domain/UBGraphicsItemDelegate.cpp index 8a7494ec..2ff9d0ed 100644 --- a/src/domain/UBGraphicsItemDelegate.cpp +++ b/src/domain/UBGraphicsItemDelegate.cpp @@ -117,7 +117,7 @@ UBGraphicsItemDelegate::UBGraphicsItemDelegate(QGraphicsItem* pDelegated, QObjec void UBGraphicsItemDelegate::init() { - mToolBarItem = new UBGraphicsToolBarItem(delegated()); + //mToolBarItem = new UBGraphicsToolBarItem(delegated()); mFrame = new UBGraphicsDelegateFrame(this, QRectF(0, 0, 0, 0), mFrameWidth, mRespectRatio); mFrame->hide(); @@ -167,7 +167,9 @@ UBGraphicsItemDelegate::~UBGraphicsItemDelegate() QVariant UBGraphicsItemDelegate::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) { - if (change == QGraphicsItem::ItemSelectedHasChanged) { + if(change == QGraphicsItem::ItemChildAddedChange){ + + }else if (change == QGraphicsItem::ItemSelectedHasChanged) { bool ok; bool selected = value.toUInt(&ok); if (ok) { @@ -321,17 +323,17 @@ void UBGraphicsItemDelegate::positionHandles() updateButtons(true); - if (mToolBarItem->isVisibleOnBoard()) - { - updateToolBar(); - mToolBarItem->show(); - } +// if (mToolBarItem->isVisibleOnBoard()) +// { +// updateToolBar(); +// mToolBarItem->show(); +// } } else { foreach(DelegateButton* button, mButtons) button->hide(); mFrame->hide(); - mToolBarItem->hide(); +// mToolBarItem->hide(); } } @@ -371,7 +373,7 @@ void UBGraphicsItemDelegate::remove(bool canUndo) scene->removeItem(mFrame); scene->removeItem(mDelegated); - scene->removeItem(mToolBarItem); + //scene->removeItem(mToolBarItem); if (canUndo) { diff --git a/src/domain/UBGraphicsScene.cpp b/src/domain/UBGraphicsScene.cpp index f6f10436..5ebab1fd 100644 --- a/src/domain/UBGraphicsScene.cpp +++ b/src/domain/UBGraphicsScene.cpp @@ -308,9 +308,11 @@ UBGraphicsScene::~UBGraphicsScene() void UBGraphicsScene::selectionChangedProcessing() { - if (selectedItems().count()) + if (selectedItems().count()){ + qDebug() << "Selected item bounding rect: " << selectedItems().first()->boundingRect(); UBApplication::showMessage("ZValue is " + QString::number(selectedItems().first()->zValue(), 'f') + "own z value is " + QString::number(selectedItems().first()->data(UBGraphicsItemData::ItemOwnZValue).toReal(), 'f')); + } } void UBGraphicsScene::enableGroupingButton() { @@ -747,6 +749,8 @@ void UBGraphicsScene::drawLineTo(const QPointF &pEndPoint, const qreal &pWidth, void UBGraphicsScene::eraseLineTo(const QPointF &pEndPoint, const qreal &pWidth) { +// QList itemList; + const QLineF line(mPreviousPoint, pEndPoint); const QPolygonF eraserPolygon = UBGeometryUtils::lineToPolygon(line, pWidth); @@ -793,24 +797,29 @@ void UBGraphicsScene::eraseLineTo(const QPointF &pEndPoint, const qreal &pWidth) if (mShouldUseOMP) { - #pragma omp parallel for +// #pragma omp parallel for for (int i = 0; i < collidItemsSize; i++) { - UBGraphicsPolygonItem *collidingPolygonItem = dynamic_cast(collidItems.at(i)); + UBGraphicsPolygonItem *collidingPolygonItem = qgraphicsitem_cast(collidItems.at(i)); if (NULL != collidingPolygonItem) { + collidingPolygonItem->setColor(QColor(Qt::green)); + UBGraphicsStrokesGroup* pGroup = collidingPolygonItem->strokesGroup(); if(eraserInnerRect.contains(collidingPolygonItem->boundingRect())) { - #pragma omp critical +// #pragma omp critical + qDebug() << "case 1"; // Put the entire polygon into the remove list + collidingPolygonItem->setColor(QColor(Qt::blue)); toBeRemovedItems << collidingPolygonItem; } else { // Here we get the polygon of the colliding item + QPolygonF collidingPolygon = collidingPolygonItem->polygon(); QPainterPath collidingPath; collidingPath.addPolygon(collidingPolygon); @@ -819,32 +828,40 @@ void UBGraphicsScene::eraseLineTo(const QPointF &pEndPoint, const qreal &pWidth) QPainterPath croppedPath = collidingPath.subtracted(eraserPath); QPainterPath croppedPathSimplified = croppedPath.simplified(); - if (croppedPath == collidingPath) + /*if (croppedPath == collidingPath) { // NOOP + qDebug() << "case 2"; + toBeRemovedItems << collidingPolygonItem; } - else if (croppedPathSimplified.isEmpty()) + else */if (croppedPathSimplified.isEmpty()) { - #pragma omp critical +// #pragma omp critical + qDebug() << "case 3"; // Put the entire polygon into the remove list if the eraser removes all its visible content + collidingPolygonItem->setColor(QColor(Qt::blue)); toBeRemovedItems << collidingPolygonItem; } else { + //qDebug() << "case 4"; // Then we convert the remaining path to a list of polygons that will be converted in // UBGraphicsPolygonItems and added to the scene foreach(const QPolygonF &pol, croppedPathSimplified.toFillPolygons()) { UBGraphicsPolygonItem* croppedPolygonItem = collidingPolygonItem->deepCopy(pol); - #pragma omp critical - // Add this new polygon to the 'added' list - toBeAddedItems << croppedPolygonItem; + croppedPolygonItem->setColor(QColor(Qt::black)); +// #pragma omp critical if(NULL != pGroup){ croppedPolygonItem->setStrokesGroup(pGroup); +// croppedPolygonItem->setTransform(pGroup->sceneTransform().inverted()); } + // Add this new polygon to the 'added' list + toBeAddedItems << croppedPolygonItem; } - #pragma omp critical +// #pragma omp critical // Remove the original polygonitem because it has been replaced by many smaller polygons + collidingPolygonItem->setColor(QColor(Qt::blue)); toBeRemovedItems << collidingPolygonItem; } } @@ -855,11 +872,12 @@ void UBGraphicsScene::eraseLineTo(const QPointF &pEndPoint, const qreal &pWidth) { for (int i = 0; i < collidItemsSize; i++) { - UBGraphicsPolygonItem *collidingPolygonItem - = qgraphicsitem_cast (collidItems.at(i)); + UBGraphicsPolygonItem *collidingPolygonItem = dynamic_cast (collidItems.at(i)); if (collidingPolygonItem) { + UBGraphicsStrokesGroup* pGroup = collidingPolygonItem->strokesGroup(); + if(eraserInnerRect.contains(collidingPolygonItem->boundingRect())) { toBeRemovedItems << collidingPolygonItem; @@ -887,6 +905,9 @@ void UBGraphicsScene::eraseLineTo(const QPointF &pEndPoint, const qreal &pWidth) { UBGraphicsPolygonItem* croppedPolygonItem = collidingPolygonItem->deepCopy(pol); toBeAddedItems << croppedPolygonItem; + if(NULL != pGroup){ + croppedPolygonItem->setStrokesGroup(pGroup); + } } toBeRemovedItems << collidingPolygonItem; @@ -896,28 +917,50 @@ void UBGraphicsScene::eraseLineTo(const QPointF &pEndPoint, const qreal &pWidth) } } - addItems(toBeAddedItems); - mAddedItems += toBeAddedItems; - if(eDrawingMode_Vector == DRAWING_MODE){ - foreach(QGraphicsItem* item, toBeAddedItems){ + foreach(QGraphicsItem* item, toBeRemovedItems){ UBGraphicsPolygonItem* poly = dynamic_cast(item); - if(NULL != poly && NULL != poly->strokesGroup()){ - poly->strokesGroup()->addToGroup(poly); + if(NULL != poly){ + if(NULL != poly->strokesGroup()){ + poly->strokesGroup()->removeFromGroup(poly); + removeItem(poly); + }else{ + qDebug() << "No group present"; + } } } + }else{ + removeItems(toBeRemovedItems); + mRemovedItems += toBeRemovedItems; } - removeItems(toBeRemovedItems); - mRemovedItems += toBeRemovedItems; - +// bool hack = false; +// UBGraphicsStrokesGroup* pG = new UBGraphicsStrokesGroup(); if(eDrawingMode_Vector == DRAWING_MODE){ - foreach(QGraphicsItem* item, toBeRemovedItems){ + foreach(QGraphicsItem* item, toBeAddedItems){ UBGraphicsPolygonItem* poly = dynamic_cast(item); if(NULL != poly && NULL != poly->strokesGroup()){ - poly->strokesGroup()->removeFromGroup(poly); +// if(!hack){ +// itemList = poly->strokesGroup()->childItems(); +// removeItem(poly->strokesGroup()); + +// foreach(QGraphicsItem* it, itemList){ +// pG->addToGroup(it); +// } +// hack = true; +// } + qreal dx = -poly->strokesGroup()->sceneTransform().inverted().dx(); + qreal dy = -poly->strokesGroup()->sceneTransform().inverted().dy(); + + //poly->setTransform(QTransform().translate(20, 0)); + poly->setTransform(QTransform().translate(dx, dy)); + poly->strokesGroup()/*pG*/->addToGroup(poly); } } + //addItem(pG); + }else{ + addItems(toBeAddedItems); + mAddedItems += toBeAddedItems; } }