Almond.cpp 12 KB

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