Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
selecttool.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 "editor.h"
19 #include "layervector.h"
20 #include "scribblearea.h"
21 #include "layermanager.h"
22 #include "toolmanager.h"
23 #include "selecttool.h"
24 
25 // Store selection origin so we can calculate
26 // the selection rectangle in mousePressEvent.
27 static QPointF gSelectionOrigin;
28 
29 SelectTool::SelectTool(QObject *parent) :
30  BaseTool(parent)
31 {
32 }
33 
34 void SelectTool::loadSettings()
35 {
36  properties.width = -1;
37  properties.feather = -1;
38  properties.inpolLevel = -1;
39  properties.useAA = -1;
40 }
41 
42 QCursor SelectTool::cursor()
43 {
44  return Qt::CrossCursor;
45 }
46 
47 void SelectTool::mousePressEvent( QMouseEvent *event )
48 {
49  Layer *layer = mEditor->layers()->currentLayer();
50  if ( layer == NULL ) { return; }
51 
52  mScribbleArea->myRotatedAngle = 0;
53 
54  if ( event->button() == Qt::LeftButton )
55  {
56  gSelectionOrigin = getLastPoint(); // Store original click position for help with selection rectangle.
57 
58  if ( layer->type() == Layer::BITMAP || layer->type() == Layer::VECTOR )
59  {
60  if ( layer->type() == Layer::VECTOR )
61  {
62  ( ( LayerVector * )layer )->getLastVectorImageAtFrame( mEditor->currentFrame(), 0 )->deselectAll();
63  }
64  mScribbleArea->setMoveMode( ScribbleArea::NONE );
65  mEditor->backup( typeName() );
66 
67  if ( mScribbleArea->somethingSelected ) // there is something selected
68  {
69  if ( BezierCurve::mLength( getLastPoint() - mScribbleArea->myTransformedSelection.topLeft() ) < 6 )
70  {
71  mScribbleArea->setMoveMode( ScribbleArea::TOPLEFT );
72  }
73  if ( BezierCurve::mLength( getLastPoint() - mScribbleArea->myTransformedSelection.topRight() ) < 6 )
74  {
75  mScribbleArea->setMoveMode( ScribbleArea::TOPRIGHT );
76  }
77  if ( BezierCurve::mLength( getLastPoint() - mScribbleArea->myTransformedSelection.bottomLeft() ) < 6 )
78  {
79  mScribbleArea->setMoveMode( ScribbleArea::BOTTOMLEFT );
80  }
81  if ( BezierCurve::mLength( getLastPoint() - mScribbleArea->myTransformedSelection.bottomRight() ) < 6 )
82  {
83  mScribbleArea->setMoveMode( ScribbleArea::BOTTOMRIGHT );
84  }
85 
86  // the user did not click on one of the corners
87  //
88  if ( mScribbleArea->getMoveMode() == ScribbleArea::NONE )
89  {
90  // Deselect all and get ready for a new selection
91  //
92  mScribbleArea->deselectAll();
93 
94  mScribbleArea->mySelection.setTopLeft( getLastPoint() );
95  mScribbleArea->mySelection.setBottomRight( getLastPoint() );
96  mScribbleArea->setSelection( mScribbleArea->mySelection, true );
97  }
98  }
99  else // there is nothing selected
100  {
101  mScribbleArea->mySelection.setTopLeft( getLastPoint() );
102  mScribbleArea->mySelection.setBottomRight( getLastPoint() );
103  mScribbleArea->setSelection( mScribbleArea->mySelection, true );
104  }
105  mScribbleArea->update();
106  }
107  }
108 }
109 
110 void SelectTool::mouseReleaseEvent( QMouseEvent *event )
111 {
112  Layer *layer = mEditor->layers()->currentLayer();
113  if ( layer == NULL ) { return; }
114 
115  if ( event->button() == Qt::LeftButton )
116  {
117  if ( layer->type() == Layer::VECTOR )
118  {
119  if ( mScribbleArea->somethingSelected )
120  {
121  mEditor->tools()->setCurrentTool( MOVE );
122 
123  VectorImage *vectorImage = ( ( LayerVector * )layer )->getLastVectorImageAtFrame( mEditor->currentFrame(), 0 );
124  mScribbleArea->setSelection( vectorImage->getSelectionRect(), true );
125  if ( mScribbleArea->mySelection.width() <= 0 && mScribbleArea->mySelection.height() <= 0 )
126  {
127  mScribbleArea->deselectAll();
128  }
129  }
130  mScribbleArea->updateCurrentFrame();
131  mScribbleArea->setAllDirty();
132  }
133  else if ( layer->type() == Layer::BITMAP )
134  {
135  if ( mScribbleArea->mySelection.width() <= 0 && mScribbleArea->mySelection.height() <= 0 )
136  {
137  mScribbleArea->deselectAll();
138  }
139  mScribbleArea->updateCurrentFrame();
140  mScribbleArea->setAllDirty();
141  }
142  }
143 }
144 
145 void SelectTool::mouseMoveEvent( QMouseEvent *event )
146 {
147  Layer* layer = mEditor->layers()->currentLayer();
148  if ( layer == NULL ) { return; }
149 
150  if ( ( event->buttons() & Qt::LeftButton ) && mScribbleArea->somethingSelected && ( layer->type() == Layer::BITMAP || layer->type() == Layer::VECTOR ) )
151  {
152  switch ( mScribbleArea->getMoveMode() )
153  {
154  case ScribbleArea::NONE:
155  {
156  // Resize the selection rectangle so it goes from the origin point
157  // (i.e. where the mouse was clicked) to the current mouse
158  // position.
159  int mouseX = getCurrentPoint().x();
160  int mouseY = getCurrentPoint().y();
161  QRectF & selectRect = mScribbleArea->mySelection;
162 
163  if(mouseX < gSelectionOrigin.x())
164  {
165  selectRect.setLeft(mouseX);
166  selectRect.setRight(gSelectionOrigin.x());
167  }
168  else
169  {
170  selectRect.setLeft(gSelectionOrigin.x());
171  selectRect.setRight(mouseX);
172  }
173 
174  if(mouseY < gSelectionOrigin.y())
175  {
176  selectRect.setTop(mouseY);
177  selectRect.setBottom(gSelectionOrigin.y());
178  }
179  else
180  {
181  selectRect.setTop(gSelectionOrigin.y());
182  selectRect.setBottom(mouseY);
183  }
184 
185  break;
186  }
187 
188  case ScribbleArea::TOPLEFT:
189  mScribbleArea->mySelection.setTopLeft( getCurrentPoint() );
190  break;
191 
192  case ScribbleArea::TOPRIGHT:
193  mScribbleArea->mySelection.setTopRight( getCurrentPoint() );
194  break;
195 
196  case ScribbleArea::BOTTOMLEFT:
197  mScribbleArea->mySelection.setBottomLeft( getCurrentPoint() );
198  break;
199 
200  case ScribbleArea::BOTTOMRIGHT:
201  mScribbleArea->mySelection.setBottomRight( getCurrentPoint() );
202  break;
203 
204  default:
205  break;
206  }
207 
208  mScribbleArea->myTransformedSelection = mScribbleArea->mySelection.adjusted( 0, 0, 0, 0 );
209  mScribbleArea->myTempTransformedSelection = mScribbleArea->mySelection.adjusted( 0, 0, 0, 0 );
210 
211  if ( layer->type() == Layer::VECTOR )
212  {
213  ( ( LayerVector * )layer )->getLastVectorImageAtFrame( mEditor->currentFrame(), 0 )->select( mScribbleArea->mySelection );
214  }
215  mScribbleArea->update();
216  }
217 }
218 
219 bool SelectTool::keyPressEvent(QKeyEvent *event)
220 {
221  switch ( event->key() ) {
222  case Qt::Key_Alt:
223  mScribbleArea->setTemporaryTool( MOVE );
224  break;
225  default:
226  break;
227  }
228 
229  // Follow the generic behaviour anyway
230  //
231  return false;
232 }
Definition: layer.h:32