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 documentation 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 <QtGui>
|
---|
43 | #include <cmath>
|
---|
44 |
|
---|
45 | class SimpleTransformation : public QWidget
|
---|
46 | {
|
---|
47 | void paintEvent(QPaintEvent *);
|
---|
48 | };
|
---|
49 |
|
---|
50 | //! [0]
|
---|
51 | void SimpleTransformation::paintEvent(QPaintEvent *)
|
---|
52 | {
|
---|
53 | QPainter painter(this);
|
---|
54 | painter.setPen(QPen(Qt::blue, 1, Qt::DashLine));
|
---|
55 | painter.drawRect(0, 0, 100, 100);
|
---|
56 |
|
---|
57 | painter.rotate(45);
|
---|
58 |
|
---|
59 | painter.setFont(QFont("Helvetica", 24));
|
---|
60 | painter.setPen(QPen(Qt::black, 1));
|
---|
61 | painter.drawText(20, 10, "QTransform");
|
---|
62 | }
|
---|
63 | //! [0]
|
---|
64 |
|
---|
65 | class CombinedTransformation : public QWidget
|
---|
66 | {
|
---|
67 | void paintEvent(QPaintEvent *);
|
---|
68 | };
|
---|
69 |
|
---|
70 | //! [1]
|
---|
71 | void CombinedTransformation::paintEvent(QPaintEvent *)
|
---|
72 | {
|
---|
73 | QPainter painter(this);
|
---|
74 | painter.setPen(QPen(Qt::blue, 1, Qt::DashLine));
|
---|
75 | painter.drawRect(0, 0, 100, 100);
|
---|
76 |
|
---|
77 | QTransform transform;
|
---|
78 | transform.translate(50, 50);
|
---|
79 | transform.rotate(45);
|
---|
80 | transform.scale(0.5, 1.0);
|
---|
81 | painter.setTransform(transform);
|
---|
82 |
|
---|
83 | painter.setFont(QFont("Helvetica", 24));
|
---|
84 | painter.setPen(QPen(Qt::black, 1));
|
---|
85 | painter.drawText(20, 10, "QTransform");
|
---|
86 | }
|
---|
87 | //! [1]
|
---|
88 |
|
---|
89 | class BasicOperations : public QWidget
|
---|
90 | {
|
---|
91 | void paintEvent(QPaintEvent *);
|
---|
92 | };
|
---|
93 |
|
---|
94 | //! [2]
|
---|
95 | void BasicOperations::paintEvent(QPaintEvent *)
|
---|
96 | {
|
---|
97 | double pi = 3.14;
|
---|
98 |
|
---|
99 | double a = pi/180 * 45.0;
|
---|
100 | double sina = sin(a);
|
---|
101 | double cosa = cos(a);
|
---|
102 |
|
---|
103 | QTransform translationTransform(1, 0, 0, 1, 50.0, 50.0);
|
---|
104 | QTransform rotationTransform(cosa, sina, -sina, cosa, 0, 0);
|
---|
105 | QTransform scalingTransform(0.5, 0, 0, 1.0, 0, 0);
|
---|
106 |
|
---|
107 | QTransform transform;
|
---|
108 | transform = scalingTransform * rotationTransform * translationTransform;
|
---|
109 |
|
---|
110 | QPainter painter(this);
|
---|
111 | painter.setPen(QPen(Qt::blue, 1, Qt::DashLine));
|
---|
112 | painter.drawRect(0, 0, 100, 100);
|
---|
113 |
|
---|
114 | painter.setTransform(transform);
|
---|
115 |
|
---|
116 | painter.setFont(QFont("Helvetica", 24));
|
---|
117 | painter.setPen(QPen(Qt::black, 1));
|
---|
118 | painter.drawText(20, 10, "QTransform");
|
---|
119 | }
|
---|
120 | //! [2]
|
---|
121 |
|
---|
122 | int main(int argc, char **argv)
|
---|
123 | {
|
---|
124 | QApplication app(argc, argv);
|
---|
125 |
|
---|
126 | QWidget widget;
|
---|
127 |
|
---|
128 | SimpleTransformation *simpleWidget = new SimpleTransformation;
|
---|
129 | CombinedTransformation *combinedWidget = new CombinedTransformation;
|
---|
130 | BasicOperations *basicWidget = new BasicOperations;
|
---|
131 |
|
---|
132 | QVBoxLayout *layout = new QVBoxLayout;
|
---|
133 | layout->addWidget(simpleWidget);
|
---|
134 | layout->addWidget(combinedWidget);
|
---|
135 | layout->addWidget(basicWidget);
|
---|
136 | widget.setLayout(layout);
|
---|
137 |
|
---|
138 | widget.show();
|
---|
139 | widget.resize(130, 350);
|
---|
140 | return app.exec();
|
---|
141 | }
|
---|