All Classes Namespaces Functions Variables Enumerations Properties Pages
handtool.cpp
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2020 Matthew Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 */
17 
18 #include "handtool.h"
19 
20 #include <QtMath>
21 #include <QPixmap>
22 #include <QVector2D>
23 #include <pointerevent.h>
24 
25 #include "layer.h"
26 #include "layercamera.h"
27 #include "editor.h"
28 #include "strokemanager.h"
29 #include "viewmanager.h"
30 #include "scribblearea.h"
31 
32 
33 HandTool::HandTool(QObject* parent) : BaseTool(parent)
34 {
35 }
36 
37 void HandTool::loadSettings()
38 {
39  properties.width = -1;
40  properties.feather = -1;
41  properties.useFeather = false;
42  properties.stabilizerLevel = -1;
43  properties.useAA = -1;
44 }
45 
46 QCursor HandTool::cursor()
47 {
48  return mIsHeld ? Qt::ClosedHandCursor : Qt::OpenHandCursor;
49 }
50 
51 void HandTool::pointerPressEvent(PointerEvent*)
52 {
53  mLastPixel = getCurrentPixel();
54  mStartPoint = mEditor->view()->mapScreenToCanvas(mLastPixel);
55  mIsHeld = true;
56 
57  mScribbleArea->updateToolCursor();
58 }
59 
60 void HandTool::pointerMoveEvent(PointerEvent* event)
61 {
62  if (event->buttons() == Qt::NoButton)
63  {
64  return;
65  }
66 
67  transformView(event->modifiers(), event->buttons());
68  mLastPixel = getCurrentPixel();
69 }
70 
71 void HandTool::pointerReleaseEvent(PointerEvent* event)
72 {
73  Q_UNUSED(event)
74  mIsHeld = false;
75  mScribbleArea->updateToolCursor();
76 }
77 
78 void HandTool::pointerDoubleClickEvent(PointerEvent* event)
79 {
80  if (event->button() == Qt::RightButton)
81  {
82  mEditor->view()->resetView();
83  }
84 }
85 
86 void HandTool::transformView(Qt::KeyboardModifiers keyMod, Qt::MouseButtons buttons)
87 {
88  bool isTranslate = keyMod == Qt::NoModifier;
89  bool isRotate = keyMod & Qt::AltModifier;
90  bool isScale = (keyMod & Qt::ControlModifier) || (buttons & Qt::RightButton);
91 
92  ViewManager* viewMgr = mEditor->view();
93 
94  if (isTranslate)
95  {
96  QPointF d = getCurrentPoint() - getLastPoint();
97  QPointF offset = viewMgr->translation() + d;
98  viewMgr->translate(offset);
99  }
100  else if (isRotate)
101  {
102  QPoint centralPixel(mScribbleArea->width() / 2, mScribbleArea->height() / 2);
103  QVector2D startV(getLastPixel() - centralPixel);
104  QVector2D curV(getCurrentPixel() - centralPixel);
105 
106  qreal angleOffset = static_cast<qreal>(std::atan2(curV.y(), curV.x()) - std::atan2(startV.y(), startV.x()));
107  angleOffset = qRadiansToDegrees(angleOffset);
108  float newAngle = viewMgr->rotation() + static_cast<float>(angleOffset);
109  viewMgr->rotate(newAngle);
110  }
111  else if (isScale)
112  {
113  float delta = (static_cast<float>(getCurrentPixel().y() - mLastPixel.y())) / 100.f;
114  qreal scaleValue = viewMgr->scaling() * (1 + delta);
115  viewMgr->scaleWithOffset(scaleValue, mStartPoint);
116  }
117 }
typedef KeyboardModifiers
Qt::KeyboardModifiers modifiers() const
Returns the modifier created by keyboard while a device was in use.
NoButton
qreal y() const const
ClosedHandCursor
Qt::MouseButtons buttons() const
Returns Qt::MouseButtons()