FractalWidget.cpp 8.6 KB

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