Debian Codegraph @

examples/widgets/painting/transformations/window.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4 #include "window.h"
5
6 #include <QComboBox>
7 #include <QGridLayout>
8
9 //! [0]
10 Window::Window()
11 {
12 originalRenderArea = new RenderArea;
13
14 shapeComboBox = new QComboBox;
15 shapeComboBox->addItem(tr("Clock"));
16 shapeComboBox->addItem(tr("House"));
17 shapeComboBox->addItem(tr("Text"));
18 shapeComboBox->addItem(tr("Truck"));
19
20 QGridLayout *layout = new QGridLayout;
21 layout->addWidget(originalRenderArea, 0, 0);
22 layout->addWidget(shapeComboBox, 1, 0);
23 //! [0]
24
25 //! [1]
26 for (int i = 0; i < NumTransformedAreas; ++i) {
27 transformedRenderAreas[i] = new RenderArea;
28
29 operationComboBoxes[i] = new QComboBox;
30 operationComboBoxes[i]->addItem(tr("No transformation"));
31 operationComboBoxes[i]->addItem(tr("Rotate by 60\xC2\xB0"));
32 operationComboBoxes[i]->addItem(tr("Scale to 75%"));
33 operationComboBoxes[i]->addItem(tr("Translate by (50, 50)"));
34
35 connect(operationComboBoxes[i], &QComboBox::activated,
36 this, &Window::operationChanged);
37
38 layout->addWidget(transformedRenderAreas[i], 0, i + 1);
39 layout->addWidget(operationComboBoxes[i], 1, i + 1);
40 }
41 //! [1]
42
43 //! [2]
44 setLayout(layout);
45 setupShapes();
46 shapeSelected(0);
47
48 setWindowTitle(tr("Transformations"));
49 }
50 //! [2]
51
52 //! [3]
53 void Window::setupShapes()
54 {
55 QPainterPath truck;
56 //! [3]
57 truck.setFillRule(Qt::WindingFill);
58 truck.moveTo(0.0, 87.0);
59 truck.lineTo(0.0, 60.0);
60 truck.lineTo(10.0, 60.0);
61 truck.lineTo(35.0, 35.0);
62 truck.lineTo(100.0, 35.0);
63 truck.lineTo(100.0, 87.0);
64 truck.lineTo(0.0, 87.0);
65 truck.moveTo(17.0, 60.0);
66 truck.lineTo(55.0, 60.0);
67 truck.lineTo(55.0, 40.0);
68 truck.lineTo(37.0, 40.0);
69 truck.lineTo(17.0, 60.0);
70 truck.addEllipse(17.0, 75.0, 25.0, 25.0);
71 truck.addEllipse(63.0, 75.0, 25.0, 25.0);
72
73 //! [4]
74 QPainterPath clock;
75 //! [4]
76 clock.addEllipse(-50.0, -50.0, 100.0, 100.0);
77 clock.addEllipse(-48.0, -48.0, 96.0, 96.0);
78 clock.moveTo(0.0, 0.0);
79 clock.lineTo(-2.0, -2.0);
80 clock.lineTo(0.0, -42.0);
81 clock.lineTo(2.0, -2.0);
82 clock.lineTo(0.0, 0.0);
83 clock.moveTo(0.0, 0.0);
84 clock.lineTo(2.732, -0.732);
85 clock.lineTo(24.495, 14.142);
86 clock.lineTo(0.732, 2.732);
87 clock.lineTo(0.0, 0.0);
88
89 //! [5]
90 QPainterPath house;
91 //! [5]
92 house.moveTo(-45.0, -20.0);
93 house.lineTo(0.0, -45.0);
94 house.lineTo(45.0, -20.0);
95 house.lineTo(45.0, 45.0);
96 house.lineTo(-45.0, 45.0);
97 house.lineTo(-45.0, -20.0);
98 house.addRect(15.0, 5.0, 20.0, 35.0);
99 house.addRect(-35.0, -15.0, 25.0, 25.0);
100
101 //! [6]
102 QPainterPath text;
103 //! [6]
104 QFont font;
105 font.setPixelSize(50);
106 QRect fontBoundingRect = QFontMetrics(font).boundingRect(tr("Qt"));
107 text.addText(-QPointF(fontBoundingRect.center()), font, tr("Qt"));
108
109 //! [7]
110 shapes.append(clock);
111 shapes.append(house);
112 shapes.append(text);
113 shapes.append(truck);
114
115 connect(shapeComboBox, &QComboBox::activated,
116 this, &Window::shapeSelected);
117 }
118 //! [7]
119
120 //! [8]
121 void Window::operationChanged()
122 {
123 static const Operation operationTable[] = {
124 NoTransformation, Rotate, Scale, Translate
125 };
126
127 QList<Operation> operations;
128 for (int i = 0; i < NumTransformedAreas; ++i) {
129 int index = operationComboBoxes[i]->currentIndex();
130 operations.append(operationTable[index]);
131 transformedRenderAreas[i]->setOperations(operations);
132 }
133 }
134 //! [8]
135
136 //! [9]
137 void Window::shapeSelected(int index)
138 {
139 QPainterPath shape = shapes[index];
140 originalRenderArea->setShape(shape);
141 for (int i = 0; i < NumTransformedAreas; ++i)
142 transformedRenderAreas[i]->setShape(shape);
143 }
144 //! [9]