Debian Codegraph @

examples/widgets/painting/painterpaths/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 "renderarea.h"
5 #include "window.h"
6
7 #include <QtWidgets>
8
9 #include <qmath.h>
10
11 //! [1]
12 Window::Window()
13 {
14 QPainterPath rectPath;
15 rectPath.moveTo(20.0, 30.0);
16 rectPath.lineTo(80.0, 30.0);
17 rectPath.lineTo(80.0, 70.0);
18 rectPath.lineTo(20.0, 70.0);
19 rectPath.closeSubpath();
20 //! [1]
21
22 //! [2]
23 QPainterPath roundRectPath;
24 roundRectPath.moveTo(80.0, 35.0);
25 roundRectPath.arcTo(70.0, 30.0, 10.0, 10.0, 0.0, 90.0);
26 roundRectPath.lineTo(25.0, 30.0);
27 roundRectPath.arcTo(20.0, 30.0, 10.0, 10.0, 90.0, 90.0);
28 roundRectPath.lineTo(20.0, 65.0);
29 roundRectPath.arcTo(20.0, 60.0, 10.0, 10.0, 180.0, 90.0);
30 roundRectPath.lineTo(75.0, 70.0);
31 roundRectPath.arcTo(70.0, 60.0, 10.0, 10.0, 270.0, 90.0);
32 roundRectPath.closeSubpath();
33 //! [2]
34
35 //! [3]
36 QPainterPath ellipsePath;
37 ellipsePath.moveTo(80.0, 50.0);
38 ellipsePath.arcTo(20.0, 30.0, 60.0, 40.0, 0.0, 360.0);
39 //! [3]
40
41 //! [4]
42 QPainterPath piePath;
43 piePath.moveTo(50.0, 50.0);
44 piePath.arcTo(20.0, 30.0, 60.0, 40.0, 60.0, 240.0);
45 piePath.closeSubpath();
46 //! [4]
47
48 //! [5]
49 QPainterPath polygonPath;
50 polygonPath.moveTo(10.0, 80.0);
51 polygonPath.lineTo(20.0, 10.0);
52 polygonPath.lineTo(80.0, 30.0);
53 polygonPath.lineTo(90.0, 70.0);
54 polygonPath.closeSubpath();
55 //! [5]
56
57 //! [6]
58 QPainterPath groupPath;
59 groupPath.moveTo(60.0, 40.0);
60 groupPath.arcTo(20.0, 20.0, 40.0, 40.0, 0.0, 360.0);
61 groupPath.moveTo(40.0, 40.0);
62 groupPath.lineTo(40.0, 80.0);
63 groupPath.lineTo(80.0, 80.0);
64 groupPath.lineTo(80.0, 40.0);
65 groupPath.closeSubpath();
66 //! [6]
67
68 //! [7]
69 QPainterPath textPath;
70 QFont timesFont("Times", 50);
71 timesFont.setStyleStrategy(QFont::ForceOutline);
72 textPath.addText(10, 70, timesFont, tr("Qt"));
73 //! [7]
74
75 //! [8]
76 QPainterPath bezierPath;
77 bezierPath.moveTo(20, 30);
78 bezierPath.cubicTo(80, 0, 50, 50, 80, 80);
79 //! [8]
80
81 //! [9]
82 QPainterPath starPath;
83 starPath.moveTo(90, 50);
84 for (int i = 1; i < 5; ++i) {
85 starPath.lineTo(50 + 40 * std::cos(0.8 * i * M_PI),
86 50 + 40 * std::sin(0.8 * i * M_PI));
87 }
88 starPath.closeSubpath();
89 //! [9]
90
91 //! [10]
92 renderAreas.push_back(new RenderArea(rectPath));
93 renderAreas.push_back(new RenderArea(roundRectPath));
94 renderAreas.push_back(new RenderArea(ellipsePath));
95 renderAreas.push_back(new RenderArea(piePath));
96 renderAreas.push_back(new RenderArea(polygonPath));
97 renderAreas.push_back(new RenderArea(groupPath));
98 renderAreas.push_back(new RenderArea(textPath));
99 renderAreas.push_back(new RenderArea(bezierPath));
100 renderAreas.push_back(new RenderArea(starPath));
101 //! [10]
102
103 //! [11]
104 fillRuleComboBox = new QComboBox;
105 fillRuleComboBox->addItem(tr("Odd Even"), Qt::OddEvenFill);
106 fillRuleComboBox->addItem(tr("Winding"), Qt::WindingFill);
107
108 fillRuleLabel = new QLabel(tr("Fill &Rule:"));
109 fillRuleLabel->setBuddy(fillRuleComboBox);
110 //! [11]
111
112 //! [12]
113 fillColor1ComboBox = new QComboBox;
114 populateWithColors(fillColor1ComboBox);
115 fillColor1ComboBox->setCurrentIndex(fillColor1ComboBox->findText("mediumslateblue"));
116
117 fillColor2ComboBox = new QComboBox;
118 populateWithColors(fillColor2ComboBox);
119 fillColor2ComboBox->setCurrentIndex(fillColor2ComboBox->findText("cornsilk"));
120
121 fillGradientLabel = new QLabel(tr("&Fill Gradient:"));
122 fillGradientLabel->setBuddy(fillColor1ComboBox);
123
124 fillToLabel = new QLabel(tr("to"));
125 fillToLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
126
127 penWidthSpinBox = new QSpinBox;
128 penWidthSpinBox->setRange(0, 20);
129
130 penWidthLabel = new QLabel(tr("&Pen Width:"));
131 penWidthLabel->setBuddy(penWidthSpinBox);
132
133 penColorComboBox = new QComboBox;
134 populateWithColors(penColorComboBox);
135 penColorComboBox->setCurrentIndex(penColorComboBox->findText("darkslateblue"));
136
137 penColorLabel = new QLabel(tr("Pen &Color:"));
138 penColorLabel->setBuddy(penColorComboBox);
139
140 rotationAngleSpinBox = new QSpinBox;
141 rotationAngleSpinBox->setRange(0, 359);
142 rotationAngleSpinBox->setWrapping(true);
143 rotationAngleSpinBox->setSuffix(QLatin1String("\xB0"));
144
145 rotationAngleLabel = new QLabel(tr("&Rotation Angle:"));
146 rotationAngleLabel->setBuddy(rotationAngleSpinBox);
147 //! [12]
148
149 //! [16]
150 connect(fillRuleComboBox, &QComboBox::activated,
151 this, &Window::fillRuleChanged);
152 connect(fillColor1ComboBox, &QComboBox::activated,
153 this, &Window::fillGradientChanged);
154 connect(fillColor2ComboBox, &QComboBox::activated,
155 this, &Window::fillGradientChanged);
156 connect(penColorComboBox, &QComboBox::activated,
157 this, &Window::penColorChanged);
158
159 for (RenderArea *area : std::as_const(renderAreas)) {
160 connect(penWidthSpinBox, &QSpinBox::valueChanged,
161 area, &RenderArea::setPenWidth);
162 connect(rotationAngleSpinBox, &QSpinBox::valueChanged,
163 area, &RenderArea::setRotationAngle);
164 }
165
166 //! [16] //! [17]
167 QGridLayout *topLayout = new QGridLayout;
168
169 int i = 0;
170 for (RenderArea *area : std::as_const(renderAreas)) {
171 topLayout->addWidget(area, i / 3, i % 3);
172 ++i;
173 }
174
175 QGridLayout *mainLayout = new QGridLayout;
176 mainLayout->addLayout(topLayout, 0, 0, 1, 4);
177 mainLayout->addWidget(fillRuleLabel, 1, 0);
178 mainLayout->addWidget(fillRuleComboBox, 1, 1, 1, 3);
179 mainLayout->addWidget(fillGradientLabel, 2, 0);
180 mainLayout->addWidget(fillColor1ComboBox, 2, 1);
181 mainLayout->addWidget(fillToLabel, 2, 2);
182 mainLayout->addWidget(fillColor2ComboBox, 2, 3);
183 mainLayout->addWidget(penWidthLabel, 3, 0);
184 mainLayout->addWidget(penWidthSpinBox, 3, 1, 1, 3);
185 mainLayout->addWidget(penColorLabel, 4, 0);
186 mainLayout->addWidget(penColorComboBox, 4, 1, 1, 3);
187 mainLayout->addWidget(rotationAngleLabel, 5, 0);
188 mainLayout->addWidget(rotationAngleSpinBox, 5, 1, 1, 3);
189 setLayout(mainLayout);
190 //! [17]
191
192 //! [18]
193 fillRuleChanged();
194 fillGradientChanged();
195 penColorChanged();
196 penWidthSpinBox->setValue(2);
197
198 setWindowTitle(tr("Painter Paths"));
199 }
200 //! [18]
201
202 //! [19]
203 void Window::fillRuleChanged()
204 {
205 Qt::FillRule rule = (Qt::FillRule)currentItemData(fillRuleComboBox).toInt();
206
207 for (RenderArea *area : std::as_const(renderAreas))
208 area->setFillRule(rule);
209 }
210 //! [19]
211
212 //! [20]
213 void Window::fillGradientChanged()
214 {
215 QColor color1 = qvariant_cast<QColor>(currentItemData(fillColor1ComboBox));
216 QColor color2 = qvariant_cast<QColor>(currentItemData(fillColor2ComboBox));
217
218 for (RenderArea *area : std::as_const(renderAreas))
219 area->setFillGradient(color1, color2);
220 }
221 //! [20]
222
223 //! [21]
224 void Window::penColorChanged()
225 {
226 QColor color = qvariant_cast<QColor>(currentItemData(penColorComboBox));
227
228 for (RenderArea *area : std::as_const(renderAreas))
229 area->setPenColor(color);
230 }
231 //! [21]
232
233 //! [22]
234 void Window::populateWithColors(QComboBox *comboBox)
235 {
236 const QStringList colorNames = QColor::colorNames();
237 for (const QString &name : colorNames)
238 comboBox->addItem(name, QColor(name));
239 }
240 //! [22]
241
242 //! [23]
243 QVariant Window::currentItemData(QComboBox *comboBox)
244 {
245 return comboBox->itemData(comboBox->currentIndex());
246 }
247 //! [23]