Almond.cpp 13 KB

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