From 0dd72f68a2b71eaede2010c7dc43e0493d7e4eb2 Mon Sep 17 00:00:00 2001 From: shibakaneki Date: Thu, 5 Apr 2012 08:51:41 +0200 Subject: [PATCH] Backup of the mirroring --- src/domain/UBGraphicsDelegateFrame.cpp | 134 ++++++++++++++++++++----- src/domain/UBGraphicsDelegateFrame.h | 10 +- src/domain/UBGraphicsItemDelegate.cpp | 19 +++- src/domain/UBGraphicsItemDelegate.h | 2 + 4 files changed, 136 insertions(+), 29 deletions(-) diff --git a/src/domain/UBGraphicsDelegateFrame.cpp b/src/domain/UBGraphicsDelegateFrame.cpp index 7dd2e5ab..21c4e8fd 100644 --- a/src/domain/UBGraphicsDelegateFrame.cpp +++ b/src/domain/UBGraphicsDelegateFrame.cpp @@ -48,6 +48,8 @@ UBGraphicsDelegateFrame::UBGraphicsDelegateFrame(UBGraphicsItemDelegate* pDelega , mTotalTranslateX(0) , mTotalTranslateY(0) , mOperationMode(Scaling) + , mMirrorX(false) + , mMirrorY(false) { setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); @@ -156,14 +158,37 @@ void UBGraphicsDelegateFrame::initializeTransform() QPointF topRight = itemTransform.map(itemRect.topRight()); QPointF bottomLeft = itemTransform.map(itemRect.bottomLeft()); + qreal horizontalFlip = (topLeft.x() > topRight.x()) ? -1 : 1; + if(horizontalFlip < 0){ + // why this is because of the way of calculating the translations that checks which side is the most is the + // nearest instead of checking which one is the left side. + QPointF tmp = topLeft; + topLeft = topRight; + topRight = tmp; + + // because of the calculation of the height is done by lenght and not deltaY + bottomLeft = itemTransform.map(itemRect.bottomRight()); + } + + qreal verticalFlip = (bottomLeft.y() < topLeft.y()) ? -1 : 1; + // not sure that is usefull + if(verticalFlip < 0){ + topLeft = itemTransform.map(itemRect.bottomLeft()); + topRight = itemTransform.map(itemRect.bottomRight()); + bottomLeft = itemTransform.map(itemRect.topLeft()); + } + QLineF topLine(topLeft, topRight); QLineF leftLine(topLeft, bottomLeft); qreal width = topLine.length(); qreal height = leftLine.length(); mAngle = topLine.angle(); - mTotalScaleX = width / itemRect.width(); - mTotalScaleY = height / itemRect.height(); + + //the fact the the lenght is used we loose the horizontalFlip information + // a better way to do this is using DeltaX that preserve the direction information. + mTotalScaleX = (width / itemRect.width()) * horizontalFlip; + mTotalScaleY = height / itemRect.height() * verticalFlip; QTransform tr; QPointF center = delegated()->boundingRect().center(); @@ -192,6 +217,7 @@ void UBGraphicsDelegateFrame::mousePressEvent(QGraphicsSceneMouseEvent *event) mAngleOffset = 0; mInitialTransform = buildTransform(); + mCurrentTool = toolFromPos(event->pos()); event->accept(); @@ -205,9 +231,16 @@ void UBGraphicsDelegateFrame::mouseMoveEvent(QGraphicsSceneMouseEvent *event) qreal moveY = -move.length() * sin((move.angle() - mAngle) * PI / 180); qreal width = delegated()->boundingRect().width() * mTotalScaleX; qreal height = delegated()->boundingRect().height() * mTotalScaleY; + mTranslateX = moveX; + if(mOperationMode == Scaling) { +// // Hide the buttons +// mDelegate->setButtonsVisible(false); +// mResizing = true; + + // Perform the resize if (resizingBottomRight()) { // ----------------------------------------------------- @@ -240,11 +273,12 @@ void UBGraphicsDelegateFrame::mouseMoveEvent(QGraphicsSceneMouseEvent *event) if(resizingLeft()){ scaleX = (width - moveX) / width; }else if(resizingRight()){ - scaleX = (width + moveX) / width; + scaleX = (width + moveX) / width; } if(mDelegate->isFlippable() && qAbs(scaleX) != 0){ - if((width * qAbs(scaleX)) < 2*mFrameWidth){ + if((qAbs(width * scaleX)) < 2*mFrameWidth){ bool negative = (scaleX < 0)?true:false; + mMirrorX = (negative?mMirrorX:!mMirrorX); if(negative){ scaleX = -2*mFrameWidth/width; }else{ @@ -269,8 +303,9 @@ void UBGraphicsDelegateFrame::mouseMoveEvent(QGraphicsSceneMouseEvent *event) } if(mDelegate->isFlippable() && qAbs(scaleY) != 0){ - if((height * qAbs(scaleY)) < 2*mFrameWidth){ + if((qAbs(height * scaleY)) < 2*mFrameWidth){ bool negative = (scaleY < 0)?true:false; + mMirrorY = (negative?mMirrorY:!mMirrorY); if(negative){ scaleY = -2*mFrameWidth/width; }else{ @@ -281,7 +316,9 @@ void UBGraphicsDelegateFrame::mouseMoveEvent(QGraphicsSceneMouseEvent *event) }else if (scaleY > 1 || (height * scaleY) > 2 * mFrameWidth) { mScaleY = scaleY; - mTranslateY = moveY; + if(resizingTop()){ + mTranslateY = moveY; + } } } } @@ -421,6 +458,12 @@ void UBGraphicsDelegateFrame::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) mCurrentTool = None; QGraphicsRectItem::mouseReleaseEvent(event); + + // Show the buttons + if(isResizing()){ + mResizing = false; + } + mDelegate->setButtonsVisible(true); } @@ -464,6 +507,14 @@ void UBGraphicsDelegateFrame::positionHandles() QPointF bottomLeft = itemTransform.map(itemRect.bottomLeft()); QPointF center = itemTransform.map(itemRect.center()); + // Handle the mirroring + if(topLeft.x() > topRight.x()){ + QPointF tmp = topRight; + topRight = topLeft; + topLeft = tmp; + bottomLeft.setX(topLeft.x()); + } + QLineF topLine(topLeft, topRight); qreal angle = topLine.angle(); qreal width = topLine.length(); @@ -507,17 +558,8 @@ void UBGraphicsDelegateFrame::positionHandles() mBottomRightResizeGripSvgItem->setPos(rect().right() - brRect.width(), rect().bottom() - brRect.height()); mBottomResizeGripSvgItem->setPos(rect().center().x() - bRect.width() / 2, rect().bottom() - bRect.height()); - if(0 <= mScaleX){ - mLeftResizeGripSvgItem->setPos(rect().left(), rect().center().y() - lRect.height() / 2); - mRightResizeGripSvgItem->setPos(rect().right() - rRect.width(), rect().center().y() - rRect.height() / 2); - }else{ - mLeftResizeGripSvgItem->setPos(rect().right() - rRect.width(), rect().center().y() - lRect.height() / 2); - mRightResizeGripSvgItem->setPos(rect().left() , rect().center().y() - rRect.height() / 2); - } - - if(0 < mScaleY){ - - } + mLeftResizeGripSvgItem->setPos(rect().left(), rect().center().y() - lRect.height() / 2); + mRightResizeGripSvgItem->setPos(rect().right() - rRect.width(), rect().center().y() - rRect.height() / 2); mTopResizeGripSvgItem->setPos(rect().center().x() - trRect.width() / 2, rect().y()); mRotateButton->setPos(rect().right() - mFrameWidth - 5, rect().top() + 5); @@ -571,14 +613,37 @@ UBGraphicsDelegateFrame::FrameTool UBGraphicsDelegateFrame::toolFromPos(QPointF return None; else if (bottomRightResizeGripRect().contains(pos)) return ResizeBottomRight; - else if (bottomResizeGripRect().contains(pos)) - return ResizeBottom; - else if (leftResizeGripRect().contains(pos)) - return ResizeLeft; - else if (rightResizeGripRect().contains(pos)) - return ResizeRight; - else if (topResizeGripRect().contains(pos)) - return ResizeTop; + else if (bottomResizeGripRect().contains(pos)){ + if(mMirrorY){ + return ResizeTop; + }else{ + return ResizeBottom; + } + } + else if (leftResizeGripRect().contains(pos)){ + qDebug() << "LEFT GRIP (" << mMirrorX << ")"; + if(mMirrorX){ + return ResizeRight; + }else{ + return ResizeLeft; + } + return ResizeLeft; + } + else if (rightResizeGripRect().contains(pos)){ + qDebug() << "RIGHT GRIP (" << mMirrorX << ")"; + if(mMirrorX){ + return ResizeLeft; + }else{ + return ResizeRight; + } + } + else if (topResizeGripRect().contains(pos)){ + if(mMirrorY){ + return ResizeBottom; + }else{ + return ResizeTop; + } + } else if (rotateButtonBounds().contains(pos) && mDelegate && mDelegate->canRotate()) return Rotate; else @@ -621,3 +686,22 @@ QRectF UBGraphicsDelegateFrame::rotateButtonBounds() const return QRectF(rect().right()- mFrameWidth, rect().top(), mFrameWidth, mFrameWidth); } +void UBGraphicsDelegateFrame::refreshGeometry() +{ + // Here we want to have the left on the left, the right on the right, the top on the top and the bottom on the bottom! + QRectF itemRect = delegated()->boundingRect(); + QTransform itemTransform = delegated()->sceneTransform(); + QPointF topLeft = itemTransform.map(itemRect.topLeft()); + QPointF topRight = itemTransform.map(itemRect.topRight()); + QPointF bottomLeft = itemTransform.map(itemRect.bottomLeft()); + QPointF center = itemTransform.map(itemRect.center()); + + qDebug() << topLeft << ", " << topRight << ", " << bottomLeft; + + QLineF topLine(topLeft, topRight); + qreal angle = topLine.angle(); + qreal width = topLine.length(); + QLineF leftLine(topLeft, bottomLeft); + qreal height = leftLine.length(); + setRect(topRight.x() - mFrameWidth, topLeft.y() - mFrameWidth, width + 2*mFrameWidth, height + 2*mFrameWidth); +} diff --git a/src/domain/UBGraphicsDelegateFrame.h b/src/domain/UBGraphicsDelegateFrame.h index 71508419..2cd1e0d1 100644 --- a/src/domain/UBGraphicsDelegateFrame.h +++ b/src/domain/UBGraphicsDelegateFrame.h @@ -47,6 +47,7 @@ class UBGraphicsDelegateFrame: public QGraphicsRectItem, public QObject enum OperationMode {Scaling, Resizing}; void setOperationMode(OperationMode pMode) {mOperationMode = pMode;} + bool isResizing(){return mResizing;} private: QRectF bottomRightResizeGripRect() const; @@ -69,8 +70,8 @@ class UBGraphicsDelegateFrame: public QGraphicsRectItem, public QObject void initializeTransform (); enum FrameTool {None, Move, Rotate, ResizeBottomRight, ResizeTop, ResizeRight, ResizeBottom, ResizeLeft}; - - FrameTool toolFromPos (QPointF pos); + FrameTool toolFromPos (QPointF pos); + void refreshGeometry(); FrameTool mCurrentTool; UBGraphicsItemDelegate* mDelegate; @@ -111,5 +112,10 @@ class UBGraphicsDelegateFrame: public QGraphicsRectItem, public QObject OperationMode mOperationMode; QGraphicsItem* delegated(); + bool mMirrorX; + bool mMirrorY; + bool mResizing; + bool mMirroredXAtStart; + bool mMirroredYAtStart; }; #endif /* UBGRAPHICSDELEGATEFRAME_H_ */ diff --git a/src/domain/UBGraphicsItemDelegate.cpp b/src/domain/UBGraphicsItemDelegate.cpp index edcc5426..235cc7ba 100644 --- a/src/domain/UBGraphicsItemDelegate.cpp +++ b/src/domain/UBGraphicsItemDelegate.cpp @@ -239,6 +239,8 @@ void UBGraphicsItemDelegate::positionHandles() mDeleteButton->setTransform(tr); + + // This is where the position of the buttons is calculated. It starts always on the topleft of mFrame! qreal x = mFrame->rect().left()- mDeleteButton->renderer()->viewBox().width() * mAntiScaleRatio / 2; qreal y = mFrame->rect().top() - mDeleteButton->renderer()->viewBox().height() * mAntiScaleRatio / 2; @@ -251,7 +253,9 @@ void UBGraphicsItemDelegate::positionHandles() mDelegated->scene()->addItem(mDeleteButton); } - mDeleteButton->show(); + if(!mFrame->isResizing()){ + mDeleteButton->show(); + } bool shownOnDisplay = mDelegated->data(UBGraphicsItemData::ItemLayerType).toInt() != UBItemLayerType::Control; showHide(shownOnDisplay); @@ -264,13 +268,17 @@ void UBGraphicsItemDelegate::positionHandles() button->setTransform(tr); button->setPos(x + (i * 1.6 * mFrameWidth * mAntiScaleRatio), y); + + if (!button->scene()) { button->setParentItem(mFrame);//update parent for the case the item has been previously removed from scene if (mDelegated->scene()) mDelegated->scene()->addItem(button); } - button->show(); + if(!mFrame->isResizing()){ + button->show(); + } } } else @@ -469,3 +477,10 @@ bool UBGraphicsItemDelegate::isFlippable() { return mFlippable; } + +void UBGraphicsItemDelegate::setButtonsVisible(bool visible) +{ + foreach(DelegateButton* pButton, mButtons){ + pButton->setVisible(visible); + } +} diff --git a/src/domain/UBGraphicsItemDelegate.h b/src/domain/UBGraphicsItemDelegate.h index 30767179..a4bd3843 100644 --- a/src/domain/UBGraphicsItemDelegate.h +++ b/src/domain/UBGraphicsItemDelegate.h @@ -133,6 +133,8 @@ class UBGraphicsItemDelegate : public QObject void setFlippable(bool flippable); bool isFlippable(); + void setButtonsVisible(bool visible); + signals: void showOnDisplayChanged(bool shown); void lockChanged(bool locked);