| 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 "window.h" |
| 52 | |
| 53 | #include <QComboBox> |
| 54 | #include <QGridLayout> |
| 55 | |
| 56 | //! [0] |
| 57 | Window::Window() |
| 58 | { |
| 59 | originalRenderArea = new RenderArea; |
| 60 | |
| 61 | shapeComboBox = new QComboBox; |
| 62 | shapeComboBox->addItem(atext: tr(s: "Clock" )); |
| 63 | shapeComboBox->addItem(atext: tr(s: "House" )); |
| 64 | shapeComboBox->addItem(atext: tr(s: "Text" )); |
| 65 | shapeComboBox->addItem(atext: tr(s: "Truck" )); |
| 66 | |
| 67 | QGridLayout *layout = new QGridLayout; |
| 68 | layout->addWidget(originalRenderArea, row: 0, column: 0); |
| 69 | layout->addWidget(shapeComboBox, row: 1, column: 0); |
| 70 | //! [0] |
| 71 | |
| 72 | //! [1] |
| 73 | for (int i = 0; i < NumTransformedAreas; ++i) { |
| 74 | transformedRenderAreas[i] = new RenderArea; |
| 75 | |
| 76 | operationComboBoxes[i] = new QComboBox; |
| 77 | operationComboBoxes[i]->addItem(atext: tr(s: "No transformation" )); |
| 78 | operationComboBoxes[i]->addItem(atext: tr(s: "Rotate by 60\xC2\xB0" )); |
| 79 | operationComboBoxes[i]->addItem(atext: tr(s: "Scale to 75%" )); |
| 80 | operationComboBoxes[i]->addItem(atext: tr(s: "Translate by (50, 50)" )); |
| 81 | |
| 82 | connect(sender: operationComboBoxes[i], signal: QOverload<int>::of(ptr: &QComboBox::activated), |
| 83 | receiver: this, slot: &Window::operationChanged); |
| 84 | |
| 85 | layout->addWidget(transformedRenderAreas[i], row: 0, column: i + 1); |
| 86 | layout->addWidget(operationComboBoxes[i], row: 1, column: i + 1); |
| 87 | } |
| 88 | //! [1] |
| 89 | |
| 90 | //! [2] |
| 91 | setLayout(layout); |
| 92 | setupShapes(); |
| 93 | shapeSelected(index: 0); |
| 94 | |
| 95 | setWindowTitle(tr(s: "Transformations" )); |
| 96 | } |
| 97 | //! [2] |
| 98 | |
| 99 | //! [3] |
| 100 | void Window::setupShapes() |
| 101 | { |
| 102 | QPainterPath truck; |
| 103 | //! [3] |
| 104 | truck.setFillRule(Qt::WindingFill); |
| 105 | truck.moveTo(x: 0.0, y: 87.0); |
| 106 | truck.lineTo(x: 0.0, y: 60.0); |
| 107 | truck.lineTo(x: 10.0, y: 60.0); |
| 108 | truck.lineTo(x: 35.0, y: 35.0); |
| 109 | truck.lineTo(x: 100.0, y: 35.0); |
| 110 | truck.lineTo(x: 100.0, y: 87.0); |
| 111 | truck.lineTo(x: 0.0, y: 87.0); |
| 112 | truck.moveTo(x: 17.0, y: 60.0); |
| 113 | truck.lineTo(x: 55.0, y: 60.0); |
| 114 | truck.lineTo(x: 55.0, y: 40.0); |
| 115 | truck.lineTo(x: 37.0, y: 40.0); |
| 116 | truck.lineTo(x: 17.0, y: 60.0); |
| 117 | truck.addEllipse(x: 17.0, y: 75.0, w: 25.0, h: 25.0); |
| 118 | truck.addEllipse(x: 63.0, y: 75.0, w: 25.0, h: 25.0); |
| 119 | |
| 120 | //! [4] |
| 121 | QPainterPath clock; |
| 122 | //! [4] |
| 123 | clock.addEllipse(x: -50.0, y: -50.0, w: 100.0, h: 100.0); |
| 124 | clock.addEllipse(x: -48.0, y: -48.0, w: 96.0, h: 96.0); |
| 125 | clock.moveTo(x: 0.0, y: 0.0); |
| 126 | clock.lineTo(x: -2.0, y: -2.0); |
| 127 | clock.lineTo(x: 0.0, y: -42.0); |
| 128 | clock.lineTo(x: 2.0, y: -2.0); |
| 129 | clock.lineTo(x: 0.0, y: 0.0); |
| 130 | clock.moveTo(x: 0.0, y: 0.0); |
| 131 | clock.lineTo(x: 2.732, y: -0.732); |
| 132 | clock.lineTo(x: 24.495, y: 14.142); |
| 133 | clock.lineTo(x: 0.732, y: 2.732); |
| 134 | clock.lineTo(x: 0.0, y: 0.0); |
| 135 | |
| 136 | //! [5] |
| 137 | QPainterPath house; |
| 138 | //! [5] |
| 139 | house.moveTo(x: -45.0, y: -20.0); |
| 140 | house.lineTo(x: 0.0, y: -45.0); |
| 141 | house.lineTo(x: 45.0, y: -20.0); |
| 142 | house.lineTo(x: 45.0, y: 45.0); |
| 143 | house.lineTo(x: -45.0, y: 45.0); |
| 144 | house.lineTo(x: -45.0, y: -20.0); |
| 145 | house.addRect(x: 15.0, y: 5.0, w: 20.0, h: 35.0); |
| 146 | house.addRect(x: -35.0, y: -15.0, w: 25.0, h: 25.0); |
| 147 | |
| 148 | //! [6] |
| 149 | QPainterPath text; |
| 150 | //! [6] |
| 151 | QFont font; |
| 152 | font.setPixelSize(50); |
| 153 | QRect fontBoundingRect = QFontMetrics(font).boundingRect(text: tr(s: "Qt" )); |
| 154 | text.addText(point: -QPointF(fontBoundingRect.center()), f: font, text: tr(s: "Qt" )); |
| 155 | |
| 156 | //! [7] |
| 157 | shapes.append(t: clock); |
| 158 | shapes.append(t: house); |
| 159 | shapes.append(t: text); |
| 160 | shapes.append(t: truck); |
| 161 | |
| 162 | connect(sender: shapeComboBox, signal: QOverload<int>::of(ptr: &QComboBox::activated), |
| 163 | receiver: this, slot: &Window::shapeSelected); |
| 164 | } |
| 165 | //! [7] |
| 166 | |
| 167 | //! [8] |
| 168 | void Window::operationChanged() |
| 169 | { |
| 170 | static const Operation operationTable[] = { |
| 171 | NoTransformation, Rotate, Scale, Translate |
| 172 | }; |
| 173 | |
| 174 | QList<Operation> operations; |
| 175 | for (int i = 0; i < NumTransformedAreas; ++i) { |
| 176 | int index = operationComboBoxes[i]->currentIndex(); |
| 177 | operations.append(t: operationTable[index]); |
| 178 | transformedRenderAreas[i]->setOperations(operations); |
| 179 | } |
| 180 | } |
| 181 | //! [8] |
| 182 | |
| 183 | //! [9] |
| 184 | void Window::shapeSelected(int index) |
| 185 | { |
| 186 | QPainterPath shape = shapes[index]; |
| 187 | originalRenderArea->setShape(shape); |
| 188 | for (int i = 0; i < NumTransformedAreas; ++i) |
| 189 | transformedRenderAreas[i]->setShape(shape); |
| 190 | } |
| 191 | //! [9] |
| 192 | |