Almond.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #include "Almond.h"
  2. #include <QIntValidator>
  3. #include <QFileDialog>
  4. #include <QMessageBox>
  5. #include <QGradient>
  6. #include "gradientchoosedialog.h"
  7. #include "ImageExport.h"
  8. #include <cmath>
  9. Almond::Almond(QWidget* parent) :
  10. QMainWindow{ parent },
  11. mandelContext{ mnd::initializeContext() }
  12. {
  13. ui.setupUi(this);
  14. mw = std::make_unique<MandelWidget>(mandelContext,
  15. &mandelContext.getDefaultGenerator(),
  16. ui.centralWidget);
  17. customGeneratorDialog = std::make_unique<CustomGenerator>(mandelContext);
  18. customGenerator = nullptr;
  19. customViewSave = mnd::MandelViewport::centerView();
  20. on_maxIterations_editingFinished();
  21. mw->setSmoothColoring(ui.smooth->isChecked());
  22. currentView = MANDELBROT;
  23. mandelGenerator = &mandelContext.getDefaultGenerator();
  24. mandelViewSave = mw->getViewport();
  25. QObject::connect(mw.get(), &MandelWidget::pointSelected, this, &Almond::pointSelected);
  26. ui.mainContainer->addWidget(mw.get());
  27. ui.maxIterations->setValidator(new QIntValidator(1, 1000000000, this));
  28. ui.backgroundProgress->setVisible(false);
  29. //ui.verticalLayout_left->addWidget(new MyGLWidget(ui.centralWidget));
  30. //mw->show();
  31. }
  32. Almond::~Almond(void)
  33. {
  34. }
  35. void Almond::submitBackgroundTask(BackgroundTask* task)
  36. {
  37. bool taken = QThreadPool::globalInstance()->tryTake(task->getRunnable());
  38. if (taken) {
  39. ui.backgroundProgress->setRange(0, 0);
  40. ui.backgroundProgress->setVisible(true);
  41. }
  42. }
  43. void Almond::backgroundTaskFinished(void)
  44. {
  45. }
  46. void Almond::on_zoom_out_clicked()
  47. {
  48. mw->zoom(2);
  49. }
  50. void Almond::on_zoom_in_clicked()
  51. {
  52. mw->zoom(0.5);
  53. }
  54. void Almond::on_maxIterations_editingFinished()
  55. {
  56. QString text = ui.maxIterations->text();
  57. int maxIter = text.toInt();
  58. mw->setMaxIterations(maxIter);
  59. }
  60. void Almond::on_chooseGradient_clicked()
  61. {
  62. gcd.exec();
  63. auto gradient = gcd.getGradient();
  64. if (gradient)
  65. mw->setGradient(std::move(*gradient));
  66. }
  67. void Almond::on_exportVideo_clicked()
  68. {
  69. ExportVideoInfo evi;
  70. evi.start = mnd::MandelViewport::standardView();
  71. evi.end = mw->getViewport();
  72. evi.gradient = mw->getGradient();
  73. ExportVideoDialog dialog(this, evi);
  74. //dialog.show();
  75. auto response = dialog.exec();
  76. printf("dialog executed\n"); fflush(stdout);
  77. if (response == 1) {
  78. mnd::MandelInfo mi;
  79. evi = dialog.getExportVideoInfo();
  80. //Video
  81. /*mi.maxIter = dialog.getMaxIterations();
  82. mi.view = mw->getViewport();
  83. mi.bWidth = dialog.getWidth();
  84. mi.bHeight = dialog.getHeight();
  85. mi.view.adjustAspectRatio(mi.bWidth, mi.bHeight);
  86. mnd::Generator& g = mandelContext.getDefaultGenerator();
  87. auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  88. g.generate(mi, fmap.pixels.get());
  89. 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) }; });
  90. QImage img((unsigned char*)bitmap.pixels.get(), bitmap.width, bitmap.height, bitmap.width * 3, QImage::Format_RGB888);
  91. img.save(dialog.getPath());*/
  92. }
  93. }
  94. void Almond::on_smooth_stateChanged(int checked)
  95. {
  96. this->mw->setSmoothColoring(checked != Qt::Unchecked);
  97. }
  98. void Almond::on_exportImage_clicked()
  99. {
  100. ExportImageDialog dialog(this);
  101. dialog.setMaxIterations(mw->getMaxIterations());
  102. //dialog.show();
  103. auto response = dialog.exec();
  104. if (response == 1) {
  105. mnd::MandelInfo mi;
  106. mi.maxIter = dialog.getMaxIterations();
  107. mi.view = mw->getViewport();
  108. mi.bWidth = dialog.getWidth();
  109. mi.bHeight = dialog.getHeight();
  110. mi.view.adjustAspectRatio(mi.bWidth, mi.bHeight);
  111. mi.smooth = mw->getSmoothColoring();
  112. if (currentView == JULIA) {
  113. mi.julia = mw->getMandelInfo().julia;
  114. mi.juliaX = mw->getJuliaX();
  115. mi.juliaY = mw->getJuliaY();
  116. }
  117. mnd::MandelGenerator* currentGenerator = mw->getGenerator();
  118. mnd::MandelGenerator& g = currentGenerator ? *currentGenerator : mandelContext.getDefaultGenerator();
  119. alm::ImageExportInfo iei;
  120. iei.drawInfo = mi;
  121. iei.generator = &g;
  122. iei.gradient = &mw->getGradient();
  123. alm::exportPng(dialog.getPath().toStdString(), iei);
  124. /*auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  125. g.generate(mi, fmap.pixels.get());
  126. auto bitmap = fmap.map<RGBColor>([&mi, this] (float i) {
  127. return i >= mi.maxIter ? RGBColor{ 0,0,0 } : mw->getGradient().get(i);
  128. });
  129. QImage img(reinterpret_cast<unsigned char*>(bitmap.pixels.get()), bitmap.width, bitmap.height, bitmap.width * 3, QImage::Format_RGB888);
  130. img.save(dialog.getPath());*/
  131. }
  132. }
  133. void Almond::on_resetZoom_clicked()
  134. {
  135. if (currentView == MANDELBROT) {
  136. mw->setViewport(mnd::MandelViewport::standardView());
  137. }
  138. else {
  139. mw->setViewport(mnd::MandelViewport::centerView());
  140. }
  141. }
  142. void Almond::on_displayInfo_stateChanged(int checked)
  143. {
  144. this->mw->setDisplayInfo(checked != Qt::Unchecked);
  145. }
  146. void Almond::on_chooseGenerator_clicked()
  147. {
  148. std::unique_ptr<ChooseGenerators> generatorsDialog;
  149. if (currentView == MANDELBROT || currentView == JULIA)
  150. generatorsDialog = std::make_unique<ChooseGenerators>(mandelContext, *mandelGenerator, *this);
  151. else if (currentView == CUSTOM)
  152. generatorsDialog = std::make_unique<ChooseGenerators>(mandelContext, this->currentCustom->gc, *customGenerator, *this);
  153. else
  154. return;
  155. auto response = generatorsDialog->exec();
  156. auto gen = generatorsDialog->extractChosenGenerator();
  157. if (gen) {
  158. if (currentView == MANDELBROT || currentView == JULIA) {
  159. mandelGenerator = gen.get();
  160. }
  161. else if (currentView == CUSTOM) {
  162. customGenerator = gen.get();
  163. }
  164. currentGenerator = gen.get();
  165. this->mw->setGenerator(currentGenerator);
  166. adjustedGenerators.push_back(std::move(gen));
  167. }
  168. else {
  169. //mandelGenerator = &mandelContext.getDefaultGenerator();
  170. }
  171. //this->currentView = MANDELBROT;
  172. //this->mw->getMandelInfo().julia = false;
  173. //printf("dialog executed\n"); fflush(stdout);
  174. }
  175. void Almond::pointSelected(mnd::Real x, mnd::Real y)
  176. {
  177. if (currentView != JULIA) {
  178. saveView();
  179. this->mw->setViewport(mnd::MandelViewport::centerView());
  180. this->mw->setJuliaPos(x, y);
  181. this->mw->getMandelInfo().julia = true;
  182. this->mw->clearAll();
  183. }
  184. currentView = JULIA;
  185. }
  186. void Almond::on_groupBox_toggled(bool arg1)
  187. {
  188. printf("arg1: %i\n", int(arg1)); fflush(stdout);
  189. }
  190. void Almond::on_wMandel_clicked()
  191. {
  192. }
  193. void Almond::saveView()
  194. {
  195. if (currentView == MANDELBROT)
  196. mandelViewSave = mw->getViewport();
  197. else if (currentView == CUSTOM)
  198. customViewSave = mw->getViewport();
  199. }
  200. void Almond::setViewType(ViewType v)
  201. {
  202. saveView();
  203. if (v == MANDELBROT) {
  204. currentGenerator = mandelGenerator;
  205. emit this->mw->stopSelectingPoint();
  206. this->mw->setViewport(mandelViewSave);
  207. this->mw->setGenerator(currentGenerator);
  208. this->mw->getMandelInfo().julia = false;
  209. this->mw->clearAll();
  210. currentView = MANDELBROT;
  211. }
  212. else if (v == CUSTOM) {
  213. if (customGenerator != nullptr) {
  214. currentGenerator = customGenerator;
  215. this->mw->setGenerator(currentGenerator);
  216. emit this->mw->stopSelectingPoint();
  217. this->mw->setViewport(customViewSave);
  218. this->mw->getMandelInfo().julia = false;
  219. this->mw->clearAll();
  220. currentView = CUSTOM;
  221. }
  222. else {
  223. setViewType(MANDELBROT);
  224. }
  225. }
  226. else if (v == JULIA) {
  227. if (currentView == MANDELBROT) {
  228. emit this->mw->selectPoint();
  229. }
  230. else {
  231. currentView = MANDELBROT;
  232. currentGenerator = mandelGenerator;
  233. this->mw->setGenerator(currentGenerator);
  234. this->mw->setViewport(mandelViewSave);
  235. this->mw->getMandelInfo().julia = false;
  236. this->mw->clearAll();
  237. emit this->mw->selectPoint();
  238. }
  239. }
  240. }
  241. void Almond::on_wMandel_toggled(bool checked)
  242. {
  243. if (checked)
  244. setViewType(MANDELBROT);
  245. }
  246. void Almond::on_radioButton_toggled(bool checked)
  247. {
  248. saveView();
  249. if (checked) {
  250. setViewType(JULIA);
  251. }
  252. }
  253. void Almond::on_radioButton_2_toggled(bool checked)
  254. {
  255. saveView();
  256. if (checked) {
  257. if (customGenerator == nullptr) {
  258. customGeneratorDialog->exec();
  259. if (auto* frac = customGeneratorDialog->getLastCompiled()) {
  260. customGenerator = frac->gc.adaptiveGenerator.get();
  261. customGenerators.push_back(std::make_unique<FractalDef>(std::move(*frac)));
  262. currentCustom = customGenerators[customGenerators.size() - 1].get();
  263. }
  264. }
  265. setViewType(CUSTOM);
  266. }
  267. }
  268. void Almond::on_createCustom_clicked()
  269. {
  270. auto response = customGeneratorDialog->exec();
  271. if (response != 1)
  272. return;
  273. if (auto* frac = customGeneratorDialog->getLastCompiled()) {
  274. customGenerator = frac->gc.adaptiveGenerator.get();
  275. customGenerators.push_back(std::make_unique<FractalDef>(std::move(*frac)));
  276. currentCustom = customGenerators[customGenerators.size() - 1].get();
  277. this->ui.radioButton_2->setChecked(true);
  278. setViewType(CUSTOM);
  279. }
  280. }