123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578 |
- #include "Almond.h"
- #include <QIntValidator>
- #include <QIcon>
- #include <QFileDialog>
- #include <QMessageBox>
- #include <QStatusBar>
- #include <QGradient>
- #include <QWindow>
- #include "gradientchoosedialog.h"
- #include "GridFlowLayout.h"
- #include <cmath>
- Almond::Almond(QWidget* parent) :
- QMainWindow{ parent, Qt::WindowFlags() },
- mandelContext{ mnd::initializeContext() }
- {
- ui.setupUi(this);
-
-
-
- fractalWidget = new FractalWidget(this);
- fractalWidget->setGenerator(&mandelContext.getDefaultGenerator());
- fractalWidget->setGradient(Gradient::defaultGradient());
- customGeneratorDialog = std::make_unique<CustomGenerator>(mandelContext);
- customGenerator = nullptr;
- customViewSave = mnd::MandelViewport::centerView();
- on_maxIterations_editingFinished();
-
- currentView = MANDELBROT;
- mandelGenerator = &mandelContext.getDefaultGenerator();
-
-
- ui.mandel_container->addWidget(fractalWidget);
-
- ui.maxIterations->setValidator(new QIntValidator(1, 1000000000, this));
- ui.backgroundProgress->setEnabled(false);
- ui.cancelProgress->setEnabled(false);
- amw = new AlmondMenuWidget(this);
- amw->setMainMenu(ui.dockWidgetContents_2);
- eim = new ExportImageMenu();
- evm = new ExportVideoMenu();
- gradientMenu = new GradientMenu();
- AlmondSubMenu* imageSm = amw->addSubMenu(eim);
- AlmondSubMenu* videoSm = amw->addSubMenu(evm);
- AlmondSubMenu* gradientSm = amw->addSubMenu(gradientMenu);
- ui.dockWidget_2->setWidget(amw);
- connect(amw, &AlmondMenuWidget::submenuCancel, [this] (int) { amw->showMainMenu(); });
-
- connect(imageSm, &AlmondSubMenu::accepted, this, &Almond::imageExportOk);
- connect(videoSm, &AlmondSubMenu::accepted, this, &Almond::videoExportOk);
- connect(gradientSm, &AlmondSubMenu::accepted, this, &Almond::gradientEditOk);
- connect(gradientMenu, &GradientMenu::gradientChanged, [this] () {
- std::vector<std::pair<RGBColor, float>> np;
- const auto& points = gradientMenu->getGradient();
- std::transform(points.begin(), points.end(), std::back_inserter(np),
- [](auto& qp) -> std::pair<RGBColor, float> {
- auto& [pos, col] = qp;
- return { RGBColor{ uint8_t(col.red()), uint8_t(col.green()), uint8_t(col.blue()) },
- pos };
- });
- std::sort(np.begin(), np.end(), [](auto& a, auto& b) { return a.second < b.second; });
- if (!np.empty()) {
- auto& first = np.at(0);
- if (first.second > 0) {
- np.insert(np.begin(), { first.first, 0.0f });
- }
- auto& last = np.at(np.size() - 1);
- if (last.second < 1) {
- np.insert(np.begin(), { last.first, 1.0f });
- }
- }
- std::for_each(np.begin(), np.end(), [](auto& x) { x.second *= 300; });
- Gradient grad{ np, true };
- fractalWidget->setGradient(grad);
- });
-
- installEventFilter(this);
- backgroundTasks.setMaxThreadCount(1);
- QIcon icon{ ":/icons/icon" };
- icon.addFile(":/icons/icon@2x");
- this->setWindowIcon(icon);
-
-
- }
- Almond::~Almond(void)
- {
- }
- void Almond::submitBackgroundTask(BackgroundTask* task)
- {
- QObject::connect(task, &BackgroundTask::finished, this, &Almond::backgroundTaskFinished);
- QObject::connect(task, &BackgroundTask::progress, this, &Almond::backgroundTaskProgress);
- backgroundTasks.start(task);
-
- ui.backgroundProgress->setRange(0, 0);
- ui.backgroundProgress->setFormat("");
- ui.backgroundProgress->setEnabled(true);
- ui.cancelProgress->setEnabled(true);
-
- }
- void Almond::stopBackgroundTask(void)
- {
- stoppingBackgroundTasks = true;
- }
- bool Almond::eventFilter(QObject *target, QEvent *event)
- {
- if (event->type() == QEvent::KeyPress) {
- QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
- if (keyEvent->key() == Qt::Key_F11) {
- emit toggleFullscreen();
- }
- }
- return QObject::eventFilter(target, event);
- }
- void Almond::submenuOK(int smIndex)
- {
- switch(smIndex) {
- case 0:
- emit imageExportOk();
- break;
- case 1:
- emit videoExportOk();
- break;
- }
- }
- void Almond::imageExportOk(void)
- {
- mnd::MandelInfo mi;
- mi.maxIter = eim->getMaxIterations();
- mi.view = mnd::MandelViewport{};
- mi.bWidth = eim->getWidth();
- mi.bHeight = eim->getHeight();
- mi.view.adjustAspectRatio(mi.bWidth, mi.bHeight);
- mi.smooth = true;
-
- mnd::MandelGenerator* currentGenerator = nullptr;
- mnd::MandelGenerator& g = currentGenerator ? *currentGenerator : mandelContext.getDefaultGenerator();
- alm::ImageExportInfo iei;
- iei.drawInfo = mi;
- iei.generator = &g;
- iei.gradient = Gradient::defaultGradient();
- iei.path = eim->getPath().toStdString();
- iei.options.jpegQuality = 95;
- submitBackgroundTask(new ImageExportTask(iei, [this] () { return stoppingBackgroundTasks; }));
-
- amw->showMainMenu();
- }
- void Almond::videoExportOk(void)
- {
-
- }
- void Almond::gradientEditOk(void)
- {
- const auto& points = gradientMenu->getGradient();
-
-
- std::vector<std::pair<RGBColor, float>> np;
- std::transform(points.begin(), points.end(), std::back_inserter(np),
- [](auto& qp) -> std::pair<RGBColor, float> {
- auto& [pos, col] = qp;
- return { RGBColor{ uint8_t(col.red()), uint8_t(col.green()), uint8_t(col.blue()) },
- pos };
- });
- std::sort(np.begin(), np.end(), [](auto& a, auto& b) { return a.second < b.second; });
- if (!np.empty()) {
- auto& first = np.at(0);
- if (first.second > 0) {
- np.insert(np.begin(), { first.first, 0.0f });
- }
- auto& last = np.at(np.size() - 1);
- if (last.second < 1) {
- np.insert(np.begin(), { last.first, 1.0f });
- }
- }
- std::for_each(np.begin(), np.end(), [](auto& x) { x.second *= 300; });
- Gradient g{ np, true };
- fractalWidget->setGradient(std::move(g));
- amw->showMainMenu();
- }
- void Almond::toggleFullscreen(void)
- {
-
- }
- void Almond::backgroundTaskFinished(bool succ, QString message)
- {
- if (succ) {
- QMessageBox info = QMessageBox(QMessageBox::Icon::Information, "Task Finished", message);
-
- emit info.exec();
- }
- else {
- QMessageBox info = QMessageBox(QMessageBox::Icon::Critical, "Task Failed", message);
-
- emit info.exec();
- }
- ui.backgroundProgress->setFormat(tr("Export Progress"));
- ui.backgroundProgress->setEnabled(false);
- ui.backgroundProgress->setRange(0, 100);
- ui.backgroundProgress->setValue(0);
- ui.cancelProgress->setEnabled(false);
- stoppingBackgroundTasks = false;
- }
- void Almond::backgroundTaskProgress(float percentage)
- {
- QObject* task = QObject::sender();
- if (auto* bt = qobject_cast<BackgroundTask*>(task)) {
- ui.backgroundProgress->setFormat(QString::fromStdString(bt->getShortDescription() + ": %p%"));
- }
- if (percentage > 0) {
- ui.backgroundProgress->setRange(0, 100);
- ui.backgroundProgress->setValue(percentage);
- }
- else {
- ui.backgroundProgress->reset();
- ui.backgroundProgress->setRange(0, 0);
- ui.backgroundProgress->setValue(-1);
- }
- }
- void Almond::on_zoom_out_clicked()
- {
- fractalWidget->zoom(2);
- }
- void Almond::on_zoom_in_clicked()
- {
- fractalWidget->zoom(0.5);
- }
- void Almond::on_maxIterations_editingFinished()
- {
- QString text = ui.maxIterations->text();
- int maxIter = text.toInt();
-
- }
- void Almond::on_chooseGradient_clicked()
- {
- const auto& gradient = fractalWidget->getGradient();
- auto points = gradient.getPoints();
- std::for_each(points.begin(), points.end(), [](auto& x) { x.second /= 300; });
- QVector<QPair<float, QColor>> np;
- std::transform(points.begin(), points.end(), std::back_inserter(np),
- [](auto& qp) -> QPair<float, QColor> {
- auto& [col, pos] = qp;
- return { pos, QColor{ (col.r), (col.g), (col.b) } };
- });
- this->gradientMenu->setGradient(std::move(np));
- emit this->amw->showSubMenu(2);
-
-
-
-
- }
- void Almond::on_exportVideo_clicked()
- {
-
-
- }
- void Almond::on_smooth_stateChanged(int checked)
- {
-
- }
- void Almond::on_exportImage_clicked()
- {
- this->amw->showSubMenu(0);
- return;
- }
- void Almond::on_resetZoom_clicked()
- {
- if (currentView == MANDELBROT) {
- fractalWidget->setViewport(mnd::MandelViewport::standardView());
- }
- else {
- fractalWidget->setViewport(mnd::MandelViewport::centerView());
- }
- }
- void Almond::on_displayInfo_stateChanged(int checked)
- {
-
- }
- void Almond::on_chooseGenerator_clicked()
- {
- std::unique_ptr<ChooseGenerators> generatorsDialog;
- if (currentView == MANDELBROT || currentView == JULIA)
- generatorsDialog = std::make_unique<ChooseGenerators>(mandelContext, *mandelGenerator, *this);
- else if (currentView == CUSTOM)
- generatorsDialog = std::make_unique<ChooseGenerators>(mandelContext, this->currentCustom->gc, *customGenerator, *this);
- else
- return;
- auto response = generatorsDialog->exec();
- auto gen = generatorsDialog->extractChosenGenerator();
- if (gen) {
- if (currentView == MANDELBROT || currentView == JULIA) {
- mandelGenerator = gen.get();
- }
- else if (currentView == CUSTOM) {
- customGenerator = gen.get();
- }
- currentGenerator = gen.get();
- this->fractalWidget->setGenerator(currentGenerator);
- adjustedGenerators.push_back(std::move(gen));
- }
- else {
-
- }
-
-
-
- }
- void Almond::pointSelected(mnd::Real x, mnd::Real y)
- {
- if (currentView != JULIA) {
- saveView();
-
-
- }
- currentView = JULIA;
- }
- void Almond::on_wMandel_clicked()
- {
- }
- void Almond::saveView()
- {
- if (currentView == MANDELBROT)
- ;
- else if (currentView == CUSTOM)
- ;
- }
- void Almond::setViewType(ViewType v)
- {
-
-
- }
- void Almond::on_wMandel_toggled(bool checked)
- {
- if (checked)
- setViewType(MANDELBROT);
- }
- void Almond::on_radioButton_toggled(bool checked)
- {
- saveView();
- if (checked) {
- setViewType(JULIA);
- }
- }
- void Almond::on_radioButton_2_toggled(bool checked)
- {
- saveView();
- if (checked) {
- if (customGenerator == nullptr) {
- customGeneratorDialog->exec();
- if (auto* frac = customGeneratorDialog->getLastCompiled()) {
- customGenerator = frac->gc.adaptiveGenerator.get();
- customGenerators.push_back(std::make_unique<FractalDef>(std::move(*frac)));
- currentCustom = customGenerators[customGenerators.size() - 1].get();
- }
- }
- setViewType(CUSTOM);
- }
- }
- void Almond::on_createCustom_clicked()
- {
- auto response = customGeneratorDialog->exec();
- if (response != 1)
- return;
- if (auto* frac = customGeneratorDialog->getLastCompiled()) {
- customGenerator = frac->gc.adaptiveGenerator.get();
- customGenerators.push_back(std::make_unique<FractalDef>(std::move(*frac)));
- currentCustom = customGenerators[customGenerators.size() - 1].get();
- this->ui.radioButton_2->setChecked(true);
- setViewType(CUSTOM);
- }
- }
- void Almond::on_cancelProgress_clicked()
- {
- stopBackgroundTask();
- }
|