1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 | #include <cmath>
|
---|
43 |
|
---|
44 | class SimpleTransformation : public QWidget
|
---|
45 | {
|
---|
46 | void paintEvent(QPaintEvent *);
|
---|
47 | };
|
---|
48 |
|
---|
49 | //! [0]
|
---|
50 | void SimpleTransformation::paintEvent(QPaintEvent *)
|
---|
51 | {
|
---|
52 | QPainter painter(this);
|
---|
53 | painter.setPen(QPen(Qt::blue, 1, Qt::DashLine));
|
---|
54 | painter.drawRect(0, 0, 100, 100);
|
---|
55 |
|
---|
56 | painter.rotate(45);
|
---|
57 |
|
---|
58 | painter.setFont(QFont("Helvetica", 24));
|
---|
59 | painter.setPen(QPen(Qt::black, 1));
|
---|
60 | painter.drawText(20, 10, "QMatrix");
|
---|
61 | }
|
---|
62 | //! [0]
|
---|
63 |
|
---|
64 | class CombinedTransformation : public QWidget
|
---|
65 | {
|
---|
66 | void paintEvent(QPaintEvent *);
|
---|
67 | };
|
---|
68 |
|
---|
69 | //! [1]
|
---|
70 | void CombinedTransformation::paintEvent(QPaintEvent *)
|
---|
71 | {
|
---|
72 | QPainter painter(this);
|
---|
73 | painter.setPen(QPen(Qt::blue, 1, Qt::DashLine));
|
---|
74 | painter.drawRect(0, 0, 100, 100);
|
---|
75 |
|
---|
76 | QMatrix matrix;
|
---|
77 | matrix.translate(50, 50);
|
---|
78 | matrix.rotate(45);
|
---|
79 | matrix.scale(0.5, 1.0);
|
---|
80 | painter.setMatrix(matrix);
|
---|
81 |
|
---|
82 | painter.setFont(QFont("Helvetica", 24));
|
---|
83 | painter.setPen(QPen(Qt::black, 1));
|
---|
84 | painter.drawText(20, 10, "QMatrix");
|
---|
85 | }
|
---|
86 | //! [1]
|
---|
87 |
|
---|
88 | class BasicOperations : public QWidget
|
---|
89 | {
|
---|
90 | void paintEvent(QPaintEvent *);
|
---|
91 | };
|
---|
92 |
|
---|
93 | //! [2]
|
---|
94 | void BasicOperations::paintEvent(QPaintEvent *)
|
---|
95 | {
|
---|
96 | double pi = 3.14;
|
---|
97 |
|
---|
98 | double a = pi/180 * 45.0;
|
---|
99 | double sina = sin(a);
|
---|
100 | double cosa = cos(a);
|
---|
101 |
|
---|
102 | QMatrix translationMatrix(1, 0, 0, 1, 50.0, 50.0);
|
---|
103 | QMatrix rotationMatrix(cosa, sina, -sina, cosa, 0, 0);
|
---|
104 | QMatrix scalingMatrix(0.5, 0, 0, 1.0, 0, 0);
|
---|
105 |
|
---|
106 | QMatrix matrix;
|
---|
107 | matrix = scalingMatrix * rotationMatrix * translationMatrix;
|
---|
108 |
|
---|
109 | QPainter painter(this);
|
---|
110 | painter.setPen(QPen(Qt::blue, 1, Qt::DashLine));
|
---|
111 | painter.drawRect(0, 0, 100, 100);
|
---|
112 |
|
---|
113 | painter.setMatrix(matrix);
|
---|
114 |
|
---|
115 | painter.setFont(QFont("Helvetica", 24));
|
---|
116 | painter.setPen(QPen(Qt::black, 1));
|
---|
117 | painter.drawText(20, 10, "QMatrix");
|
---|
118 | }
|
---|
119 | //! [2]
|
---|
120 |
|
---|
121 | int main(int argc, char **argv)
|
---|
122 | {
|
---|
123 | QApplication app(argc, argv);
|
---|
124 |
|
---|
125 | QWidget widget;
|
---|
126 |
|
---|
127 | SimpleTransformation *simpleWidget = new SimpleTransformation;
|
---|
128 | CombinedTransformation *combinedWidget = new CombinedTransformation;
|
---|
129 | BasicOperations *basicWidget = new BasicOperations;
|
---|
130 |
|
---|
131 | QVBoxLayout *layout = new QVBoxLayout;
|
---|
132 | layout->addWidget(simpleWidget);
|
---|
133 | layout->addWidget(combinedWidget);
|
---|
134 | layout->addWidget(basicWidget);
|
---|
135 | widget.setLayout(layout);
|
---|
136 |
|
---|
137 | widget.show();
|
---|
138 | widget.resize(130, 350);
|
---|
139 | return app.exec();
|
---|
140 | }
|
---|