MandelWidget.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #pragma once
  2. #include <QGLWidget>
  3. #include <QThread>
  4. #include <qopengl.h>
  5. #include <qopenglfunctions.h>
  6. #include <qopenglcontext.h>
  7. #include <qscrollarea.h>
  8. #include <qlabel.h>
  9. #include <qevent.h>
  10. #include <qrubberband.h>
  11. #include "Bitmap.h"
  12. #include "Gradient.h"
  13. #include <Mandel.h>
  14. #include <future>
  15. #include <thread>
  16. #include <mutex>
  17. #include <atomic>
  18. class MandelWidget;
  19. class Texture
  20. {
  21. GLuint id;
  22. QOpenGLContext* context;
  23. public:
  24. Texture(const Bitmap<RGBColor>& pict);
  25. Texture(const Bitmap<RGBColor>& pict, QOpenGLContext* context);
  26. ~Texture(void);
  27. Texture(const Texture& other) = delete;
  28. Texture& operator=(const Texture& other) = delete;
  29. Texture(Texture&& other) = default;
  30. Texture& operator=(Texture&& other) = default;
  31. void bind(void) const;
  32. void drawRect(float x, float y, float width, float height);
  33. };
  34. struct MandelClip
  35. {
  36. mnd::MandelViewport view;
  37. };
  38. class MandelView : public QObject
  39. {
  40. Q_OBJECT
  41. private:
  42. std::future<void> calc;
  43. QThread calcThread;
  44. std::mutex mut;
  45. std::condition_variable condVar;
  46. std::atomic<mnd::MandelInfo> toCalc;
  47. std::atomic_bool hasToCalc;
  48. std::atomic_bool finish;
  49. mnd::Generator* generator;
  50. Gradient& gradient;
  51. MandelWidget* mWidget;
  52. //QOpenGLContext* context;
  53. public:
  54. MandelView(mnd::Generator& generator, Gradient& gradient, MandelWidget* mWidget);
  55. ~MandelView(void);
  56. void setGenerator(mnd::Generator &value);
  57. void start();
  58. private slots:
  59. void loop();
  60. public slots:
  61. void adaptViewport(const mnd::MandelInfo vp);
  62. signals:
  63. void updated(Bitmap<RGBColor>* bitmap);
  64. };
  65. class MandelWidget : public QGLWidget
  66. {
  67. Q_OBJECT
  68. private:
  69. //QScrollArea qsa;
  70. //QLabel ql;
  71. mnd::MandelContext& mndContext;
  72. Gradient gradient;
  73. bool initialized = false;
  74. int maxIterations = 2000;
  75. bool rubberbandDragging = false;
  76. QRectF rubberband;
  77. std::unique_ptr<Texture> tex;
  78. mnd::MandelViewport viewport;
  79. MandelView mv;
  80. public:
  81. MandelWidget(mnd::MandelContext& ctxt, QWidget* parent = nullptr);
  82. ~MandelWidget(void) override;
  83. /*inline MandelWidget(const MandelWidget& other) :
  84. mndContext{ other.mndContext },
  85. mv{ other.mndContext }
  86. {
  87. }*/
  88. inline const Gradient& getGradient(void) const { return gradient; }
  89. void initializeGL(void) override;
  90. void resizeGL(int w, int h) override;
  91. void paintGL() override;
  92. void drawRubberband(void);
  93. void zoom(float scale);
  94. void setMaxIterations(int maxIter);
  95. //void redraw();
  96. void requestRecalc(void);
  97. void resizeEvent(QResizeEvent* re) override;
  98. void mousePressEvent(QMouseEvent* me) override;
  99. void mouseMoveEvent(QMouseEvent* me) override;
  100. void mouseReleaseEvent(QMouseEvent* me) override;
  101. inline const mnd::MandelViewport& getViewport(void) const { return viewport; }
  102. signals:
  103. void needsUpdate(const mnd::MandelInfo vp);
  104. public slots:
  105. void viewUpdated(Bitmap<RGBColor>* bitmap);
  106. };