1
0

Almond.cpp 4.1 KB

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