1
0

Almond.cpp 17 KB

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