customgenerator.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "customgenerator.h"
  2. #include "ui_customgenerator.h"
  3. #include <QMessageBox>
  4. #include <IterationCompiler.h>
  5. CustomGenerator::CustomGenerator(mnd::MandelContext& mndCtxt, QWidget *parent) :
  6. QDialog{ parent },
  7. mndCtxt{ mndCtxt },
  8. ui{ new Ui::CustomGenerator }
  9. {
  10. ui->setupUi(this);
  11. }
  12. CustomGenerator::~CustomGenerator()
  13. {
  14. delete ui;
  15. }
  16. void CustomGenerator::compile()
  17. {
  18. QString z0formula = this->ui->formula_z0->text();
  19. QString ziformula = this->ui->formula_zi->text();
  20. auto msgError = [this] (const std::string& msgText) {
  21. QMessageBox msg("Compile Error", QString::fromStdString(msgText),
  22. QMessageBox::Icon::Critical, 0, 0, 0, this);
  23. return msg.exec();
  24. };
  25. mnd::IterationFormula zi;
  26. mnd::IterationFormula z0;
  27. try {
  28. zi = { mnd::parse(ziformula.toStdString()), { "c", "z" } };
  29. } catch (const mnd::ParseError& pe) {
  30. msgError(pe.what());
  31. return;
  32. }
  33. try {
  34. z0 = { mnd::parse(z0formula.toStdString()), { "c" } };
  35. } catch (const mnd::ParseError& pe) {
  36. msgError(pe.what());
  37. return;
  38. }
  39. mnd::GeneratorCollection cr;
  40. try {
  41. //std::cout << mnd::toString(*z0.expr) << std::endl;
  42. //std::cout << mnd::toString(*zi.expr) << std::endl;
  43. cr = mnd::compileFormula(mndCtxt, z0, zi);
  44. }
  45. catch(const mnd::ParseError& pe) {
  46. printf("Parse error: %s\n", pe.what());
  47. return;
  48. }
  49. catch(const std::string& e) {
  50. printf("error: %s\n", e.c_str());
  51. return;
  52. }
  53. /*catch(const char* e) {
  54. printf("error: %s\n", e);
  55. return;
  56. }*/
  57. fflush(stdout);
  58. fractalDefs.push_back(FractalDef {
  59. "name",
  60. z0formula,
  61. ziformula,
  62. std::move(cr)
  63. });
  64. }
  65. FractalDef* CustomGenerator::getLastCompiled(void)
  66. {
  67. if (!fractalDefs.empty())
  68. return &fractalDefs[fractalDefs.size() - 1];
  69. else
  70. return nullptr;
  71. }
  72. void CustomGenerator::on_buttonBox_accepted()
  73. {
  74. compile();
  75. }