Almond.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. etvw = new EscapeTimeVisualWidget(this);
  21. customGeneratorDialog = std::make_unique<CustomGenerator>(mandelContext);
  22. customGenerator = nullptr;
  23. customViewSave = mnd::MandelViewport::centerView();
  24. on_maxIterations_editingFinished();
  25. mw->setSmoothColoring(ui.smooth->isChecked());
  26. currentView = MANDELBROT;
  27. mandelGenerator = &mandelContext.getDefaultGenerator();
  28. mandelViewSave = mw->getViewport();
  29. QObject::connect(mw.get(), &MandelWidget::pointSelected, this, &Almond::pointSelected);
  30. ui.mandel_container->addWidget(etvw);
  31. //ui.mandel_container->addWidget(mw.get());
  32. ui.maxIterations->setValidator(new QIntValidator(1, 1000000000, this));
  33. ui.backgroundProgress->setEnabled(false);
  34. ui.cancelProgress->setEnabled(false);
  35. amw = new AlmondMenuWidget(this);
  36. amw->setMainMenu(ui.dockWidgetContents_2);
  37. eim = new ExportImageMenu();
  38. evm = new ExportVideoMenu();
  39. gradientMenu = new GradientMenu();
  40. AlmondSubMenu* imageSm = amw->addSubMenu(eim);
  41. AlmondSubMenu* videoSm = amw->addSubMenu(evm);
  42. AlmondSubMenu* gradientSm = amw->addSubMenu(gradientMenu);
  43. ui.dockWidget_2->setWidget(amw);
  44. connect(amw, &AlmondMenuWidget::submenuCancel, [this] (int) { amw->showMainMenu(); });
  45. //connect(amw, &AlmondMenuWidget::submenuOK, this, &Almond::submenuOK);
  46. connect(imageSm, &AlmondSubMenu::accepted, this, &Almond::imageExportOk);
  47. connect(videoSm, &AlmondSubMenu::accepted, this, &Almond::videoExportOk);
  48. connect(gradientSm, &AlmondSubMenu::accepted, this, &Almond::gradientEditOk);
  49. /*QStatusBar* bar = new QStatusBar(this);
  50. bar->addWidget(new QLabel("ayay"));
  51. auto* p = new QPushButton("About");
  52. bar->addPermanentWidget(p);
  53. QObject::connect(p, &QPushButton::clicked, [this]() {
  54. toggleFullscreen();
  55. });
  56. bar->setFixedHeight(bar->sizeHint().height());
  57. //ui.mainContainer->addWidget(bar);
  58. this->setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
  59. this->setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);*/
  60. installEventFilter(this);
  61. backgroundTasks.setMaxThreadCount(1);
  62. QIcon icon{ ":/icons/icon" };
  63. icon.addFile(":/icons/icon@2x");
  64. this->setWindowIcon(icon);
  65. // replace vertical layout with gridflowlayout
  66. /*GridFlowLayout* gfl = new GridFlowLayout(nullptr);
  67. //ui.horizontalLayout_4->addItem(gfl);
  68. for (int i = 0; i < ui.verticalLayout_right->count(); i++) {
  69. printf("%d: \n", i);
  70. gfl->addItem(ui.verticalLayout_right->takeAt(i));
  71. }
  72. ui.verticalLayout_right->setEnabled(false);
  73. delete ui.dockWidgetContents_2->layout();
  74. ui.dockWidgetContents_2->setLayout(gfl);*/
  75. }
  76. Almond::~Almond(void)
  77. {
  78. }
  79. void Almond::submitBackgroundTask(BackgroundTask* task)
  80. {
  81. QObject::connect(task, &BackgroundTask::finished, this, &Almond::backgroundTaskFinished);
  82. QObject::connect(task, &BackgroundTask::progress, this, &Almond::backgroundTaskProgress);
  83. backgroundTasks.start(task);
  84. //if (taken) {
  85. ui.backgroundProgress->setRange(0, 0);
  86. ui.backgroundProgress->setFormat("");
  87. ui.backgroundProgress->setEnabled(true);
  88. ui.cancelProgress->setEnabled(true);
  89. //}
  90. }
  91. void Almond::stopBackgroundTask(void)
  92. {
  93. stoppingBackgroundTasks = true;
  94. }
  95. bool Almond::eventFilter(QObject *target, QEvent *event)
  96. {
  97. if (event->type() == QEvent::KeyPress) {
  98. QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
  99. if (keyEvent->key() == Qt::Key_F11) {
  100. emit toggleFullscreen();
  101. }
  102. }
  103. return QObject::eventFilter(target, event);
  104. }
  105. void Almond::submenuOK(int smIndex)
  106. {
  107. switch(smIndex) {
  108. case 0:
  109. emit imageExportOk();
  110. break;
  111. case 1:
  112. emit videoExportOk();
  113. break;
  114. }
  115. }
  116. void Almond::imageExportOk(void)
  117. {
  118. mnd::MandelInfo mi;
  119. mi.maxIter = eim->getMaxIterations();
  120. mi.view = mw->getViewport();
  121. mi.bWidth = eim->getWidth();
  122. mi.bHeight = eim->getHeight();
  123. mi.view.adjustAspectRatio(mi.bWidth, mi.bHeight);
  124. mi.smooth = mw->getSmoothColoring();
  125. if (currentView == JULIA) {
  126. mi.julia = mw->getMandelInfo().julia;
  127. mi.juliaX = mw->getJuliaX();
  128. mi.juliaY = mw->getJuliaY();
  129. }
  130. mnd::MandelGenerator* currentGenerator = mw->getGenerator();
  131. mnd::MandelGenerator& g = currentGenerator ? *currentGenerator : mandelContext.getDefaultGenerator();
  132. alm::ImageExportInfo iei;
  133. iei.drawInfo = mi;
  134. iei.generator = &g;
  135. iei.gradient = mw->getGradient();
  136. iei.path = eim->getPath().toStdString();
  137. iei.options.jpegQuality = 95;
  138. submitBackgroundTask(new ImageExportTask(iei, [this] () { return stoppingBackgroundTasks; }));
  139. amw->showMainMenu();
  140. }
  141. void Almond::videoExportOk(void)
  142. {
  143. ExportVideoInfo evi = evm->getInfo();
  144. evi.gradient = mw->getGradient();
  145. evi.mi.smooth = mw->getSmoothColoring();
  146. if (currentView == JULIA) {
  147. evi.mi.julia = mw->getMandelInfo().julia;
  148. evi.mi.juliaX = mw->getMandelInfo().juliaX;
  149. evi.mi.juliaY = mw->getMandelInfo().juliaY;
  150. }
  151. if (evi.path == "") {
  152. QMessageBox errMsg = QMessageBox(QMessageBox::Icon::Critical, "Error", "No path specified.");
  153. errMsg.exec();
  154. }
  155. else {
  156. MandelVideoGenerator mvg(evi);
  157. mnd::MandelGenerator& g = *mw->getGenerator();
  158. printf("wii: %ld\n", evi.mi.bWidth);
  159. fflush(stdout);
  160. submitBackgroundTask(new VideoExportTask(std::move(mvg), g));
  161. amw->showMainMenu();
  162. }
  163. }
  164. void Almond::gradientEditOk(void)
  165. {
  166. const auto& points = gradientMenu->getGradient();
  167. // convert from QVector<QPair<float, QColor>> to
  168. // -> std::vector<std::pair<RGBColor, float>>
  169. std::vector<std::pair<RGBColor, float>> np;
  170. std::transform(points.begin(), points.end(), std::back_inserter(np),
  171. [](auto& qp) -> std::pair<RGBColor, float> {
  172. auto& [pos, col] = qp;
  173. return { RGBColor{ uint8_t(col.red()), uint8_t(col.green()), uint8_t(col.blue()) },
  174. pos };
  175. });
  176. std::sort(np.begin(), np.end(), [](auto& a, auto& b) { return a.second < b.second; });
  177. if (!np.empty()) {
  178. auto& first = np.at(0);
  179. if (first.second > 0) {
  180. np.insert(np.begin(), { first.first, 0.0f });
  181. }
  182. auto& last = np.at(np.size() - 1);
  183. if (last.second < 1) {
  184. np.insert(np.begin(), { last.first, 1.0f });
  185. }
  186. }
  187. std::for_each(np.begin(), np.end(), [](auto& x) { x.second *= 300; });
  188. Gradient g{ np, true };
  189. mw->setGradient(std::move(g));
  190. amw->showMainMenu();
  191. }
  192. void Almond::toggleFullscreen(void)
  193. {
  194. if (fullscreenMode) {
  195. auto* m = this->takeCentralWidget();
  196. ui.mandel_container->addWidget(m);
  197. this->setCentralWidget(cw);
  198. emit this->showNormal();
  199. fullscreenMode = false;
  200. }
  201. else {
  202. cw = this->takeCentralWidget();
  203. this->setCentralWidget(mw.get());
  204. emit this->showFullScreen();
  205. fullscreenMode = true;
  206. }
  207. }
  208. void Almond::backgroundTaskFinished(bool succ, QString message)
  209. {
  210. if (succ) {
  211. QMessageBox info = QMessageBox(QMessageBox::Icon::Information, "Task Finished", message);
  212. //info->setParent(this);
  213. emit info.exec();
  214. }
  215. else {
  216. QMessageBox info = QMessageBox(QMessageBox::Icon::Critical, "Task Failed", message);
  217. //info->setParent(this);
  218. emit info.exec();
  219. }
  220. ui.backgroundProgress->setFormat(tr("Export Progress"));
  221. ui.backgroundProgress->setEnabled(false);
  222. ui.backgroundProgress->setRange(0, 100);
  223. ui.backgroundProgress->setValue(0);
  224. ui.cancelProgress->setEnabled(false);
  225. stoppingBackgroundTasks = false;
  226. }
  227. void Almond::backgroundTaskProgress(float percentage)
  228. {
  229. QObject* task = QObject::sender();
  230. if (auto* bt = qobject_cast<BackgroundTask*>(task)) {
  231. ui.backgroundProgress->setFormat(QString::fromStdString(bt->getShortDescription() + ": %p%"));
  232. }
  233. if (percentage > 0) {
  234. ui.backgroundProgress->setRange(0, 100);
  235. ui.backgroundProgress->setValue(percentage);
  236. }
  237. else {
  238. ui.backgroundProgress->reset();
  239. ui.backgroundProgress->setRange(0, 0);
  240. ui.backgroundProgress->setValue(-1);
  241. }
  242. }
  243. void Almond::on_zoom_out_clicked()
  244. {
  245. mw->zoom(2);
  246. }
  247. void Almond::on_zoom_in_clicked()
  248. {
  249. mw->zoom(0.5);
  250. }
  251. void Almond::on_maxIterations_editingFinished()
  252. {
  253. QString text = ui.maxIterations->text();
  254. int maxIter = text.toInt();
  255. mw->setMaxIterations(maxIter);
  256. }
  257. void Almond::on_chooseGradient_clicked()
  258. {
  259. const auto& gradient = mw->getGradient();
  260. auto points = gradient.getPoints();
  261. std::for_each(points.begin(), points.end(), [](auto& x) { x.second /= 300; });
  262. QVector<QPair<float, QColor>> np;
  263. std::transform(points.begin(), points.end(), std::back_inserter(np),
  264. [](auto& qp) -> QPair<float, QColor> {
  265. auto& [col, pos] = qp;
  266. return { pos, QColor{ (col.r), (col.g), (col.b) } };
  267. });
  268. this->gradientMenu->setGradient(std::move(np));
  269. emit this->amw->showSubMenu(2);
  270. //gcd.exec();
  271. //auto gradient = gcd.getGradient();
  272. //if (gradient)
  273. // mw->setGradient(std::move(*gradient));
  274. }
  275. void Almond::on_exportVideo_clicked()
  276. {
  277. evm->setEndViewport(mw->getViewport());
  278. emit this->amw->showSubMenu(1);
  279. return;
  280. ExportVideoInfo evi;
  281. evi.start = mnd::MandelViewport::standardView();
  282. evi.end = mw->getViewport();
  283. evi.gradient = mw->getGradient();
  284. ExportVideoDialog dialog(this, evi);
  285. //dialog.show();
  286. auto response = dialog.exec();
  287. printf("dialog executed\n"); fflush(stdout);
  288. if (response == 1) {
  289. mnd::MandelInfo mi;
  290. evi = dialog.getExportVideoInfo();
  291. MandelVideoGenerator mvg(evi);
  292. mnd::MandelGenerator& g = *mw->getGenerator();
  293. submitBackgroundTask(new VideoExportTask(std::move(mvg), g));
  294. //if (exportVideo(evi)) {
  295. //Video
  296. /*mi.maxIter = dialog.getMaxIterations();
  297. mi.view = mw->getViewport();
  298. mi.bWidth = dialog.getWidth();
  299. mi.bHeight = dialog.getHeight();
  300. mi.view.adjustAspectRatio(mi.bWidth, mi.bHeight);
  301. mnd::Generator& g = mandelContext.getDefaultGenerator();
  302. auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  303. g.generate(mi, fmap.pixels.get());
  304. 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) }; });
  305. QImage img((unsigned char*)bitmap.pixels.get(), bitmap.width, bitmap.height, bitmap.width * 3, QImage::Format_RGB888);
  306. img.save(dialog.getPath());*/
  307. }
  308. }
  309. void Almond::on_smooth_stateChanged(int checked)
  310. {
  311. this->mw->setSmoothColoring(checked != Qt::Unchecked);
  312. }
  313. void Almond::on_exportImage_clicked()
  314. {
  315. this->amw->showSubMenu(0);
  316. return;
  317. ExportImageDialog dialog(this);
  318. dialog.setMaxIterations(mw->getMaxIterations());
  319. //dialog.show();
  320. auto response = dialog.exec();
  321. if (response == 1) {
  322. mnd::MandelInfo mi;
  323. mi.maxIter = dialog.getMaxIterations();
  324. mi.view = mw->getViewport();
  325. mi.bWidth = dialog.getWidth();
  326. mi.bHeight = dialog.getHeight();
  327. mi.view.adjustAspectRatio(mi.bWidth, mi.bHeight);
  328. mi.smooth = mw->getSmoothColoring();
  329. if (currentView == JULIA) {
  330. mi.julia = mw->getMandelInfo().julia;
  331. mi.juliaX = mw->getJuliaX();
  332. mi.juliaY = mw->getJuliaY();
  333. }
  334. mnd::MandelGenerator* currentGenerator = mw->getGenerator();
  335. mnd::MandelGenerator& g = currentGenerator ? *currentGenerator : mandelContext.getDefaultGenerator();
  336. alm::ImageExportInfo iei;
  337. iei.drawInfo = mi;
  338. iei.generator = &g;
  339. iei.gradient = mw->getGradient();
  340. iei.path = dialog.getPath().toStdString();
  341. iei.options.jpegQuality = 95;
  342. submitBackgroundTask(new ImageExportTask(iei, [this] () { return stoppingBackgroundTasks; }));
  343. /*auto exprt = [iei, path = dialog.getPath().toStdString()]() {
  344. alm::exportPng(path, iei);
  345. };
  346. submitBackgroundTask();*/
  347. /*auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  348. g.generate(mi, fmap.pixels.get());
  349. auto bitmap = fmap.map<RGBColor>([&mi, this] (float i) {
  350. return i >= mi.maxIter ? RGBColor{ 0,0,0 } : mw->getGradient().get(i);
  351. });
  352. QImage img(reinterpret_cast<unsigned char*>(bitmap.pixels.get()), bitmap.width, bitmap.height, bitmap.width * 3, QImage::Format_RGB888);
  353. img.save(dialog.getPath());*/
  354. }
  355. }
  356. void Almond::on_resetZoom_clicked()
  357. {
  358. if (currentView == MANDELBROT) {
  359. mw->setViewport(mnd::MandelViewport::standardView());
  360. }
  361. else {
  362. mw->setViewport(mnd::MandelViewport::centerView());
  363. }
  364. }
  365. void Almond::on_displayInfo_stateChanged(int checked)
  366. {
  367. this->mw->setDisplayInfo(checked != Qt::Unchecked);
  368. }
  369. void Almond::on_chooseGenerator_clicked()
  370. {
  371. std::unique_ptr<ChooseGenerators> generatorsDialog;
  372. if (currentView == MANDELBROT || currentView == JULIA)
  373. generatorsDialog = std::make_unique<ChooseGenerators>(mandelContext, *mandelGenerator, *this);
  374. else if (currentView == CUSTOM)
  375. generatorsDialog = std::make_unique<ChooseGenerators>(mandelContext, this->currentCustom->gc, *customGenerator, *this);
  376. else
  377. return;
  378. auto response = generatorsDialog->exec();
  379. auto gen = generatorsDialog->extractChosenGenerator();
  380. if (gen) {
  381. if (currentView == MANDELBROT || currentView == JULIA) {
  382. mandelGenerator = gen.get();
  383. }
  384. else if (currentView == CUSTOM) {
  385. customGenerator = gen.get();
  386. }
  387. currentGenerator = gen.get();
  388. this->mw->setGenerator(currentGenerator);
  389. adjustedGenerators.push_back(std::move(gen));
  390. }
  391. else {
  392. //mandelGenerator = &mandelContext.getDefaultGenerator();
  393. }
  394. //this->currentView = MANDELBROT;
  395. //this->mw->getMandelInfo().julia = false;
  396. //printf("dialog executed\n"); fflush(stdout);
  397. }
  398. void Almond::pointSelected(mnd::Real x, mnd::Real y)
  399. {
  400. if (currentView != JULIA) {
  401. saveView();
  402. this->mw->setViewport(mnd::MandelViewport::centerView());
  403. this->mw->setJuliaPos(x, y);
  404. this->mw->getMandelInfo().julia = true;
  405. this->mw->clearAll();
  406. }
  407. currentView = JULIA;
  408. }
  409. void Almond::on_wMandel_clicked()
  410. {
  411. }
  412. void Almond::saveView()
  413. {
  414. if (currentView == MANDELBROT)
  415. mandelViewSave = mw->getViewport();
  416. else if (currentView == CUSTOM)
  417. customViewSave = mw->getViewport();
  418. }
  419. void Almond::setViewType(ViewType v)
  420. {
  421. saveView();
  422. if (v == MANDELBROT) {
  423. currentGenerator = mandelGenerator;
  424. emit this->mw->stopSelectingPoint();
  425. this->mw->setViewport(mandelViewSave);
  426. this->mw->setGenerator(currentGenerator);
  427. this->mw->getMandelInfo().julia = false;
  428. this->mw->clearAll();
  429. currentView = MANDELBROT;
  430. }
  431. else if (v == CUSTOM) {
  432. if (customGenerator != nullptr) {
  433. currentGenerator = customGenerator;
  434. this->mw->setGenerator(currentGenerator);
  435. emit this->mw->stopSelectingPoint();
  436. this->mw->setViewport(customViewSave);
  437. this->mw->getMandelInfo().julia = false;
  438. this->mw->clearAll();
  439. currentView = CUSTOM;
  440. }
  441. else {
  442. setViewType(MANDELBROT);
  443. }
  444. }
  445. else if (v == JULIA) {
  446. if (currentView == MANDELBROT) {
  447. emit this->mw->selectPoint();
  448. }
  449. else {
  450. currentView = MANDELBROT;
  451. currentGenerator = mandelGenerator;
  452. this->mw->setGenerator(currentGenerator);
  453. this->mw->setViewport(mandelViewSave);
  454. this->mw->getMandelInfo().julia = false;
  455. this->mw->clearAll();
  456. emit this->mw->selectPoint();
  457. }
  458. }
  459. }
  460. void Almond::on_wMandel_toggled(bool checked)
  461. {
  462. if (checked)
  463. setViewType(MANDELBROT);
  464. }
  465. void Almond::on_radioButton_toggled(bool checked)
  466. {
  467. saveView();
  468. if (checked) {
  469. setViewType(JULIA);
  470. }
  471. }
  472. void Almond::on_radioButton_2_toggled(bool checked)
  473. {
  474. saveView();
  475. if (checked) {
  476. if (customGenerator == nullptr) {
  477. customGeneratorDialog->exec();
  478. if (auto* frac = customGeneratorDialog->getLastCompiled()) {
  479. customGenerator = frac->gc.adaptiveGenerator.get();
  480. customGenerators.push_back(std::make_unique<FractalDef>(std::move(*frac)));
  481. currentCustom = customGenerators[customGenerators.size() - 1].get();
  482. }
  483. }
  484. setViewType(CUSTOM);
  485. }
  486. }
  487. void Almond::on_createCustom_clicked()
  488. {
  489. auto response = customGeneratorDialog->exec();
  490. if (response != 1)
  491. return;
  492. if (auto* frac = customGeneratorDialog->getLastCompiled()) {
  493. customGenerator = frac->gc.adaptiveGenerator.get();
  494. customGenerators.push_back(std::make_unique<FractalDef>(std::move(*frac)));
  495. currentCustom = customGenerators[customGenerators.size() - 1].get();
  496. this->ui.radioButton_2->setChecked(true);
  497. setViewType(CUSTOM);
  498. }
  499. }
  500. void Almond::on_cancelProgress_clicked()
  501. {
  502. stopBackgroundTask();
  503. }