Commit 7a013242 authored by Dmitry Kazakov's avatar Dmitry Kazakov
Browse files

Added discrete rotation and zooming shortcuts

BUG:302758
parent 53e9ce51
......@@ -97,6 +97,9 @@ static inline QList<Qt::Key> KEYS(Qt::Key key) {
static inline QList<Qt::Key> KEYS(Qt::Key key1, Qt::Key key2) {
return QList<Qt::Key>() << key1 << key2;
}
static inline QList<Qt::Key> KEYS(Qt::Key key1, Qt::Key key2, Qt::Key key3) {
return QList<Qt::Key>() << key1 << key2 << key3;
}
static inline QList<Qt::MouseButton> BUTTONS(Qt::MouseButton button) {
return QList<Qt::MouseButton>() << button;
}
......@@ -104,6 +107,7 @@ static inline QList<Qt::MouseButton> BUTTONS(Qt::MouseButton button1, Qt::MouseB
return QList<Qt::MouseButton>() << button1 << button2;
}
void KisInputManager::Private::addStrokeShortcut(KisAbstractInputAction* action, int index,
const QList<Qt::Key> &modifiers,
const QList<Qt::MouseButton> &buttons)
......@@ -173,8 +177,9 @@ void KisInputManager::Private::setupActions()
action = new KisRotateCanvasAction(q);
matcher.addAction(action);
addStrokeShortcut(action, KisPanAction::PanToggleShortcut, KEYS(Qt::Key_Shift, Qt::Key_Space), BUTTONS(Qt::LeftButton));
addStrokeShortcut(action, KisPanAction::PanToggleShortcut, KEYS(Qt::Key_Shift), BUTTONS(middleButton));
addStrokeShortcut(action, KisRotateCanvasAction::RotateToggleShortcut, KEYS(Qt::Key_Shift, Qt::Key_Space), BUTTONS(Qt::LeftButton));
addStrokeShortcut(action, KisRotateCanvasAction::DiscreteRotateToggleShortcut, KEYS(Qt::Key_Shift, Qt::Key_Alt, Qt::Key_Space), BUTTONS(Qt::LeftButton));
addStrokeShortcut(action, KisRotateCanvasAction::RotateToggleShortcut, KEYS(Qt::Key_Shift), BUTTONS(middleButton));
addKeyShortcut(action, KisRotateCanvasAction::RotateLeftShortcut, KEYS(), Qt::Key_4);
addKeyShortcut(action, KisRotateCanvasAction::RotateResetShortcut, KEYS(), Qt::Key_5);
......@@ -184,9 +189,11 @@ void KisInputManager::Private::setupActions()
action = new KisZoomAction(q);
matcher.addAction(action);
addStrokeShortcut(action, KisZoomAction::ZoomToggleShortcut, KEYS(Qt::Key_Control, Qt::Key_Space), BUTTONS(Qt::LeftButton));
addStrokeShortcut(action, KisZoomAction::ZoomToggleShortcut, KEYS(Qt::Key_Control), BUTTONS(middleButton));
addStrokeShortcut(action, KisZoomAction::ZoomToggleShortcut, KEYS(Qt::Key_Control, Qt::Key_Space), BUTTONS(Qt::LeftButton));
addStrokeShortcut(action, KisZoomAction::DiscreteZoomToggleShortcut, KEYS(Qt::Key_Control, Qt::Key_Alt, Qt::Key_Space), BUTTONS(Qt::LeftButton));
addWheelShortcut(action, KisZoomAction::ZoomInShortcut, KEYS(), KisKeyShortcut::WheelUp);
addWheelShortcut(action, KisZoomAction::ZoomOutShortcut, KEYS(), KisKeyShortcut::WheelDown);
......
......@@ -25,13 +25,23 @@
#include <kis_canvas2.h>
#include "kis_input_manager.h"
class KisRotateCanvasAction::Private
{
public:
Private() : angleDrift(0) {}
Shortcut mode;
qreal angleDrift;
};
KisRotateCanvasAction::KisRotateCanvasAction(KisInputManager* manager)
: KisAbstractInputAction(manager)
: KisAbstractInputAction(manager), d(new Private())
{
setName(i18n("Rotate Canvas"));
QHash<QString, int> shortcuts;
shortcuts.insert(i18n("Toggle Rotate Mode"), RotateToggleShortcut);
shortcuts.insert(i18n("Toggle Discrete Rotate Mode"), DiscreteRotateToggleShortcut);
shortcuts.insert(i18n("Rotate Left"), RotateLeftShortcut);
shortcuts.insert(i18n("Rotate Right"), RotateRightShortcut);
shortcuts.insert(i18n("Reset Rotation"), RotateResetShortcut);
......@@ -40,6 +50,7 @@ KisRotateCanvasAction::KisRotateCanvasAction(KisInputManager* manager)
KisRotateCanvasAction::~KisRotateCanvasAction()
{
delete d;
}
void KisRotateCanvasAction::activate()
......@@ -61,6 +72,11 @@ void KisRotateCanvasAction::begin(int shortcut, QEvent *event)
switch(shortcut) {
case RotateToggleShortcut:
d->mode = (Shortcut)shortcut;
break;
case DiscreteRotateToggleShortcut:
d->mode = (Shortcut)shortcut;
d->angleDrift = 0;
break;
case RotateLeftShortcut:
canvasController->rotateCanvasLeft15();
......@@ -84,7 +100,15 @@ void KisRotateCanvasAction::mouseMoved(const QPointF &lastPos, const QPointF &po
qreal oldAngle = atan2(oldPoint.y(), oldPoint.x());
qreal newAngle = atan2(newPoint.y(), newPoint.x());
float angle = (180 / M_PI) * (newAngle - oldAngle);
qreal angle = (180 / M_PI) * (newAngle - oldAngle);
if (d->mode == DiscreteRotateToggleShortcut) {
const qreal angleStep = 15;
qreal initialAngle = inputManager()->canvas()->rotationAngle();
qreal roundedAngle = qRound((initialAngle + angle + d->angleDrift) / angleStep) * angleStep - initialAngle;
d->angleDrift += angle - roundedAngle;
angle = roundedAngle;
}
KisCanvasController *canvasController =
dynamic_cast<KisCanvasController*>(inputManager()->canvas()->canvasController());
......
......@@ -36,6 +36,7 @@ public:
*/
enum Shortcut {
RotateToggleShortcut, ///< Toggle Rotate mode.
DiscreteRotateToggleShortcut, ///< Toggle Discrete Rotate mode.
RotateLeftShortcut, ///< Rotate left by a fixed amount.
RotateRightShortcut, ///< Rotate right by a fixed amount.
RotateResetShortcut ///< Reset the rotation to 0.
......@@ -47,6 +48,10 @@ public:
void deactivate();
void begin(int shortcut, QEvent *event);
void mouseMoved(const QPointF &lastPos, const QPointF &pos);
private:
class Private;
Private * const d;
};
#endif // KIS_ROTATE_CANVAS_ACTION_H
......@@ -31,8 +31,10 @@
class KisZoomAction::Private
{
public:
Private(KisZoomAction *qq) : q(qq) {}
Private(KisZoomAction *qq) : q(qq), distance(0) {}
KisZoomAction *q;
int distance;
Shortcuts mode;
void zoomTo(bool zoomIn, QEvent *event);
};
......@@ -80,6 +82,7 @@ KisZoomAction::KisZoomAction(KisInputManager* manager)
QHash< QString, int > shortcuts;
shortcuts.insert(i18n("Toggle Zoom Mode"), ZoomToggleShortcut);
shortcuts.insert(i18n("Toggle Discrete Zoom Mode"), DiscreteZoomToggleShortcut);
shortcuts.insert(i18n("Zoom In"), ZoomInShortcut);
shortcuts.insert(i18n("Zoom Out"), ZoomOutShortcut);
shortcuts.insert(i18n("Reset Zoom to 100%"), ZoomResetShortcut);
......@@ -109,6 +112,11 @@ void KisZoomAction::begin(int shortcut, QEvent *event)
switch(shortcut) {
case ZoomToggleShortcut:
d->mode = (Shortcuts)shortcut;
break;
case DiscreteZoomToggleShortcut:
d->mode = (Shortcuts)shortcut;
d->distance = 0;
break;
case ZoomInShortcut:
d->zoomTo(true, event);
......@@ -130,8 +138,21 @@ void KisZoomAction::begin(int shortcut, QEvent *event)
void KisZoomAction::mouseMoved(const QPointF &lastPos, const QPointF &pos)
{
QPointF relMovement = -(pos - lastPos);
float zoom = inputManager()->canvas()->view()->zoomController()->zoomAction()->effectiveZoom() + relMovement.y() / 100;
inputManager()->canvas()->view()->zoomController()->setZoom(KoZoomMode::ZOOM_CONSTANT, zoom);
QPointF diff = -(pos - lastPos);
const int stepCont = 100;
const int stepDisc = 20;
if (d->mode == ZoomToggleShortcut) {
float coeff = 1.0 + qreal(diff.y()) / stepCont;
float zoom = coeff * inputManager()->canvas()->view()->zoomController()->zoomAction()->effectiveZoom();
inputManager()->canvas()->view()->zoomController()->setZoom(KoZoomMode::ZOOM_CONSTANT, zoom);
} else if (d->mode == DiscreteZoomToggleShortcut) {
d->distance += diff.y();
bool zoomIn = d->distance > 0;
while (qAbs(d->distance) > stepDisc) {
d->zoomTo(zoomIn, 0);
d->distance += zoomIn ? -stepDisc : stepDisc;
}
}
}
......@@ -34,6 +34,7 @@ public:
*/
enum Shortcuts {
ZoomToggleShortcut, ///< Toggle zoom mode.
DiscreteZoomToggleShortcut, ///< Toggle discrete zoom mode
ZoomInShortcut, ///< Zoom in by a fixed amount.
ZoomOutShortcut, ///< Zoom out by a fixed amount.
ZoomResetShortcut, ///< Reset zoom to 100%.
......
Supports Markdown
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