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  //---- stop the hand tool if this was mid button
74  if (event->button() == Qt::MidButton)
75  {
76  qDebug("[HandTool] Stop Hand Tool");
77  mScribbleArea->setPrevTool();
78  }
79  mIsHeld = false;
80  mScribbleArea->updateToolCursor();
81 }
82 
83 void HandTool::pointerDoubleClickEvent(PointerEvent* event)
84 {
85  if (event->button() == Qt::RightButton)
86  {
87  mEditor->view()->resetView();
88  }
89 }
90 
91 void HandTool::transformView(Qt::KeyboardModifiers keyMod, Qt::MouseButtons buttons)
92 {
93  bool isTranslate = keyMod == Qt::NoModifier;
94  bool isRotate = keyMod & Qt::AltModifier;
95  bool isScale = (keyMod & Qt::ControlModifier) || (buttons & Qt::RightButton);
96 
97  ViewManager* viewMgr = mEditor->view();
98 
99  if (isTranslate)
100  {
101  QPointF d = getCurrentPoint() - getLastPoint();
102  QPointF offset = viewMgr->translation() + d;
103  viewMgr->translate(offset);
104  }
105  else if (isRotate)
106  {
107  QPoint centralPixel(mScribbleArea->width() / 2, mScribbleArea->height() / 2);
108  QVector2D startV(getLastPixel() - centralPixel);
109  QVector2D curV(getCurrentPixel() - centralPixel);
110 
111  qreal angleOffset = static_cast<qreal>(std::atan2(curV.y(), curV.x()) - std::atan2(startV.y(), startV.x()));
112  angleOffset = qRadiansToDegrees(angleOffset);
113  float newAngle = viewMgr->rotation() + static_cast<float>(angleOffset);
114  viewMgr->rotate(newAngle);
115  }
116  else if (isScale)
117  {
118  float delta = (static_cast<float>(getCurrentPixel().y() - mLastPixel.y())) / 100.f;
119  qreal scaleValue = viewMgr->scaling() * (1 + delta);
120  viewMgr->scaleWithOffset(scaleValue, mStartPoint);
121  }
122 }
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()
Qt::MouseButton button() const
Returns Qt::MouseButton()