Almond.cpp 13 KB

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