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