Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • File List
Loading...
Searching...
No Matches
  • core_lib
  • src
  • tool
handtool.cpp
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5Copyright (C) 2012-2020 Matthew Chiawen Chang
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; version 2 of the License.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16*/
17
18#include "handtool.h"
19
20#include <QtMath>
21#include <QVector2D>
22#include <pointerevent.h>
23
24#include "layer.h"
25#include "layercamera.h"
26#include "editor.h"
27#include "strokeinterpolator.h"
28#include "viewmanager.h"
29#include "scribblearea.h"
30
31
32HandTool::HandTool(QObject* parent) : BaseTool(parent)
33{
34}
35
36void HandTool::loadSettings()
37{
38 properties.width = -1;
39 properties.feather = -1;
40 properties.useFeather = false;
41 properties.stabilizerLevel = -1;
42 properties.useAA = -1;
43
44 mDeltaFactor = mEditor->preference()->isOn(SETTING::INVERT_DRAG_ZOOM_DIRECTION) ? -1 : 1;
45 connect(mEditor->preference(), &PreferenceManager::optionChanged, this, &HandTool::updateSettings);
46}
47
48void HandTool::saveSettings()
49{
50}
51
52void HandTool::updateSettings(const SETTING setting)
53{
54 switch (setting)
55 {
56 case SETTING::INVERT_DRAG_ZOOM_DIRECTION:
57 {
58 mDeltaFactor = mEditor->preference()->isOn(SETTING::INVERT_DRAG_ZOOM_DIRECTION) ? -1 : 1;
59 break;
60 }
61 default:
62 break;
63 }
64}
65
66QCursor HandTool::cursor()
67{
68 return mIsHeld ? QCursor(Qt::ClosedHandCursor) : QCursor(Qt::OpenHandCursor);
69}
70
71void HandTool::pointerPressEvent(PointerEvent* event)
72{
73 mLastPixel = event->viewportPos();
74 mStartPoint = event->canvasPos();
75 mIsHeld = true;
76
77 mScribbleArea->updateToolCursor();
78}
79
80void HandTool::pointerMoveEvent(PointerEvent* event)
81{
82 if (event->buttons() == Qt::NoButton)
83 {
84 return;
85 }
86
87 transformView(event->modifiers(), event->viewportPos(), event->buttons());
88 mLastPixel = event->viewportPos();
89}
90
91void HandTool::pointerReleaseEvent(PointerEvent* event)
92{
93 Q_UNUSED(event)
94 mIsHeld = false;
95 mScribbleArea->updateToolCursor();
96}
97
98void HandTool::pointerDoubleClickEvent(PointerEvent* event)
99{
100 if (event->button() == Qt::RightButton)
101 {
102 mEditor->view()->resetView();
103 }
104}
105
106void HandTool::transformView(Qt::KeyboardModifiers keyMod, const QPointF& pos, Qt::MouseButtons buttons)
107{
108 bool isTranslate = keyMod == Qt::NoModifier;
109 bool isRotate = keyMod & Qt::AltModifier;
110 bool isScale = (keyMod & Qt::ControlModifier) || (buttons & Qt::RightButton);
111
112 ViewManager* viewMgr = mEditor->view();
113
114 if (isTranslate)
115 {
116 QPointF d = viewMgr->mapScreenToCanvas(pos) - viewMgr->mapScreenToCanvas(mLastPixel);
117 QPointF offset = viewMgr->translation() + d;
118 viewMgr->translate(offset);
119 }
120 else if (isRotate)
121 {
122 QPoint centralPixel(mScribbleArea->width() / 2, mScribbleArea->height() / 2);
123 QVector2D startV(mLastPixel - centralPixel);
124 QVector2D curV(pos - centralPixel);
125
126 qreal angleOffset = static_cast<qreal>(std::atan2(curV.y(), curV.x()) - std::atan2(startV.y(), startV.x()));
127 angleOffset = qRadiansToDegrees(angleOffset);
128 // Invert rotation direction if view is flipped either vertically or horizontally
129 const float delta = viewMgr->isFlipHorizontal() == !viewMgr->isFlipVertical()
130 ? static_cast<float>(angleOffset * -1) : static_cast<float>(angleOffset);
131 viewMgr->rotateRelative(delta);
132 }
133 else if (isScale)
134 {
135 const float delta = (static_cast<float>(pos.y() - mLastPixel.y())) / 100.f;
136 const qreal scaleValue = viewMgr->scaling() * (1 + (delta * mDeltaFactor));
137 viewMgr->scaleAtOffset(scaleValue, mStartPoint);
138 }
139}
BaseTool
Definition: basetool.h:70
PointerEvent
Definition: pointerevent.h:8
ViewManager
Definition: viewmanager.h:26
QCursor
QObject
QObject::connect
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::event
virtual bool event(QEvent *e)
QPoint
QPointF
QPointF::y
qreal y() const const
Qt
Qt::ClosedHandCursor
ClosedHandCursor
Qt::KeyboardModifiers
typedef KeyboardModifiers
Qt::NoButton
NoButton
QVector2D
QWidget::height
height
QWidget::width
width
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39