Almond.cpp 14 KB

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