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 mDeltaFactor = mEditor->preference()->isOn(SETTING::INVERT_DRAG_ZOOM_DIRECTION) ? -1 : 1;
39 connect(mEditor->preference(), &PreferenceManager::optionChanged, this, &HandTool::updateSettings);
40}
41
42void HandTool::updateSettings(const SETTING setting)
43{
44 switch (setting)
45 {
46 case SETTING::INVERT_DRAG_ZOOM_DIRECTION:
47 {
48 mDeltaFactor = mEditor->preference()->isOn(SETTING::INVERT_DRAG_ZOOM_DIRECTION) ? -1 : 1;
49 break;
50 }
51 default:
52 break;
53 }
54}
55
56QCursor HandTool::cursor()
57{
58 return mIsHeld ? QCursor(Qt::ClosedHandCursor) : QCursor(Qt::OpenHandCursor);
59}
60
61void HandTool::pointerPressEvent(PointerEvent* event)
62{
63 mLastPixel = event->viewportPos();
64 mStartPoint = event->canvasPos();
65 mIsHeld = true;
66
67 mScribbleArea->updateToolCursor();
68}
69
70void HandTool::pointerMoveEvent(PointerEvent* event)
71{
72 if (event->buttons() == Qt::NoButton)
73 {
74 return;
75 }
76
77 transformView(event->modifiers(), event->viewportPos(), event->buttons());
78 mLastPixel = event->viewportPos();
79}
80
81void HandTool::pointerReleaseEvent(PointerEvent* event)
82{
83 Q_UNUSED(event)
84 mIsHeld = false;
85 mScribbleArea->updateToolCursor();
86}
87
88void HandTool::pointerDoubleClickEvent(PointerEvent* event)
89{
90 if (event->button() == Qt::RightButton)
91 {
92 mEditor->view()->resetView();
93 }
94}
95
96void HandTool::transformView(Qt::KeyboardModifiers keyMod, const QPointF& pos, Qt::MouseButtons buttons)
97{
98 bool isTranslate = keyMod == Qt::NoModifier;
99 bool isRotate = keyMod & Qt::AltModifier;
100 bool isScale = (keyMod & Qt::ControlModifier) || (buttons & Qt::RightButton);
101
102 ViewManager* viewMgr = mEditor->view();
103
104 if (isTranslate)
105 {
106 QPointF d = viewMgr->mapScreenToCanvas(pos) - viewMgr->mapScreenToCanvas(mLastPixel);
107 QPointF offset = viewMgr->translation() + d;
108 viewMgr->translate(offset);
109 }
110 else if (isRotate)
111 {
112 QPoint centralPixel(mScribbleArea->width() / 2, mScribbleArea->height() / 2);
113 QVector2D startV(mLastPixel - centralPixel);
114 QVector2D curV(pos - centralPixel);
115
116 qreal angleOffset = static_cast<qreal>(std::atan2(curV.y(), curV.x()) - std::atan2(startV.y(), startV.x()));
117 angleOffset = qRadiansToDegrees(angleOffset);
118 // Invert rotation direction if view is flipped either vertically or horizontally
119 const float delta = viewMgr->isFlipHorizontal() == !viewMgr->isFlipVertical()
120 ? static_cast<float>(angleOffset * -1) : static_cast<float>(angleOffset);
121 viewMgr->rotateRelative(delta);
122 }
123 else if (isScale)
124 {
125 const float delta = (static_cast<float>(pos.y() - mLastPixel.y())) / 100.f;
126 const qreal scaleValue = viewMgr->scaling() * (1 + (delta * mDeltaFactor));
127 viewMgr->scaleAtOffset(scaleValue, mStartPoint);
128 }
129}
BaseTool
Definition: basetool.h:47
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 Tue Jan 6 2026 11:38:51 for Pencil2D by doxygen 1.9.6 based on revision f91a96748ec6712509b9b0ff47979db9c34d556f