FractalWidget.cpp 9.3 KB

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