MandelWidget.h 2.7 KB

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