Almond.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. QObject::connect(task, &BackgroundTask::finished, this, &Almond::backgroundTaskFinished);
  37. QObject::connect(task, &BackgroundTask::progress, this, &Almond::backgroundTaskProgress);
  38. backgroundTasks.start(task);
  39. //if (taken) {
  40. ui.backgroundProgress->setRange(0, 0);
  41. ui.backgroundProgress->setVisible(true);
  42. ui.backgroundProgress->setFormat("");
  43. //}
  44. }
  45. void Almond::backgroundTaskFinished(bool succ)
  46. {
  47. ui.backgroundProgress->setVisible(false);
  48. ui.backgroundProgress->setFormat("");
  49. }
  50. void Almond::backgroundTaskProgress(float percentage)
  51. {
  52. QObject* task = QObject::sender();
  53. if (auto* bt = qobject_cast<BackgroundTask*>(task)) {
  54. ui.backgroundProgress->setFormat(QString::fromStdString(bt->getShortDescription() + ": %p%"));
  55. }
  56. if (percentage > 0) {
  57. ui.backgroundProgress->setRange(0, 100);
  58. ui.backgroundProgress->setValue(percentage);
  59. }
  60. else {
  61. ui.backgroundProgress->setRange(0, 0);
  62. }
  63. }
  64. void Almond::on_zoom_out_clicked()
  65. {
  66. mw->zoom(2);
  67. }
  68. void Almond::on_zoom_in_clicked()
  69. {
  70. mw->zoom(0.5);
  71. }
  72. void Almond::on_maxIterations_editingFinished()
  73. {
  74. QString text = ui.maxIterations->text();
  75. int maxIter = text.toInt();
  76. mw->setMaxIterations(maxIter);
  77. }
  78. void Almond::on_chooseGradient_clicked()
  79. {
  80. gcd.exec();
  81. auto gradient = gcd.getGradient();
  82. if (gradient)
  83. mw->setGradient(std::move(*gradient));
  84. }
  85. void Almond::on_exportVideo_clicked()
  86. {
  87. ExportVideoInfo evi;
  88. evi.start = mnd::MandelViewport::standardView();
  89. evi.end = mw->getViewport();
  90. evi.gradient = mw->getGradient();
  91. ExportVideoDialog dialog(this, evi);
  92. //dialog.show();
  93. auto response = dialog.exec();
  94. printf("dialog executed\n"); fflush(stdout);
  95. if (response == 1) {
  96. mnd::MandelInfo mi;
  97. evi = dialog.getExportVideoInfo();
  98. MandelVideoGenerator mvg(evi);
  99. mnd::MandelGenerator& g = *mw->getGenerator();
  100. submitBackgroundTask(new VideoExportTask(std::move(mvg), g));
  101. //if (exportVideo(evi)) {
  102. //Video
  103. /*mi.maxIter = dialog.getMaxIterations();
  104. mi.view = mw->getViewport();
  105. mi.bWidth = dialog.getWidth();
  106. mi.bHeight = dialog.getHeight();
  107. mi.view.adjustAspectRatio(mi.bWidth, mi.bHeight);
  108. mnd::Generator& g = mandelContext.getDefaultGenerator();
  109. auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  110. g.generate(mi, fmap.pixels.get());
  111. 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) }; });
  112. QImage img((unsigned char*)bitmap.pixels.get(), bitmap.width, bitmap.height, bitmap.width * 3, QImage::Format_RGB888);
  113. img.save(dialog.getPath());*/
  114. }
  115. }
  116. void Almond::on_smooth_stateChanged(int checked)
  117. {
  118. this->mw->setSmoothColoring(checked != Qt::Unchecked);
  119. }
  120. void Almond::on_exportImage_clicked()
  121. {
  122. ExportImageDialog dialog(this);
  123. dialog.setMaxIterations(mw->getMaxIterations());
  124. //dialog.show();
  125. auto response = dialog.exec();
  126. if (response == 1) {
  127. mnd::MandelInfo mi;
  128. mi.maxIter = dialog.getMaxIterations();
  129. mi.view = mw->getViewport();
  130. mi.bWidth = dialog.getWidth();
  131. mi.bHeight = dialog.getHeight();
  132. mi.view.adjustAspectRatio(mi.bWidth, mi.bHeight);
  133. mi.smooth = mw->getSmoothColoring();
  134. if (currentView == JULIA) {
  135. mi.julia = mw->getMandelInfo().julia;
  136. mi.juliaX = mw->getJuliaX();
  137. mi.juliaY = mw->getJuliaY();
  138. }
  139. mnd::MandelGenerator* currentGenerator = mw->getGenerator();
  140. mnd::MandelGenerator& g = currentGenerator ? *currentGenerator : mandelContext.getDefaultGenerator();
  141. alm::ImageExportInfo iei;
  142. iei.drawInfo = mi;
  143. iei.generator = &g;
  144. iei.gradient = mw->getGradient();
  145. iei.path = dialog.getPath().toStdString();
  146. submitBackgroundTask(new ImageExportTask(iei));
  147. /*auto exprt = [iei, path = dialog.getPath().toStdString()]() {
  148. alm::exportPng(path, iei);
  149. };
  150. submitBackgroundTask();*/
  151. /*auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  152. g.generate(mi, fmap.pixels.get());
  153. auto bitmap = fmap.map<RGBColor>([&mi, this] (float i) {
  154. return i >= mi.maxIter ? RGBColor{ 0,0,0 } : mw->getGradient().get(i);
  155. });
  156. QImage img(reinterpret_cast<unsigned char*>(bitmap.pixels.get()), bitmap.width, bitmap.height, bitmap.width * 3, QImage::Format_RGB888);
  157. img.save(dialog.getPath());*/
  158. }
  159. }
  160. void Almond::on_resetZoom_clicked()
  161. {
  162. if (currentView == MANDELBROT) {
  163. mw->setViewport(mnd::MandelViewport::standardView());
  164. }
  165. else {
  166. mw->setViewport(mnd::MandelViewport::centerView());
  167. }
  168. }
  169. void Almond::on_displayInfo_stateChanged(int checked)
  170. {
  171. this->mw->setDisplayInfo(checked != Qt::Unchecked);
  172. }
  173. void Almond::on_chooseGenerator_clicked()
  174. {
  175. std::unique_ptr<ChooseGenerators> generatorsDialog;
  176. if (currentView == MANDELBROT || currentView == JULIA)
  177. generatorsDialog = std::make_unique<ChooseGenerators>(mandelContext, *mandelGenerator, *this);
  178. else if (currentView == CUSTOM)
  179. generatorsDialog = std::make_unique<ChooseGenerators>(mandelContext, this->currentCustom->gc, *customGenerator, *this);
  180. else
  181. return;
  182. auto response = generatorsDialog->exec();
  183. auto gen = generatorsDialog->extractChosenGenerator();
  184. if (gen) {
  185. if (currentView == MANDELBROT || currentView == JULIA) {
  186. mandelGenerator = gen.get();
  187. }
  188. else if (currentView == CUSTOM) {
  189. customGenerator = gen.get();
  190. }
  191. currentGenerator = gen.get();
  192. this->mw->setGenerator(currentGenerator);
  193. adjustedGenerators.push_back(std::move(gen));
  194. }
  195. else {
  196. //mandelGenerator = &mandelContext.getDefaultGenerator();
  197. }
  198. //this->currentView = MANDELBROT;
  199. //this->mw->getMandelInfo().julia = false;
  200. //printf("dialog executed\n"); fflush(stdout);
  201. }
  202. void Almond::pointSelected(mnd::Real x, mnd::Real y)
  203. {
  204. if (currentView != JULIA) {
  205. saveView();
  206. this->mw->setViewport(mnd::MandelViewport::centerView());
  207. this->mw->setJuliaPos(x, y);
  208. this->mw->getMandelInfo().julia = true;
  209. this->mw->clearAll();
  210. }
  211. currentView = JULIA;
  212. }
  213. void Almond::on_groupBox_toggled(bool arg1)
  214. {
  215. printf("arg1: %i\n", int(arg1)); fflush(stdout);
  216. }
  217. void Almond::on_wMandel_clicked()
  218. {
  219. }
  220. void Almond::saveView()
  221. {
  222. if (currentView == MANDELBROT)
  223. mandelViewSave = mw->getViewport();
  224. else if (currentView == CUSTOM)
  225. customViewSave = mw->getViewport();
  226. }
  227. void Almond::setViewType(ViewType v)
  228. {
  229. saveView();
  230. if (v == MANDELBROT) {
  231. currentGenerator = mandelGenerator;
  232. emit this->mw->stopSelectingPoint();
  233. this->mw->setViewport(mandelViewSave);
  234. this->mw->setGenerator(currentGenerator);
  235. this->mw->getMandelInfo().julia = false;
  236. this->mw->clearAll();
  237. currentView = MANDELBROT;
  238. }
  239. else if (v == CUSTOM) {
  240. if (customGenerator != nullptr) {
  241. currentGenerator = customGenerator;
  242. this->mw->setGenerator(currentGenerator);
  243. emit this->mw->stopSelectingPoint();
  244. this->mw->setViewport(customViewSave);
  245. this->mw->getMandelInfo().julia = false;
  246. this->mw->clearAll();
  247. currentView = CUSTOM;
  248. }
  249. else {
  250. setViewType(MANDELBROT);
  251. }
  252. }
  253. else if (v == JULIA) {
  254. if (currentView == MANDELBROT) {
  255. emit this->mw->selectPoint();
  256. }
  257. else {
  258. currentView = MANDELBROT;
  259. currentGenerator = mandelGenerator;
  260. this->mw->setGenerator(currentGenerator);
  261. this->mw->setViewport(mandelViewSave);
  262. this->mw->getMandelInfo().julia = false;
  263. this->mw->clearAll();
  264. emit this->mw->selectPoint();
  265. }
  266. }
  267. }
  268. void Almond::on_wMandel_toggled(bool checked)
  269. {
  270. if (checked)
  271. setViewType(MANDELBROT);
  272. }
  273. void Almond::on_radioButton_toggled(bool checked)
  274. {
  275. saveView();
  276. if (checked) {
  277. setViewType(JULIA);
  278. }
  279. }
  280. void Almond::on_radioButton_2_toggled(bool checked)
  281. {
  282. saveView();
  283. if (checked) {
  284. if (customGenerator == nullptr) {
  285. customGeneratorDialog->exec();
  286. if (auto* frac = customGeneratorDialog->getLastCompiled()) {
  287. customGenerator = frac->gc.adaptiveGenerator.get();
  288. customGenerators.push_back(std::make_unique<FractalDef>(std::move(*frac)));
  289. currentCustom = customGenerators[customGenerators.size() - 1].get();
  290. }
  291. }
  292. setViewType(CUSTOM);
  293. }
  294. }
  295. void Almond::on_createCustom_clicked()
  296. {
  297. auto response = customGeneratorDialog->exec();
  298. if (response != 1)
  299. return;
  300. if (auto* frac = customGeneratorDialog->getLastCompiled()) {
  301. customGenerator = frac->gc.adaptiveGenerator.get();
  302. customGenerators.push_back(std::make_unique<FractalDef>(std::move(*frac)));
  303. currentCustom = customGenerators[customGenerators.size() - 1].get();
  304. this->ui.radioButton_2->setChecked(true);
  305. setViewType(CUSTOM);
  306. }
  307. }