Almond.cpp 15 KB

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