Almond.cpp 13 KB

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