0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 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:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include <QtGui>
|
|
43 |
|
|
44 |
#include <math.h>
|
|
45 |
|
|
46 |
#include "mandelbrotwidget.h"
|
|
47 |
|
|
48 |
//! [0]
|
|
49 |
const double DefaultCenterX = -0.637011f;
|
|
50 |
const double DefaultCenterY = -0.0395159f;
|
|
51 |
const double DefaultScale = 0.00403897f;
|
|
52 |
|
|
53 |
const double ZoomInFactor = 0.8f;
|
|
54 |
const double ZoomOutFactor = 1 / ZoomInFactor;
|
|
55 |
const int ScrollStep = 20;
|
|
56 |
//! [0]
|
|
57 |
|
|
58 |
//! [1]
|
|
59 |
MandelbrotWidget::MandelbrotWidget(QWidget *parent)
|
|
60 |
: QWidget(parent)
|
|
61 |
{
|
|
62 |
centerX = DefaultCenterX;
|
|
63 |
centerY = DefaultCenterY;
|
|
64 |
pixmapScale = DefaultScale;
|
|
65 |
curScale = DefaultScale;
|
|
66 |
|
|
67 |
qRegisterMetaType<QImage>("QImage");
|
|
68 |
connect(&thread, SIGNAL(renderedImage(const QImage &, double)),
|
|
69 |
this, SLOT(updatePixmap(const QImage &, double)));
|
|
70 |
|
|
71 |
setWindowTitle(tr("Mandelbrot"));
|
|
72 |
#ifndef QT_NO_CURSOR
|
|
73 |
setCursor(Qt::CrossCursor);
|
|
74 |
#endif
|
|
75 |
resize(550, 400);
|
|
76 |
}
|
|
77 |
//! [1]
|
|
78 |
|
|
79 |
//! [2]
|
|
80 |
void MandelbrotWidget::paintEvent(QPaintEvent * /* event */)
|
|
81 |
{
|
|
82 |
QPainter painter(this);
|
|
83 |
painter.fillRect(rect(), Qt::black);
|
|
84 |
|
|
85 |
if (pixmap.isNull()) {
|
|
86 |
painter.setPen(Qt::white);
|
|
87 |
painter.drawText(rect(), Qt::AlignCenter,
|
|
88 |
tr("Rendering initial image, please wait..."));
|
|
89 |
//! [2] //! [3]
|
|
90 |
return;
|
|
91 |
//! [3] //! [4]
|
|
92 |
}
|
|
93 |
//! [4]
|
|
94 |
|
|
95 |
//! [5]
|
|
96 |
if (curScale == pixmapScale) {
|
|
97 |
//! [5] //! [6]
|
|
98 |
painter.drawPixmap(pixmapOffset, pixmap);
|
|
99 |
//! [6] //! [7]
|
|
100 |
} else {
|
|
101 |
//! [7] //! [8]
|
|
102 |
double scaleFactor = pixmapScale / curScale;
|
|
103 |
int newWidth = int(pixmap.width() * scaleFactor);
|
|
104 |
int newHeight = int(pixmap.height() * scaleFactor);
|
|
105 |
int newX = pixmapOffset.x() + (pixmap.width() - newWidth) / 2;
|
|
106 |
int newY = pixmapOffset.y() + (pixmap.height() - newHeight) / 2;
|
|
107 |
|
|
108 |
painter.save();
|
|
109 |
painter.translate(newX, newY);
|
|
110 |
painter.scale(scaleFactor, scaleFactor);
|
|
111 |
QRectF exposed = painter.matrix().inverted().mapRect(rect()).adjusted(-1, -1, 1, 1);
|
|
112 |
painter.drawPixmap(exposed, pixmap, exposed);
|
|
113 |
painter.restore();
|
|
114 |
}
|
|
115 |
//! [8] //! [9]
|
|
116 |
|
|
117 |
QString text = tr("Use mouse wheel or the '+' and '-' keys to zoom. "
|
|
118 |
"Press and hold left mouse button to scroll.");
|
|
119 |
QFontMetrics metrics = painter.fontMetrics();
|
|
120 |
int textWidth = metrics.width(text);
|
|
121 |
|
|
122 |
painter.setPen(Qt::NoPen);
|
|
123 |
painter.setBrush(QColor(0, 0, 0, 127));
|
|
124 |
painter.drawRect((width() - textWidth) / 2 - 5, 0, textWidth + 10,
|
|
125 |
metrics.lineSpacing() + 5);
|
|
126 |
painter.setPen(Qt::white);
|
|
127 |
painter.drawText((width() - textWidth) / 2,
|
|
128 |
metrics.leading() + metrics.ascent(), text);
|
|
129 |
}
|
|
130 |
//! [9]
|
|
131 |
|
|
132 |
//! [10]
|
|
133 |
void MandelbrotWidget::resizeEvent(QResizeEvent * /* event */)
|
|
134 |
{
|
|
135 |
thread.render(centerX, centerY, curScale, size());
|
|
136 |
}
|
|
137 |
//! [10]
|
|
138 |
|
|
139 |
//! [11]
|
|
140 |
void MandelbrotWidget::keyPressEvent(QKeyEvent *event)
|
|
141 |
{
|
|
142 |
switch (event->key()) {
|
|
143 |
case Qt::Key_Plus:
|
|
144 |
zoom(ZoomInFactor);
|
|
145 |
break;
|
|
146 |
case Qt::Key_Minus:
|
|
147 |
zoom(ZoomOutFactor);
|
|
148 |
break;
|
|
149 |
case Qt::Key_Left:
|
|
150 |
scroll(-ScrollStep, 0);
|
|
151 |
break;
|
|
152 |
case Qt::Key_Right:
|
|
153 |
scroll(+ScrollStep, 0);
|
|
154 |
break;
|
|
155 |
case Qt::Key_Down:
|
|
156 |
scroll(0, -ScrollStep);
|
|
157 |
break;
|
|
158 |
case Qt::Key_Up:
|
|
159 |
scroll(0, +ScrollStep);
|
|
160 |
break;
|
|
161 |
default:
|
|
162 |
QWidget::keyPressEvent(event);
|
|
163 |
}
|
|
164 |
}
|
|
165 |
//! [11]
|
|
166 |
|
|
167 |
//! [12]
|
|
168 |
void MandelbrotWidget::wheelEvent(QWheelEvent *event)
|
|
169 |
{
|
|
170 |
int numDegrees = event->delta() / 8;
|
|
171 |
double numSteps = numDegrees / 15.0f;
|
|
172 |
zoom(pow(ZoomInFactor, numSteps));
|
|
173 |
}
|
|
174 |
//! [12]
|
|
175 |
|
|
176 |
//! [13]
|
|
177 |
void MandelbrotWidget::mousePressEvent(QMouseEvent *event)
|
|
178 |
{
|
|
179 |
if (event->button() == Qt::LeftButton)
|
|
180 |
lastDragPos = event->pos();
|
|
181 |
}
|
|
182 |
//! [13]
|
|
183 |
|
|
184 |
//! [14]
|
|
185 |
void MandelbrotWidget::mouseMoveEvent(QMouseEvent *event)
|
|
186 |
{
|
|
187 |
if (event->buttons() & Qt::LeftButton) {
|
|
188 |
pixmapOffset += event->pos() - lastDragPos;
|
|
189 |
lastDragPos = event->pos();
|
|
190 |
update();
|
|
191 |
}
|
|
192 |
}
|
|
193 |
//! [14]
|
|
194 |
|
|
195 |
//! [15]
|
|
196 |
void MandelbrotWidget::mouseReleaseEvent(QMouseEvent *event)
|
|
197 |
{
|
|
198 |
if (event->button() == Qt::LeftButton) {
|
|
199 |
pixmapOffset += event->pos() - lastDragPos;
|
|
200 |
lastDragPos = QPoint();
|
|
201 |
|
|
202 |
int deltaX = (width() - pixmap.width()) / 2 - pixmapOffset.x();
|
|
203 |
int deltaY = (height() - pixmap.height()) / 2 - pixmapOffset.y();
|
|
204 |
scroll(deltaX, deltaY);
|
|
205 |
}
|
|
206 |
}
|
|
207 |
//! [15]
|
|
208 |
|
|
209 |
//! [16]
|
|
210 |
void MandelbrotWidget::updatePixmap(const QImage &image, double scaleFactor)
|
|
211 |
{
|
|
212 |
if (!lastDragPos.isNull())
|
|
213 |
return;
|
|
214 |
|
|
215 |
pixmap = QPixmap::fromImage(image);
|
|
216 |
pixmapOffset = QPoint();
|
|
217 |
lastDragPos = QPoint();
|
|
218 |
pixmapScale = scaleFactor;
|
|
219 |
update();
|
|
220 |
}
|
|
221 |
//! [16]
|
|
222 |
|
|
223 |
//! [17]
|
|
224 |
void MandelbrotWidget::zoom(double zoomFactor)
|
|
225 |
{
|
|
226 |
curScale *= zoomFactor;
|
|
227 |
update();
|
|
228 |
thread.render(centerX, centerY, curScale, size());
|
|
229 |
}
|
|
230 |
//! [17]
|
|
231 |
|
|
232 |
//! [18]
|
|
233 |
void MandelbrotWidget::scroll(int deltaX, int deltaY)
|
|
234 |
{
|
|
235 |
centerX += deltaX * curScale;
|
|
236 |
centerY += deltaY * curScale;
|
|
237 |
update();
|
|
238 |
thread.render(centerX, centerY, curScale, size());
|
|
239 |
}
|
|
240 |
//! [18]
|