Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
viewmanager.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 "viewmanager.h"
19 #include "object.h"
20 #include <QVector2D>
21 
22 const static float mMinScale = 0.01f;
23 const static float mMaxScale = 100.0f;
24 
25 
26 ViewManager::ViewManager(QObject *parent) : BaseManager(parent)
27 {
28 }
29 
30 bool ViewManager::init()
31 {
32  return true;
33 }
34 
35 Status ViewManager::load( Object* o )
36 {
37  mView = o->data()->getCurrentView();
38 
39  if ( mView.isIdentity() )
40  {
41  translate( 0, 0 );
42  }
43  updateViewTransforms();
44 
45  return Status::OK;
46 }
47 
48 Status ViewManager::save( Object* o )
49 {
50  o->data()->setCurrentView( mView );
51  return Status();
52 }
53 
54 QPointF ViewManager::mapCanvasToScreen( QPointF p )
55 {
56  return mViewCanvas.map( p );
57 }
58 
59 QPointF ViewManager::mapScreenToCanvas(QPointF p)
60 {
61  return mViewCanvasInverse.map( p );
62 }
63 
64 QPainterPath ViewManager::mapCanvasToScreen( const QPainterPath& path )
65 {
66  return mViewCanvas.map( path );
67 }
68 
69 QRectF ViewManager::mapCanvasToScreen( const QRectF& rect )
70 {
71  return mViewCanvas.mapRect( rect ) ;
72 }
73 
74 QRectF ViewManager::mapScreenToCanvas( const QRectF& rect )
75 {
76  return mViewCanvasInverse.mapRect( rect ) ;
77 }
78 
79 QPainterPath ViewManager::mapScreenToCanvas( const QPainterPath& path )
80 {
81  return mViewCanvasInverse.map( path );
82 }
83 
84 QTransform ViewManager::getView()
85 {
86  return mViewCanvas;
87 }
88 
89 void ViewManager::updateViewTransforms()
90 {
91  QTransform t;
92  t.translate(mTranslate.x(), mTranslate.y());
93 
94  QTransform r;
95  r.rotate(mRotate);
96 
97  float flipX = mIsFlipHorizontal ? -1.f : 1.f;
98  float flipY = mIsFlipVertical ? -1.f : 1.f;
99 
100  QTransform s;
101  s.scale(mScale * flipX, mScale * flipY);
102 
103  QTransform c;
104  c.translate(mCanvasSize.width() / 2.f, mCanvasSize.height() / 2.f);
105 
106  mView = t * s * r;
107 
108  mViewInverse = mView.inverted();
109  mViewCanvas = mView * mCentre;
110  mViewCanvasInverse = mViewCanvas.inverted();
111 }
112 
113 void ViewManager::translate(float dx, float dy)
114 {
115  mTranslate = QPointF(dx, dy);
116  updateViewTransforms();
117 
118  Q_EMIT viewChanged();
119 }
120 
121 void ViewManager::translate(QPointF offset)
122 {
123  translate( offset.x(), offset.y() );
124 }
125 
126 void ViewManager::rotate(float degree)
127 {
128  mRotate = degree;
129  if (mRotate > 360.f)
130  {
131  mRotate = mRotate - 360.f;
132  }
133  else if (mRotate < 0.f)
134  {
135  mRotate = mRotate + 360.f;
136  }
137 
138  updateViewTransforms();
139 
140  Q_EMIT viewChanged();
141 }
142 
143 void ViewManager::scaleUp()
144 {
145  scale(mScale * 1.18f);
146 }
147 
148 void ViewManager::scaleDown()
149 {
150  scale(mScale * 0.8333f);
151 }
152 
153 void ViewManager::scale(float scaleValue)
154 {
155  if (scaleValue < mMinScale)
156  {
157  scaleValue = mMinScale;
158  }
159  else if (scaleValue > mMaxScale)
160  {
161  scaleValue = mMaxScale;
162  }
163  else if (scaleValue == mMinScale || scaleValue == mMaxScale)
164  {
165  return;
166  }
167  mScale = scaleValue;
168  updateViewTransforms();
169 
170  Q_EMIT viewChanged();
171 }
172 
173 void ViewManager::flipHorizontal( bool b )
174 {
175  if ( b != mIsFlipHorizontal )
176  {
177  mIsFlipHorizontal = b;
178  updateViewTransforms();
179 
180  Q_EMIT viewChanged();
181  }
182 }
183 
184 void ViewManager::flipVertical( bool b )
185 {
186  if ( b != mIsFlipVertical )
187  {
188  mIsFlipVertical = b;
189  updateViewTransforms();
190 
191  Q_EMIT viewChanged();
192  }
193 }
194 
195 void ViewManager::setCanvasSize( QSize size )
196 {
197  mCanvasSize = size;
198  mCentre = QTransform::fromTranslate(mCanvasSize.width() / 2.f, mCanvasSize.height() / 2.f);
199 
200  updateViewTransforms();
201  Q_EMIT viewChanged();
202 }
203 
204 void ViewManager::resetView()
205 {
206  mScale = 1.f;
207  mRotate = 0.f;
208  translate(0.f, 0.f); // this fucntion will emit ViewChanged signal, no need to emit again.
209 }
Definition: object.h:71