ROOT  6.06/09
Reference Guide
main.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 Matthias Kretz <kretz@kde.org>
3 
4  Permission to use, copy, modify, and distribute this software
5  and its documentation for any purpose and without fee is hereby
6  granted, provided that the above copyright notice appear in all
7  copies and that both that the copyright notice and this
8  permission notice and warranty disclaimer appear in supporting
9  documentation, and that the name of the author not be used in
10  advertising or publicity pertaining to distribution of the
11  software without specific, written prior permission.
12 
13  The author disclaim all warranties with regard to this
14  software, including all implied warranties of merchantability
15  and fitness. In no event shall the author be liable for any
16  special, indirect or consequential damages or any damages
17  whatsoever resulting from loss of use, data or profits, whether
18  in an action of contract, negligence or other tortious action,
19  arising out of or in connection with the use or performance of
20  this software.
21 
22 */
23 
24 #include "main.h"
25 
26 #include <QApplication>
27 #include <QPainter>
28 //#include <QProgressBar>
29 
30 MainWindow::MainWindow(QWidget *_parent)
31  : QWidget(_parent),
32  m_scale(0.01f)
33 {
34  m_x = width() * m_scale * -0.667f;
35  m_y = height() * m_scale * -0.5f;
36 
37  m_rect1 = m_rect2 = rect();
38  m_rect1.setWidth(m_rect1.width() / 2);
39  m_rect2.setX(m_rect1.width());
40 
41  qRegisterMetaType<QImage>();
42  qRegisterMetaType<quint64>();
43  connect(&m_mandelVc, SIGNAL(ready(QImage, quint64)), SLOT(vcImage(QImage, quint64)));
44  connect(&m_mandelScalar, SIGNAL(ready(QImage, quint64)), SLOT(scalarImage(QImage, quint64)));
45 
46  setWindowTitle(tr("Mandelbrot"));
47  setCursor(Qt::CrossCursor);
48 }
49 
50 void MainWindow::vcImage(const QImage &img, quint64 cycles)
51 {
52  m_img1 = img;
53  update(m_rect1);
54  if (cycles > 1) {
55  m_cycles1 = cycles;
56  updateTitle();
57  }
58  if (QCoreApplication::arguments().contains("--benchmark")) {
60  }
61 }
62 
63 void MainWindow::scalarImage(const QImage &img, quint64 cycles)
64 {
65  m_img2 = img;
66  update(m_rect2);
67  if (cycles > 1) {
68  m_cycles2 = cycles;
69  updateTitle();
70  }
71 }
72 
74 {
75  setWindowTitle(tr("Mandelbrot [Speedup: %1] [%2]").arg(m_cycles2 / m_cycles1).arg(m_img1 == m_img2 ? "Equal" : "Not Equal"));
76 }
77 
78 void MainWindow::paintEvent(QPaintEvent *e)
79 {
80  QPainter p(this);
81  QRect r1 = m_rect1 & e->rect();
82  p.drawImage(r1, m_img1, r1.translated(m_dragDelta));
83  QRect r2 = m_rect2 & e->rect();
84  p.drawImage(r2, m_img2, QRect(QPoint(), r2.size()).translated(m_dragDelta));
85 }
86 
87 void MainWindow::mousePressEvent(QMouseEvent *e)
88 {
89  m_dragStart = e->pos();
90 }
91 
92 void MainWindow::mouseMoveEvent(QMouseEvent *e)
93 {
94  m_dragDelta = m_dragStart - e->pos();
95  update();
96 }
97 
98 void MainWindow::mouseReleaseEvent(QMouseEvent *e)
99 {
100  m_dragDelta = m_dragStart - e->pos();
101  // translate m_x, m_y accordingly and recreate the image
102  m_x += m_dragDelta.x() * m_scale;
103  m_y += m_dragDelta.y() * m_scale;
104  recreateImage();
105  m_dragDelta = QPoint();
106 }
107 
108 void MainWindow::wheelEvent(QWheelEvent *e)
109 {
110  if (e->delta() < 0 && width() * m_scale > 3.f && height() * m_scale > 2.f) {
111  return;
112  }
113  const float xx = e->x() >= m_rect1.width() ? e->x() - m_rect1.width() : e->x();
114  const float constX = m_x + m_scale * xx;
115  const float constY = m_y + m_scale * e->y();
116  if (e->delta() > 0) {
117  m_scale *= 1.f / (1.f + e->delta() * 0.001f);
118  } else {
119  m_scale *= 1.f - e->delta() * 0.001f;
120  }
121  m_x = constX - m_scale * xx;
122  m_y = constY - m_scale * e->y();
123  recreateImage();
124  //update();
125 }
126 
127 void MainWindow::resizeEvent(QResizeEvent *e)
128 {
129  if (e->oldSize().isValid()) {
130  m_x += 0.25f * m_scale * (e->oldSize().width() - e->size().width());
131  m_y += 0.5f * m_scale * (e->oldSize().height() - e->size().height());
132  } else {
133  m_x = e->size().width() * m_scale * -0.333f;
134  m_y = e->size().height() * m_scale * -0.5f;
135  }
136 
137  m_rect1 = m_rect2 = QRect(QPoint(), e->size());
138  m_rect1.setWidth(m_rect1.width() / 2);
139  m_rect2.setX(m_rect1.width());
140 
141  recreateImage();
142  update();
143 }
144 
146 {
147  if (!QCoreApplication::arguments().contains("--benchmark")) {
149  }
150  m_mandelVc.brot(m_rect1.size(), m_x, m_y, m_scale);
151 }
152 
153 int main(int argc, char **argv)
154 {
155  QApplication app(argc, argv);
156  MainWindow w;
157  w.resize(600, 200);
158  w.show();
159  return app.exec();
160 }
QRect m_rect2
Definition: main.h:64
Mandel< ScalarImpl > m_mandelScalar
Definition: main.h:71
void resizeEvent(QResizeEvent *)
Definition: main.cpp:127
void updateTitle()
Definition: main.cpp:73
float m_y
Definition: main.h:59
QImage m_img2
Definition: main.h:62
Mandel< VcImpl > m_mandelVc
Definition: main.h:70
QPoint m_dragDelta
Definition: main.h:66
float m_x
Definition: main.h:58
float m_cycles2
Definition: main.h:68
void mouseMoveEvent(QMouseEvent *)
Definition: main.cpp:92
MainWindow(QWidget *parent=0)
Definition: main.cpp:30
void wheelEvent(QWheelEvent *)
Definition: main.cpp:108
unsigned int r1[N_CITIES]
Definition: simanTSP.cxx:321
static void update(gsl_integration_workspace *workspace, double a1, double b1, double area1, double error1, double a2, double b2, double area2, double error2)
QRect m_rect1
Definition: main.h:63
document ready(function(){if(document.title=="Home"){$('div#header-container').append("<a href='terminals/1' class='btn btn-default btn-sm navbar-btn pull-right' style='margin-right: 4px; margin-left: 2px;'>Terminal</a>");}})
float m_cycles1
Definition: main.h:68
void recreateImage()
Definition: main.cpp:145
void mousePressEvent(QMouseEvent *)
Definition: main.cpp:87
double f(double x)
int main(int argc, char **argv)
Main program.
Definition: main.cpp:22
void vcImage(const QImage &, quint64)
Definition: main.cpp:50
void scalarImage(const QImage &, quint64)
Definition: main.cpp:63
void mouseReleaseEvent(QMouseEvent *)
Definition: main.cpp:98
void brot(const QSize &size, float x, float y, float scale)
Definition: mandel.cpp:59
QImage m_img1
Definition: main.h:61
QPoint m_dragStart
Definition: main.h:65
float m_scale
Definition: main.h:60
unsigned int r2[N_CITIES]
Definition: simanTSP.cxx:322
void paintEvent(QPaintEvent *)
Definition: main.cpp:78