1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the demonstration applications of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "xform.h"
|
---|
43 | #include "hoverpoints.h"
|
---|
44 |
|
---|
45 | #include <QLayout>
|
---|
46 | #include <QPainter>
|
---|
47 | #include <QPainterPath>
|
---|
48 |
|
---|
49 | const int alpha = 155;
|
---|
50 |
|
---|
51 | XFormView::XFormView(QWidget *parent)
|
---|
52 | : ArthurFrame(parent)
|
---|
53 | {
|
---|
54 | setAttribute(Qt::WA_MouseTracking);
|
---|
55 | m_type = VectorType;
|
---|
56 | m_rotation = 0.0;
|
---|
57 | m_scale = 1.0;
|
---|
58 | m_shear = 0.0;
|
---|
59 |
|
---|
60 | m_pixmap = QPixmap(":res/affine/bg1.jpg");
|
---|
61 | pts = new HoverPoints(this, HoverPoints::CircleShape);
|
---|
62 | pts->setConnectionType(HoverPoints::LineConnection);
|
---|
63 | pts->setEditable(false);
|
---|
64 | pts->setPointSize(QSize(15, 15));
|
---|
65 | pts->setShapeBrush(QBrush(QColor(151, 0, 0, alpha)));
|
---|
66 | pts->setShapePen(QPen(QColor(255, 100, 50, alpha)));
|
---|
67 | pts->setConnectionPen(QPen(QColor(151, 0, 0, 50)));
|
---|
68 | pts->setBoundingRect(QRectF(0, 0, 500, 500));
|
---|
69 | ctrlPoints << QPointF(250, 250) << QPointF(350, 250);
|
---|
70 | pts->setPoints(ctrlPoints);
|
---|
71 | connect(pts, SIGNAL(pointsChanged(const QPolygonF&)),
|
---|
72 | this, SLOT(updateCtrlPoints(const QPolygonF &)));
|
---|
73 | setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
---|
74 | }
|
---|
75 |
|
---|
76 | XFormView::XFormType XFormView::type() const
|
---|
77 | {
|
---|
78 | return m_type;
|
---|
79 | }
|
---|
80 |
|
---|
81 | QPixmap XFormView::pixmap() const
|
---|
82 | {
|
---|
83 | return m_pixmap;
|
---|
84 | }
|
---|
85 |
|
---|
86 | QString XFormView::text() const
|
---|
87 | {
|
---|
88 | return m_text;
|
---|
89 | }
|
---|
90 |
|
---|
91 | void XFormView::setText(const QString &t)
|
---|
92 | {
|
---|
93 | m_text = t;
|
---|
94 | update();
|
---|
95 | }
|
---|
96 |
|
---|
97 | void XFormView::setPixmap(const QPixmap &p)
|
---|
98 | {
|
---|
99 | m_pixmap = p;
|
---|
100 | update();
|
---|
101 | }
|
---|
102 |
|
---|
103 | void XFormView::setType(XFormType t)
|
---|
104 | {
|
---|
105 | m_type = t;
|
---|
106 | update();
|
---|
107 | }
|
---|
108 |
|
---|
109 | void XFormView::mousePressEvent(QMouseEvent *)
|
---|
110 | {
|
---|
111 | setDescriptionEnabled(false);
|
---|
112 | }
|
---|
113 |
|
---|
114 | void XFormView::resizeEvent(QResizeEvent *e)
|
---|
115 | {
|
---|
116 | pts->setBoundingRect(rect());
|
---|
117 | ArthurFrame::resizeEvent(e);
|
---|
118 | }
|
---|
119 |
|
---|
120 | void XFormView::paint(QPainter *p)
|
---|
121 | {
|
---|
122 | p->save();
|
---|
123 | p->setRenderHint(QPainter::Antialiasing);
|
---|
124 | p->setRenderHint(QPainter::SmoothPixmapTransform);
|
---|
125 | switch (m_type) {
|
---|
126 | case VectorType:
|
---|
127 | drawVectorType(p);
|
---|
128 | break;
|
---|
129 | case PixmapType:
|
---|
130 | drawPixmapType(p);
|
---|
131 | break;
|
---|
132 | case TextType:
|
---|
133 | drawTextType(p);
|
---|
134 | break;
|
---|
135 | }
|
---|
136 | p->restore();
|
---|
137 | }
|
---|
138 |
|
---|
139 | void XFormView::updateCtrlPoints(const QPolygonF &points)
|
---|
140 | {
|
---|
141 | QPointF trans = points.at(0) - ctrlPoints.at(0);
|
---|
142 |
|
---|
143 | if (qAbs(points.at(0).x() - points.at(1).x()) < 10
|
---|
144 | && qAbs(points.at(0).y() - points.at(1).y()) < 10)
|
---|
145 | pts->setPoints(ctrlPoints);
|
---|
146 | if (!trans.isNull()) {
|
---|
147 | ctrlPoints[0] = points.at(0);
|
---|
148 | ctrlPoints[1] += trans;
|
---|
149 | pts->setPoints(ctrlPoints);
|
---|
150 | }
|
---|
151 | ctrlPoints = points;
|
---|
152 |
|
---|
153 | QLineF line(ctrlPoints.at(0), ctrlPoints.at(1));
|
---|
154 | m_rotation = line.angle(QLineF(0, 0, 1, 0));
|
---|
155 | if (line.dy() < 0)
|
---|
156 | m_rotation = 360 - m_rotation;
|
---|
157 |
|
---|
158 | if (trans.isNull())
|
---|
159 | emit rotationChanged(int(m_rotation*10));
|
---|
160 | }
|
---|
161 |
|
---|
162 | void XFormView::setVectorType()
|
---|
163 | {
|
---|
164 | m_type = VectorType;
|
---|
165 | update();
|
---|
166 | }
|
---|
167 |
|
---|
168 | void XFormView::setPixmapType()
|
---|
169 | {
|
---|
170 | m_type = PixmapType;
|
---|
171 | update();
|
---|
172 | }
|
---|
173 |
|
---|
174 | void XFormView::setTextType()
|
---|
175 | {
|
---|
176 | m_type = TextType;
|
---|
177 | update();
|
---|
178 | }
|
---|
179 |
|
---|
180 | void XFormView::setAnimation(bool animate)
|
---|
181 | {
|
---|
182 | timer.stop();
|
---|
183 | if (animate)
|
---|
184 | timer.start(25, this);
|
---|
185 | }
|
---|
186 |
|
---|
187 | void XFormView::changeRotation(int r)
|
---|
188 | {
|
---|
189 | setRotation(qreal(r) / 10);
|
---|
190 | }
|
---|
191 |
|
---|
192 | void XFormView::changeScale(int s)
|
---|
193 | {
|
---|
194 | setScale(qreal(s) / 1000);
|
---|
195 | }
|
---|
196 |
|
---|
197 | void XFormView::changeShear(int s)
|
---|
198 | {
|
---|
199 | setShear(qreal(s) / 1000);
|
---|
200 | }
|
---|
201 |
|
---|
202 | void XFormView::setShear(qreal s)
|
---|
203 | {
|
---|
204 | m_shear = s;
|
---|
205 | update();
|
---|
206 | }
|
---|
207 |
|
---|
208 | void XFormView::setScale(qreal s)
|
---|
209 | {
|
---|
210 | m_scale = s;
|
---|
211 | update();
|
---|
212 | }
|
---|
213 |
|
---|
214 | void XFormView::setRotation(qreal r)
|
---|
215 | {
|
---|
216 | qreal old_rot = m_rotation;
|
---|
217 | m_rotation = r;
|
---|
218 |
|
---|
219 | QPointF center(pts->points().at(0));
|
---|
220 | QMatrix m;
|
---|
221 | m.translate(center.x(), center.y());
|
---|
222 | m.rotate(m_rotation - old_rot);
|
---|
223 | m.translate(-center.x(), -center.y());
|
---|
224 | pts->setPoints(pts->points() * m);
|
---|
225 |
|
---|
226 | update();
|
---|
227 | }
|
---|
228 |
|
---|
229 | void XFormView::timerEvent(QTimerEvent *e)
|
---|
230 | {
|
---|
231 | if (e->timerId() == timer.timerId()) {
|
---|
232 | QPointF center(pts->points().at(0));
|
---|
233 | QMatrix m;
|
---|
234 | m.translate(center.x(), center.y());
|
---|
235 | m.rotate(0.2);
|
---|
236 | m.translate(-center.x(), -center.y());
|
---|
237 | pts->setPoints(pts->points() * m);
|
---|
238 |
|
---|
239 | setUpdatesEnabled(false);
|
---|
240 | static qreal scale_inc = 0.003;
|
---|
241 | static qreal shear_inc = -0.001;
|
---|
242 | emit scaleChanged(int((m_scale + scale_inc) * 1000));
|
---|
243 | emit shearChanged(int((m_shear + shear_inc) * 1000));
|
---|
244 | if (m_scale >= 4.0 || m_scale <= 0.1)
|
---|
245 | scale_inc = -scale_inc;
|
---|
246 | if (m_shear >= 1.0 || m_shear <= -1.0)
|
---|
247 | shear_inc = -shear_inc;
|
---|
248 | setUpdatesEnabled(true);
|
---|
249 |
|
---|
250 | pts->firePointChange();
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | void XFormView::wheelEvent(QWheelEvent *e)
|
---|
255 | {
|
---|
256 | m_scale += e->delta() / qreal(600);
|
---|
257 | m_scale = qMax(qreal(0.1), qMin(qreal(4), m_scale));
|
---|
258 | emit scaleChanged(int(m_scale*1000));
|
---|
259 | }
|
---|
260 |
|
---|
261 | void XFormView::reset()
|
---|
262 | {
|
---|
263 | emit rotationChanged(0);
|
---|
264 | emit scaleChanged(1000);
|
---|
265 | emit shearChanged(0);
|
---|
266 | ctrlPoints = QPolygonF();
|
---|
267 | ctrlPoints << QPointF(250, 250) << QPointF(350, 250);
|
---|
268 | pts->setPoints(ctrlPoints);
|
---|
269 | pts->firePointChange();
|
---|
270 | }
|
---|
271 |
|
---|
272 | void XFormView::drawPixmapType(QPainter *painter)
|
---|
273 | {
|
---|
274 | QPointF center(m_pixmap.width() / qreal(2), m_pixmap.height() / qreal(2));
|
---|
275 | painter->translate(ctrlPoints.at(0) - center);
|
---|
276 |
|
---|
277 | painter->translate(center);
|
---|
278 | painter->rotate(m_rotation);
|
---|
279 | painter->scale(m_scale, m_scale);
|
---|
280 | painter->shear(0, m_shear);
|
---|
281 | painter->translate(-center);
|
---|
282 |
|
---|
283 | painter->drawPixmap(QPointF(0, 0), m_pixmap);
|
---|
284 | painter->setPen(QPen(QColor(255, 0, 0, alpha), 0.25, Qt::SolidLine, Qt::FlatCap, Qt::BevelJoin));
|
---|
285 | painter->setBrush(Qt::NoBrush);
|
---|
286 | painter->drawRect(QRectF(0, 0, m_pixmap.width(), m_pixmap.height()).adjusted(-2, -2, 2, 2));
|
---|
287 | }
|
---|
288 |
|
---|
289 | void XFormView::drawTextType(QPainter *painter)
|
---|
290 | {
|
---|
291 | QPainterPath path;
|
---|
292 | QFont f("times new roman,utopia");
|
---|
293 | f.setStyleStrategy(QFont::ForceOutline);
|
---|
294 | f.setPointSize(72);
|
---|
295 | f.setStyleHint(QFont::Times);
|
---|
296 | path.addText(0, 0, f, m_text);
|
---|
297 |
|
---|
298 | QFontMetrics fm(f);
|
---|
299 | QRectF br(fm.boundingRect(m_text));
|
---|
300 | QPointF center(br.center());
|
---|
301 | painter->translate(ctrlPoints.at(0) - center);
|
---|
302 |
|
---|
303 | painter->translate(center);
|
---|
304 | painter->rotate(m_rotation);
|
---|
305 | painter->scale(m_scale, m_scale);
|
---|
306 | painter->shear(0, m_shear);
|
---|
307 | painter->translate(-center);
|
---|
308 |
|
---|
309 | painter->fillPath(path, Qt::black);
|
---|
310 |
|
---|
311 | painter->setPen(QPen(QColor(255, 0, 0, alpha), 0.25, Qt::SolidLine, Qt::FlatCap, Qt::BevelJoin));
|
---|
312 | painter->setBrush(Qt::NoBrush);
|
---|
313 | painter->drawRect(br.adjusted(-1, -1, 1, 1));
|
---|
314 | }
|
---|
315 |
|
---|
316 | void XFormView::drawVectorType(QPainter *painter)
|
---|
317 | {
|
---|
318 | QPainterPath path;
|
---|
319 | painter->translate(ctrlPoints.at(0) - QPointF(250,250));
|
---|
320 |
|
---|
321 | painter->scale(0.77, 0.77);
|
---|
|
---|