| 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(atext: tr(s: "Polygon" ), auserData: RenderArea::Polygon); |
| 67 | shapeComboBox->addItem(atext: tr(s: "Rectangle" ), auserData: RenderArea::Rect); |
| 68 | shapeComboBox->addItem(atext: tr(s: "Rounded Rectangle" ), auserData: RenderArea::RoundedRect); |
| 69 | shapeComboBox->addItem(atext: tr(s: "Ellipse" ), auserData: RenderArea::Ellipse); |
| 70 | shapeComboBox->addItem(atext: tr(s: "Pie" ), auserData: RenderArea::Pie); |
| 71 | shapeComboBox->addItem(atext: tr(s: "Chord" ), auserData: RenderArea::Chord); |
| 72 | shapeComboBox->addItem(atext: tr(s: "Path" ), auserData: RenderArea::Path); |
| 73 | shapeComboBox->addItem(atext: tr(s: "Line" ), auserData: RenderArea::Line); |
| 74 | shapeComboBox->addItem(atext: tr(s: "Polyline" ), auserData: RenderArea::Polyline); |
| 75 | shapeComboBox->addItem(atext: tr(s: "Arc" ), auserData: RenderArea::Arc); |
| 76 | shapeComboBox->addItem(atext: tr(s: "Points" ), auserData: RenderArea::Points); |
| 77 | shapeComboBox->addItem(atext: tr(s: "Text" ), auserData: RenderArea::Text); |
| 78 | shapeComboBox->addItem(atext: tr(s: "Pixmap" ), auserData: RenderArea::Pixmap); |
| 79 | |
| 80 | shapeLabel = new QLabel(tr(s: "&Shape:" )); |
| 81 | shapeLabel->setBuddy(shapeComboBox); |
| 82 | //! [1] |
| 83 | |
| 84 | //! [2] |
| 85 | penWidthSpinBox = new QSpinBox; |
| 86 | penWidthSpinBox->setRange(min: 0, max: 20); |
| 87 | penWidthSpinBox->setSpecialValueText(tr(s: "0 (cosmetic pen)" )); |
| 88 | |
| 89 | penWidthLabel = new QLabel(tr(s: "Pen &Width:" )); |
| 90 | penWidthLabel->setBuddy(penWidthSpinBox); |
| 91 | //! [2] |
| 92 | |
| 93 | //! [3] |
| 94 | penStyleComboBox = new QComboBox; |
| 95 | penStyleComboBox->addItem(atext: tr(s: "Solid" ), auserData: static_cast<int>(Qt::SolidLine)); |
| 96 | penStyleComboBox->addItem(atext: tr(s: "Dash" ), auserData: static_cast<int>(Qt::DashLine)); |
| 97 | penStyleComboBox->addItem(atext: tr(s: "Dot" ), auserData: static_cast<int>(Qt::DotLine)); |
| 98 | penStyleComboBox->addItem(atext: tr(s: "Dash Dot" ), auserData: static_cast<int>(Qt::DashDotLine)); |
| 99 | penStyleComboBox->addItem(atext: tr(s: "Dash Dot Dot" ), auserData: static_cast<int>(Qt::DashDotDotLine)); |
| 100 | penStyleComboBox->addItem(atext: tr(s: "None" ), auserData: static_cast<int>(Qt::NoPen)); |
| 101 | |
| 102 | penStyleLabel = new QLabel(tr(s: "&Pen Style:" )); |
| 103 | penStyleLabel->setBuddy(penStyleComboBox); |
| 104 | |
| 105 | penCapComboBox = new QComboBox; |
| 106 | penCapComboBox->addItem(atext: tr(s: "Flat" ), auserData: Qt::FlatCap); |
| 107 | penCapComboBox->addItem(atext: tr(s: "Square" ), auserData: Qt::SquareCap); |
| 108 | penCapComboBox->addItem(atext: tr(s: "Round" ), auserData: Qt::RoundCap); |
| 109 | |
| 110 | penCapLabel = new QLabel(tr(s: "Pen &Cap:" )); |
| 111 | penCapLabel->setBuddy(penCapComboBox); |
| 112 | |
| 113 | penJoinComboBox = new QComboBox; |
| 114 | penJoinComboBox->addItem(atext: tr(s: "Miter" ), auserData: Qt::MiterJoin); |
| 115 | penJoinComboBox->addItem(atext: tr(s: "Bevel" ), auserData: Qt::BevelJoin); |
| 116 | penJoinComboBox->addItem(atext: tr(s: "Round" ), auserData: Qt::RoundJoin); |
| 117 | |
| 118 | penJoinLabel = new QLabel(tr(s: "Pen &Join:" )); |
| 119 | penJoinLabel->setBuddy(penJoinComboBox); |
| 120 | //! [3] |
| 121 | |
| 122 | //! [4] |
| 123 | brushStyleComboBox = new QComboBox; |
| 124 | brushStyleComboBox->addItem(atext: tr(s: "Linear Gradient" ), |
| 125 | auserData: static_cast<int>(Qt::LinearGradientPattern)); |
| 126 | brushStyleComboBox->addItem(atext: tr(s: "Radial Gradient" ), |
| 127 | auserData: static_cast<int>(Qt::RadialGradientPattern)); |
| 128 | brushStyleComboBox->addItem(atext: tr(s: "Conical Gradient" ), |
| 129 | auserData: static_cast<int>(Qt::ConicalGradientPattern)); |
| 130 | brushStyleComboBox->addItem(atext: tr(s: "Texture" ), auserData: static_cast<int>(Qt::TexturePattern)); |
| 131 | brushStyleComboBox->addItem(atext: tr(s: "Solid" ), auserData: static_cast<int>(Qt::SolidPattern)); |
| 132 | brushStyleComboBox->addItem(atext: tr(s: "Horizontal" ), auserData: static_cast<int>(Qt::HorPattern)); |
| 133 | brushStyleComboBox->addItem(atext: tr(s: "Vertical" ), auserData: static_cast<int>(Qt::VerPattern)); |
| 134 | brushStyleComboBox->addItem(atext: tr(s: "Cross" ), auserData: static_cast<int>(Qt::CrossPattern)); |
| 135 | brushStyleComboBox->addItem(atext: tr(s: "Backward Diagonal" ), auserData: static_cast<int>(Qt::BDiagPattern)); |
| 136 | brushStyleComboBox->addItem(atext: tr(s: "Forward Diagonal" ), auserData: static_cast<int>(Qt::FDiagPattern)); |
| 137 | brushStyleComboBox->addItem(atext: tr(s: "Diagonal Cross" ), auserData: static_cast<int>(Qt::DiagCrossPattern)); |
| 138 | brushStyleComboBox->addItem(atext: tr(s: "Dense 1" ), auserData: static_cast<int>(Qt::Dense1Pattern)); |
| 139 | brushStyleComboBox->addItem(atext: tr(s: "Dense 2" ), auserData: static_cast<int>(Qt::Dense2Pattern)); |
| 140 | brushStyleComboBox->addItem(atext: tr(s: "Dense 3" ), auserData: static_cast<int>(Qt::Dense3Pattern)); |
| 141 | brushStyleComboBox->addItem(atext: tr(s: "Dense 4" ), auserData: static_cast<int>(Qt::Dense4Pattern)); |
| 142 | brushStyleComboBox->addItem(atext: tr(s: "Dense 5" ), auserData: static_cast<int>(Qt::Dense5Pattern)); |
| 143 | brushStyleComboBox->addItem(atext: tr(s: "Dense 6" ), auserData: static_cast<int>(Qt::Dense6Pattern)); |
| 144 | brushStyleComboBox->addItem(atext: tr(s: "Dense 7" ), auserData: static_cast<int>(Qt::Dense7Pattern)); |
| 145 | brushStyleComboBox->addItem(atext: tr(s: "None" ), auserData: static_cast<int>(Qt::NoBrush)); |
| 146 | |
| 147 | brushStyleLabel = new QLabel(tr(s: "&Brush:" )); |
| 148 | brushStyleLabel->setBuddy(brushStyleComboBox); |
| 149 | //! [4] |
| 150 | |
| 151 | //! [5] |
| 152 | otherOptionsLabel = new QLabel(tr(s: "Options:" )); |
| 153 | //! [5] //! [6] |
| 154 | antialiasingCheckBox = new QCheckBox(tr(s: "&Antialiasing" )); |
| 155 | //! [6] //! [7] |
| 156 | transformationsCheckBox = new QCheckBox(tr(s: "&Transformations" )); |
| 157 | //! [7] |
| 158 | |
| 159 | //! [8] |
| 160 | connect(sender: shapeComboBox, signal: QOverload<int>::of(ptr: &QComboBox::activated), |
| 161 | receiver: this, slot: &Window::shapeChanged); |
| 162 | connect(sender: penWidthSpinBox, signal: QOverload<int>::of(ptr: &QSpinBox::valueChanged), |
| 163 | receiver: this, slot: &Window::penChanged); |
| 164 | connect(sender: penStyleComboBox, signal: QOverload<int>::of(ptr: &QComboBox::activated), |
| 165 | receiver: this, slot: &Window::penChanged); |
| 166 | connect(sender: penCapComboBox, signal: QOverload<int>::of(ptr: &QComboBox::activated), |
| 167 | receiver: this, slot: &Window::penChanged); |
| 168 | connect(sender: penJoinComboBox, signal: QOverload<int>::of(ptr: &QComboBox::activated), |
| 169 | receiver: this, slot: &Window::penChanged); |
| 170 | connect(sender: brushStyleComboBox, signal: QOverload<int>::of(ptr: &QComboBox::activated), |
| 171 | receiver: this, slot: &Window::brushChanged); |
| 172 | connect(sender: antialiasingCheckBox, signal: &QAbstractButton::toggled, |
| 173 | receiver: renderArea, slot: &RenderArea::setAntialiased); |
| 174 | connect(sender: transformationsCheckBox, signal: &QAbstractButton::toggled, |
| 175 | receiver: renderArea, slot: &RenderArea::setTransformed); |
| 176 | //! [8] |
| 177 | |
| 178 | //! [9] |
| 179 | QGridLayout *mainLayout = new QGridLayout; |
| 180 | //! [9] //! [10] |
| 181 | mainLayout->setColumnStretch(column: 0, stretch: 1); |
| 182 | mainLayout->setColumnStretch(column: 3, stretch: 1); |
| 183 | mainLayout->addWidget(renderArea, row: 0, column: 0, rowSpan: 1, columnSpan: 4); |
| 184 | mainLayout->addWidget(shapeLabel, row: 2, column: 0, Qt::AlignRight); |
| 185 | mainLayout->addWidget(shapeComboBox, row: 2, column: 1); |
| 186 | mainLayout->addWidget(penWidthLabel, row: 3, column: 0, Qt::AlignRight); |
| 187 | mainLayout->addWidget(penWidthSpinBox, row: 3, column: 1); |
| 188 | mainLayout->addWidget(penStyleLabel, row: 4, column: 0, Qt::AlignRight); |
| 189 | mainLayout->addWidget(penStyleComboBox, row: 4, column: 1); |
| 190 | mainLayout->addWidget(penCapLabel, row: 3, column: 2, Qt::AlignRight); |
| 191 | mainLayout->addWidget(penCapComboBox, row: 3, column: 3); |
| 192 | mainLayout->addWidget(penJoinLabel, row: 2, column: 2, Qt::AlignRight); |
| 193 | mainLayout->addWidget(penJoinComboBox, row: 2, column: 3); |
| 194 | mainLayout->addWidget(brushStyleLabel, row: 4, column: 2, Qt::AlignRight); |
| 195 | mainLayout->addWidget(brushStyleComboBox, row: 4, column: 3); |
| 196 | mainLayout->addWidget(otherOptionsLabel, row: 5, column: 0, Qt::AlignRight); |
| 197 | mainLayout->addWidget(antialiasingCheckBox, row: 5, column: 1, rowSpan: 1, columnSpan: 1, Qt::AlignRight); |
| 198 | mainLayout->addWidget(transformationsCheckBox, row: 5, column: 2, rowSpan: 1, columnSpan: 2, Qt::AlignRight); |
| 199 | setLayout(mainLayout); |
| 200 | |
| 201 | shapeChanged(); |
| 202 | penChanged(); |
| 203 | brushChanged(); |
| 204 | antialiasingCheckBox->setChecked(true); |
| 205 | |
| 206 | setWindowTitle(tr(s: "Basic Drawing" )); |
| 207 | } |
| 208 | //! [10] |
| 209 | |
| 210 | //! [11] |
| 211 | void Window::shapeChanged() |
| 212 | { |
| 213 | RenderArea::Shape shape = RenderArea::Shape(shapeComboBox->itemData( |
| 214 | index: shapeComboBox->currentIndex(), role: 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 | index: penStyleComboBox->currentIndex(), role: IdRole).toInt()); |
| 225 | Qt::PenCapStyle cap = Qt::PenCapStyle(penCapComboBox->itemData( |
| 226 | index: penCapComboBox->currentIndex(), role: IdRole).toInt()); |
| 227 | Qt::PenJoinStyle join = Qt::PenJoinStyle(penJoinComboBox->itemData( |
| 228 | index: penJoinComboBox->currentIndex(), role: 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 | index: brushStyleComboBox->currentIndex(), role: IdRole).toInt()); |
| 240 | |
| 241 | //! [14] |
| 242 | if (style == Qt::LinearGradientPattern) { |
| 243 | QLinearGradient linearGradient(0, 0, 100, 100); |
| 244 | linearGradient.setColorAt(pos: 0.0, color: Qt::white); |
| 245 | linearGradient.setColorAt(pos: 0.2, color: Qt::green); |
| 246 | linearGradient.setColorAt(pos: 1.0, color: 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(pos: 0.0, color: Qt::white); |
| 252 | radialGradient.setColorAt(pos: 0.2, color: Qt::green); |
| 253 | radialGradient.setColorAt(pos: 1.0, color: Qt::black); |
| 254 | renderArea->setBrush(radialGradient); |
| 255 | } else if (style == Qt::ConicalGradientPattern) { |
| 256 | QConicalGradient conicalGradient(50, 50, 150); |
| 257 | conicalGradient.setColorAt(pos: 0.0, color: Qt::white); |
| 258 | conicalGradient.setColorAt(pos: 0.2, color: Qt::green); |
| 259 | conicalGradient.setColorAt(pos: 1.0, color: 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] |
| 270 | |