Almond.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. currentGenerator = &mandelContext.getDefaultGenerator();
  14. mw = std::make_unique<MandelWidget>(mandelContext, currentGenerator, ui.centralWidget);
  15. ui.mainContainer->addWidget(mw.get());
  16. ui.maxIterations->setValidator(new QIntValidator(1, 1000000000, this));
  17. ui.backgroundProgress->setVisible(false);
  18. //ui.verticalLayout_left->addWidget(new MyGLWidget(ui.centralWidget));
  19. //mw->show();
  20. }
  21. Almond::~Almond(void)
  22. {
  23. }
  24. void Almond::submitBackgroundTask(BackgroundTask* task)
  25. {
  26. bool taken = QThreadPool::globalInstance()->tryTake(task->getRunnable());
  27. if (taken) {
  28. ui.backgroundProgress->setRange(0, 0);
  29. ui.backgroundProgress->setVisible(true);
  30. }
  31. }
  32. void Almond::on_zoom_out_clicked()
  33. {
  34. mw->zoom(2);
  35. }
  36. void Almond::on_zoom_in_clicked()
  37. {
  38. mw->zoom(0.5);
  39. }
  40. void Almond::on_maxIterations_editingFinished()
  41. {
  42. QString text = ui.maxIterations->text();
  43. int maxIter = text.toInt();
  44. mw->setMaxIterations(maxIter);
  45. }
  46. void Almond::on_chooseGradient_clicked()
  47. {
  48. auto response = gcd.exec();
  49. auto gradient = gcd.getGradient();
  50. if (gradient)
  51. mw->setGradient(std::move(*gradient));
  52. }
  53. void Almond::on_exportVideo_clicked()
  54. {
  55. ExportVideoInfo evi;
  56. evi.start = mnd::MandelViewport::standardView();
  57. evi.end = mw->getViewport();
  58. evi.gradient = mw->getGradient();
  59. ExportVideoDialog dialog(this, evi);
  60. //dialog.show();
  61. auto response = dialog.exec();
  62. printf("dialog executed\n"); fflush(stdout);
  63. if (response == 1) {
  64. mnd::MandelInfo mi;
  65. evi = dialog.getExportVideoInfo();
  66. //Video
  67. /*mi.maxIter = dialog.getMaxIterations();
  68. mi.view = mw->getViewport();
  69. mi.bWidth = dialog.getWidth();
  70. mi.bHeight = dialog.getHeight();
  71. mi.view.adjustAspectRatio(mi.bWidth, mi.bHeight);
  72. mnd::Generator& g = mandelContext.getDefaultGenerator();
  73. auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  74. g.generate(mi, fmap.pixels.get());
  75. 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) }; });
  76. QImage img((unsigned char*)bitmap.pixels.get(), bitmap.width, bitmap.height, bitmap.width * 3, QImage::Format_RGB888);
  77. img.save(dialog.getPath());*/
  78. }
  79. }
  80. void Almond::on_smooth_stateChanged(int checked)
  81. {
  82. this->mw->setSmoothColoring(checked != Qt::Unchecked);
  83. }
  84. void Almond::on_exportImage_clicked()
  85. {
  86. ExportImageDialog dialog(this);
  87. dialog.setMaxIterations(mw->getMaxIterations());
  88. //dialog.show();
  89. auto response = dialog.exec();
  90. if (response == 1) {
  91. mnd::MandelInfo mi;
  92. mi.maxIter = dialog.getMaxIterations();
  93. mi.view = mw->getViewport();
  94. mi.bWidth = dialog.getWidth();
  95. mi.bHeight = dialog.getHeight();
  96. mi.view.adjustAspectRatio(mi.bWidth, mi.bHeight);
  97. mnd::Generator& g = mandelContext.getDefaultGenerator();
  98. auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  99. g.generate(mi, fmap.pixels.get());
  100. auto bitmap = fmap.map<RGBColor>([&mi, this] (float i) {
  101. return i >= mi.maxIter ? RGBColor{ 0,0,0 } : mw->getGradient().get(i);
  102. });
  103. QImage img(reinterpret_cast<unsigned char*>(bitmap.pixels.get()), bitmap.width, bitmap.height, bitmap.width * 3, QImage::Format_RGB888);
  104. img.save(dialog.getPath());
  105. }
  106. }
  107. void Almond::on_resetZoom_clicked()
  108. {
  109. mw->setViewport(mnd::MandelViewport::standardView());
  110. }
  111. void Almond::on_displayInfo_stateChanged(int checked)
  112. {
  113. this->mw->setDisplayInfo(checked != Qt::Unchecked);
  114. }
  115. void Almond::on_chooseGenerator_clicked()
  116. {
  117. if (!generatorsDialog)
  118. generatorsDialog = std::make_unique<ChooseGenerators>(mandelContext, this);
  119. generatorsDialog->exec();
  120. if (generatorsDialog->getChosenGenerator()) {
  121. this->currentGenerator = generatorsDialog->getChosenGenerator();
  122. }
  123. else {
  124. this->currentGenerator = &mandelContext.getDefaultGenerator();
  125. }
  126. this->mw->setGenerator(currentGenerator);
  127. printf("dialog executed\n"); fflush(stdout);
  128. }