FractalWidget.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #include "FractalWidget.h"
  2. #include <QMouseEvent>
  3. #include <QPainter>
  4. FractalWidget::FractalWidget(QWidget* parent) :
  5. FractalZoomWidget{ parent }
  6. {
  7. }
  8. void FractalWidget::mousePressEvent(QMouseEvent* me)
  9. {
  10. QOpenGLWidget::mousePressEvent(me);
  11. if (me->button() == Qt::RightButton) {
  12. rubberbanding = true;
  13. rubberband.setCoords(me->x(), me->y(), me->x(), me->y());
  14. update();
  15. me->accept();
  16. }
  17. else if (me->button() == Qt::LeftButton) {
  18. dragging = true;
  19. dragX = me->x();
  20. dragY = me->y();
  21. me->accept();
  22. }
  23. }
  24. void FractalWidget::mouseMoveEvent(QMouseEvent* me)
  25. {
  26. QOpenGLWidget::mouseMoveEvent(me);
  27. if (rubberbanding) {
  28. QRectF& rect = rubberband;
  29. double aspect = double(geometry().width()) / geometry().height();
  30. rect.setBottomRight(QPoint(me->x(), me->y()));
  31. if (rect.width() > rect.height() * aspect)
  32. rect.setHeight(rect.width() / aspect);
  33. else
  34. rect.setWidth(rect.height() * aspect);
  35. update();
  36. }
  37. else if (selectingPoint) {
  38. pointX = me->x();
  39. pointY = me->y();
  40. update();
  41. }
  42. else if (dragging) {
  43. double deltaX = me->x() - dragX;
  44. double deltaY = me->y() - dragY;
  45. auto& viewport = mandelInfo.view;
  46. viewport.x -= deltaX * viewport.width / this->width();
  47. viewport.y -= deltaY * viewport.height / this->height();
  48. targetViewport = viewport;
  49. dragX = me->x(); dragY = me->y();
  50. update();
  51. }
  52. me->accept();
  53. }
  54. void FractalWidget::mouseReleaseEvent(QMouseEvent* me)
  55. {
  56. QOpenGLWidget::mouseReleaseEvent(me);
  57. if (rubberbanding) {
  58. QRect rect = rubberband.toRect();
  59. if(rect.width() != 0 && rect.height() != 0) {
  60. QRect full = this->geometry();
  61. auto& viewport = targetViewport;
  62. viewport.x += mnd::Real(rect.left()) * viewport.width / full.width();
  63. viewport.y += mnd::Real(rect.top()) * viewport.height / full.height();
  64. viewport.width *= mnd::Real(rect.width()) / full.width();
  65. viewport.height *= mnd::Real(rect.height()) / full.height();
  66. viewport.normalize();
  67. viewport.adjustAspectRatio(getResolutionX(), getResolutionY());
  68. newAnimation();
  69. //currentViewport = viewport;
  70. }
  71. update();
  72. rubberbanding = false;
  73. }
  74. else if (selectingPoint) {
  75. selectingPoint = false;
  76. this->setMouseTracking(false);
  77. const auto& vp = getViewport();
  78. mnd::Real x = vp.x + vp.width * (float(me->pos().x()) / this->width());
  79. mnd::Real y = vp.y + vp.height * (float(me->pos().y()) / this->height());
  80. emit pointSelected(x, y);
  81. update();
  82. }
  83. dragging = false;
  84. }
  85. void FractalWidget::wheelEvent(QWheelEvent* we)
  86. {
  87. QOpenGLWidget::wheelEvent(we);
  88. float x = float(we->x()) / this->width();
  89. float y = float(we->y()) / this->height();
  90. float scale = ::powf(0.9975f, we->angleDelta().y());
  91. //mandelInfo.view.zoom(scale, x, y);
  92. zoom(scale, x, y);
  93. //if (!we->pixelDelta().isNull())
  94. // this->currentViewport = this->viewport;
  95. we->accept();
  96. }
  97. void FractalWidget::zoom(float factor)
  98. {
  99. targetViewport.zoomCenter(factor);
  100. newAnimation();
  101. update();
  102. }
  103. void FractalWidget::zoom(float factor, float fx, float fy)
  104. {
  105. targetViewport.zoom(factor, fx, fy);
  106. newAnimation();
  107. update();
  108. /*viewportSmoother = new ViewportAnimation(this);
  109. viewportSmoother->setStartValue(QVariant::fromValue(getViewport()));
  110. viewportSmoother->setEndValue(QVariant::fromValue(newVp));
  111. viewportSmoother->setTargetObject(this);
  112. viewportSmoother->setPropertyName("viewport");
  113. viewportSmoother->setDuration(200);
  114. viewportSmoother->setEasingCurve(QEasingCurve::OutExpo);
  115. viewportSmoother->start(QAbstractAnimation::DeletionPolicy::DeleteWhenStopped);*/
  116. }
  117. void FractalWidget::setViewport(const mnd::MandelViewport& viewport)
  118. {
  119. FractalZoomWidget::setViewport(viewport);
  120. targetViewport = mandelInfo.view;
  121. update();
  122. }
  123. const mnd::MandelViewport& FractalWidget::getViewport(void) const
  124. {
  125. return mandelInfo.view;
  126. }
  127. void FractalWidget::setDisplayInfo(bool displayInfo)
  128. {
  129. if (displayInfo != this->displayInfo) {
  130. this->displayInfo = displayInfo;
  131. update();
  132. }
  133. }
  134. void FractalWidget::selectJuliaPoint(void)
  135. {
  136. this->selectingPoint = true;
  137. this->setMouseTracking(true);
  138. update();
  139. }
  140. void FractalWidget::stopSelectingPoint(void)
  141. {
  142. this->selectingPoint = false;
  143. this->setMouseTracking(false);
  144. update();
  145. }
  146. void FractalWidget::resizeGL(int w, int h)
  147. {
  148. FractalZoomWidget::resizeGL(w, h);
  149. targetViewport.height = targetViewport.width * h / w;
  150. }
  151. void FractalWidget::paintGL(void)
  152. {
  153. updateAnimations();
  154. FractalZoomWidget::paintGL();
  155. EscapeTimeVisualWidget::drawJulia(0.3, 0.2);
  156. if (selectingPoint) {
  157. const auto& vp = getViewport();
  158. float jx = float(vp.x) + float(vp.width) * pointX / this->width();
  159. float jy = float(vp.y) + float(vp.height) * pointY / this->height();
  160. EscapeTimeVisualWidget::drawJulia(jx, jy);
  161. drawSelectingPoint();
  162. }
  163. if (rubberbanding)
  164. drawRubberband();
  165. if (displayInfo)
  166. drawDisplayInfo();
  167. }
  168. void FractalWidget::drawDisplayInfo(void)
  169. {
  170. QPainter infoPainter{ this };
  171. const float DIST_FROM_BORDER = 15;
  172. float maxWidth = this->width() - 2 * DIST_FROM_BORDER;
  173. mnd::Real distPerPixel = getViewport().width / this->width();
  174. float log10 = (mnd::convert<float>(mnd::log(distPerPixel)) + ::logf(maxWidth)) / ::logf(10);
  175. mnd::Real displayDist = mnd::pow(mnd::Real(10), ::floor(log10));
  176. float pixels = mnd::convert<float>(displayDist / distPerPixel);
  177. int factor = 1;
  178. for (int i = 9; i > 1; i--) {
  179. if (pixels * i < maxWidth) {
  180. factor *= i;
  181. pixels *= i;
  182. displayDist *= i;
  183. break;
  184. }
  185. }
  186. std::stringstream dis;
  187. if (::abs(log10) < 3) {
  188. dis << mnd::convert<float>(displayDist);
  189. }
  190. else {
  191. dis << factor << "e" << int(::floor(log10));
  192. }
  193. if (maxWidth > 400) {
  194. dis << "; per pixel: " << distPerPixel;
  195. }
  196. float lineY = this->height() - DIST_FROM_BORDER;
  197. float lineXEnd = DIST_FROM_BORDER + pixels;
  198. infoPainter.setPen(Qt::white);
  199. infoPainter.setFont(QFont("Arial", 12));
  200. infoPainter.drawLine(QPointF{ DIST_FROM_BORDER, lineY }, QPointF{ lineXEnd, lineY });
  201. infoPainter.drawLine(QPointF{ DIST_FROM_BORDER, lineY }, QPointF{ DIST_FROM_BORDER, lineY - 5 });
  202. infoPainter.drawLine(QPointF{ lineXEnd, lineY }, QPointF{ lineXEnd, lineY - 5 });
  203. infoPainter.drawText(int(DIST_FROM_BORDER), int(lineY - 20), int(lineXEnd - DIST_FROM_BORDER), 20,
  204. Qt::AlignCenter, QString::fromStdString(dis.str()));
  205. }
  206. void FractalWidget::drawSelectingPoint(void)
  207. {
  208. QPainter pointPainter{ this };
  209. pointPainter.setPen(QColor{ 255, 255, 255 });
  210. pointPainter.drawLine(0, pointY, width(), pointY);
  211. pointPainter.drawLine(pointX, 0, pointX, height());
  212. }
  213. void FractalWidget::drawRubberband(void)
  214. {
  215. QPainter rubberbandPainter{ this };
  216. rubberbandPainter.fillRect(rubberband, QColor{ 125, 140, 225, 120 });
  217. QPen pen{ QColor{ 100, 115, 200 } };
  218. pen.setWidth(2);
  219. rubberbandPainter.setPen(pen);
  220. rubberbandPainter.drawRect(rubberband);
  221. }
  222. void FractalWidget::newAnimation(void)
  223. {
  224. auto now = std::chrono::high_resolution_clock::now();
  225. lastAnimUpdate = now;
  226. }
  227. void FractalWidget::updateAnimations(void)
  228. {
  229. auto& currentViewport = mandelInfo.view;
  230. if (mnd::abs(currentViewport.width / targetViewport.width - 1.0) < 1e-3
  231. && mnd::abs(currentViewport.height / targetViewport.height - 1.0) < 1e-3) {
  232. // animation finished
  233. currentViewport = targetViewport;
  234. }
  235. else {
  236. auto now = std::chrono::high_resolution_clock::now();
  237. auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(now - lastAnimUpdate).count();
  238. const mnd::Real factor = mnd::Real(::pow(0.97, millis));
  239. const mnd::Real one(1.0);
  240. currentViewport.x = currentViewport.x * factor + targetViewport.x * (one - factor);
  241. currentViewport.y = currentViewport.y * factor + targetViewport.y * (one - factor);
  242. currentViewport.width = currentViewport.width * factor + targetViewport.width * (one - factor);
  243. currentViewport.height = currentViewport.height * factor + targetViewport.height * (one - factor);
  244. lastAnimUpdate = now;
  245. emit update();
  246. }
  247. }