Almond.cpp 11 KB

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