Fixed (returned back) mouse move events handling for clean scene.

Reworked resizing of items on board. Still have troubles with resizing at BottomRight resizer.
preferencesAboutTextFull
Aleksei Kanash 12 years ago
parent d469951de1
commit c57beb74ca
  1. 3
      src/board/UBBoardView.cpp
  2. 287
      src/domain/UBGraphicsDelegateFrame.cpp
  3. 4
      src/domain/UBGraphicsDelegateFrame.h

@ -749,9 +749,6 @@ void UBBoardView::handleItemMousePress(QMouseEvent *event)
void UBBoardView::handleItemMouseMove(QMouseEvent *event) void UBBoardView::handleItemMouseMove(QMouseEvent *event)
{ {
if (!movingItem)
return;
// determine item to move (maybee we need to move group of item or his parent. // determine item to move (maybee we need to move group of item or his parent.
movingItem = determineItemToMove(movingItem); movingItem = determineItemToMove(movingItem);

@ -224,12 +224,14 @@ void UBGraphicsDelegateFrame::mousePressEvent(QGraphicsSceneMouseEvent *event)
mAngleOffset = 0; mAngleOffset = 0;
mInitialTransform = buildTransform(); mInitialTransform = buildTransform();
mOriginalSize = delegated()->boundingRect().size();
mCurrentTool = toolFromPos(event->pos()); mCurrentTool = toolFromPos(event->pos());
setCursorFromAngle(QString::number((int)mAngle % 360)); setCursorFromAngle(QString::number((int)mAngle % 360));
event->accept(); event->accept();
prepareFramesToMove(getLinkedFrames()); if (moving())
prepareFramesToMove(getLinkedFrames());
} }
@ -358,20 +360,25 @@ QSizeF UBGraphicsDelegateFrame::getResizeVector(qreal moveX, qreal moveY)
return QSizeF(dPosX, dPosY); return QSizeF(dPosX, dPosY);
} }
void UBGraphicsDelegateFrame::resizeDelegate(qreal moveX, qreal moveY) QSizeF UBGraphicsDelegateFrame::resizeDelegate(qreal moveX, qreal moveY)
{ {
QPointF fixedPoint = getFixedPointFromPos(); QSizeF incVector;
mFixedPoint = getFixedPointFromPos();
UBResizableGraphicsItem* resizableItem = dynamic_cast<UBResizableGraphicsItem*>(delegated()); UBResizableGraphicsItem* resizableItem = dynamic_cast<UBResizableGraphicsItem*>(delegated());
if (resizableItem) if (resizableItem)
{ {
QSizeF originalSize = delegated()->boundingRect().size(); incVector = getResizeVector(moveX, moveY);
resizableItem->resize(originalSize + getResizeVector(moveX, moveY)); resizableItem->resize(mOriginalSize + incVector);
if (resizingTop() || resizingLeft() || ((mMirrorX || mMirrorY) && resizingBottomRight())) if (resizingTop() || resizingLeft() || ((mMirrorX || mMirrorY) && resizingBottomRight()))
{ {
delegated()->setPos(delegated()->pos()-getFixedPointFromPos()+fixedPoint); QPointF pos1 = getFixedPointFromPos();
delegated()->setPos(delegated()->pos()-pos1+mFixedPoint);
} }
} }
return incVector;
} }
void UBGraphicsDelegateFrame::mouseMoveEvent(QGraphicsSceneMouseEvent *event) void UBGraphicsDelegateFrame::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
@ -379,113 +386,111 @@ void UBGraphicsDelegateFrame::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
if (None == mCurrentTool) if (None == mCurrentTool)
return; return;
QLineF move; QLineF move = QLineF(mStartingPoint, event->scenePos());
if(rotating() || moving() || mOperationMode == Scaling)
move = QLineF(mStartingPoint, event->scenePos());
else
move = QLineF(event->lastScenePos(), event->scenePos());
qreal moveX = move.length() * cos((move.angle() - mAngle) * PI / 180); qreal moveX = move.length() * cos((move.angle() - mAngle) * PI / 180);
qreal moveY = -move.length() * sin((move.angle() - mAngle) * PI / 180); qreal moveY = -move.length() * sin((move.angle() - mAngle) * PI / 180);
qreal width = delegated()->boundingRect().width() * mTotalScaleX; qreal width = delegated()->boundingRect().width() * mTotalScaleX;
qreal height = delegated()->boundingRect().height() * mTotalScaleY; qreal height = delegated()->boundingRect().height() * mTotalScaleY;
if(!rotating()) if (mOperationMode == Scaling)
{ {
mTranslateX = moveX; if(!rotating())
// Perform the resize
if (resizingBottomRight())
{ {
// ----------------------------------------------------- mTranslateX = moveX;
// ! We want to keep the aspect ratio with this resize ! // Perform the resize
// ----------------------------------------------------- if (resizingBottomRight())
qreal scaleX; {
qreal scaleY; // -----------------------------------------------------
// ! We want to keep the aspect ratio with this resize !
if(!mMirrorX){ // -----------------------------------------------------
scaleX = (width + moveX) / width; qreal scaleX;
}else{ qreal scaleY;
scaleX = (width - moveX) / width;
}
if(!mMirrorY){
scaleY = (height + moveY) / height;
}else{
scaleY = (height - moveY) / height;
}
qreal scaleFactor = (scaleX + scaleY) / 2; if(!mMirrorX){
scaleX = (width + moveX) / width;
}else{
scaleX = (width - moveX) / width;
}
// Do not allow resizing of image size under frame size if(!mMirrorY){
if (canResizeBottomRight(width, height, scaleFactor)) scaleY = (height + moveY) / height;
{ }else{
if (mRespectRatio) scaleY = (height - moveY) / height;
{
mScaleX = scaleFactor;
mScaleY = scaleFactor;
} }
else
qreal scaleFactor = (scaleX + scaleY) / 2;
// Do not allow resizing of image size under frame size
if (canResizeBottomRight(width, height, scaleFactor))
{ {
mScaleX = scaleX; if (mRespectRatio)
mScaleY = scaleY; {
} mScaleX = scaleFactor;
} mScaleY = scaleFactor;
}else if (resizingLeft() || resizingRight())
{
if(width != 0){
qreal scaleX = 0.0;
if(resizingLeft()){
scaleX = (width - moveX) / width;
}else if(resizingRight()){
scaleX = (width + moveX) / width;
}
if(mDelegate->isFlippable() && qAbs(scaleX) != 0){
if((qAbs(width * scaleX)) < 2*mFrameWidth){
bool negative = (scaleX < 0)?true:false;
if(negative){
if(mMirrorX)
scaleX = 2*mFrameWidth/width;
else
scaleX = -2*mFrameWidth/width;
}else{
scaleX = -1;
}
} }
mScaleX = scaleX; else
}else if (scaleX > 1 || (width * scaleX) > 2 * mFrameWidth){ {
mScaleX = scaleX; mScaleX = scaleX;
if(resizingLeft()){ mScaleY = scaleY;
mTranslateX = moveX;
} }
} }
} }else if (resizingLeft() || resizingRight())
}else if(resizingTop() || resizingBottom()){ {
if(height != 0){ if(width != 0){
qreal scaleY = 0.0; qreal scaleX = 0.0;
if(resizingTop()){ if(resizingLeft()){
scaleY = (height - moveY) / height; scaleX = (width - moveX) / width;
}else if(resizingBottom()){ }else if(resizingRight()){
scaleY = (height + moveY) / height; scaleX = (width + moveX) / width;
} }
if(mDelegate->isFlippable() && qAbs(scaleX) != 0){
if(mDelegate->isFlippable() && qAbs(scaleY) != 0){ if((qAbs(width * scaleX)) < 2*mFrameWidth){
if((qAbs(height * scaleY)) < 2*mFrameWidth){ bool negative = (scaleX < 0)?true:false;
bool negative = (scaleY < 0)?true:false; if(negative){
if(negative){ if(mMirrorX)
if(mMirrorY) scaleX = 2*mFrameWidth/width;
scaleY = 2*mFrameWidth/width; else
else scaleX = -2*mFrameWidth/width;
scaleY = -2*mFrameWidth/width; }else{
}else{ scaleX = -1;
scaleY = -1; }
}
mScaleX = scaleX;
}else if (scaleX > 1 || (width * scaleX) > 2 * mFrameWidth){
mScaleX = scaleX;
if(resizingLeft()){
mTranslateX = moveX;
} }
} }
mScaleY = scaleY; }
}else if (scaleY > 1 || (height * scaleY) > 2 * mFrameWidth) }else if(resizingTop() || resizingBottom()){
{ if(height != 0){
mScaleY = scaleY; qreal scaleY = 0.0;
if(resizingTop()){ if(resizingTop()){
mTranslateY = moveY; scaleY = (height - moveY) / height;
}else if(resizingBottom()){
scaleY = (height + moveY) / height;
}
if(mDelegate->isFlippable() && qAbs(scaleY) != 0){
if((qAbs(height * scaleY)) < 2*mFrameWidth){
bool negative = (scaleY < 0)?true:false;
if(negative){
if(mMirrorY)
scaleY = 2*mFrameWidth/width;
else
scaleY = -2*mFrameWidth/width;
}else{
scaleY = -1;
}
}
mScaleY = scaleY;
}else if (scaleY > 1 || (height * scaleY) > 2 * mFrameWidth)
{
mScaleY = scaleY;
if(resizingTop()){
mTranslateY = moveY;
}
} }
} }
} }
@ -531,64 +536,69 @@ void UBGraphicsDelegateFrame::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
moveLinkedItems(move); moveLinkedItems(move);
} }
QTransform tr = buildTransform(); if (mOperationMode == Scaling || moving() || rotating())
if (resizingRight() || resizingBottom() || resizingBottomRight())
{ {
QPointF ref; QTransform tr = buildTransform();
// we just detects coordinates of corner before and after scaling and then moves object at diff between them. if (resizingRight() || resizingBottom() || resizingBottomRight())
if (resizingBottomRight() && mMirrorX)
{ {
mTranslateX += mInitialTransform.map(delegated()->boundingRect().bottomRight()).x() - tr.map(delegated()->boundingRect().bottomRight()).x(); QPointF ref;
mTranslateY += mInitialTransform.map(delegated()->boundingRect().bottomRight()).y() - tr.map(delegated()->boundingRect().bottomRight()).y();
// we just detects coordinates of corner before and after scaling and then moves object at diff between them.
if (resizingBottomRight() && mMirrorX)
{
mTranslateX += mInitialTransform.map(delegated()->boundingRect().bottomRight()).x() - tr.map(delegated()->boundingRect().bottomRight()).x();
mTranslateY += mInitialTransform.map(delegated()->boundingRect().bottomRight()).y() - tr.map(delegated()->boundingRect().bottomRight()).y();
}
else
{
mTranslateX += mInitialTransform.map(delegated()->boundingRect().topLeft()).x() - tr.map(delegated()->boundingRect().topLeft()).x();
mTranslateY += mInitialTransform.map(delegated()->boundingRect().topLeft()).y() - tr.map(delegated()->boundingRect().topLeft()).y();
}
} }
else else if (resizingTop() || resizingLeft())
{ {
mTranslateX += mInitialTransform.map(delegated()->boundingRect().topLeft()).x() - tr.map(delegated()->boundingRect().topLeft()).x(); QPointF bottomRight = tr.map(delegated()->boundingRect().bottomRight());
mTranslateY += mInitialTransform.map(delegated()->boundingRect().topLeft()).y() - tr.map(delegated()->boundingRect().topLeft()).y(); QPointF fixedPoint = mInitialTransform.map(delegated()->boundingRect().bottomRight());
mTranslateX += fixedPoint.x() - bottomRight.x();
mTranslateY += fixedPoint.y() - bottomRight.y();
} }
else if (moving() || rotating())
delegated()->setTransform(tr);
} }
else if (resizingTop() || resizingLeft()) else // resizing/resizing horizontally
{ {
QPointF bottomRight = tr.map(delegated()->boundingRect().bottomRight());
QPointF fixedPoint = mInitialTransform.map(delegated()->boundingRect().bottomRight());
mTranslateX += fixedPoint.x() - bottomRight.x();
mTranslateY += fixedPoint.y() - bottomRight.y();
}
if (resizingBottomRight())
if (mOperationMode == Scaling || moving() || rotating())
{
tr = buildTransform();
delegated()->setTransform(tr);
}
else if (mOperationMode == Resizing)
{
if (!moving() && !rotating())
{ {
if (resizingBottomRight()) static QSizeF incV = QSizeF();
{ static QSizeF incH = QSizeF();
if (mMirrorX && mMirrorY)
mCurrentTool = ResizeTop;
else
mCurrentTool = ResizeBottom;
resizeDelegate(moveX, moveY); if (mMirrorX && mMirrorY)
mCurrentTool = ResizeTop;
else
mCurrentTool = ResizeBottom;
if (mMirrorX && mMirrorY) mOriginalSize -= incH;
mCurrentTool = ResizeLeft;
else
mCurrentTool = ResizeRight;
resizeDelegate(moveX, moveY); incV = resizeDelegate(moveX, moveY);
mCurrentTool = ResizeBottomRight; mOriginalSize += incV;
}
if (mMirrorX && mMirrorY)
mCurrentTool = ResizeLeft;
else else
resizeDelegate(moveX, moveY); mCurrentTool = ResizeRight;
incH = resizeDelegate(moveX, moveY);
mOriginalSize -= incV;
mOriginalSize += incH;
mCurrentTool = ResizeBottomRight;
} }
else
resizeDelegate(moveX, moveY);
} }
event->accept(); event->accept();
} }
@ -689,6 +699,7 @@ void UBGraphicsDelegateFrame::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
event->accept(); event->accept();
mCurrentTool = None; mCurrentTool = None;
QGraphicsRectItem::mouseReleaseEvent(event); QGraphicsRectItem::mouseReleaseEvent(event);
// Show the buttons // Show the buttons

@ -39,7 +39,7 @@ class UBGraphicsDelegateFrame: public QGraphicsRectItem, public QObject
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
QPointF getFixedPointFromPos(); QPointF getFixedPointFromPos();
QSizeF getResizeVector(qreal moveX, qreal moveY); QSizeF getResizeVector(qreal moveX, qreal moveY);
void resizeDelegate(qreal moveX, qreal moveY); QSizeF resizeDelegate(qreal moveX, qreal moveY);
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
@ -105,6 +105,8 @@ class UBGraphicsDelegateFrame: public QGraphicsRectItem, public QObject
QPointF mStartingPoint; QPointF mStartingPoint;
QTransform mInitialTransform; QTransform mInitialTransform;
QSizeF mOriginalSize;
QPointF mFixedPoint;
QGraphicsSvgItem* mBottomRightResizeGripSvgItem; QGraphicsSvgItem* mBottomRightResizeGripSvgItem;
QGraphicsSvgItem* mBottomResizeGripSvgItem; QGraphicsSvgItem* mBottomResizeGripSvgItem;

Loading…
Cancel
Save