| 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 |
|
| 53 |
#include <QPainter> |
| 54 |
#include <QPaintEvent> |
| 55 |
|
| 56 |
//! [0] |
| 57 |
RenderArea::RenderArea(QWidget *parent) |
| 58 |
: QWidget(parent) |
| 59 |
{ |
| 60 |
QFont newFont = font(); |
| 61 |
newFont.setPixelSize(12); |
| 62 |
setFont(newFont); |
| 63 |
|
| 64 |
QFontMetrics fontMetrics(newFont); |
| 65 |
xBoundingRect = fontMetrics.boundingRect(tr("x")); |
| 66 |
yBoundingRect = fontMetrics.boundingRect(tr("y")); |
| 67 |
} |
| 68 |
//! [0] |
| 69 |
|
| 70 |
//! [1] |
| 71 |
void RenderArea::setOperations(const QList<Operation> &operations) |
| 72 |
{ |
| 73 |
this->operations = operations; |
| 74 |
update(); |
| 75 |
} |
| 76 |
//! [1] |
| 77 |
|
| 78 |
//! [2] |
| 79 |
void RenderArea::setShape(const QPainterPath &shape) |
| 80 |
{ |
| 81 |
this->shape = shape; |
| 82 |
update(); |
| 83 |
} |
| 84 |
//! [2] |
| 85 |
|
| 86 |
//! [3] |
| 87 |
QSize RenderArea::minimumSizeHint() const |
| 88 |
{ |
| 89 |
return QSize(182, 182); |
| 90 |
} |
| 91 |
//! [3] |
| 92 |
|
| 93 |
//! [4] |
| 94 |
QSize RenderArea::sizeHint() const |
| 95 |
{ |
| 96 |
return QSize(232, 232); |
| 97 |
} |
| 98 |
//! [4] |
| 99 |
|
| 100 |
//! [5] |
| 101 |
void RenderArea::paintEvent(QPaintEvent *event) |
| 102 |
{ |
| 103 |
QPainter painter(this); |
| 104 |
painter.setRenderHint(QPainter::Antialiasing); |
| 105 |
painter.fillRect(event->rect(), QBrush(Qt::white)); |
| 106 |
|
| 107 |
painter.translate(66, 66); |
| 108 |
//! [5] |
| 109 |
|
| 110 |
//! [6] |
| 111 |
painter.save(); |
| 112 |
transformPainter(painter); |
| 113 |
drawShape(painter); |
| 114 |
painter.restore(); |
| 115 |
//! [6] |
| 116 |
|
| 117 |
//! [7] |
| 118 |
drawOutline(painter); |
| 119 |
//! [7] |
| 120 |
|
| 121 |
//! [8] |
| 122 |
transformPainter(painter); |
| 123 |
drawCoordinates(painter); |
| 124 |
} |
| 125 |
//! [8] |
| 126 |
|
| 127 |
//! [9] |
| 128 |
void RenderArea::drawCoordinates(QPainter &painter) |
| 129 |
{ |
| 130 |
painter.setPen(Qt::red); |
| 131 |
|
| 132 |
painter.drawLine(0, 0, 50, 0); |
| 133 |
painter.drawLine(48, -2, 50, 0); |
| 134 |
painter.drawLine(48, 2, 50, 0); |
| 135 |
painter.drawText(60 - xBoundingRect.width() / 2, |
| 136 |
0 + xBoundingRect.height() / 2, tr("x")); |
| 137 |
|
| 138 |
painter.drawLine(0, 0, 0, 50); |
| 139 |
painter.drawLine(-2, 48, 0, 50); |
| 140 |
painter.drawLine(2, 48, 0, 50); |
| 141 |
painter.drawText(0 - yBoundingRect.width() / 2, |
| 142 |
60 + yBoundingRect.height() / 2, tr("y")); |
| 143 |
} |
| 144 |
//! [9] |
| 145 |
|
| 146 |
//! [10] |
| 147 |
void RenderArea::drawOutline(QPainter &painter) |
| 148 |
{ |
| 149 |
painter.setPen(Qt::darkGreen); |
| 150 |
painter.setPen(Qt::DashLine); |
| 151 |
painter.setBrush(Qt::NoBrush); |
| 152 |
painter.drawRect(0, 0, 100, 100); |
| 153 |
} |
| 154 |
//! [10] |
| 155 |
|
| 156 |
//! [11] |
| 157 |
void RenderArea::drawShape(QPainter &painter) |
| 158 |
{ |
| 159 |
painter.fillPath(shape, Qt::blue); |
| 160 |
} |
| 161 |
//! [11] |
| 162 |
|
| 163 |
//! [12] |
| 164 |
void RenderArea::transformPainter(QPainter &painter) |
| 165 |
{ |
| 166 |
for (int i = 0; i < operations.size(); ++i) { |
| 167 |
switch (operations[i]) { |
| 168 |
case Translate: |
| 169 |
painter.translate(50, 50); |
| 170 |
break; |
| 171 |
case Scale: |
| 172 |
painter.scale(0.75, 0.75); |
| 173 |
break; |
| 174 |
case Rotate: |
| 175 |
painter.rotate(60); |
| 176 |
break; |
| 177 |
case NoTransformation: |
| 178 |
default: |
| 179 |
; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
//! [12] |