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