Debian Codegraph @

examples/widgets/painting/transformations/window.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50
51 #include "window.h"
52
53 #include <QComboBox>
54 #include <QGridLayout>
55
56 //! [0]
57 Window::Window()
58 {
59 originalRenderArea = new RenderArea;
60
61 shapeComboBox = new QComboBox;
62 shapeComboBox->addItem(tr("Clock"));
63 shapeComboBox->addItem(tr("House"));
64 shapeComboBox->addItem(tr("Text"));
65 shapeComboBox->addItem(tr("Truck"));
66
67 QGridLayout *layout = new QGridLayout;
68 layout->addWidget(originalRenderArea, 0, 0);
69 layout->addWidget(shapeComboBox, 1, 0);
70 //! [0]
71
72 //! [1]
73 for (int i = 0; i < NumTransformedAreas; ++i) {
74 transformedRenderAreas[i] = new RenderArea;
75
76 operationComboBoxes[i] = new QComboBox;
77 operationComboBoxes[i]->addItem(tr("No transformation"));
78 operationComboBoxes[i]->addItem(tr("Rotate by 60\xC2\xB0"));
79 operationComboBoxes[i]->addItem(tr("Scale to 75%"));
80 operationComboBoxes[i]->addItem(tr("Translate by (50, 50)"));
81
82 connect(operationComboBoxes[i], QOverload<int>::of(&QComboBox::activated),
83 this, &Window::operationChanged);
84
85 layout->addWidget(transformedRenderAreas[i], 0, i + 1);
86 layout->addWidget(operationComboBoxes[i], 1, i + 1);
87 }
88 //! [1]
89
90 //! [2]
91 setLayout(layout);
92 setupShapes();
93 shapeSelected(0);
94
95 setWindowTitle(tr("Transformations"));
96 }
97 //! [2]
98
99 //! [3]
100 void Window::setupShapes()
101 {
102 QPainterPath truck;
103 //! [3]
104 truck.setFillRule(Qt::WindingFill);
105 truck.moveTo(0.0, 87.0);
106 truck.lineTo(0.0, 60.0);
107 truck.lineTo(10.0, 60.0);
108 truck.lineTo(35.0, 35.0);
109 truck.lineTo(100.0, 35.0);
110 truck.lineTo(100.0, 87.0);
111 truck.lineTo(0.0, 87.0);
112 truck.moveTo(17.0, 60.0);
113 truck.lineTo(55.0, 60.0);
114 truck.lineTo(55.0, 40.0);
115 truck.lineTo(37.0, 40.0);
116 truck.lineTo(17.0, 60.0);
117 truck.addEllipse(17.0, 75.0, 25.0, 25.0);
118 truck.addEllipse(63.0, 75.0, 25.0, 25.0);
119
120 //! [4]
121 QPainterPath clock;
122 //! [4]
123 clock.addEllipse(-50.0, -50.0, 100.0, 100.0);
124 clock.addEllipse(-48.0, -48.0, 96.0, 96.0);
125 clock.moveTo(0.0, 0.0);
126 clock.lineTo(-2.0, -2.0);
127 clock.lineTo(0.0, -42.0);
128 clock.lineTo(2.0, -2.0);
129 clock.lineTo(0.0, 0.0);
130 clock.moveTo(0.0, 0.0);
131 clock.lineTo(2.732, -0.732);
132 clock.lineTo(24.495, 14.142);
133 clock.lineTo(0.732, 2.732);
134 clock.lineTo(0.0, 0.0);
135
136 //! [5]
137 QPainterPath house;
138 //! [5]
139 house.moveTo(-45.0, -20.0);
140 house.lineTo(0.0, -45.0);
141 house.lineTo(45.0, -20.0);
142 house.lineTo(45.0, 45.0);
143 house.lineTo(-45.0, 45.0);
144 house.lineTo(-45.0, -20.0);
145 house.addRect(15.0, 5.0, 20.0, 35.0);
146 house.addRect(-35.0, -15.0, 25.0, 25.0);
147
148 //! [6]
149 QPainterPath text;
150 //! [6]
151 QFont font;
152 font.setPixelSize(50);
153 QRect fontBoundingRect = QFontMetrics(font).boundingRect(tr("Qt"));
154 text.addText(-QPointF(fontBoundingRect.center()), font, tr("Qt"));
155
156 //! [7]
157 shapes.append(clock);
158 shapes.append(house);
159 shapes.append(text);
160 shapes.append(truck);
161
162 connect(shapeComboBox, QOverload<int>::of(&QComboBox::activated),
163 this, &Window::shapeSelected);
164 }
165 //! [7]
166
167 //! [8]
168 void Window::operationChanged()
169 {
170 static const Operation operationTable[] = {
171 NoTransformation, Rotate, Scale, Translate
172 };
173
174 QList<Operation> operations;
175 for (int i = 0; i < NumTransformedAreas; ++i) {
176 int index = operationComboBoxes[i]->currentIndex();
177 operations.append(operationTable[index]);
178 transformedRenderAreas[i]->setOperations(operations);
179 }
180 }
181 //! [8]
182
183 //! [9]
184 void Window::shapeSelected(int index)
185 {
186 QPainterPath shape = shapes[index];
187 originalRenderArea->setShape(shape);
188 for (int i = 0; i < NumTransformedAreas; ++i)
189 transformedRenderAreas[i]->setShape(shape);
190 }
191 //! [9]