Almond.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include "Almond.h"
  2. #include <QIntValidator>
  3. #include <QFileDialog>
  4. #include <QMessageBox>
  5. #include <QGradient>
  6. #include "gradientchoosedialog.h"
  7. #include <cmath>
  8. Almond::Almond(QWidget* parent) :
  9. QMainWindow{ parent },
  10. mandelContext{ mnd::initializeContext() }
  11. {
  12. ui.setupUi(this);
  13. mw = std::make_unique<MandelWidget>(mandelContext,
  14. &mandelContext.getDefaultGenerator(),
  15. ui.centralWidget);
  16. on_maxIterations_editingFinished();
  17. mw->setSmoothColoring(ui.smooth->isChecked());
  18. mw->getMandelInfo().julia = true;
  19. mw->getMandelInfo().juliaX = 0.2;
  20. mw->getMandelInfo().juliaY = -0.4;
  21. currentView = MANDELBROT;
  22. mandelGeneratorSave = &mandelContext.getDefaultGenerator();
  23. mandelViewSave = mw->getViewport();
  24. QObject::connect(mw.get(), &MandelWidget::pointSelected, this, &Almond::pointSelected);
  25. ui.mainContainer->addWidget(mw.get());
  26. ui.maxIterations->setValidator(new QIntValidator(1, 1000000000, this));
  27. ui.backgroundProgress->setVisible(false);
  28. //ui.verticalLayout_left->addWidget(new MyGLWidget(ui.centralWidget));
  29. //mw->show();
  30. }
  31. Almond::~Almond(void)
  32. {
  33. }
  34. void Almond::submitBackgroundTask(BackgroundTask* task)
  35. {
  36. bool taken = QThreadPool::globalInstance()->tryTake(task->getRunnable());
  37. if (taken) {
  38. ui.backgroundProgress->setRange(0, 0);
  39. ui.backgroundProgress->setVisible(true);
  40. }
  41. }
  42. void Almond::backgroundTaskFinished(void)
  43. {
  44. }
  45. void Almond::on_zoom_out_clicked()
  46. {
  47. mw->zoom(2);
  48. }
  49. void Almond::on_zoom_in_clicked()
  50. {
  51. mw->zoom(0.5);
  52. }
  53. void Almond::on_maxIterations_editingFinished()
  54. {
  55. QString text = ui.maxIterations->text();
  56. int maxIter = text.toInt();
  57. mw->setMaxIterations(maxIter);
  58. }
  59. void Almond::on_chooseGradient_clicked()
  60. {
  61. auto response = gcd.exec();
  62. auto gradient = gcd.getGradient();
  63. if (gradient)
  64. mw->setGradient(std::move(*gradient));
  65. }
  66. void Almond::on_exportVideo_clicked()
  67. {
  68. ExportVideoInfo evi;
  69. evi.start = mnd::MandelViewport::standardView();
  70. evi.end = mw->getViewport();
  71. evi.gradient = mw->getGradient();
  72. ExportVideoDialog dialog(this, evi);
  73. //dialog.show();
  74. auto response = dialog.exec();
  75. printf("dialog executed\n"); fflush(stdout);
  76. if (response == 1) {
  77. mnd::MandelInfo mi;
  78. evi = dialog.getExportVideoInfo();
  79. //Video
  80. /*mi.maxIter = dialog.getMaxIterations();
  81. mi.view = mw->getViewport();
  82. mi.bWidth = dialog.getWidth();
  83. mi.bHeight = dialog.getHeight();
  84. mi.view.adjustAspectRatio(mi.bWidth, mi.bHeight);
  85. mnd::Generator& g = mandelContext.getDefaultGenerator();
  86. auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  87. g.generate(mi, fmap.pixels.get());
  88. auto bitmap = fmap.map<RGBColor>([](float i) { return i < 0 ? RGBColor{ 0,0,0 } : RGBColor{ uint8_t(cos(i * 0.015f) * 127 + 127), uint8_t(sin(i * 0.01f) * 127 + 127), uint8_t(i) }; });//uint8_t(::sin(i * 0.01f) * 100 + 100), uint8_t(i) }; });
  89. QImage img((unsigned char*)bitmap.pixels.get(), bitmap.width, bitmap.height, bitmap.width * 3, QImage::Format_RGB888);
  90. img.save(dialog.getPath());*/
  91. }
  92. }
  93. void Almond::on_smooth_stateChanged(int checked)
  94. {
  95. this->mw->setSmoothColoring(checked != Qt::Unchecked);
  96. }
  97. void Almond::on_exportImage_clicked()
  98. {
  99. ExportImageDialog dialog(this);
  100. dialog.setMaxIterations(mw->getMaxIterations());
  101. //dialog.show();
  102. auto response = dialog.exec();
  103. if (response == 1) {
  104. mnd::MandelInfo mi;
  105. mi.maxIter = dialog.getMaxIterations();
  106. mi.view = mw->getViewport();
  107. mi.bWidth = dialog.getWidth();
  108. mi.bHeight = dialog.getHeight();
  109. mi.view.adjustAspectRatio(mi.bWidth, mi.bHeight);
  110. mi.smooth = mw->getSmoothColoring();
  111. if (currentView == JULIA) {
  112. mi.julia = mw->getMandelInfo().julia;
  113. mi.juliaX = mw->getJuliaX();
  114. mi.juliaY = mw->getJuliaY();
  115. }
  116. mnd::MandelGenerator* currentGenerator = mw->getGenerator();
  117. mnd::MandelGenerator& g = currentGenerator ? *currentGenerator : mandelContext.getDefaultGenerator();
  118. auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  119. g.generate(mi, fmap.pixels.get());
  120. auto bitmap = fmap.map<RGBColor>([&mi, this] (float i) {
  121. return i >= mi.maxIter ? RGBColor{ 0,0,0 } : mw->getGradient().get(i);
  122. });
  123. QImage img(reinterpret_cast<unsigned char*>(bitmap.pixels.get()), bitmap.width, bitmap.height, bitmap.width * 3, QImage::Format_RGB888);
  124. img.save(dialog.getPath());
  125. }
  126. }
  127. void Almond::on_resetZoom_clicked()
  128. {
  129. mw->setViewport(mnd::MandelViewport::standardView());
  130. }
  131. void Almond::on_displayInfo_stateChanged(int checked)
  132. {
  133. this->mw->setDisplayInfo(checked != Qt::Unchecked);
  134. }
  135. void Almond::on_chooseGenerator_clicked()
  136. {
  137. if (!generatorsDialog)
  138. generatorsDialog = std::make_unique<ChooseGenerators>(mandelContext, this);
  139. generatorsDialog->exec();
  140. if (generatorsDialog->getChosenGenerator()) {
  141. mandelGeneratorSave = generatorsDialog->getChosenGenerator();
  142. }
  143. else {
  144. mandelGeneratorSave = &mandelContext.getDefaultGenerator();
  145. }
  146. //this->currentView = MANDELBROT;
  147. this->mw->setGenerator(mandelGeneratorSave);
  148. //this->mw->getMandelInfo().julia = false;
  149. printf("dialog executed\n"); fflush(stdout);
  150. }
  151. void Almond::on_selectPoint_clicked()
  152. {
  153. if (currentView == MANDELBROT) {
  154. emit this->mw->selectPoint();
  155. }
  156. }
  157. void Almond::pointSelected(mnd::Real x, mnd::Real y)
  158. {
  159. if (currentView != JULIA) {
  160. //auto& gen = mandelContext.getJuliaGenerator();
  161. mandelViewSave = mw->getViewport();
  162. //this->mw->setGenerator(&gen);
  163. this->mw->setViewport(mnd::MandelViewport::centerView());
  164. this->mw->setJuliaPos(x, y);
  165. this->mw->getMandelInfo().julia = true;
  166. this->mw->clearAll();
  167. }
  168. currentView = JULIA;
  169. }
  170. void Almond::on_viewMandelbrot_clicked()
  171. {
  172. if (currentView != MANDELBROT) {
  173. //this->mw->setGenerator(mandelGeneratorSave);
  174. this->mw->setViewport(mandelViewSave);
  175. this->mw->getMandelInfo().julia = false;
  176. this->mw->clearAll();
  177. currentView = MANDELBROT;
  178. }
  179. }