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