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 "mandelbrotwidget.h"
52
53#include <QPainter>
54#include <QKeyEvent>
55
56#include <math.h>
57
58//! [0]
59const double DefaultCenterX = -0.637011;
60const double DefaultCenterY = -0.0395159;
61const double DefaultScale = 0.00403897;
62
63const double ZoomInFactor = 0.8;
64const double ZoomOutFactor = 1 / ZoomInFactor;
65const int ScrollStep = 20;
66//! [0]
67
68//! [1]
69MandelbrotWidget::MandelbrotWidget(QWidget *parent) :
70 QWidget(parent),
71 centerX(DefaultCenterX),
72 centerY(DefaultCenterY),
73 pixmapScale(DefaultScale),
74 curScale(DefaultScale)
75{
76 connect(sender: &thread, signal: &RenderThread::renderedImage,
77 receiver: this, slot: &MandelbrotWidget::updatePixmap);
78
79 setWindowTitle(tr(s: "Mandelbrot"));
80#if QT_CONFIG(cursor)
81 setCursor(Qt::CrossCursor);
82#endif
83 resize(w: 550, h: 400);
84
85}
86//! [1]
87
88//! [2]
89void MandelbrotWidget::paintEvent(QPaintEvent * /* event */)
90{
91 QPainter painter(this);
92 painter.fillRect(r: rect(), c: Qt::black);
93
94 if (pixmap.isNull()) {
95 painter.setPen(Qt::white);
96 painter.drawText(r: rect(), flags: Qt::AlignCenter, text: tr(s: "Rendering initial image, please wait..."));
97//! [2] //! [3]
98 return;
99//! [3] //! [4]
100 }
101//! [4]
102
103//! [5]
104 if (qFuzzyCompare(p1: curScale, p2: pixmapScale)) {
105//! [5] //! [6]
106 painter.drawPixmap(p: pixmapOffset, pm: pixmap);
107//! [6] //! [7]
108 } else {
109//! [7] //! [8]
110 auto previewPixmap = qFuzzyCompare(p1: pixmap.devicePixelRatioF(), p2: qreal(1))
111 ? pixmap
112 : pixmap.scaled(s: pixmap.size() / pixmap.devicePixelRatioF(), aspectMode: Qt::KeepAspectRatio,
113 mode: Qt::SmoothTransformation);
114 double scaleFactor = pixmapScale / curScale;
115 int newWidth = int(previewPixmap.width() * scaleFactor);
116 int newHeight = int(previewPixmap.height() * scaleFactor);
117 int newX = pixmapOffset.x() + (previewPixmap.width() - newWidth) / 2;
118 int newY = pixmapOffset.y() + (previewPixmap.height() - newHeight) / 2;
119
120 painter.save();
121 painter.translate(dx: newX, dy: newY);
122 painter.scale(sx: scaleFactor, sy: scaleFactor);
123
124 QRectF exposed = painter.transform().inverted().mapRect(rect()).adjusted(xp1: -1, yp1: -1, xp2: 1, yp2: 1);
125 painter.drawPixmap(targetRect: exposed, pixmap: previewPixmap, sourceRect: exposed);
126 painter.restore();
127 }
128//! [8] //! [9]
129
130 QString text = tr(s: "Use mouse wheel or the '+' and '-' keys to zoom. "
131 "Press and hold left mouse button to scroll.");
132 QFontMetrics metrics = painter.fontMetrics();
133 int textWidth = metrics.horizontalAdvance(text);
134
135 painter.setPen(Qt::NoPen);
136 painter.setBrush(QColor(0, 0, 0, 127));
137 painter.drawRect(x: (width() - textWidth) / 2 - 5, y: 0, w: textWidth + 10, h: metrics.lineSpacing() + 5);
138 painter.setPen(Qt::white);
139 painter.drawText(x: (width() - textWidth) / 2, y: metrics.leading() + metrics.ascent(), s: text);
140}
141//! [9]
142
143//! [10]
144void MandelbrotWidget::resizeEvent(QResizeEvent * /* event */)
145{
146 thread.render(centerX, centerY, scaleFactor: curScale, resultSize: size(), devicePixelRatio: devicePixelRatioF());
147}
148//! [10]
149
150//! [11]
151void MandelbrotWidget::keyPressEvent(QKeyEvent *event)
152{
153 switch (event->key()) {
154 case Qt::Key_Plus:
155 zoom(zoomFactor: ZoomInFactor);
156 break;
157 case Qt::Key_Minus:
158 zoom(zoomFactor: ZoomOutFactor);
159 break;
160 case Qt::Key_Left:
161 scroll(deltaX: -ScrollStep, deltaY: 0);
162 break;
163 case Qt::Key_Right:
164 scroll(deltaX: +ScrollStep, deltaY: 0);
165 break;
166 case Qt::Key_Down:
167 scroll(deltaX: 0, deltaY: -ScrollStep);
168 break;
169 case Qt::Key_Up:
170 scroll(deltaX: 0, deltaY: +ScrollStep);
171 break;
172 default:
173 QWidget::keyPressEvent(event);
174 }
175}
176//! [11]
177
178#if QT_CONFIG(wheelevent)
179//! [12]
180void MandelbrotWidget::wheelEvent(QWheelEvent *event)
181{
182 const int numDegrees = event->angleDelta().y() / 8;
183 const double numSteps = numDegrees / double(15);
184 zoom(zoomFactor: pow(x: ZoomInFactor, y: numSteps));
185}
186//! [12]
187#endif
188
189//! [13]
190void MandelbrotWidget::mousePressEvent(QMouseEvent *event)
191{
192 if (event->button() == Qt::LeftButton)
193 lastDragPos = event->pos();
194}
195//! [13]
196
197//! [14]
198void MandelbrotWidget::mouseMoveEvent(QMouseEvent *event)
199{
200 if (event->buttons() & Qt::LeftButton) {
201 pixmapOffset += event->pos() - lastDragPos;
202 lastDragPos = event->pos();
203 update();
204 }
205}
206//! [14]
207
208//! [15]
209void MandelbrotWidget::mouseReleaseEvent(QMouseEvent *event)
210{
211 if (event->button() == Qt::LeftButton) {
212 pixmapOffset += event->pos() - lastDragPos;
213 lastDragPos = QPoint();
214
215 const auto pixmapSize = pixmap.size() / pixmap.devicePixelRatioF();
216 int deltaX = (width() - pixmapSize.width()) / 2 - pixmapOffset.x();
217 int deltaY = (height() - pixmapSize.height()) / 2 - pixmapOffset.y();
218 scroll(deltaX, deltaY);
219 }
220}
221//! [15]
222
223//! [16]
224void MandelbrotWidget::updatePixmap(const QImage &image, double scaleFactor)
225{
226 if (!lastDragPos.isNull())
227 return;
228
229 pixmap = QPixmap::fromImage(image);
230 pixmapOffset = QPoint();
231 lastDragPos = QPoint();
232 pixmapScale = scaleFactor;
233 update();
234}
235//! [16]
236
237//! [17]
238void MandelbrotWidget::zoom(double zoomFactor)
239{
240 curScale *= zoomFactor;
241 update();
242 thread.render(centerX, centerY, scaleFactor: curScale, resultSize: size(), devicePixelRatio: devicePixelRatioF());
243}
244//! [17]
245
246//! [18]
247void MandelbrotWidget::scroll(int deltaX, int deltaY)
248{
249 centerX += deltaX * curScale;
250 centerY += deltaY * curScale;
251 update();
252 thread.render(centerX, centerY, scaleFactor: curScale, resultSize: size(), devicePixelRatio: devicePixelRatioF());
253}
254//! [18]
255

source code of qtbase/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp