From eaa01d33c39451905cc0cbde84aa74145c399e92 Mon Sep 17 00:00:00 2001 From: mar77i Date: Sat, 13 Apr 2024 10:26:57 +0200 Subject: [PATCH] allow modifying max_iter --- bigintwidget.cpp | 67 ++++++++++++++++++++++++++++++---------------- bigintwidget.h | 10 ++++--- mandel.cpp | 23 +++++++++++----- mandel.h | 4 ++- settingswidget.cpp | 28 +++++++++++++------ settingswidget.h | 5 ++-- 6 files changed, 94 insertions(+), 43 deletions(-) diff --git a/bigintwidget.cpp b/bigintwidget.cpp index ed05441..6a5cfa2 100644 --- a/bigintwidget.cpp +++ b/bigintwidget.cpp @@ -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::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(); } diff --git a/bigintwidget.h b/bigintwidget.h index 1881852..6db7f5c 100644 --- a/bigintwidget.h +++ b/bigintwidget.h @@ -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 diff --git a/mandel.cpp b/mandel.cpp index 205a2a9..a86c057 100644 --- a/mandel.cpp +++ b/mandel.cpp @@ -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::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, ¤t); } +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, ¤t); } diff --git a/mandel.h b/mandel.h index 74377f7..b86784f 100644 --- 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 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); diff --git a/settingswidget.cpp b/settingswidget.cpp index fecc47f..1dd5de9 100644 --- a/settingswidget.cpp +++ b/settingswidget.cpp @@ -4,27 +4,38 @@ #include #include #include +#include #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 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 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 ¶ms) { 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())); } diff --git a/settingswidget.h b/settingswidget.h index cd5d178..5621627 100644 --- a/settingswidget.h +++ b/settingswidget.h @@ -13,11 +13,12 @@ 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 ¶ms); + inline const QString get_max_iter() const { return max_iter->text(); }; }; #endif // SETTINGSWIDGET_H -- 2.47.0