Almond.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. customGeneratorDialog = std::make_unique<CustomGenerator>(mandelContext);
  17. customGenerator = nullptr;
  18. customViewSave = mnd::MandelViewport::centerView();
  19. on_maxIterations_editingFinished();
  20. mw->setSmoothColoring(ui.smooth->isChecked());
  21. currentView = MANDELBROT;
  22. mandelGenerator = &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. 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. if (currentView == MANDELBROT) {
  130. mw->setViewport(mnd::MandelViewport::standardView());
  131. }
  132. else {
  133. mw->setViewport(mnd::MandelViewport::centerView());
  134. }
  135. }
  136. void Almond::on_displayInfo_stateChanged(int checked)
  137. {
  138. this->mw->setDisplayInfo(checked != Qt::Unchecked);
  139. }
  140. void Almond::on_chooseGenerator_clicked()
  141. {
  142. std::unique_ptr<ChooseGenerators> generatorsDialog;
  143. if (currentView == MANDELBROT || currentView == JULIA)
  144. generatorsDialog = std::make_unique<ChooseGenerators>(mandelContext, *mandelGenerator, *this);
  145. else if (currentView == CUSTOM)
  146. generatorsDialog = std::make_unique<ChooseGenerators>(mandelContext, this->currentCustom->gc, *customGenerator, *this);
  147. else
  148. return;
  149. auto response = generatorsDialog->exec();
  150. auto gen = generatorsDialog->extractChosenGenerator();
  151. if (gen) {
  152. if (currentView == MANDELBROT || currentView == JULIA) {
  153. mandelGenerator = gen.get();
  154. }
  155. else if (currentView == CUSTOM) {
  156. customGenerator = gen.get();
  157. }
  158. currentGenerator = gen.get();
  159. this->mw->setGenerator(currentGenerator);
  160. adjustedGenerators.push_back(std::move(gen));
  161. }
  162. else {
  163. //mandelGenerator = &mandelContext.getDefaultGenerator();
  164. }
  165. //this->currentView = MANDELBROT;
  166. //this->mw->getMandelInfo().julia = false;
  167. //printf("dialog executed\n"); fflush(stdout);
  168. }
  169. void Almond::pointSelected(mnd::Real x, mnd::Real y)
  170. {
  171. if (currentView != JULIA) {
  172. saveView();
  173. this->mw->setViewport(mnd::MandelViewport::centerView());
  174. this->mw->setJuliaPos(x, y);
  175. this->mw->getMandelInfo().julia = true;
  176. this->mw->clearAll();
  177. }
  178. currentView = JULIA;
  179. }
  180. void Almond::on_groupBox_toggled(bool arg1)
  181. {
  182. printf("arg1: %i\n", int(arg1)); fflush(stdout);
  183. }
  184. void Almond::on_wMandel_clicked()
  185. {
  186. }
  187. void Almond::saveView()
  188. {
  189. if (currentView == MANDELBROT)
  190. mandelViewSave = mw->getViewport();
  191. else if (currentView == CUSTOM)
  192. customViewSave = mw->getViewport();
  193. }
  194. void Almond::setViewType(ViewType v)
  195. {
  196. saveView();
  197. if (v == MANDELBROT) {
  198. currentGenerator = mandelGenerator;
  199. emit this->mw->stopSelectingPoint();
  200. this->mw->setViewport(mandelViewSave);
  201. this->mw->setGenerator(currentGenerator);
  202. this->mw->getMandelInfo().julia = false;
  203. this->mw->clearAll();
  204. currentView = MANDELBROT;
  205. }
  206. else if (v == CUSTOM) {
  207. if (customGenerator != nullptr) {
  208. currentGenerator = customGenerator;
  209. this->mw->setGenerator(currentGenerator);
  210. emit this->mw->stopSelectingPoint();
  211. this->mw->setViewport(customViewSave);
  212. this->mw->getMandelInfo().julia = false;
  213. this->mw->clearAll();
  214. currentView = CUSTOM;
  215. }
  216. else {
  217. setViewType(MANDELBROT);
  218. }
  219. }
  220. else if (v == JULIA) {
  221. if (currentView == MANDELBROT) {
  222. emit this->mw->selectPoint();
  223. }
  224. else {
  225. currentView = MANDELBROT;
  226. currentGenerator = mandelGenerator;
  227. this->mw->setGenerator(currentGenerator);
  228. this->mw->setViewport(mandelViewSave);
  229. this->mw->getMandelInfo().julia = false;
  230. this->mw->clearAll();
  231. emit this->mw->selectPoint();
  232. }
  233. }
  234. }
  235. void Almond::on_wMandel_toggled(bool checked)
  236. {
  237. if (checked)
  238. setViewType(MANDELBROT);
  239. }
  240. void Almond::on_radioButton_toggled(bool checked)
  241. {
  242. saveView();
  243. if (checked) {
  244. setViewType(JULIA);
  245. }
  246. }
  247. void Almond::on_radioButton_2_toggled(bool checked)
  248. {
  249. saveView();
  250. if (checked) {
  251. if (customGenerator == nullptr) {
  252. customGeneratorDialog->exec();
  253. if (auto* frac = customGeneratorDialog->getLastCompiled()) {
  254. customGenerator = frac->gc.adaptiveGenerator.get();
  255. customGenerators.push_back(std::make_unique<FractalDef>(std::move(*frac)));
  256. currentCustom = customGenerators[customGenerators.size() - 1].get();
  257. }
  258. }
  259. setViewType(CUSTOM);
  260. }
  261. }
  262. void Almond::on_createCustom_clicked()
  263. {
  264. auto response = customGeneratorDialog->exec();
  265. if (response != 1)
  266. return;
  267. if (auto* frac = customGeneratorDialog->getLastCompiled()) {
  268. customGenerator = frac->gc.adaptiveGenerator.get();
  269. customGenerators.push_back(std::make_unique<FractalDef>(std::move(*frac)));
  270. currentCustom = customGenerators[customGenerators.size() - 1].get();
  271. this->ui.radioButton_2->setChecked(true);
  272. setViewType(CUSTOM);
  273. }
  274. }