source: trunk/examples/threads/mandelbrot/mandelbrotwidget.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 6.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43
44#include <math.h>
45
46#include "mandelbrotwidget.h"
47
48//! [0]
49const double DefaultCenterX = -0.637011f;
50const double DefaultCenterY = -0.0395159f;
51const double DefaultScale = 0.00403897f;
52
53const double ZoomInFactor = 0.8f;
54const double ZoomOutFactor = 1 / ZoomInFactor;
55const int ScrollStep = 20;
56//! [0]
57
58//! [1]
59MandelbrotWidget::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]
80void 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]
133void MandelbrotWidget::resizeEvent(QResizeEvent * /* event */)
134{
135 thread.render(centerX, centerY, curScale, size());
136}
137//! [10]
138
139//! [11]
140void 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]
168void 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]
177void MandelbrotWidget::mousePressEvent(QMouseEvent *event)
178{
179 if (event->button() == Qt::LeftButton)
180 lastDragPos = event->pos();
181}
182//! [13]
183
184//! [14]
185void 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]
196void 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]
210void 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]
224void MandelbrotWidget::zoom(double zoomFactor)
225{
226 curScale *= zoomFactor;
227 update();
228 thread.render(centerX, centerY, curScale, size());
229}
230//! [17]
231
232//! [18]
233void 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]
Note: See TracBrowser for help on using the repository browser.