void CRailItemResizer::mousePressEvent(QGraphicsSceneMouseEvent *event){ if(event->button()==Qt::LeftButton) { if(editableItem()) foldItemPosition = editableItem()->pos(); fdragOrigin = event->pos(); fsizeOrigin = fsize; fcurrentAnchor = getCurrentAnchor(event->pos()); if(fcurrentAnchor!=AnchorNone) { QRectF anchorRect = fanchors.at(fcurrentAnchor); switch (fcurrentAnchor) { case AnchorLeft: foriginOffset.setX(fdragOrigin.x()-anchorRect.right()); break; case AnchorTopLeft: foriginOffset.setX(fdragOrigin.x()-anchorRect.right()); foriginOffset.setY(fdragOrigin.y()-anchorRect.bottom()); break; case AnchorTop: foriginOffset.setY(fdragOrigin.y()-anchorRect.bottom()); break; case AnchorTopRight: foriginOffset.setX(fdragOrigin.x()-anchorRect.left()); foriginOffset.setY(fdragOrigin.y()-anchorRect.bottom()); break; case AnchorRight: foriginOffset.setX(fdragOrigin.x()-anchorRect.left()); break; case AnchorBottomRight: foriginOffset.setX(fdragOrigin.x()-anchorRect.left()); foriginOffset.setY(fdragOrigin.y()-anchorRect.top()); break; case AnchorBottom: foriginOffset.setY(fdragOrigin.y()-anchorRect.top()); break; case AnchorBottomLeft: foriginOffset.setX(fdragOrigin.x()-anchorRect.right()); foriginOffset.setY(fdragOrigin.y()-anchorRect.top()); break; default: break; } } update(); //обновить картинку с выделеным анкером } else { QGraphicsObject::mousePressEvent(event); }}void CRailItemResizer::mouseMoveEvent(QGraphicsSceneMouseEvent *event){ QSizeF oldSize = fsize; switch(fcurrentAnchor) { case AnchorNone: onDrag(event->scenePos()-fdragOrigin); return; case AnchorTopLeft: oldSize.setHeight(oldSize.height()-(event->pos().y()-foriginOffset.y())); oldSize.setWidth(oldSize.width()-(event->pos().x()-foriginOffset.x())); onDrag(event->scenePos()-fdragOrigin); break; case AnchorTop: { QPointF oldPos = pos(); oldPos.setY(event->scenePos().y()-fdragOrigin.y()); oldSize.setHeight(oldSize.height()-(event->pos().y()-foriginOffset.y())); onDrag(oldPos); } break; case AnchorTopRight: { QPointF oldPos = pos(); oldPos.setY(event->scenePos().y()-fdragOrigin.y()); oldSize.setHeight(oldSize.height()-event->pos().y()+foriginOffset.y()); oldSize.setWidth(event->pos().x()-foriginOffset.x()); onDrag(oldPos); } break; case AnchorRight: oldSize.setWidth(event->pos().x()-foriginOffset.x()); break; case AnchorBottomRight: oldSize.setHeight(event->pos().y()-foriginOffset.y()); oldSize.setWidth(event->pos().x()-foriginOffset.x()); break; case AnchorBottom: { oldSize.setHeight(event->pos().y()-foriginOffset.y()); } break; case AnchorBottomLeft: { QPointF oldPos = pos(); oldPos.setX(event->scenePos().x()-fdragOrigin.x()); oldSize.setHeight(event->pos().y()-foriginOffset.y()); oldSize.setWidth(oldSize.width()-(event->pos().x()-foriginOffset.x())); onDrag(oldPos); } break; case AnchorLeft: { QPointF oldPos = pos(); oldPos.setX(event->scenePos().x()-fdragOrigin.x()); oldSize.setWidth(oldSize.width()-(event->pos().x()-foriginOffset.x())); onDrag(oldPos); } break; } fsize = oldSize; prepareGeometryChange(); recalculate(); onResize(fsize); //вызываем}void CRailItemResizer::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){ if(event->button()==Qt::LeftButton && editableItem()) { if(foldItemPosition!=editableItem()->pos() || fsizeOrigin!=fsize) emit changed(editableItem(),foldItemPosition,editableItem()->pos(),fsizeOrigin,fsize); }}
QTransform matrix; QRectF boundRect = item->boundingRect(); matrix.translate(boundRect.width()/2,boundRect.height()/2); matrix.rotate(angle); matrix.translate(-boundRect.width()/2,-boundRect.height()/2); //item->setRotation(angle); item->setTransform(matrix);
C++ (Qt)QRectF QGraphicsItem::sceneBoundingRect () const
C++ (Qt) case AnchorTopRight: { /* QPointF oldPos = pos(); oldPos.setY(event->scenePos().y()-fdragOrigin.y()); oldSize.setHeight(oldSize.height()-event->pos().y()+foriginOffset.y()); oldSize.setWidth(event->pos().x()-foriginOffset.x()); onDrag(oldPos); */ qreal x1 = 0, y1 = oldSize.height(); // opposite (left bottom) corner qreal x2 = event->pos().x(), y2 = event->pos().y(); // dragged (right top) corner oldSize = QSizeF(qAbs(x1 - x2), qAbs(y1 - y2)); // recalc size // QPointF newPos = QPointF(0, y1 - oldSize.height()) + pos(); // неверно QPointF newPos = editableItem()->mapToScene(QPointF(0, y2)); onDrag(newPos); } break;
C++ (Qt)void CRailItemResizer::mouseMoveEvent(QGraphicsSceneMouseEvent *event){ QPointF d = event->scenePos() - event->lastScenePos(); qreal dx1 = 0; qreal dy1 = 0; qreal dx2 = 0; qreal dy2 = 0; switch(fcurrentAnchor) { case AnchorNone: onDrag( pos() + d ); return; break; case AnchorTopLeft: dy1 = d.y(); dx1 = d.x(); break; case AnchorTop: dy1 = d.y(); break; case AnchorTopRight: dx2 = d.x(); dy1 = d.y(); break; case AnchorRight: dx2 = d.x(); break; case AnchorBottomRight: dx2 = d.x(); dy2 = d.y(); break; case AnchorBottom: dy2 = d.y(); break; case AnchorBottomLeft: dx1 = d.x(); dy2 = d.y(); break; case AnchorLeft: dx1 = d.x(); break; } QRectF r( pos(), size() ); r.adjust( dx1, dy1, dx2, dy2 ); onDrag( r.topLeft() ); fsize = r.size(); onResize( fsize ); prepareGeometryChange(); recalculate();}