Almond.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. amw->showMainMenu();
  165. }
  166. void Almond::toggleFullscreen(void)
  167. {
  168. if (fullscreenMode) {
  169. auto* m = this->takeCentralWidget();
  170. ui.mandel_container->addWidget(m);
  171. this->setCentralWidget(cw);
  172. emit this->showNormal();
  173. fullscreenMode = false;
  174. }
  175. else {
  176. cw = this->takeCentralWidget();
  177. this->setCentralWidget(mw.get());
  178. emit this->showFullScreen();
  179. fullscreenMode = true;
  180. }
  181. }
  182. void Almond::backgroundTaskFinished(bool succ, QString message)
  183. {
  184. if (succ) {
  185. QMessageBox info = QMessageBox(QMessageBox::Icon::Information, "Task Finished", message);
  186. //info->setParent(this);
  187. emit info.exec();
  188. }
  189. else {
  190. QMessageBox info = QMessageBox(QMessageBox::Icon::Critical, "Task Failed", message);
  191. //info->setParent(this);
  192. emit info.exec();
  193. }
  194. ui.backgroundProgress->setFormat(tr("Export Progress"));
  195. ui.backgroundProgress->setEnabled(false);
  196. ui.backgroundProgress->setRange(0, 100);
  197. ui.backgroundProgress->setValue(0);
  198. ui.cancelProgress->setEnabled(false);
  199. stoppingBackgroundTasks = false;
  200. }
  201. void Almond::backgroundTaskProgress(float percentage)
  202. {
  203. QObject* task = QObject::sender();
  204. if (auto* bt = qobject_cast<BackgroundTask*>(task)) {
  205. ui.backgroundProgress->setFormat(QString::fromStdString(bt->getShortDescription() + ": %p%"));
  206. }
  207. if (percentage > 0) {
  208. ui.backgroundProgress->setRange(0, 100);
  209. ui.backgroundProgress->setValue(percentage);
  210. }
  211. else {
  212. ui.backgroundProgress->reset();
  213. ui.backgroundProgress->setRange(0, 0);
  214. ui.backgroundProgress->setValue(-1);
  215. }
  216. }
  217. void Almond::on_zoom_out_clicked()
  218. {
  219. mw->zoom(2);
  220. }
  221. void Almond::on_zoom_in_clicked()
  222. {
  223. mw->zoom(0.5);
  224. }
  225. void Almond::on_maxIterations_editingFinished()
  226. {
  227. QString text = ui.maxIterations->text();
  228. int maxIter = text.toInt();
  229. mw->setMaxIterations(maxIter);
  230. }
  231. void Almond::on_chooseGradient_clicked()
  232. {
  233. emit this->amw->showSubMenu(2);
  234. //gcd.exec();
  235. //auto gradient = gcd.getGradient();
  236. //if (gradient)
  237. // mw->setGradient(std::move(*gradient));
  238. }
  239. void Almond::on_exportVideo_clicked()
  240. {
  241. evm->setEndViewport(mw->getViewport());
  242. emit this->amw->showSubMenu(1);
  243. return;
  244. ExportVideoInfo evi;
  245. evi.start = mnd::MandelViewport::standardView();
  246. evi.end = mw->getViewport();
  247. evi.gradient = mw->getGradient();
  248. ExportVideoDialog dialog(this, evi);
  249. //dialog.show();
  250. auto response = dialog.exec();
  251. printf("dialog executed\n"); fflush(stdout);
  252. if (response == 1) {
  253. mnd::MandelInfo mi;
  254. evi = dialog.getExportVideoInfo();
  255. MandelVideoGenerator mvg(evi);
  256. mnd::MandelGenerator& g = *mw->getGenerator();
  257. submitBackgroundTask(new VideoExportTask(std::move(mvg), g));
  258. //if (exportVideo(evi)) {
  259. //Video
  260. /*mi.maxIter = dialog.getMaxIterations();
  261. mi.view = mw->getViewport();
  262. mi.bWidth = dialog.getWidth();
  263. mi.bHeight = dialog.getHeight();
  264. mi.view.adjustAspectRatio(mi.bWidth, mi.bHeight);
  265. mnd::Generator& g = mandelContext.getDefaultGenerator();
  266. auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  267. g.generate(mi, fmap.pixels.get());
  268. 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) }; });
  269. QImage img((unsigned char*)bitmap.pixels.get(), bitmap.width, bitmap.height, bitmap.width * 3, QImage::Format_RGB888);
  270. img.save(dialog.getPath());*/
  271. }
  272. }
  273. void Almond::on_smooth_stateChanged(int checked)
  274. {
  275. this->mw->setSmoothColoring(checked != Qt::Unchecked);
  276. }
  277. void Almond::on_exportImage_clicked()
  278. {
  279. this->amw->showSubMenu(0);
  280. return;
  281. ExportImageDialog dialog(this);
  282. dialog.setMaxIterations(mw->getMaxIterations());
  283. //dialog.show();
  284. auto response = dialog.exec();
  285. if (response == 1) {
  286. mnd::MandelInfo mi;
  287. mi.maxIter = dialog.getMaxIterations();
  288. mi.view = mw->getViewport();
  289. mi.bWidth = dialog.getWidth();
  290. mi.bHeight = dialog.getHeight();
  291. mi.view.adjustAspectRatio(mi.bWidth, mi.bHeight);
  292. mi.smooth = mw->getSmoothColoring();
  293. if (currentView == JULIA) {
  294. mi.julia = mw->getMandelInfo().julia;
  295. mi.juliaX = mw->getJuliaX();
  296. mi.juliaY = mw->getJuliaY();
  297. }
  298. mnd::MandelGenerator* currentGenerator = mw->getGenerator();
  299. mnd::MandelGenerator& g = currentGenerator ? *currentGenerator : mandelContext.getDefaultGenerator();
  300. alm::ImageExportInfo iei;
  301. iei.drawInfo = mi;
  302. iei.generator = &g;
  303. iei.gradient = mw->getGradient();
  304. iei.path = dialog.getPath().toStdString();
  305. iei.options.jpegQuality = 95;
  306. submitBackgroundTask(new ImageExportTask(iei, [this] () { return stoppingBackgroundTasks; }));
  307. /*auto exprt = [iei, path = dialog.getPath().toStdString()]() {
  308. alm::exportPng(path, iei);
  309. };
  310. submitBackgroundTask();*/
  311. /*auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  312. g.generate(mi, fmap.pixels.get());
  313. auto bitmap = fmap.map<RGBColor>([&mi, this] (float i) {
  314. return i >= mi.maxIter ? RGBColor{ 0,0,0 } : mw->getGradient().get(i);
  315. });
  316. QImage img(reinterpret_cast<unsigned char*>(bitmap.pixels.get()), bitmap.width, bitmap.height, bitmap.width * 3, QImage::Format_RGB888);
  317. img.save(dialog.getPath());*/
  318. }
  319. }
  320. void Almond::on_resetZoom_clicked()
  321. {
  322. if (currentView == MANDELBROT) {
  323. mw->setViewport(mnd::MandelViewport::standardView());
  324. }
  325. else {
  326. mw->setViewport(mnd::MandelViewport::centerView());
  327. }
  328. }
  329. void Almond::on_displayInfo_stateChanged(int checked)
  330. {
  331. this->mw->setDisplayInfo(checked != Qt::Unchecked);
  332. }
  333. void Almond::on_chooseGenerator_clicked()
  334. {
  335. std::unique_ptr<ChooseGenerators> generatorsDialog;
  336. if (currentView == MANDELBROT || currentView == JULIA)
  337. generatorsDialog = std::make_unique<ChooseGenerators>(mandelContext, *mandelGenerator, *this);
  338. else if (currentView == CUSTOM)
  339. generatorsDialog = std::make_unique<ChooseGenerators>(mandelContext, this->currentCustom->gc, *customGenerator, *this);
  340. else
  341. return;
  342. auto response = generatorsDialog->exec();
  343. auto gen = generatorsDialog->extractChosenGenerator();
  344. if (gen) {
  345. if (currentView == MANDELBROT || currentView == JULIA) {
  346. mandelGenerator = gen.get();
  347. }
  348. else if (currentView == CUSTOM) {
  349. customGenerator = gen.get();
  350. }
  351. currentGenerator = gen.get();
  352. this->mw->setGenerator(currentGenerator);
  353. adjustedGenerators.push_back(std::move(gen));
  354. }
  355. else {
  356. //mandelGenerator = &mandelContext.getDefaultGenerator();
  357. }
  358. //this->currentView = MANDELBROT;
  359. //this->mw->getMandelInfo().julia = false;
  360. //printf("dialog executed\n"); fflush(stdout);
  361. }
  362. void Almond::pointSelected(mnd::Real x, mnd::Real y)
  363. {
  364. if (currentView != JULIA) {
  365. saveView();
  366. this->mw->setViewport(mnd::MandelViewport::centerView());
  367. this->mw->setJuliaPos(x, y);
  368. this->mw->getMandelInfo().julia = true;
  369. this->mw->clearAll();
  370. }
  371. currentView = JULIA;
  372. }
  373. void Almond::on_wMandel_clicked()
  374. {
  375. }
  376. void Almond::saveView()
  377. {
  378. if (currentView == MANDELBROT)
  379. mandelViewSave = mw->getViewport();
  380. else if (currentView == CUSTOM)
  381. customViewSave = mw->getViewport();
  382. }
  383. void Almond::setViewType(ViewType v)
  384. {
  385. saveView();
  386. if (v == MANDELBROT) {
  387. currentGenerator = mandelGenerator;
  388. emit this->mw->stopSelectingPoint();
  389. this->mw->setViewport(mandelViewSave);
  390. this->mw->setGenerator(currentGenerator);
  391. this->mw->getMandelInfo().julia = false;
  392. this->mw->clearAll();
  393. currentView = MANDELBROT;
  394. }
  395. else if (v == CUSTOM) {
  396. if (customGenerator != nullptr) {
  397. currentGenerator = customGenerator;
  398. this->mw->setGenerator(currentGenerator);
  399. emit this->mw->stopSelectingPoint();
  400. this->mw->setViewport(customViewSave);
  401. this->mw->getMandelInfo().julia = false;
  402. this->mw->clearAll();
  403. currentView = CUSTOM;
  404. }
  405. else {
  406. setViewType(MANDELBROT);
  407. }
  408. }
  409. else if (v == JULIA) {
  410. if (currentView == MANDELBROT) {
  411. emit this->mw->selectPoint();
  412. }
  413. else {
  414. currentView = MANDELBROT;
  415. currentGenerator = mandelGenerator;
  416. this->mw->setGenerator(currentGenerator);
  417. this->mw->setViewport(mandelViewSave);
  418. this->mw->getMandelInfo().julia = false;
  419. this->mw->clearAll();
  420. emit this->mw->selectPoint();
  421. }
  422. }
  423. }
  424. void Almond::on_wMandel_toggled(bool checked)
  425. {
  426. if (checked)
  427. setViewType(MANDELBROT);
  428. }
  429. void Almond::on_radioButton_toggled(bool checked)
  430. {
  431. saveView();
  432. if (checked) {
  433. setViewType(JULIA);
  434. }
  435. }
  436. void Almond::on_radioButton_2_toggled(bool checked)
  437. {
  438. saveView();
  439. if (checked) {
  440. if (customGenerator == nullptr) {
  441. customGeneratorDialog->exec();
  442. if (auto* frac = customGeneratorDialog->getLastCompiled()) {
  443. customGenerator = frac->gc.adaptiveGenerator.get();
  444. customGenerators.push_back(std::make_unique<FractalDef>(std::move(*frac)));
  445. currentCustom = customGenerators[customGenerators.size() - 1].get();
  446. }
  447. }
  448. setViewType(CUSTOM);
  449. }
  450. }
  451. void Almond::on_createCustom_clicked()
  452. {
  453. auto response = customGeneratorDialog->exec();
  454. if (response != 1)
  455. return;
  456. if (auto* frac = customGeneratorDialog->getLastCompiled()) {
  457. customGenerator = frac->gc.adaptiveGenerator.get();
  458. customGenerators.push_back(std::make_unique<FractalDef>(std::move(*frac)));
  459. currentCustom = customGenerators[customGenerators.size() - 1].get();
  460. this->ui.radioButton_2->setChecked(true);
  461. setViewType(CUSTOM);
  462. }
  463. }
  464. void Almond::on_cancelProgress_clicked()
  465. {
  466. stopBackgroundTask();
  467. }