| 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 |
//! [0] |
| 10 |
const int IdRole = Qt::UserRole; |
| 11 |
//! [0] |
| 12 |
|
| 13 |
//! [1] |
| 14 |
Window::Window() |
| 15 |
{ |
| 16 |
renderArea = new RenderArea; |
| 17 |
|
| 18 |
shapeComboBox = new QComboBox; |
| 19 |
shapeComboBox->addItem(tr("Polygon"), RenderArea::Polygon); |
| 20 |
shapeComboBox->addItem(tr("Rectangle"), RenderArea::Rect); |
| 21 |
shapeComboBox->addItem(tr("Rounded Rectangle"), RenderArea::RoundedRect); |
| 22 |
shapeComboBox->addItem(tr("Ellipse"), RenderArea::Ellipse); |
| 23 |
shapeComboBox->addItem(tr("Pie"), RenderArea::Pie); |
| 24 |
shapeComboBox->addItem(tr("Chord"), RenderArea::Chord); |
| 25 |
shapeComboBox->addItem(tr("Path"), RenderArea::Path); |
| 26 |
shapeComboBox->addItem(tr("Line"), RenderArea::Line); |
| 27 |
shapeComboBox->addItem(tr("Polyline"), RenderArea::Polyline); |
| 28 |
shapeComboBox->addItem(tr("Arc"), RenderArea::Arc); |
| 29 |
shapeComboBox->addItem(tr("Points"), RenderArea::Points); |
| 30 |
shapeComboBox->addItem(tr("Text"), RenderArea::Text); |
| 31 |
shapeComboBox->addItem(tr("Pixmap"), RenderArea::Pixmap); |
| 32 |
|
| 33 |
shapeLabel = new QLabel(tr("&Shape:")); |
| 34 |
shapeLabel->setBuddy(shapeComboBox); |
| 35 |
//! [1] |
| 36 |
|
| 37 |
//! [2] |
| 38 |
penWidthSpinBox = new QSpinBox; |
| 39 |
penWidthSpinBox->setRange(0, 20); |
| 40 |
penWidthSpinBox->setSpecialValueText(tr("0 (cosmetic pen)")); |
| 41 |
|
| 42 |
penWidthLabel = new QLabel(tr("Pen &Width:")); |
| 43 |
penWidthLabel->setBuddy(penWidthSpinBox); |
| 44 |
//! [2] |
| 45 |
|
| 46 |
//! [3] |
| 47 |
penStyleComboBox = new QComboBox; |
| 48 |
penStyleComboBox->addItem(tr("Solid"), static_cast<int>(Qt::SolidLine)); |
| 49 |
penStyleComboBox->addItem(tr("Dash"), static_cast<int>(Qt::DashLine)); |
| 50 |
penStyleComboBox->addItem(tr("Dot"), static_cast<int>(Qt::DotLine)); |
| 51 |
penStyleComboBox->addItem(tr("Dash Dot"), static_cast<int>(Qt::DashDotLine)); |
| 52 |
penStyleComboBox->addItem(tr("Dash Dot Dot"), static_cast<int>(Qt::DashDotDotLine)); |
| 53 |
penStyleComboBox->addItem(tr("None"), static_cast<int>(Qt::NoPen)); |
| 54 |
|
| 55 |
penStyleLabel = new QLabel(tr("&Pen Style:")); |
| 56 |
penStyleLabel->setBuddy(penStyleComboBox); |
| 57 |
|
| 58 |
penCapComboBox = new QComboBox; |
| 59 |
penCapComboBox->addItem(tr("Flat"), Qt::FlatCap); |
| 60 |
penCapComboBox->addItem(tr("Square"), Qt::SquareCap); |
| 61 |
penCapComboBox->addItem(tr("Round"), Qt::RoundCap); |
| 62 |
|
| 63 |
penCapLabel = new QLabel(tr("Pen &Cap:")); |
| 64 |
penCapLabel->setBuddy(penCapComboBox); |
| 65 |
|
| 66 |
penJoinComboBox = new QComboBox; |
| 67 |
penJoinComboBox->addItem(tr("Miter"), Qt::MiterJoin); |
| 68 |
penJoinComboBox->addItem(tr("Bevel"), Qt::BevelJoin); |
| 69 |
penJoinComboBox->addItem(tr("Round"), Qt::RoundJoin); |
| 70 |
|
| 71 |
penJoinLabel = new QLabel(tr("Pen &Join:")); |
| 72 |
penJoinLabel->setBuddy(penJoinComboBox); |
| 73 |
//! [3] |
| 74 |
|
| 75 |
//! [4] |
| 76 |
brushStyleComboBox = new QComboBox; |
| 77 |
brushStyleComboBox->addItem(tr("Linear Gradient"), |
| 78 |
static_cast<int>(Qt::LinearGradientPattern)); |
| 79 |
brushStyleComboBox->addItem(tr("Radial Gradient"), |
| 80 |
static_cast<int>(Qt::RadialGradientPattern)); |
| 81 |
brushStyleComboBox->addItem(tr("Conical Gradient"), |
| 82 |
static_cast<int>(Qt::ConicalGradientPattern)); |
| 83 |
brushStyleComboBox->addItem(tr("Texture"), static_cast<int>(Qt::TexturePattern)); |
| 84 |
brushStyleComboBox->addItem(tr("Solid"), static_cast<int>(Qt::SolidPattern)); |
| 85 |
brushStyleComboBox->addItem(tr("Horizontal"), static_cast<int>(Qt::HorPattern)); |
| 86 |
brushStyleComboBox->addItem(tr("Vertical"), static_cast<int>(Qt::VerPattern)); |
| 87 |
brushStyleComboBox->addItem(tr("Cross"), static_cast<int>(Qt::CrossPattern)); |
| 88 |
brushStyleComboBox->addItem(tr("Backward Diagonal"), static_cast<int>(Qt::BDiagPattern)); |
| 89 |
brushStyleComboBox->addItem(tr("Forward Diagonal"), static_cast<int>(Qt::FDiagPattern)); |
| 90 |
brushStyleComboBox->addItem(tr("Diagonal Cross"), static_cast<int>(Qt::DiagCrossPattern)); |
| 91 |
brushStyleComboBox->addItem(tr("Dense 1"), static_cast<int>(Qt::Dense1Pattern)); |
| 92 |
brushStyleComboBox->addItem(tr("Dense 2"), static_cast<int>(Qt::Dense2Pattern)); |
| 93 |
brushStyleComboBox->addItem(tr("Dense 3"), static_cast<int>(Qt::Dense3Pattern)); |
| 94 |
brushStyleComboBox->addItem(tr("Dense 4"), static_cast<int>(Qt::Dense4Pattern)); |
| 95 |
brushStyleComboBox->addItem(tr("Dense 5"), static_cast<int>(Qt::Dense5Pattern)); |
| 96 |
brushStyleComboBox->addItem(tr("Dense 6"), static_cast<int>(Qt::Dense6Pattern)); |
| 97 |
brushStyleComboBox->addItem(tr("Dense 7"), static_cast<int>(Qt::Dense7Pattern)); |
| 98 |
brushStyleComboBox->addItem(tr("None"), static_cast<int>(Qt::NoBrush)); |
| 99 |
|
| 100 |
brushStyleLabel = new QLabel(tr("&Brush:")); |
| 101 |
brushStyleLabel->setBuddy(brushStyleComboBox); |
| 102 |
//! [4] |
| 103 |
|
| 104 |
//! [5] |
| 105 |
otherOptionsLabel = new QLabel(tr("Options:")); |
| 106 |
//! [5] //! [6] |
| 107 |
antialiasingCheckBox = new QCheckBox(tr("&Antialiasing")); |
| 108 |
//! [6] //! [7] |
| 109 |
transformationsCheckBox = new QCheckBox(tr("&Transformations")); |
| 110 |
//! [7] |
| 111 |
|
| 112 |
//! [8] |
| 113 |
connect(shapeComboBox, &QComboBox::activated, |
| 114 |
this, &Window::shapeChanged); |
| 115 |
connect(penWidthSpinBox, &QSpinBox::valueChanged, |
| 116 |
this, &Window::penChanged); |
| 117 |
connect(penStyleComboBox, &QComboBox::activated, |
| 118 |
this, &Window::penChanged); |
| 119 |
connect(penCapComboBox, &QComboBox::activated, |
| 120 |
this, &Window::penChanged); |
| 121 |
connect(penJoinComboBox, &QComboBox::activated, |
| 122 |
this, &Window::penChanged); |
| 123 |
connect(brushStyleComboBox, &QComboBox::activated, |
| 124 |
this, &Window::brushChanged); |
| 125 |
connect(antialiasingCheckBox, &QAbstractButton::toggled, |
| 126 |
renderArea, &RenderArea::setAntialiased); |
| 127 |
connect(transformationsCheckBox, &QAbstractButton::toggled, |
| 128 |
renderArea, &RenderArea::setTransformed); |
| 129 |
//! [8] |
| 130 |
|
| 131 |
//! [9] |
| 132 |
QGridLayout *mainLayout = new QGridLayout; |
| 133 |
//! [9] //! [10] |
| 134 |
mainLayout->setColumnStretch(0, 1); |
| 135 |
mainLayout->setColumnStretch(3, 1); |
| 136 |
mainLayout->addWidget(renderArea, 0, 0, 1, 4); |
| 137 |
mainLayout->addWidget(shapeLabel, 2, 0, Qt::AlignRight); |
| 138 |
mainLayout->addWidget(shapeComboBox, 2, 1); |
| 139 |
mainLayout->addWidget(penWidthLabel, 3, 0, Qt::AlignRight); |
| 140 |
mainLayout->addWidget(penWidthSpinBox, 3, 1); |
| 141 |
mainLayout->addWidget(penStyleLabel, 4, 0, Qt::AlignRight); |
| 142 |
mainLayout->addWidget(penStyleComboBox, 4, 1); |
| 143 |
mainLayout->addWidget(penCapLabel, 3, 2, Qt::AlignRight); |
| 144 |
mainLayout->addWidget(penCapComboBox, 3, 3); |
| 145 |
mainLayout->addWidget(penJoinLabel, 2, 2, Qt::AlignRight); |
| 146 |
mainLayout->addWidget(penJoinComboBox, 2, 3); |
| 147 |
mainLayout->addWidget(brushStyleLabel, 4, 2, Qt::AlignRight); |
| 148 |
mainLayout->addWidget(brushStyleComboBox, 4, 3); |
| 149 |
mainLayout->addWidget(otherOptionsLabel, 5, 0, Qt::AlignRight); |
| 150 |
mainLayout->addWidget(antialiasingCheckBox, 5, 1, 1, 1, Qt::AlignRight); |
| 151 |
mainLayout->addWidget(transformationsCheckBox, 5, 2, 1, 2, Qt::AlignRight); |
| 152 |
setLayout(mainLayout); |
| 153 |
|
| 154 |
shapeChanged(); |
| 155 |
penChanged(); |
| 156 |
brushChanged(); |
| 157 |
antialiasingCheckBox->setChecked(true); |
| 158 |
|
| 159 |
setWindowTitle(tr("Basic Drawing")); |
| 160 |
} |
| 161 |
//! [10] |
| 162 |
|
| 163 |
//! [11] |
| 164 |
void Window::shapeChanged() |
| 165 |
{ |
| 166 |
RenderArea::Shape shape = RenderArea::Shape(shapeComboBox->itemData( |
| 167 |
shapeComboBox->currentIndex(), IdRole).toInt()); |
| 168 |
renderArea->setShape(shape); |
| 169 |
} |
| 170 |
//! [11] |
| 171 |
|
| 172 |
//! [12] |
| 173 |
void Window::penChanged() |
| 174 |
{ |
| 175 |
int width = penWidthSpinBox->value(); |
| 176 |
Qt::PenStyle style = Qt::PenStyle(penStyleComboBox->itemData( |
| 177 |
penStyleComboBox->currentIndex(), IdRole).toInt()); |
| 178 |
Qt::PenCapStyle cap = Qt::PenCapStyle(penCapComboBox->itemData( |
| 179 |
penCapComboBox->currentIndex(), IdRole).toInt()); |
| 180 |
Qt::PenJoinStyle join = Qt::PenJoinStyle(penJoinComboBox->itemData( |
| 181 |
penJoinComboBox->currentIndex(), IdRole).toInt()); |
| 182 |
|
| 183 |
renderArea->setPen(QPen(Qt::blue, width, style, cap, join)); |
| 184 |
} |
| 185 |
//! [12] |
| 186 |
|
| 187 |
//! [13] |
| 188 |
void Window::brushChanged() |
| 189 |
{ |
| 190 |
Qt::BrushStyle style = Qt::BrushStyle(brushStyleComboBox->itemData( |
| 191 |
//! [13] |
| 192 |
brushStyleComboBox->currentIndex(), IdRole).toInt()); |
| 193 |
|
| 194 |
//! [14] |
| 195 |
if (style == Qt::LinearGradientPattern) { |
| 196 |
QLinearGradient linearGradient(0, 0, 100, 100); |
| 197 |
linearGradient.setColorAt(0.0, Qt::white); |
| 198 |
linearGradient.setColorAt(0.2, Qt::green); |
| 199 |
linearGradient.setColorAt(1.0, Qt::black); |
| 200 |
renderArea->setBrush(linearGradient); |
| 201 |
//! [14] //! [15] |
| 202 |
} else if (style == Qt::RadialGradientPattern) { |
| 203 |
QRadialGradient radialGradient(50, 50, 50, 70, 70); |
| 204 |
radialGradient.setColorAt(0.0, Qt::white); |
| 205 |
radialGradient.setColorAt(0.2, Qt::green); |
| 206 |
radialGradient.setColorAt(1.0, Qt::black); |
| 207 |
renderArea->setBrush(radialGradient); |
| 208 |
} else if (style == Qt::ConicalGradientPattern) { |
| 209 |
QConicalGradient conicalGradient(50, 50, 150); |
| 210 |
conicalGradient.setColorAt(0.0, Qt::white); |
| 211 |
conicalGradient.setColorAt(0.2, Qt::green); |
| 212 |
conicalGradient.setColorAt(1.0, Qt::black); |
| 213 |
renderArea->setBrush(conicalGradient); |
| 214 |
//! [15] //! [16] |
| 215 |
} else if (style == Qt::TexturePattern) { |
| 216 |
renderArea->setBrush(QBrush(QPixmap(":/images/brick.png"))); |
| 217 |
//! [16] //! [17] |
| 218 |
} else { |
| 219 |
renderArea->setBrush(QBrush(Qt::green, style)); |
| 220 |
} |
| 221 |
} |
| 222 |
//! [17] |