main.cpp 4.6 KB

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