Skip to content

Commit

Permalink
Remove constrained rotation from hand tool, refactor further, and doc…
Browse files Browse the repository at this point in the history
…ument
  • Loading branch information
scribblemaniac committed Jul 1, 2019
1 parent c6f1f05 commit f9dcc9c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
8 changes: 1 addition & 7 deletions core_lib/src/managers/selectionmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ void SelectionManager::adjustSelection(const QPointF& currentPoint, qreal offset
{
mTempTransformedSelection = transformedSelection;
QPointF anchorPoint = transformedSelection.center();
qreal rotatedAngle = rotatedAngleFromPos(currentPoint, anchorPoint, rotationOffset);
qreal rotatedAngle = MathUtils::radToDeg(MathUtils::getDifferenceAngle(anchorPoint, currentPoint)) - rotationOffset;
if (rotationIncrement > 0) {
mRotatedAngle = constrainRotationToAngle(rotatedAngle, rotationIncrement);
} else {
Expand All @@ -262,12 +262,6 @@ int SelectionManager::constrainRotationToAngle(const qreal& rotatedAngle, const
return qRound(rotatedAngle / rotationIncrement) * rotationIncrement;
}

qreal SelectionManager::rotatedAngleFromPos(const QPointF& currentPoint, const QPointF& anchorPoint, const qreal& currentRotatedOffset) const
{
qreal deltaPoint = atan2( currentPoint.y() - anchorPoint.y(), currentPoint.x() - anchorPoint.x());
return MathUtils::radToDeg(deltaPoint) - currentRotatedOffset;
}

void SelectionManager::setSelection(QRectF rect)
{
resetSelectionTransformProperties();
Expand Down
1 change: 0 additions & 1 deletion core_lib/src/managers/selectionmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ class SelectionManager : public BaseManager
private:

int constrainRotationToAngle(const qreal& rotatedAngle, const int& rotationIncrement) const;
qreal rotatedAngleFromPos(const QPointF& currentPoint, const QPointF& anchorPoint, const qreal& currentRotatedOffset) const;

QRectF mSelection;
QRectF mTempTransformedSelection;
Expand Down
4 changes: 0 additions & 4 deletions core_lib/src/tool/handtool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ void HandTool::transformView(Qt::KeyboardModifiers keyMod, Qt::MouseButtons butt

qreal angleOffset = static_cast<qreal>(atan2(curV.y(), curV.x()) - atan2(startV.y(), startV.x()));
angleOffset = MathUtils::radToDeg(angleOffset);
if (keyMod & Qt::ShiftModifier)
{
angleOffset = qRound(angleOffset / 15) * 15;
}
float newAngle = viewMgr->rotation() + static_cast<float>(angleOffset);
viewMgr->rotate(newAngle);
}
Expand Down
3 changes: 2 additions & 1 deletion core_lib/src/tool/movetool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ GNU General Public License for more details.
#include "scribblearea.h"
#include "layervector.h"
#include "layermanager.h"
#include "mathutils.h"
#include "vectorimage.h"


Expand Down Expand Up @@ -213,7 +214,7 @@ void MoveTool::beginInteraction(Qt::KeyboardModifiers keyMod, Layer* layer)
if(selectMan->getMoveMode() == MoveMode::ROTATION) {
QPointF curPoint = getCurrentPoint();
QPointF anchorPoint = selectionRect.center();
mRotatedAngle = ( atan2( curPoint.y() - anchorPoint.y(), curPoint.x() - anchorPoint.x() ) ) * 180.0 / M_PI - selectMan->myRotation();
mRotatedAngle = MathUtils::radToDeg(MathUtils::getDifferenceAngle(anchorPoint, curPoint)) - selectMan->myRotation();
}
}

Expand Down
31 changes: 29 additions & 2 deletions core_lib/src/util/mathutils.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
#ifndef MATHUTILS_H
#define MATHUTILS_H

#define M_PI 3.14159265358979323846264338327950288
#include <QtMath>
#include <QPoint>

namespace MathUtils
{
inline double radToDeg(const double& radians)
/** Convert angles from radians to degrees.
*
* \param radians Angle in radians.
* \return Angle in degrees.
*/
inline qreal radToDeg(const qreal radians)
{
return radians * 180.0 / M_PI;
}

/** Convert angles from degrees to radians.
*
* \param degrees Angle in degrees.
* \return Angle in radians.
*/
inline qreal degToRad(const qreal degrees)
{
return degrees * M_PI / 180.0;
}

/** Get the angle from the difference vector a->b to the x-axis.
*
* \param a Start point of vector
* \param b End point of vector
* \return Angle in radians from [-pi,+pi]
*/
inline qreal getDifferenceAngle(const QPointF a, const QPointF b)
{
return qAtan2(b.y() - a.y(), b.x() - a.x());
}
}

#endif // MATHUTILS_H

0 comments on commit f9dcc9c

Please sign in to comment.