~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to examples/threads/mandelbrot/mandelbrotwidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the example classes of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <QtGui>
 
30
 
 
31
#include <math.h>
 
32
 
 
33
#include "mandelbrotwidget.h"
 
34
 
 
35
const double DefaultCenterX = -0.637011f;
 
36
const double DefaultCenterY = -0.0395159f;
 
37
const double DefaultScale = 0.00403897f;
 
38
 
 
39
const double ZoomInFactor = 0.8f;
 
40
const double ZoomOutFactor = 1 / ZoomInFactor;
 
41
const int ScrollStep = 20;
 
42
 
 
43
MandelbrotWidget::MandelbrotWidget(QWidget *parent)
 
44
    : QWidget(parent)
 
45
{
 
46
    centerX = DefaultCenterX;
 
47
    centerY = DefaultCenterY;
 
48
    pixmapScale = DefaultScale;
 
49
    curScale = DefaultScale;
 
50
 
 
51
    qRegisterMetaType<QImage>("QImage");
 
52
    connect(&thread, SIGNAL(renderedImage(const QImage &, double)),
 
53
            this, SLOT(updatePixmap(const QImage &, double)));
 
54
 
 
55
    setWindowTitle(tr("Mandelbrot"));
 
56
    setCursor(Qt::CrossCursor);
 
57
    resize(550, 400);
 
58
}
 
59
 
 
60
void MandelbrotWidget::paintEvent(QPaintEvent * /* event */)
 
61
{
 
62
    QPainter painter(this);
 
63
    painter.fillRect(rect(), Qt::black);
 
64
 
 
65
    if (pixmap.isNull()) {
 
66
        painter.setPen(Qt::white);
 
67
        painter.drawText(rect(), Qt::AlignCenter,
 
68
                         tr("Rendering initial image, please wait..."));
 
69
        return;
 
70
    }
 
71
 
 
72
    if (curScale == pixmapScale) {
 
73
        painter.drawPixmap(pixmapOffset, pixmap);
 
74
    } else {
 
75
        double scaleFactor = pixmapScale / curScale;
 
76
        int newWidth = int(pixmap.width() * scaleFactor);
 
77
        int newHeight = int(pixmap.height() * scaleFactor);
 
78
        int newX = pixmapOffset.x() + (pixmap.width() - newWidth) / 2;
 
79
        int newY = pixmapOffset.y() + (pixmap.height() - newHeight) / 2;
 
80
 
 
81
        painter.save();
 
82
        painter.translate(newX, newY);
 
83
        painter.scale(scaleFactor, scaleFactor);
 
84
        painter.drawPixmap(0, 0, pixmap);
 
85
        painter.restore();
 
86
    }
 
87
 
 
88
    QString text = tr("Use mouse wheel to zoom. "
 
89
                      "Press and hold left mouse button to scroll.");
 
90
    QFontMetrics metrics = painter.fontMetrics();
 
91
    int textWidth = metrics.width(text);
 
92
 
 
93
    painter.setPen(Qt::NoPen);
 
94
    painter.setBrush(QColor(0, 0, 0, 127));
 
95
    painter.drawRect((width() - textWidth) / 2 - 5, 0, textWidth + 10,
 
96
                     metrics.lineSpacing() + 5);
 
97
    painter.setPen(Qt::white);
 
98
    painter.drawText((width() - textWidth) / 2,
 
99
                     metrics.leading() + metrics.ascent(), text);
 
100
}
 
101
 
 
102
void MandelbrotWidget::resizeEvent(QResizeEvent * /* event */)
 
103
{
 
104
    thread.render(centerX, centerY, curScale, size());
 
105
}
 
106
 
 
107
void MandelbrotWidget::keyPressEvent(QKeyEvent *event)
 
108
{
 
109
    switch (event->key()) {
 
110
    case Qt::Key_Plus:
 
111
        zoom(ZoomInFactor);
 
112
        break;
 
113
    case Qt::Key_Minus:
 
114
        zoom(ZoomOutFactor);
 
115
        break;
 
116
    case Qt::Key_Left:
 
117
        scroll(-ScrollStep, 0);
 
118
        break;
 
119
    case Qt::Key_Right:
 
120
        scroll(+ScrollStep, 0);
 
121
        break;
 
122
    case Qt::Key_Down:
 
123
        scroll(0, -ScrollStep);
 
124
        break;
 
125
    case Qt::Key_Up:
 
126
        scroll(0, +ScrollStep);
 
127
        break;
 
128
    default:
 
129
        QWidget::keyPressEvent(event);
 
130
    }
 
131
}
 
132
 
 
133
void MandelbrotWidget::wheelEvent(QWheelEvent *event)
 
134
{
 
135
    int numDegrees = event->delta() / 8;
 
136
    double numSteps = numDegrees / 15.0f;
 
137
    zoom(pow(ZoomInFactor, numSteps));
 
138
}
 
139
 
 
140
void MandelbrotWidget::mousePressEvent(QMouseEvent *event)
 
141
{
 
142
    if (event->button() == Qt::LeftButton)
 
143
        lastDragPos = event->pos();
 
144
}
 
145
 
 
146
void MandelbrotWidget::mouseMoveEvent(QMouseEvent *event)
 
147
{
 
148
    if (event->buttons() & Qt::LeftButton) {
 
149
        pixmapOffset += event->pos() - lastDragPos;
 
150
        lastDragPos = event->pos();
 
151
        update();
 
152
    }
 
153
}
 
154
 
 
155
void MandelbrotWidget::mouseReleaseEvent(QMouseEvent *event)
 
156
{
 
157
    if (event->button() == Qt::LeftButton) {
 
158
        pixmapOffset += event->pos() - lastDragPos;
 
159
        lastDragPos = QPoint();
 
160
 
 
161
        int deltaX = (width() - pixmap.width()) / 2 - pixmapOffset.x();
 
162
        int deltaY = (height() - pixmap.height()) / 2 - pixmapOffset.y();
 
163
        scroll(deltaX, deltaY);
 
164
    }
 
165
}
 
166
 
 
167
void MandelbrotWidget::updatePixmap(const QImage &image, double scaleFactor)
 
168
{
 
169
    if (!lastDragPos.isNull())
 
170
        return;
 
171
 
 
172
    pixmap = QPixmap::fromImage(image);
 
173
    pixmapOffset = QPoint();
 
174
    lastDragPos = QPoint();
 
175
    pixmapScale = scaleFactor;
 
176
    update();
 
177
}
 
178
 
 
179
void MandelbrotWidget::zoom(double zoomFactor)
 
180
{
 
181
    curScale *= zoomFactor;
 
182
    update();
 
183
    thread.render(centerX, centerY, curScale, size());
 
184
}
 
185
 
 
186
void MandelbrotWidget::scroll(int deltaX, int deltaY)
 
187
{
 
188
    centerX += deltaX * curScale;
 
189
    centerY += deltaY * curScale;
 
190
    update();
 
191
    thread.render(centerX, centerY, curScale, size());
 
192
}