| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:BSD$ | 
|---|
| 10 | ** You may use this file under the terms of the BSD license as follows: | 
|---|
| 11 | ** | 
|---|
| 12 | ** "Redistribution and use in source and binary forms, with or without | 
|---|
| 13 | ** modification, are permitted provided that the following conditions are | 
|---|
| 14 | ** met: | 
|---|
| 15 | **   * Redistributions of source code must retain the above copyright | 
|---|
| 16 | **     notice, this list of conditions and the following disclaimer. | 
|---|
| 17 | **   * Redistributions in binary form must reproduce the above copyright | 
|---|
| 18 | **     notice, this list of conditions and the following disclaimer in | 
|---|
| 19 | **     the documentation and/or other materials provided with the | 
|---|
| 20 | **     distribution. | 
|---|
| 21 | **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor | 
|---|
| 22 | **     the names of its contributors may be used to endorse or promote | 
|---|
| 23 | **     products derived from this software without specific prior written | 
|---|
| 24 | **     permission. | 
|---|
| 25 | ** | 
|---|
| 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
|---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
|---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
|---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
|---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
|---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | 
|---|
| 37 | ** $QT_END_LICENSE$ | 
|---|
| 38 | ** | 
|---|
| 39 | ****************************************************************************/ | 
|---|
| 40 |  | 
|---|
| 41 | #include <QtGui> | 
|---|
| 42 |  | 
|---|
| 43 | #include "renderarea.h" | 
|---|
| 44 | #include "window.h" | 
|---|
| 45 |  | 
|---|
| 46 | //! [0] | 
|---|
| 47 | const int IdRole = Qt::UserRole; | 
|---|
| 48 | //! [0] | 
|---|
| 49 |  | 
|---|
| 50 | //! [1] | 
|---|
| 51 | Window::Window() | 
|---|
| 52 | { | 
|---|
| 53 | renderArea = new RenderArea; | 
|---|
| 54 |  | 
|---|
| 55 | shapeComboBox = new QComboBox; | 
|---|
| 56 | shapeComboBox->addItem(tr("Polygon"), RenderArea::Polygon); | 
|---|
| 57 | shapeComboBox->addItem(tr("Rectangle"), RenderArea::Rect); | 
|---|
| 58 | shapeComboBox->addItem(tr("Rounded Rectangle"), RenderArea::RoundedRect); | 
|---|
| 59 | shapeComboBox->addItem(tr("Ellipse"), RenderArea::Ellipse); | 
|---|
| 60 | shapeComboBox->addItem(tr("Pie"), RenderArea::Pie); | 
|---|
| 61 | shapeComboBox->addItem(tr("Chord"), RenderArea::Chord); | 
|---|
| 62 | shapeComboBox->addItem(tr("Path"), RenderArea::Path); | 
|---|
| 63 | shapeComboBox->addItem(tr("Line"), RenderArea::Line); | 
|---|
| 64 | shapeComboBox->addItem(tr("Polyline"), RenderArea::Polyline); | 
|---|
| 65 | shapeComboBox->addItem(tr("Arc"), RenderArea::Arc); | 
|---|
| 66 | shapeComboBox->addItem(tr("Points"), RenderArea::Points); | 
|---|
| 67 | shapeComboBox->addItem(tr("Text"), RenderArea::Text); | 
|---|
| 68 | shapeComboBox->addItem(tr("Pixmap"), RenderArea::Pixmap); | 
|---|
| 69 |  | 
|---|
| 70 | shapeLabel = new QLabel(tr("&Shape:")); | 
|---|
| 71 | shapeLabel->setBuddy(shapeComboBox); | 
|---|
| 72 | //! [1] | 
|---|
| 73 |  | 
|---|
| 74 | //! [2] | 
|---|
| 75 | penWidthSpinBox = new QSpinBox; | 
|---|
| 76 | penWidthSpinBox->setRange(0, 20); | 
|---|
| 77 | penWidthSpinBox->setSpecialValueText(tr("0 (cosmetic pen)")); | 
|---|
| 78 |  | 
|---|
| 79 | penWidthLabel = new QLabel(tr("Pen &Width:")); | 
|---|
| 80 | penWidthLabel->setBuddy(penWidthSpinBox); | 
|---|
| 81 | //! [2] | 
|---|
| 82 |  | 
|---|
| 83 | //! [3] | 
|---|
| 84 | penStyleComboBox = new QComboBox; | 
|---|
| 85 | penStyleComboBox->addItem(tr("Solid"), Qt::SolidLine); | 
|---|
| 86 | penStyleComboBox->addItem(tr("Dash"), Qt::DashLine); | 
|---|
| 87 | penStyleComboBox->addItem(tr("Dot"), Qt::DotLine); | 
|---|
| 88 | penStyleComboBox->addItem(tr("Dash Dot"), Qt::DashDotLine); | 
|---|
| 89 | penStyleComboBox->addItem(tr("Dash Dot Dot"), Qt::DashDotDotLine); | 
|---|
| 90 | penStyleComboBox->addItem(tr("None"), Qt::NoPen); | 
|---|
| 91 |  | 
|---|
| 92 | penStyleLabel = new QLabel(tr("&Pen Style:")); | 
|---|
| 93 | penStyleLabel->setBuddy(penStyleComboBox); | 
|---|
| 94 |  | 
|---|
| 95 | penCapComboBox = new QComboBox; | 
|---|
| 96 | penCapComboBox->addItem(tr("Flat"), Qt::FlatCap); | 
|---|
| 97 | penCapComboBox->addItem(tr("Square"), Qt::SquareCap); | 
|---|
| 98 | penCapComboBox->addItem(tr("Round"), Qt::RoundCap); | 
|---|
| 99 |  | 
|---|
| 100 | penCapLabel = new QLabel(tr("Pen &Cap:")); | 
|---|
| 101 | penCapLabel->setBuddy(penCapComboBox); | 
|---|
| 102 |  | 
|---|
| 103 | penJoinComboBox = new QComboBox; | 
|---|
| 104 | penJoinComboBox->addItem(tr("Miter"), Qt::MiterJoin); | 
|---|
| 105 | penJoinComboBox->addItem(tr("Bevel"), Qt::BevelJoin); | 
|---|
| 106 | penJoinComboBox->addItem(tr("Round"), Qt::RoundJoin); | 
|---|
| 107 |  | 
|---|
| 108 | penJoinLabel = new QLabel(tr("Pen &Join:")); | 
|---|
| 109 | penJoinLabel->setBuddy(penJoinComboBox); | 
|---|
| 110 | //! [3] | 
|---|
| 111 |  | 
|---|
| 112 | //! [4] | 
|---|
| 113 | brushStyleComboBox = new QComboBox; | 
|---|
| 114 | brushStyleComboBox->addItem(tr("Linear Gradient"), | 
|---|
| 115 | Qt::LinearGradientPattern); | 
|---|
| 116 | brushStyleComboBox->addItem(tr("Radial Gradient"), | 
|---|
| 117 | Qt::RadialGradientPattern); | 
|---|
| 118 | brushStyleComboBox->addItem(tr("Conical Gradient"), | 
|---|
| 119 | Qt::ConicalGradientPattern); | 
|---|
| 120 | brushStyleComboBox->addItem(tr("Texture"), Qt::TexturePattern); | 
|---|
| 121 | brushStyleComboBox->addItem(tr("Solid"), Qt::SolidPattern); | 
|---|
| 122 | brushStyleComboBox->addItem(tr("Horizontal"), Qt::HorPattern); | 
|---|
| 123 | brushStyleComboBox->addItem(tr("Vertical"), Qt::VerPattern); | 
|---|
| 124 | brushStyleComboBox->addItem(tr("Cross"), Qt::CrossPattern); | 
|---|
| 125 | brushStyleComboBox->addItem(tr("Backward Diagonal"), Qt::BDiagPattern); | 
|---|
| 126 | brushStyleComboBox->addItem(tr("Forward Diagonal"), Qt::FDiagPattern); | 
|---|
| 127 | brushStyleComboBox->addItem(tr("Diagonal Cross"), Qt::DiagCrossPattern); | 
|---|
| 128 | brushStyleComboBox->addItem(tr("Dense 1"), Qt::Dense1Pattern); | 
|---|
| 129 | brushStyleComboBox->addItem(tr("Dense 2"), Qt::Dense2Pattern); | 
|---|
| 130 | brushStyleComboBox->addItem(tr("Dense 3"), Qt::Dense3Pattern); | 
|---|
| 131 | brushStyleComboBox->addItem(tr("Dense 4"), Qt::Dense4Pattern); | 
|---|
| 132 | brushStyleComboBox->addItem(tr("Dense 5"), Qt::Dense5Pattern); | 
|---|
| 133 | brushStyleComboBox->addItem(tr("Dense 6"), Qt::Dense6Pattern); | 
|---|
| 134 | brushStyleComboBox->addItem(tr("Dense 7"), Qt::Dense7Pattern); | 
|---|
| 135 | brushStyleComboBox->addItem(tr("None"), Qt::NoBrush); | 
|---|
| 136 |  | 
|---|
| 137 | brushStyleLabel = new QLabel(tr("&Brush Style:")); | 
|---|
| 138 | brushStyleLabel->setBuddy(brushStyleComboBox); | 
|---|
| 139 | //! [4] | 
|---|
| 140 |  | 
|---|
| 141 | //! [5] | 
|---|
| 142 | otherOptionsLabel = new QLabel(tr("Other Options:")); | 
|---|
| 143 | //! [5] //! [6] | 
|---|
| 144 | antialiasingCheckBox = new QCheckBox(tr("&Antialiasing")); | 
|---|
| 145 | //! [6] //! [7] | 
|---|
| 146 | transformationsCheckBox = new QCheckBox(tr("&Transformations")); | 
|---|
| 147 | //! [7] | 
|---|
| 148 |  | 
|---|
| 149 | //! [8] | 
|---|
| 150 | connect(shapeComboBox, SIGNAL(activated(int)), | 
|---|
| 151 | this, SLOT(shapeChanged())); | 
|---|
| 152 | connect(penWidthSpinBox, SIGNAL(valueChanged(int)), | 
|---|
| 153 | this, SLOT(penChanged())); | 
|---|
| 154 | connect(penStyleComboBox, SIGNAL(activated(int)), | 
|---|
| 155 | this, SLOT(penChanged())); | 
|---|
| 156 | connect(penCapComboBox, SIGNAL(activated(int)), | 
|---|
| 157 | this, SLOT(penChanged())); | 
|---|
| 158 | connect(penJoinComboBox, SIGNAL(activated(int)), | 
|---|
| 159 | this, SLOT(penChanged())); | 
|---|
| 160 | connect(brushStyleComboBox, SIGNAL(activated(int)), | 
|---|
| 161 | this, SLOT(brushChanged())); | 
|---|
| 162 | connect(antialiasingCheckBox, SIGNAL(toggled(bool)), | 
|---|
| 163 | renderArea, SLOT(setAntialiased(bool))); | 
|---|
| 164 | connect(transformationsCheckBox, SIGNAL(toggled(bool)), | 
|---|
| 165 | renderArea, SLOT(setTransformed(bool))); | 
|---|
| 166 | //! [8] | 
|---|
| 167 |  | 
|---|
| 168 | //! [9] | 
|---|
| 169 | QGridLayout *mainLayout = new QGridLayout; | 
|---|
| 170 | //! [9] //! [10] | 
|---|
| 171 | mainLayout->setColumnStretch(0, 1); | 
|---|
| 172 | mainLayout->setColumnStretch(3, 1); | 
|---|
| 173 | mainLayout->addWidget(renderArea, 0, 0, 1, 4); | 
|---|
| 174 | mainLayout->setRowMinimumHeight(1, 6); | 
|---|
| 175 | mainLayout->addWidget(shapeLabel, 2, 1, Qt::AlignRight); | 
|---|
| 176 | mainLayout->addWidget(shapeComboBox, 2, 2); | 
|---|
| 177 | mainLayout->addWidget(penWidthLabel, 3, 1, Qt::AlignRight); | 
|---|
| 178 | mainLayout->addWidget(penWidthSpinBox, 3, 2); | 
|---|
| 179 | mainLayout->addWidget(penStyleLabel, 4, 1, Qt::AlignRight); | 
|---|
| 180 | mainLayout->addWidget(penStyleComboBox, 4, 2); | 
|---|
| 181 | mainLayout->addWidget(penCapLabel, 5, 1, Qt::AlignRight); | 
|---|
| 182 | mainLayout->addWidget(penCapComboBox, 5, 2); | 
|---|
| 183 | mainLayout->addWidget(penJoinLabel, 6, 1, Qt::AlignRight); | 
|---|
| 184 | mainLayout->addWidget(penJoinComboBox, 6, 2); | 
|---|
| 185 | mainLayout->addWidget(brushStyleLabel, 7, 1, Qt::AlignRight); | 
|---|
| 186 | mainLayout->addWidget(brushStyleComboBox, 7, 2); | 
|---|
| 187 | mainLayout->setRowMinimumHeight(8, 6); | 
|---|
| 188 | mainLayout->addWidget(otherOptionsLabel, 9, 1, Qt::AlignRight); | 
|---|
| 189 | mainLayout->addWidget(antialiasingCheckBox, 9, 2); | 
|---|
| 190 | mainLayout->addWidget(transformationsCheckBox, 10, 2); | 
|---|
| 191 | setLayout(mainLayout); | 
|---|
| 192 |  | 
|---|
| 193 | shapeChanged(); | 
|---|
| 194 | penChanged(); | 
|---|
| 195 | brushChanged(); | 
|---|
| 196 | antialiasingCheckBox->setChecked(true); | 
|---|
| 197 |  | 
|---|
| 198 | setWindowTitle(tr("Basic Drawing")); | 
|---|
| 199 | } | 
|---|
| 200 | //! [10] | 
|---|
| 201 |  | 
|---|
| 202 | //! [11] | 
|---|
| 203 | void Window::shapeChanged() | 
|---|
| 204 | { | 
|---|
| 205 | RenderArea::Shape shape = RenderArea::Shape(shapeComboBox->itemData( | 
|---|
| 206 | shapeComboBox->currentIndex(), IdRole).toInt()); | 
|---|
| 207 | renderArea->setShape(shape); | 
|---|
| 208 | } | 
|---|
| 209 | //! [11] | 
|---|
| 210 |  | 
|---|
| 211 | //! [12] | 
|---|
| 212 | void Window::penChanged() | 
|---|
| 213 | { | 
|---|
| 214 | int width = penWidthSpinBox->value(); | 
|---|
| 215 | Qt::PenStyle style = Qt::PenStyle(penStyleComboBox->itemData( | 
|---|
| 216 | penStyleComboBox->currentIndex(), IdRole).toInt()); | 
|---|
| 217 | Qt::PenCapStyle cap = Qt::PenCapStyle(penCapComboBox->itemData( | 
|---|
| 218 | penCapComboBox->currentIndex(), IdRole).toInt()); | 
|---|
| 219 | Qt::PenJoinStyle join = Qt::PenJoinStyle(penJoinComboBox->itemData( | 
|---|
| 220 | penJoinComboBox->currentIndex(), IdRole).toInt()); | 
|---|
| 221 |  | 
|---|
| 222 | renderArea->setPen(QPen(Qt::blue, width, style, cap, join)); | 
|---|
| 223 | } | 
|---|
| 224 | //! [12] | 
|---|
| 225 |  | 
|---|
| 226 | //! [13] | 
|---|
| 227 | void Window::brushChanged() | 
|---|
| 228 | { | 
|---|
| 229 | Qt::BrushStyle style = Qt::BrushStyle(brushStyleComboBox->itemData( | 
|---|
| 230 | //! [13] | 
|---|
| 231 | brushStyleComboBox->currentIndex(), IdRole).toInt()); | 
|---|
| 232 |  | 
|---|
| 233 | //! [14] | 
|---|
| 234 | if (style == Qt::LinearGradientPattern) { | 
|---|
| 235 | QLinearGradient linearGradient(0, 0, 100, 100); | 
|---|
| 236 | linearGradient.setColorAt(0.0, Qt::white); | 
|---|
| 237 | linearGradient.setColorAt(0.2, Qt::green); | 
|---|
| 238 | linearGradient.setColorAt(1.0, Qt::black); | 
|---|
| 239 | renderArea->setBrush(linearGradient); | 
|---|
| 240 | //! [14] //! [15] | 
|---|
| 241 | } else if (style == Qt::RadialGradientPattern) { | 
|---|
| 242 | QRadialGradient radialGradient(50, 50, 50, 70, 70); | 
|---|
| 243 | radialGradient.setColorAt(0.0, Qt::white); | 
|---|
| 244 | radialGradient.setColorAt(0.2, Qt::green); | 
|---|
| 245 | radialGradient.setColorAt(1.0, Qt::black); | 
|---|
| 246 | renderArea->setBrush(radialGradient); | 
|---|
| 247 | } else if (style == Qt::ConicalGradientPattern) { | 
|---|
| 248 | QConicalGradient conicalGradient(50, 50, 150); | 
|---|
| 249 | conicalGradient.setColorAt(0.0, Qt::white); | 
|---|
| 250 | conicalGradient.setColorAt(0.2, Qt::green); | 
|---|
| 251 | conicalGradient.setColorAt(1.0, Qt::black); | 
|---|
| 252 | renderArea->setBrush(conicalGradient); | 
|---|
| 253 | //! [15] //! [16] | 
|---|
| 254 | } else if (style == Qt::TexturePattern) { | 
|---|
| 255 | renderArea->setBrush(QBrush(QPixmap(":/images/brick.png"))); | 
|---|
| 256 | //! [16] //! [17] | 
|---|
| 257 | } else { | 
|---|
| 258 | renderArea->setBrush(QBrush(Qt::green, style)); | 
|---|
| 259 | } | 
|---|
| 260 | } | 
|---|
| 261 | //! [17] | 
|---|