1
0

FractalWidget.cpp 9.0 KB

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