MandelWidget.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. class MandelView : public QObject
  35. {
  36. Q_OBJECT
  37. private:
  38. std::future<void> calc;
  39. QThread calcThread;
  40. std::mutex mut;
  41. std::condition_variable condVar;
  42. std::atomic<mnd::MandelInfo> toCalc;
  43. std::atomic_bool hasToCalc;
  44. std::atomic_bool finish;
  45. mnd::Generator* generator;
  46. Gradient& gradient;
  47. MandelWidget* mWidget;
  48. //QOpenGLContext* context;
  49. public:
  50. MandelView(mnd::Generator& generator, Gradient& gradient, MandelWidget* mWidget);
  51. ~MandelView(void);
  52. void setGenerator(mnd::Generator &value);
  53. void start();
  54. private slots:
  55. void loop();
  56. public slots:
  57. void adaptViewport(const mnd::MandelInfo vp);
  58. signals:
  59. void updated(Bitmap<RGBColor>* bitmap);
  60. };
  61. class MandelWidget : public QGLWidget
  62. {
  63. Q_OBJECT
  64. private:
  65. //QScrollArea qsa;
  66. //QLabel ql;
  67. mnd::MandelContext& mndContext;
  68. Gradient gradient;
  69. bool initialized = false;
  70. int maxIterations = 2000;
  71. bool rubberbandDragging = false;
  72. QRectF rubberband;
  73. std::unique_ptr<Texture> tex;
  74. mnd::MandelViewport viewport;
  75. MandelView mv;
  76. public:
  77. MandelWidget(mnd::MandelContext& ctxt, QWidget* parent = nullptr);
  78. ~MandelWidget(void) override;
  79. /*inline MandelWidget(const MandelWidget& other) :
  80. mndContext{ other.mndContext },
  81. mv{ other.mndContext }
  82. {
  83. }*/
  84. inline const Gradient& getGradient(void) const { return gradient; }
  85. void initializeGL(void) override;
  86. void resizeGL(int w, int h) override;
  87. void paintGL() override;
  88. void drawRubberband(void);
  89. void zoom(float scale);
  90. void setMaxIterations(int maxIter);
  91. //void redraw();
  92. void requestRecalc(void);
  93. void resizeEvent(QResizeEvent* re) override;
  94. void mousePressEvent(QMouseEvent* me) override;
  95. void mouseMoveEvent(QMouseEvent* me) override;
  96. void mouseReleaseEvent(QMouseEvent* me) override;
  97. inline const mnd::MandelViewport& getViewport(void) const { return viewport; }
  98. signals:
  99. void needsUpdate(const mnd::MandelInfo vp);
  100. public slots:
  101. void viewUpdated(Bitmap<RGBColor>* bitmap);
  102. };