/* * settingswidget.cpp * * This file is covered by the LICENSE file in the root of this project. */ #include #include #include #include #include "settingswidget.h" static inline void setup_button( QLayout *layout, QPushButton *button, const char *method_name ) { QObject::connect( button, SIGNAL(clicked()), button->parentWidget(), method_name ); layout->addWidget(button); } static inline void setup_edit( SettingsWidget *parent, QGridLayout *layout, const QString &label, QLineEdit *edit, int y ) { layout->addWidget(new QLabel(label, parent), y, 0); edit->setMinimumWidth(256); QObject::connect( edit, &QLineEdit::editingFinished, parent, &SettingsWidget::update_form ); layout->addWidget(edit, y, 1); } SettingsWidget::SettingsWidget(QWidget *parent) : QDialog(parent), max_iter(new TypedLineEdit(this)), width(new TypedLineEdit(this)), height(new TypedLineEdit(this)), center_f_x(new TypedLineEdit(this)), center_f_y(new TypedLineEdit(this)), one(new TypedLineEdit(this)), clear_image(new QCheckBox("Clear image", this)), updating(false) { QGridLayout *grid_layout = new QGridLayout(this); QBoxLayout *button_row = new QBoxLayout(QBoxLayout::Direction::RightToLeft); setup_edit(this, grid_layout, "max_iter", max_iter, 0); setup_edit(this, grid_layout, "width", width, 1); setup_edit(this, grid_layout, "height", height, 2); setup_edit(this, grid_layout, "x", center_f_x, 3); setup_edit(this, grid_layout, "y", center_f_y, 4); setup_edit(this, grid_layout, "one", one, 5); connect( clear_image, &QCheckBox::clicked, this, &SettingsWidget::update_form ); grid_layout->addWidget(clear_image, grid_layout->rowCount(), 1); setup_button( button_row, new QPushButton("C&alculate", this), SLOT(accept()) ); setup_button( button_row, new QPushButton("&Reset", this), SLOT(reset_form()) ); setup_button(button_row, new QPushButton("&Cancel", this), SLOT(reject())); grid_layout->addLayout(button_row, grid_layout->rowCount(), 0, 1, 2); } void SettingsWidget::update_params(const MandelParams ¶ms) { max_iter->set_value(params.get_max_iter()); orig = params; reset_params_fields(); clear_image->setChecked(false); } void SettingsWidget::reset_params_fields() { QSize size = orig.get_size(); MpzPoint center_f = orig.get_center_f(); width->set_value(size.width()); height->set_value(size.height()); center_f_x->set_value(center_f.get_x()); center_f_y->set_value(center_f.get_y()); one->set_value(orig.get_one()); } bool SettingsWidget::are_params_fields_changed() { const QSize size = orig.get_size(); const MpzPoint center_f = orig.get_center_f(); return ( width->get_value() != size.width() || height->get_value() != size.height() || center_f_x->get_value() != center_f.get_x() || center_f_y->get_value() != center_f.get_y() || one->get_value() != orig.get_one() ); } MandelParams SettingsWidget::get_params() const { return MandelParams( max_iter->get_value(), QSize(width->get_value(), height->get_value()), MpzPoint(center_f_x->get_value(), center_f_y->get_value()), one->get_value() ); } void SettingsWidget::update_form() { bool params_fields_changed = are_params_fields_changed(); bool max_iter_is_less = max_iter->get_value() < orig.get_max_iter(); if ( updating || clear_image->isChecked() || (!params_fields_changed && !max_iter_is_less) ) return; updating = true; if (QMessageBox::question( this, "Bigintmandel Settings", ( "Do you want to clear and recalculate the image?\n" "Choosing no will undo your change." ) ) == QMessageBox::StandardButton::Yes) clear_image->setChecked(true); else { if (params_fields_changed) reset_params_fields(); if (max_iter_is_less) max_iter->set_value(orig.get_max_iter()); } updating = false; } void SettingsWidget::reset_form() { max_iter->set_value(orig.get_max_iter()); reset_params_fields(); clear_image->setChecked(false); }