Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions qrutils/graphicsUtils/rotater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,41 +122,43 @@ void Rotater::calcResizeItem(QGraphicsSceneMouseEvent *event)
const qreal mouseX = event->scenePos().x() - masterCenter.x();
const qreal mouseY = event->scenePos().y() - masterCenter.y();

// Master rotation is signed angle between initial and mouse vector.
// Calculating it from theese vectors product and cosine theorem
const qreal vectorProduct = zeroRotationVector.x() * mouseY
- zeroRotationVector.y() * mouseX;
const qreal mouseVectorLength = sqrt(mouseX * mouseX + mouseY * mouseY);
if (mouseVectorLength < EPS) {
return;
}

// Master rotation is signed angle between initial and mouse vector.
// Calculating it from theese vectors product and cosine theorem
const qreal vectorProductLength = zeroRotationVector.x() * mouseY
- zeroRotationVector.y() * mouseX;
const qreal sin = vectorProductLength / (mouseVectorLength * mLength);

const qreal translationX = mouseX - zeroRotationVector.x();
const qreal translationY = mouseY - zeroRotationVector.y();
const qreal translation = translationX * translationX + translationY * translationY;

const qreal sin = vectorProduct / (mouseVectorLength * mLength);
const bool cosIsNegative = mouseVectorLength * mouseVectorLength + mLength * mLength < translation;
const bool cosIsNegative = mouseVectorLength * mouseVectorLength + mLength * mLength
< translationX * translationX + translationY * translationY;

const qreal angleInWrongQuarter = asin(sin);
const qreal littleAngle = cosIsNegative ? M_PI - angleInWrongQuarter : angleInWrongQuarter;
const qreal angleInRightQuarter = cosIsNegative ? M_PI - angleInWrongQuarter : angleInWrongQuarter;
const qreal littleAngle = angleInRightQuarter * 180 / M_PI;

const qreal masterAngleCompensation = mMaster->parentItem()
? mMaster->parentItem()->rotation()
: 0.0;

const qreal deltaAngle = fmod(littleAngle - mMaster->rotation() * M_PI / 180, 2 * M_PI);
const qreal addAngle = deltaAngle > M_PI ? -2 * M_PI : deltaAngle < -M_PI ? 2 * M_PI : 0;
const qreal angle = mMaster->rotation() * M_PI / 180 + deltaAngle + addAngle;
const qreal deltaAngle = fmod(littleAngle - mMaster->rotation()- masterAngleCompensation, 360);
const qreal addAngle = deltaAngle > 180 ? -360 : deltaAngle < -180 ? 360 : 0;
const qreal angle = mMaster->rotation() + deltaAngle + addAngle;

if (event->modifiers() & Qt::ShiftModifier) {
qreal roundedAngle = (angle - fmod(angle, M_PI_4));
if (qAbs(roundedAngle - angle) > qAbs(roundedAngle + M_PI_4 - angle)) {
roundedAngle += M_PI_4;
qreal roundedAngle = (angle - fmod(angle, 45));
qreal nextRoundedAngle = roundedAngle + (angle > 0 ? 45 : -45);
if (qAbs(roundedAngle - angle) > qAbs(nextRoundedAngle - angle)) {
roundedAngle = nextRoundedAngle;
}
mMaster->setRotation(roundedAngle * 180 / M_PI - masterAngleCompensation);
mMaster->setRotation(roundedAngle);
} else {
mMaster->setRotation(angle * 180 / M_PI - masterAngleCompensation);
mMaster->setRotation(angle);
}
}

Expand Down