Commit ef8bfd40 authored by Huon Imberger's avatar Huon Imberger

Lock crop ratio to current rect when holding Ctrl/Shift

Summary:
In many graphics apps, holding Ctrl and/or Shift locks the ratio of
the rectangle being resized, making it quicker than manually choosing
a ratio. This patch adds this functionality to the Crop tool.

When Ctrl or Shift is pressed, the current ratio of the crop rect
is saved, "locking it". When resizing, so long as Ctrl or Shift is
pressed, the ratio is enforced to this locked value. Otherwise it
behaves the same as before - enforcing ratio if one was chosen in
the GUI

Depends on D11379

Test Plan:
  - Crop tool behaves normally when Ctrl/Shift not pressed
  - Crop ratio locked when Ctrl/Shift is pressed
  - Ctrl/Shift has no effect if ratio is already restricted
    by the {nav Basic} toggle or {nav Advanced} combobox

Reviewers: #gwenview, rkflx

Reviewed By: #gwenview, rkflx

Differential Revision: https://phabricator.kde.org/D11380
parent 0cf3b3d7
...@@ -88,6 +88,7 @@ struct CropToolPrivate ...@@ -88,6 +88,7 @@ struct CropToolPrivate
CropHandle mMovingHandle; CropHandle mMovingHandle;
QPoint mLastMouseMovePos; QPoint mLastMouseMovePos;
double mCropRatio; double mCropRatio;
double mLockedCropRatio;
CropWidget* mCropWidget; CropWidget* mCropWidget;
QRect viewportCropRect() const QRect viewportCropRect() const
...@@ -223,6 +224,7 @@ CropTool::CropTool(RasterImageView* view) ...@@ -223,6 +224,7 @@ CropTool::CropTool(RasterImageView* view)
d->mCropHandleList << CH_Left << CH_Right << CH_Top << CH_Bottom << CH_TopLeft << CH_TopRight << CH_BottomLeft << CH_BottomRight; d->mCropHandleList << CH_Left << CH_Right << CH_Top << CH_Bottom << CH_TopLeft << CH_TopRight << CH_BottomLeft << CH_BottomRight;
d->mMovingHandle = CH_None; d->mMovingHandle = CH_None;
d->mCropRatio = 0.; d->mCropRatio = 0.;
d->mLockedCropRatio = 0.;
d->mRect = d->computeVisibleImageRect(); d->mRect = d->computeVisibleImageRect();
d->setupWidget(); d->setupWidget();
} }
...@@ -342,23 +344,29 @@ void CropTool::mouseMoveEvent(QGraphicsSceneMouseEvent* event) ...@@ -342,23 +344,29 @@ void CropTool::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
} }
d->mRect = d->mRect.normalized(); d->mRect = d->mRect.normalized();
// Enforce ratio // Enforce ratio:
if (d->mCropRatio > 0.) { double ratioToEnforce = d->mCropRatio;
// - if user is holding down Ctrl/Shift when resizing rect, lock to current rect ratio
if (event->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier) && d->mLockedCropRatio > 0) {
ratioToEnforce = d->mLockedCropRatio;
}
// - if user has restricted the ratio via the GUI
if (ratioToEnforce > 0.) {
if (d->mMovingHandle == CH_Top || d->mMovingHandle == CH_Bottom) { if (d->mMovingHandle == CH_Top || d->mMovingHandle == CH_Bottom) {
// Top or bottom // Top or bottom
int width = int(d->mRect.height() / d->mCropRatio); int width = int(d->mRect.height() / ratioToEnforce);
d->mRect.setWidth(width); d->mRect.setWidth(width);
} else if (d->mMovingHandle == CH_Left || d->mMovingHandle == CH_Right) { } else if (d->mMovingHandle == CH_Left || d->mMovingHandle == CH_Right) {
// Left or right // Left or right
int height = int(d->mRect.width() * d->mCropRatio); int height = int(d->mRect.width() * ratioToEnforce);
d->mRect.setHeight(height); d->mRect.setHeight(height);
} else if (d->mMovingHandle & CH_Top) { } else if (d->mMovingHandle & CH_Top) {
// Top left or top right // Top left or top right
int height = int(d->mRect.width() * d->mCropRatio); int height = int(d->mRect.width() * ratioToEnforce);
d->mRect.setTop(d->mRect.bottom() - height); d->mRect.setTop(d->mRect.bottom() - height);
} else if (d->mMovingHandle & CH_Bottom) { } else if (d->mMovingHandle & CH_Bottom) {
// Bottom left or bottom right // Bottom left or bottom right
int height = int(d->mRect.width() * d->mCropRatio); int height = int(d->mRect.width() * ratioToEnforce);
d->mRect.setHeight(height); d->mRect.setHeight(height);
} }
} }
...@@ -394,6 +402,11 @@ void CropTool::hoverMoveEvent(QGraphicsSceneHoverEvent* event) ...@@ -394,6 +402,11 @@ void CropTool::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
void CropTool::keyPressEvent(QKeyEvent* event) void CropTool::keyPressEvent(QKeyEvent* event)
{ {
// Lock crop ratio to current rect when user presses Control or Shift
if (event->key() == Qt::Key_Control || event->key() == Qt::Key_Shift) {
d->mLockedCropRatio = 1. * d->mRect.height() / d->mRect.width();
}
QDialogButtonBox *buttons = d->mCropWidget->findChild<QDialogButtonBox *>(); QDialogButtonBox *buttons = d->mCropWidget->findChild<QDialogButtonBox *>();
switch (event->key()) { switch (event->key()) {
case Qt::Key_Escape: case Qt::Key_Escape:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment