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