]> git.mar77i.info Git - bigintmandel/commitdiff
allow modifying max_iter
authormar77i <mar77i@protonmail.ch>
Sat, 13 Apr 2024 08:26:57 +0000 (10:26 +0200)
committermar77i <mar77i@protonmail.ch>
Sat, 13 Apr 2024 08:26:57 +0000 (10:26 +0200)
bigintwidget.cpp
bigintwidget.h
mandel.cpp
mandel.h
settingswidget.cpp
settingswidget.h

index ed054412584aa0cab9f24c8c395cf77ac76d48b1..6a5cfa2410a8324e815461f63aee746e2cd96fcf 100644 (file)
@@ -56,6 +56,12 @@ BigintWidget::BigintWidget(QWidget *parent)
     settings_widget(new SettingsWidget(this))
 {
     scroll_area->setWidget(img_label);
+    connect(
+        settings_widget,
+        &QDialog::accepted,
+        this,
+        &BigintWidget::settings_widget_accepted
+    );
     connect(
         fw,
         &QFutureWatcher<MandelResultCell>::resultReadyAt,
@@ -86,13 +92,11 @@ BigintWidget::~BigintWidget() {
 
 void BigintWidget::finished_cell(int num) {
     settings.finished_cell(num, fw->resultAt(num));
-    img_dirty = true;
-    update();
+    update_img();
 }
 
 void BigintWidget::finished() {
-    img_dirty = true;
-    update();
+    update_img();
 }
 
 static inline void calculating_status(
@@ -113,31 +117,40 @@ static inline void finished_status(QStatusBar *status_bar) {
 
 void BigintWidget::paintEvent(QPaintEvent *event) {
     static int prev_num_threads = -1;
-    if (img_dirty) {
-        img_label->setPixmap(settings.get_pixmap());
-        img_label->resize(img_label->pixmap().size());
-        img_dirty = false;
-        if (fw->isFinished())
-            finished_status(status_bar);
-        else {
-            calculating_status(status_bar, prev_num_threads);
-            prev_num_threads = -1;
-        }
+    if (!img_dirty)
+        return;
+    img_label->setPixmap(settings.get_pixmap());
+    img_label->resize(img_label->pixmap().size());
+    img_dirty = false;
+    if (fw->isFinished())
+        finished_status(status_bar);
+    else {
+        calculating_status(status_bar, prev_num_threads);
+        prev_num_threads = -1;
     }
 }
 
 void BigintWidget::mousePressEvent(QMouseEvent *event) {
-    if (!fw->isFinished() || event->button() != Qt::MouseButton::LeftButton)
-        return;
     QSize size(settings.get_current().get_size());
-    QPoint pos = img_label->mapFromParent(event->pos());
-    if (pos.x() < 0 || pos.x() >= size.width()
-            || pos.y() < 0 || pos.y() >= size.height())
+    QPoint pos(img_label->mapFromParent(event->pos()));
+    if (event->button() != Qt::MouseButton::LeftButton
+            || !fw->isFinished()
+            || pos.x() < 0
+            || pos.x() >= size.width()
+            || pos.y() < 0
+            || pos.y() >= size.height())
         return;
-    settings.zoom(pos);
+/*
+    settings.zoom(scroll_area->viewport()->size(), pos);
     fw->setFuture(
         QtConcurrent::mapped(settings.get_cells(), MandelSettings::iterate)
     );
+*/
+    // paint a small circle at point pos
+    update_img();
+}
+
+void BigintWidget::update_img() {
     img_dirty = true;
     update();
 }
@@ -151,19 +164,27 @@ void BigintWidget::export_img() {
 }
 
 void BigintWidget::exec_settings_widget() {
+    if (!fw->isFinished())
+        return;
     settings_widget->update_fields(settings.get_current());
     settings_widget->exec();
 }
 
 void BigintWidget::reset() {
     QSize size(scroll_area->viewport()->size());
-    qDebug() << "size" << size;
     fw->cancel();
     fw->waitForFinished();
     settings.reset(128, size);
     fw->setFuture(
         QtConcurrent::mapped(settings.get_cells(), MandelSettings::iterate)
     );
-    img_dirty = true;
-    update();
+    update_img();
+}
+
+void BigintWidget::settings_widget_accepted() {
+    settings.set_max_iter(settings_widget->get_max_iter().toULongLong());
+    fw->setFuture(
+        QtConcurrent::mapped(settings.get_cells(), MandelSettings::iterate)
+    );
+    update_img();
 }
index 188185243c40d6c3a98afd2256052d010c8d0cba..6db7f5c6999050962544207a98641d4bf4cbc1ba 100644 (file)
@@ -28,13 +28,17 @@ class BigintWidget : public QWidget {
 public:
     BigintWidget(QWidget *parent = nullptr);
     ~BigintWidget();
-    void finished_cell(int num);
-    void finished();
     void paintEvent(QPaintEvent *event);
     void mousePressEvent(QMouseEvent *event);
+    void update_img();
+
+public Q_SLOT:
+    void reset();
     void export_img();
     void exec_settings_widget();
-    void reset();
+    void settings_widget_accepted();
+    void finished_cell(int num);
+    void finished();
 };
 
 #endif // BIGINTWIDGET_H
index 205a2a99144a718332cedffc32e08a6e55d9c7f7..a86c057af9426b8cb0acac809351c776b678334f 100644 (file)
@@ -53,25 +53,38 @@ static inline void setup_cells(
         i->setup(pos);
 }
 
+static inline QImage *setup_image(QImage *img, const QSize &size) {
+    delete img;
+    img = new QImage(size, QImage::Format_RGB888);
+    img->fill(Qt::GlobalColor::black);
+    return img;
+}
+
 MandelSettings::MandelSettings(size_t max_iter, const QSize size)
 : img(nullptr)
 {
     reset(max_iter, size);
 }
 
-void MandelSettings::zoom(QPoint pos) {
-    QVector<MandelCell>::iterator i;
+void MandelSettings::zoom(const QSize size, const QPoint pos) {
     MpzPoint p = cells[img->width() * pos.y() + pos.x()].get_rpos0();
+    bool resize_img = size != current.get_size();
     current = MandelParams(
         current.get_max_iter(),
-        current.get_size(),
+        size,
         MpzPoint(p.get_x() * 2, p.get_y() * 2),
         current.get_one() * 2
     );
+    if (resize_img)
+        img = setup_image(img, size);
     img->fill(Qt::GlobalColor::black);
     setup_cells(cells, &current);
 }
 
+void MandelSettings::set_max_iter(size_t max_iter) {
+    current.set_max_iter(max_iter);
+}
+
 void MandelSettings::finished_cell(int num, const MandelResultCell &result) {
     size_t iter = result.get_iter();
     cells[num].set_result(result);
@@ -107,9 +120,7 @@ void MandelSettings::reset(size_t max_iter, const QSize size) {
     one = mpz_class(ione + 1);
     center_f = MpzPoint(one * -3 / 4, 0);
     current = MandelParams(max_iter, size, center_f, one);
-    delete img;
-    img = new QImage(size, QImage::Format_RGB888);
-    img->fill(Qt::GlobalColor::black);
+    img = setup_image(img, size);
     setup_cells(cells, &current);
 }
 
index 74377f755934fc4f1487e5e379aa30af908ebcdb..b86784f6e07463df3002db39baa642b6b899d79f 100644 (file)
--- a/mandel.h
+++ b/mandel.h
@@ -50,6 +50,7 @@ public:
     MandelParams(const MandelParams &other);
 
     const size_t get_max_iter() const { return max_iter; }
+    void set_max_iter(const size_t max_iter) { this->max_iter = max_iter; }
     const QSize get_size() const { return size; }
     const MpzPoint get_center_f() const { return center_f; }
     const mpz_class get_one() const { return one; }
@@ -66,7 +67,8 @@ public:
     const QVector<MandelCell> get_cells() const { return cells; }
     const MandelParams get_current() const { return current; }
 
-    void zoom(QPoint pos);
+    void zoom(const QSize size, const QPoint pos);
+    void set_max_iter(size_t max_iter);
     void finished_cell(int num, const MandelResultCell &cell);
     static MandelResultCell iterate(const MandelCell &cell);
     void save_img(QString file_name);
index fecc47f012da2c525423a3dbc52c17d1b9f8fd58..1dd5de9c91ebcf3527b7a517c8aa3ac4a651d134 100644 (file)
@@ -4,27 +4,38 @@
 #include <QPushButton>
 #include <QLabel>
 #include <QLayout>
+#include <QIntValidator>
 
 #include "settingswidget.h"
 
-static QStringList labels = { "width", "height", "x", "y", "one" };
 
 SettingsWidget::SettingsWidget(QWidget *parent)
 :   QDialog(parent),
+    max_iter(new QLineEdit("", this)),
     width(new QLineEdit("", this)),
     height(new QLineEdit("", this)),
-    x(new QLineEdit("", this)),
-    y(new QLineEdit("", this)),
+    center_f_x(new QLineEdit("", this)),
+    center_f_y(new QLineEdit("", this)),
     one(new QLineEdit("", this))
 {
+    static QStringList labels = {
+        "max_iter", "width", "height", "x", "y", "one"
+    };
+    static QList<QLineEdit*> line_edits = {
+        max_iter, width, height, center_f_x, center_f_y, one
+    };
     QPushButton *apply_button = new QPushButton("Apply");
     QGridLayout *grid_layout = new QGridLayout(this);
-    QList<QLineEdit*> line_edits = { width, height, x, y, one };
     int i;
     setLayout(grid_layout);
     for (i = 0; i < line_edits.size(); i++) {
         grid_layout->addWidget(new QLabel(labels[i], this), i, 0);
-        line_edits[i]->setReadOnly(true);
+        if (i == 0)
+            line_edits[i]->setValidator(
+                new QIntValidator(0, INT_MAX, line_edits[i])
+            );
+        else
+            line_edits[i]->setReadOnly(true);
         line_edits[i]->setMinimumWidth(256);
         grid_layout->addWidget(line_edits[i], i, 1);
     }
@@ -37,12 +48,13 @@ SettingsWidget::SettingsWidget(QWidget *parent)
     grid_layout->addWidget(apply_button, i, 1);
 }
 
-void SettingsWidget::update_fields(const MandelParams params) {
+void SettingsWidget::update_fields(const MandelParams &params) {
     QSize size = params.get_size();
     MpzPoint center_f = params.get_center_f();
+    max_iter->setText(QString::number(params.get_max_iter()));
     width->setText(QString::number(size.width()));
     height->setText(QString::number(size.height()));
-    x->setText(QString::fromStdString(center_f.get_x().get_str()));
-    y->setText(QString::fromStdString(center_f.get_y().get_str()));
+    center_f_x->setText(QString::fromStdString(center_f.get_x().get_str()));
+    center_f_y->setText(QString::fromStdString(center_f.get_y().get_str()));
     one->setText(QString::fromStdString(params.get_one().get_str()));
 }
index cd5d1785aa367c6b2fef296f7823de5bb8d2de78..5621627b3ae23f38e3f8043c918ee128dcd38582 100644 (file)
 class SettingsWidget : public QDialog {
     Q_OBJECT
 
-    QLineEdit *width, *height, *x, *y, *one;
+    QLineEdit *max_iter, *width, *height, *center_f_x, *center_f_y, *one;
 
 public:
     SettingsWidget(QWidget *parent = nullptr);
-    void update_fields(const MandelParams params);
+    void update_fields(const MandelParams &params);
+    inline const QString get_max_iter() const { return max_iter->text(); };
 };
 
 #endif // SETTINGSWIDGET_H