Almond.cpp 4.2 KB

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