Almond.cpp 16 KB

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