Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
handtool.cpp
1 /*
2 
3 Pencil - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2017 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 #include <cmath>
20 #include <QPixmap>
21 #include <QVector2D>
22 #include "layer.h"
23 #include "layercamera.h"
24 #include "editor.h"
25 #include "viewmanager.h"
26 #include "scribblearea.h"
27 
28 
29 HandTool::HandTool(QObject *parent) :
30  BaseTool(parent)
31 {
32 }
33 
34 void HandTool::loadSettings()
35 {
36  properties.width = -1;
37  properties.feather = -1;
38  properties.useFeather = false;
39  properties.inpolLevel = -1;
40  properties.useAA = -1;
41 }
42 
43 QCursor HandTool::cursor()
44 {
45  return mButtonsDown > 0 ? Qt::ClosedHandCursor : Qt::OpenHandCursor;
46 }
47 
48 void HandTool::mousePressEvent( QMouseEvent* )
49 {
50  mLastPixel = getLastPressPixel();
51  ++mButtonsDown;
52  mScribbleArea->updateToolCursor();
53 }
54 
55 void HandTool::mouseReleaseEvent( QMouseEvent* event )
56 {
57  //---- stop the hand tool if this was mid button
58  if ( event->button() == Qt::MidButton )
59  {
60  qDebug( "[HandTool] Stop Hand Tool" );
61  mScribbleArea->setPrevTool();
62  }
63  --mButtonsDown;
64  mScribbleArea->updateToolCursor();
65 }
66 
67 void HandTool::mouseMoveEvent( QMouseEvent* evt )
68 {
69  if ( evt->buttons() == Qt::NoButton )
70  {
71  return;
72  }
73 
74  bool isTranslate = evt->modifiers() == Qt::NoModifier;
75  bool isRotate = evt->modifiers() & Qt::AltModifier;
76  bool isScale = ( evt->modifiers() & Qt::ControlModifier ) || ( evt->buttons() & Qt::RightButton );
77 
78 
79  ViewManager* viewMgr = mEditor->view();
80 
81  if ( isTranslate )
82  {
83  QPointF d = getCurrentPoint() - getLastPoint();
84  QPointF offset = viewMgr->translation() + d;
85  viewMgr->translate( offset );
86  }
87  else if ( isRotate )
88  {
89  QPoint centralPixel( mScribbleArea->width() / 2, mScribbleArea->height() / 2 );
90  QVector2D startV( getLastPixel() - centralPixel );
91  QVector2D curV( getCurrentPixel() - centralPixel );
92 
93  float angleOffset = ( atan2( curV.y(), curV.x() ) - atan2( startV.y(), startV.x() ) ) * 180.0 / M_PI;
94  float newAngle = viewMgr->rotation() + angleOffset;
95  viewMgr->rotate(newAngle);
96  }
97  else if ( isScale )
98  {
99  float delta = ( getCurrentPixel().y() - mLastPixel.y() ) / 100.f ;
100  float scaleValue = viewMgr->scaling() * (1.f + delta);
101  viewMgr->scale(scaleValue);
102  }
103 
104  mLastPixel = getCurrentPixel();
105 }
106 
107 void HandTool::mouseDoubleClickEvent( QMouseEvent *event )
108 {
109  if ( event->button() == Qt::RightButton )
110  {
111  mEditor->view()->resetView();
112  }
113 }