Almond.cpp 12 KB

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