1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 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 examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <QtGui>
|
---|
43 |
|
---|
44 | #include "window.h"
|
---|
45 |
|
---|
46 | //! [0]
|
---|
47 | Window::Window()
|
---|
48 | {
|
---|
49 | originalRenderArea = new RenderArea;
|
---|
50 |
|
---|
51 | shapeComboBox = new QComboBox;
|
---|
52 | shapeComboBox->addItem(tr("Clock"));
|
---|
53 | shapeComboBox->addItem(tr("House"));
|
---|
54 | shapeComboBox->addItem(tr("Text"));
|
---|
55 | shapeComboBox->addItem(tr("Truck"));
|
---|
56 |
|
---|
57 | QGridLayout *layout = new QGridLayout;
|
---|
58 | layout->addWidget(originalRenderArea, 0, 0);
|
---|
59 | layout->addWidget(shapeComboBox, 1, 0);
|
---|
60 | //! [0]
|
---|
61 |
|
---|
62 | //! [1]
|
---|
63 | for (int i = 0; i < NumTransformedAreas; ++i) {
|
---|
64 | transformedRenderAreas[i] = new RenderArea;
|
---|
65 |
|
---|
66 | operationComboBoxes[i] = new QComboBox;
|
---|
67 | operationComboBoxes[i]->addItem(tr("No transformation"));
|
---|
68 | operationComboBoxes[i]->addItem(tr("Rotate by 60\xB0"));
|
---|
69 | operationComboBoxes[i]->addItem(tr("Scale to 75%"));
|
---|
70 | operationComboBoxes[i]->addItem(tr("Translate by (50, 50)"));
|
---|
71 |
|
---|
72 | connect(operationComboBoxes[i], SIGNAL(activated(int)),
|
---|
73 | this, SLOT(operationChanged()));
|
---|
74 |
|
---|
75 | layout->addWidget(transformedRenderAreas[i], 0, i + 1);
|
---|
76 | layout->addWidget(operationComboBoxes[i], 1, i + 1);
|
---|
77 | }
|
---|
78 | //! [1]
|
---|
79 |
|
---|
80 | //! [2]
|
---|
81 | setLayout(layout);
|
---|
82 | setupShapes();
|
---|
83 | shapeSelected(0);
|
---|
84 |
|
---|
85 | setWindowTitle(tr("Transformations"));
|
---|
86 | }
|
---|
87 | //! [2]
|
---|
88 |
|
---|
89 | //! [3]
|
---|
90 | void Window::setupShapes()
|
---|
91 | {
|
---|
92 | QPainterPath truck;
|
---|
93 | //! [3]
|
---|
94 | truck.setFillRule(Qt::WindingFill);
|
---|
95 | truck.moveTo(0.0, 87.0);
|
---|
96 | truck.lineTo(0.0, 60.0);
|
---|
97 | truck.lineTo(10.0, 60.0);
|
---|
98 | truck.lineTo(35.0, 35.0);
|
---|
99 | truck.lineTo(100.0, 35.0);
|
---|
100 | truck.lineTo(100.0, 87.0);
|
---|
101 | truck.lineTo(0.0, 87.0);
|
---|
102 | truck.moveTo(17.0, 60.0);
|
---|
103 | truck.lineTo(55.0, 60.0);
|
---|
104 | truck.lineTo(55.0, 40.0);
|
---|
105 | truck.lineTo(37.0, 40.0);
|
---|
106 | truck.lineTo(17.0, 60.0);
|
---|
107 | truck.addEllipse(17.0, 75.0, 25.0, 25.0);
|
---|
108 | truck.addEllipse(63.0, 75.0, 25.0, 25.0);
|
---|
109 |
|
---|
110 | //! [4]
|
---|
111 | QPainterPath clock;
|
---|
112 | //! [4]
|
---|
113 | clock.addEllipse(-50.0, -50.0, 100.0, 100.0);
|
---|
114 | clock.addEllipse(-48.0, -48.0, 96.0, 96.0);
|
---|
115 | clock.moveTo(0.0, 0.0);
|
---|
116 | clock.lineTo(-2.0, -2.0);
|
---|
117 | clock.lineTo(0.0, -42.0);
|
---|
118 | clock.lineTo(2.0, -2.0);
|
---|
119 | clock.lineTo(0.0, 0.0);
|
---|
120 | clock.moveTo(0.0, 0.0);
|
---|
121 | clock.lineTo(2.732, -0.732);
|
---|
122 | clock.lineTo(24.495, 14.142);
|
---|
123 | clock.lineTo(0.732, 2.732);
|
---|
124 | clock.lineTo(0.0, 0.0);
|
---|
125 |
|
---|
126 | //! [5]
|
---|
127 | QPainterPath house;
|
---|
128 | //! [5]
|
---|
129 | house.moveTo(-45.0, -20.0);
|
---|
130 | house.lineTo(0.0, -45.0);
|
---|
131 | house.lineTo(45.0, -20.0);
|
---|
132 | house.lineTo(45.0, 45.0);
|
---|
133 | house.lineTo(-45.0, 45.0);
|
---|
134 | house.lineTo(-45.0, -20.0);
|
---|
135 | house.addRect(15.0, 5.0, 20.0, 35.0);
|
---|
136 | house.addRect(-35.0, -15.0, 25.0, 25.0);
|
---|
137 |
|
---|
138 | //! [6]
|
---|
139 | QPainterPath text;
|
---|
140 | //! [6]
|
---|
141 | QFont font;
|
---|
142 | font.setPixelSize(50);
|
---|
143 | QRect fontBoundingRect = QFontMetrics(font).boundingRect(tr("Qt"));
|
---|
144 | text.addText(-QPointF(fontBoundingRect.center()), font, tr("Qt"));
|
---|
145 |
|
---|
146 | //! [7]
|
---|
147 | shapes.append(clock);
|
---|
148 | shapes.append(house);
|
---|
149 | shapes.append(text);
|
---|
150 | shapes.append(truck);
|
---|
151 |
|
---|
152 | connect(shapeComboBox, SIGNAL(activated(int)),
|
---|
153 | this, SLOT(shapeSelected(int)));
|
---|
154 | }
|
---|
155 | //! [7]
|
---|
156 |
|
---|
157 | //! [8]
|
---|
158 | void Window::operationChanged()
|
---|
159 | {
|
---|
160 | static const Operation operationTable[] = {
|
---|
161 | NoTransformation, Rotate, Scale, Translate
|
---|
162 | };
|
---|
163 |
|
---|
164 | QList<Operation> operations;
|
---|
165 | for (int i = 0; i < NumTransformedAreas; ++i) {
|
---|
166 | int index = operationComboBoxes[i]->currentIndex();
|
---|
167 | operations.append(operationTable[index]);
|
---|
168 | transformedRenderAreas[i]->setOperations(operations);
|
---|
169 | }
|
---|
170 | }
|
---|
171 | //! [8]
|
---|
172 |
|
---|
173 | //! [9]
|
---|
174 | void Window::shapeSelected(int index)
|
---|
175 | {
|
---|
176 | QPainterPath shape = shapes[index];
|
---|
177 | originalRenderArea->setShape(shape);
|
---|
178 | for (int i = 0; i < NumTransformedAreas; ++i)
|
---|
179 | transformedRenderAreas[i]->setShape(shape);
|
---|
180 | }
|
---|
181 | //! [9]
|
---|