1
0

Almond.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include "Almond.h"
  2. #include <QIntValidator>
  3. #include <QFileDialog>
  4. #include <QMessageBox>
  5. #include <QGradient>
  6. #include "gradientchoosedialog.h"
  7. #include <cmath>
  8. Almond::Almond(QWidget* parent) :
  9. QMainWindow{ parent },
  10. mandelContext{ mnd::initializeContext() }
  11. {
  12. ui.setupUi(this);
  13. mw = std::make_unique<MandelWidget>(mandelContext,
  14. &mandelContext.getDefaultGenerator(),
  15. ui.centralWidget);
  16. customGeneratorDialog = std::make_unique<CustomGenerator>(mandelContext);
  17. customGenerator = nullptr;
  18. customViewSave = mnd::MandelViewport::centerView();
  19. on_maxIterations_editingFinished();
  20. mw->setSmoothColoring(ui.smooth->isChecked());
  21. currentView = MANDELBROT;
  22. mandelGenerator = &mandelContext.getDefaultGenerator();
  23. mandelViewSave = mw->getViewport();
  24. QObject::connect(mw.get(), &MandelWidget::pointSelected, this, &Almond::pointSelected);
  25. ui.mainContainer->addWidget(mw.get());
  26. ui.maxIterations->setValidator(new QIntValidator(1, 1000000000, this));
  27. ui.backgroundProgress->setVisible(false);
  28. //ui.verticalLayout_left->addWidget(new MyGLWidget(ui.centralWidget));
  29. //mw->show();
  30. }
  31. Almond::~Almond(void)
  32. {
  33. }
  34. void Almond::submitBackgroundTask(BackgroundTask* task)
  35. {
  36. bool taken = QThreadPool::globalInstance()->tryTake(task->getRunnable());
  37. if (taken) {
  38. ui.backgroundProgress->setRange(0, 0);
  39. ui.backgroundProgress->setVisible(true);
  40. }
  41. }
  42. void Almond::backgroundTaskFinished(void)
  43. {
  44. }
  45. void Almond::on_zoom_out_clicked()
  46. {
  47. mw->zoom(2);
  48. }
  49. void Almond::on_zoom_in_clicked()
  50. {
  51. mw->zoom(0.5);
  52. }
  53. void Almond::on_maxIterations_editingFinished()
  54. {
  55. QString text = ui.maxIterations->text();
  56. int maxIter = text.toInt();
  57. mw->setMaxIterations(maxIter);
  58. }
  59. void Almond::on_chooseGradient_clicked()
  60. {
  61. auto response = gcd.exec();
  62. auto gradient = gcd.getGradient();
  63. if (gradient)
  64. mw->setGradient(std::move(*gradient));
  65. }
  66. void Almond::on_exportVideo_clicked()
  67. {
  68. ExportVideoInfo evi;
  69. evi.start = mnd::MandelViewport::standardView();
  70. evi.end = mw->getViewport();
  71. evi.gradient = mw->getGradient();
  72. ExportVideoDialog dialog(this, evi);
  73. //dialog.show();
  74. auto response = dialog.exec();
  75. printf("dialog executed\n"); fflush(stdout);
  76. if (response == 1) {
  77. mnd::MandelInfo mi;
  78. evi = dialog.getExportVideoInfo();
  79. //Video
  80. /*mi.maxIter = dialog.getMaxIterations();
  81. mi.view = mw->getViewport();
  82. mi.bWidth = dialog.getWidth();
  83. mi.bHeight = dialog.getHeight();
  84. mi.view.adjustAspectRatio(mi.bWidth, mi.bHeight);
  85. mnd::Generator& g = mandelContext.getDefaultGenerator();
  86. auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  87. g.generate(mi, fmap.pixels.get());
  88. 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) }; });
  89. QImage img((unsigned char*)bitmap.pixels.get(), bitmap.width, bitmap.height, bitmap.width * 3, QImage::Format_RGB888);
  90. img.save(dialog.getPath());*/
  91. }
  92. }
  93. void Almond::on_smooth_stateChanged(int checked)
  94. {
  95. this->mw->setSmoothColoring(checked != Qt::Unchecked);
  96. }
  97. void Almond::on_exportImage_clicked()
  98. {
  99. ExportImageDialog dialog(this);
  100. dialog.setMaxIterations(mw->getMaxIterations());
  101. //dialog.show();
  102. auto response = dialog.exec();
  103. if (response == 1) {
  104. mnd::MandelInfo mi;
  105. mi.maxIter = dialog.getMaxIterations();
  106. mi.view = mw->getViewport();
  107. mi.bWidth = dialog.getWidth();
  108. mi.bHeight = dialog.getHeight();
  109. mi.view.adjustAspectRatio(mi.bWidth, mi.bHeight);
  110. mi.smooth = mw->getSmoothColoring();
  111. if (currentView == JULIA) {
  112. mi.julia = mw->getMandelInfo().julia;
  113. mi.juliaX = mw->getJuliaX();
  114. mi.juliaY = mw->getJuliaY();
  115. }
  116. mnd::MandelGenerator* currentGenerator = mw->getGenerator();
  117. mnd::MandelGenerator& g = currentGenerator ? *currentGenerator : mandelContext.getDefaultGenerator();
  118. auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  119. g.generate(mi, fmap.pixels.get());
  120. auto bitmap = fmap.map<RGBColor>([&mi, this] (float i) {
  121. return i >= mi.maxIter ? RGBColor{ 0,0,0 } : mw->getGradient().get(i);
  122. });
  123. QImage img(reinterpret_cast<unsigned char*>(bitmap.pixels.get()), bitmap.width, bitmap.height, bitmap.width * 3, QImage::Format_RGB888);
  124. img.save(dialog.getPath());
  125. }
  126. }
  127. void Almond::on_resetZoom_clicked()
  128. {
  129. if (currentView == MANDELBROT) {
  130. mw->setViewport(mnd::MandelViewport::standardView());
  131. }
  132. else {
  133. mw->setViewport(mnd::MandelViewport::centerView());
  134. }
  135. }
  136. void Almond::on_displayInfo_stateChanged(int checked)
  137. {
  138. this->mw->setDisplayInfo(checked != Qt::Unchecked);
  139. }
  140. void Almond::on_chooseGenerator_clicked()
  141. {
  142. if (!generatorsDialog)
  143. generatorsDialog = std::make_unique<ChooseGenerators>(mandelContext, *this);
  144. generatorsDialog->exec();
  145. auto gen = generatorsDialog->extractChosenGenerator();
  146. if (gen) {
  147. currentGenerator = gen.get();
  148. adjustedGenerators.push_back(std::move(gen));
  149. }
  150. else {
  151. //mandelGenerator = &mandelContext.getDefaultGenerator();
  152. }
  153. //this->currentView = MANDELBROT;
  154. this->mw->setGenerator(currentGenerator);
  155. //this->mw->getMandelInfo().julia = false;
  156. //printf("dialog executed\n"); fflush(stdout);
  157. }
  158. void Almond::on_selectPoint_clicked()
  159. {
  160. if (currentView == MANDELBROT) {
  161. emit this->mw->selectPoint();
  162. }
  163. }
  164. void Almond::pointSelected(mnd::Real x, mnd::Real y)
  165. {
  166. if (currentView != JULIA) {
  167. saveView();
  168. this->mw->setViewport(mnd::MandelViewport::centerView());
  169. this->mw->setJuliaPos(x, y);
  170. this->mw->getMandelInfo().julia = true;
  171. this->mw->clearAll();
  172. }
  173. currentView = JULIA;
  174. }
  175. void Almond::on_viewMandelbrot_clicked()
  176. {
  177. if (currentView != MANDELBROT) {
  178. //this->mw->setGenerator(mandelGeneratorSave);
  179. this->mw->setViewport(mandelViewSave);
  180. this->mw->getMandelInfo().julia = false;
  181. this->mw->clearAll();
  182. currentView = MANDELBROT;
  183. }
  184. }
  185. void Almond::on_groupBox_toggled(bool arg1)
  186. {
  187. printf("arg1: %i\n", int(arg1)); fflush(stdout);
  188. }
  189. void Almond::on_wMandel_clicked()
  190. {
  191. }
  192. void Almond::saveView()
  193. {
  194. if (currentView == MANDELBROT)
  195. mandelViewSave = mw->getViewport();
  196. else if (currentView == CUSTOM)
  197. customViewSave = mw->getViewport();
  198. }
  199. void Almond::setViewType(ViewType v)
  200. {
  201. saveView();
  202. if (v == MANDELBROT) {
  203. currentGenerator = mandelGenerator;
  204. emit this->mw->stopSelectingPoint();
  205. this->mw->setViewport(mandelViewSave);
  206. this->mw->setGenerator(currentGenerator);
  207. this->mw->getMandelInfo().julia = false;
  208. this->mw->clearAll();
  209. currentView = MANDELBROT;
  210. }
  211. else if (v == CUSTOM) {
  212. if (customGenerator != nullptr) {
  213. currentGenerator = customGenerator;
  214. this->mw->setGenerator(currentGenerator);
  215. emit this->mw->stopSelectingPoint();
  216. this->mw->setViewport(customViewSave);
  217. this->mw->getMandelInfo().julia = false;
  218. this->mw->clearAll();
  219. currentView = CUSTOM;
  220. }
  221. else {
  222. setViewType(MANDELBROT);
  223. }
  224. }
  225. else if (v == JULIA) {
  226. if (currentView == MANDELBROT) {
  227. emit this->mw->selectPoint();
  228. }
  229. else {
  230. currentView = MANDELBROT;
  231. currentGenerator = mandelGenerator;
  232. this->mw->setGenerator(currentGenerator);
  233. this->mw->setViewport(mandelViewSave);
  234. this->mw->getMandelInfo().julia = false;
  235. this->mw->clearAll();
  236. emit this->mw->selectPoint();
  237. }
  238. }
  239. }
  240. void Almond::on_wMandel_toggled(bool checked)
  241. {
  242. if (checked)
  243. setViewType(MANDELBROT);
  244. }
  245. void Almond::on_radioButton_toggled(bool checked)
  246. {
  247. saveView();
  248. if (checked) {
  249. setViewType(JULIA);
  250. }
  251. }
  252. void Almond::on_radioButton_2_toggled(bool checked)
  253. {
  254. saveView();
  255. if (checked) {
  256. if (customGenerator == nullptr) {
  257. customGeneratorDialog->exec();
  258. if (auto* frac = customGeneratorDialog->getLastCompiled()) {
  259. customGenerator = frac->gc.cpuGenerators[0].get();
  260. }
  261. }
  262. setViewType(CUSTOM);
  263. }
  264. }
  265. void Almond::on_createCustom_clicked()
  266. {
  267. customGeneratorDialog->exec();
  268. if (auto* frac = customGeneratorDialog->getLastCompiled()) {
  269. customGenerator = frac->gc.cpuGenerators[0].get();
  270. }
  271. setViewType(CUSTOM);
  272. }