Almond.cpp 11 KB

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