From d518433b94e1a0fd9483ca5fb7b0743e105b4e89 Mon Sep 17 00:00:00 2001 From: mar77i Date: Mon, 13 May 2024 01:35:33 +0200 Subject: [PATCH] clean up settingswidget fields --- bigintmandelwidget.cpp | 4 +- settingswidget.cpp | 181 ++++++++++++++++++----------------------- settingswidget.h | 121 ++++++++------------------- 3 files changed, 119 insertions(+), 187 deletions(-) diff --git a/bigintmandelwidget.cpp b/bigintmandelwidget.cpp index 91a99f1..43c240f 100644 --- a/bigintmandelwidget.cpp +++ b/bigintmandelwidget.cpp @@ -171,7 +171,7 @@ void BigintMandelWidget::export_img() { } void BigintMandelWidget::exec_settings_widget() { - settings_widget->update_fields(settings.get_params()); + settings_widget->update_params(settings.get_params()); settings_widget->exec(); } @@ -181,7 +181,7 @@ void BigintMandelWidget::reset() { } void BigintMandelWidget::settings_widget_accepted() { - if (settings_widget->clear_image()) { + if (settings_widget->is_clear_image_checked()) { settings.set_params(settings_widget->get_params()); settings.clear(); } else { diff --git a/settingswidget.cpp b/settingswidget.cpp index 4bb00c9..ab7c5e5 100644 --- a/settingswidget.cpp +++ b/settingswidget.cpp @@ -5,115 +5,70 @@ * This file is covered by the LICENSE file in the root of this project. */ -#include -#include #include -#include +#include #include +#include #include "settingswidget.h" -#include "uint64validator.h" - -SettingsWidgetFieldBase::SettingsWidgetFieldBase( - const QString &label, QWidget *parent -) : edit(new QLineEdit("", parent)), - label(label) -{ - edit->setMinimumWidth(256); -} - -void SettingsWidgetFieldBase::setup( - QGridLayout *grid_layout, - SettingsWidget *settings_widget, - const char *method_name -) { - int row_count = grid_layout->rowCount(); - grid_layout->addWidget( - new QLabel(label, edit->parentWidget()), row_count, 0 - ); - grid_layout->addWidget(edit, row_count, 1); - QObject::connect( - edit, SIGNAL(editingFinished()), settings_widget, method_name - ); -} - -void SettingsWidgetFieldBase::reset() { - edit->setText(orig_value); -} - -SettingsWidgetField::SettingsWidgetField( - const QString &label, QWidget *parent -) : SettingsWidgetFieldBase(label, parent) { - edit->setValidator(new UInt64Validator(edit)); -} - -void SettingsWidgetField::set_value(quint64 value) { - orig_value = QString::number(value); - reset(); -} - -SettingsWidgetField::SettingsWidgetField( - const QString &label, QWidget *parent -) : SettingsWidgetFieldBase(label, parent) { - edit->setValidator(new QIntValidator(edit)); -} - -void SettingsWidgetField::set_value(int value) { - orig_value = QString::number(value); - reset(); -} - -SettingsWidgetField::SettingsWidgetField( - const QString &label, QWidget *parent -) : SettingsWidgetFieldBase(label, parent) { - static QRegularExpression number_re("^\\d+$"); - edit->setValidator(new QRegularExpressionValidator(number_re)); -} - -void SettingsWidgetField::set_value(const mpz_class &value) { - orig_value = QString::QString::fromStdString(value.get_str()); - reset(); -} -inline void SettingsWidget::setup_button( +static inline void setup_button( QLayout *layout, QPushButton *button, const char *method_name ) { - connect( + QObject::connect( button, SIGNAL(clicked()), - this, + 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("max_iter", this), - width("width", this), - height("height", this), - center_f_x("x", this), - center_f_y("y", this), - one("one", this), - clear_image_checkbox(new QCheckBox("Clear image", this)), + 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); - int i; - max_iter.setup(grid_layout, this, SLOT(update_form())); - width.setup(grid_layout, this, SLOT(update_form())); - height.setup(grid_layout, this, SLOT(update_form())); - center_f_x.setup(grid_layout, this, SLOT(update_form())); - center_f_y.setup(grid_layout, this, SLOT(update_form())); - one.setup(grid_layout, this, SLOT(update_form())); + 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_checkbox, + clear_image, &QCheckBox::clicked, this, &SettingsWidget::update_form ); - grid_layout->addWidget(clear_image_checkbox, grid_layout->rowCount(), 1); + grid_layout->addWidget(clear_image, grid_layout->rowCount(), 1); setup_button( button_row, new QPushButton("C&alculate", this), SLOT(accept()) ); @@ -124,24 +79,50 @@ SettingsWidget::SettingsWidget(QWidget *parent) grid_layout->addLayout(button_row, grid_layout->rowCount(), 0, 1, 2); } -void SettingsWidget::update_fields(const MandelParams ¶ms) { - QSize size = params.get_size(); - MpzPoint center_f = params.get_center_f(); - max_iter.set_value(params.get_max_iter()); - 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(params.get_one()); - clear_image_checkbox->setChecked(false); +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() < max_iter.get_orig_value(); + bool max_iter_is_less = max_iter->get_value() < orig.get_max_iter(); if ( updating - || clear_image_checkbox->isChecked() + || clear_image->isChecked() || (!params_fields_changed && !max_iter_is_less) ) return; @@ -154,18 +135,18 @@ void SettingsWidget::update_form() { "Choosing no will undo your change." ) ) == QMessageBox::StandardButton::Yes) - clear_image_checkbox->setChecked(true); + clear_image->setChecked(true); else { if (params_fields_changed) reset_params_fields(); if (max_iter_is_less) - max_iter.reset(); + max_iter->set_value(orig.get_max_iter()); } updating = false; } void SettingsWidget::reset_form() { - max_iter.reset(); + max_iter->set_value(orig.get_max_iter()); reset_params_fields(); - clear_image_checkbox->setChecked(false); + clear_image->setChecked(false); } diff --git a/settingswidget.h b/settingswidget.h index 6789230..be1df0b 100644 --- a/settingswidget.h +++ b/settingswidget.h @@ -10,119 +10,70 @@ #include #include -#include -#include #include #include "mandel.h" +#include "uint64validator.h" -class SettingsWidget; +template class TypedLineEdit; -class SettingsWidgetFieldBase { -protected: - QLineEdit *edit; - const QString label; - QString orig_value; - -public: - SettingsWidgetFieldBase(const QString &label, QWidget *parent = nullptr); - inline bool is_edited() const { return edit->text() != orig_value; } - void setup( - QGridLayout *grid_layout, - SettingsWidget *settings_widget, - const char *method_name - ); - void reset(); -}; - -template class SettingsWidgetField; -template <> -class SettingsWidgetField : public SettingsWidgetFieldBase { +template <> class TypedLineEdit : public QLineEdit { public: - explicit SettingsWidgetField( - const QString &label, QWidget *parent = nullptr - ); - void set_value(quint64 value); - inline const quint64 get_value() const { - return edit->text().toULongLong(); + explicit TypedLineEdit(QWidget *parent = nullptr) : QLineEdit(parent) { + setValidator(new UInt64Validator(this)); } - inline const quint64 get_orig_value() const { - return orig_value.toULongLong(); + inline void set_value(const quint64 &value) { + setText(QString::number(value)); } + inline quint64 get_value() const { return text().toULongLong(); } }; -template <> -class SettingsWidgetField : public SettingsWidgetFieldBase { +template <> class TypedLineEdit : public QLineEdit { public: - explicit SettingsWidgetField( - const QString &label, QWidget *parent = nullptr - ); - void set_value(int value); - inline const int get_value() const { - return edit->text().toInt(); + explicit TypedLineEdit(QWidget *parent = nullptr) : QLineEdit(parent) { + setValidator(new QIntValidator(this)); } - inline const int get_orig_value() const { - return orig_value.toInt(); + inline void set_value(const int &value) { + setText(QString::number(value)); } + inline int get_value() const { return text().toInt(); } }; -template <> -class SettingsWidgetField : public SettingsWidgetFieldBase { +template <> class TypedLineEdit : public QLineEdit { public: - explicit SettingsWidgetField( - const QString &label, QWidget *parent = nullptr - ); - void set_value(const mpz_class &value); - inline const mpz_class get_value() const { - return mpz_class(edit->text().toStdString()); + explicit TypedLineEdit(QWidget *parent = nullptr) : QLineEdit(parent) { + QRegularExpression number_re("^-?\\d+$"); + setValidator(new QRegularExpressionValidator(number_re, this)); } - inline const mpz_class get_orig_value() const { - return mpz_class(orig_value.toStdString()); + inline void set_value(const mpz_class &value) { + setText(QString::fromStdString(value.get_str())); + } + inline mpz_class get_value() const { + return mpz_class(text().toStdString()); } }; class SettingsWidget : public QDialog { Q_OBJECT - SettingsWidgetField max_iter; - SettingsWidgetField width, height; - SettingsWidgetField center_f_x, center_f_y, one; - QCheckBox *clear_image_checkbox; + MandelParams orig; + TypedLineEdit *max_iter; + TypedLineEdit *width, *height; + TypedLineEdit *center_f_x, *center_f_y, *one; + QCheckBox *clear_image; bool updating; - inline void setup_button( - QLayout *layout, QPushButton *button, const char *method_name - ); public: explicit SettingsWidget(QWidget *parent = nullptr); - void update_fields(const MandelParams ¶ms); - inline const quint64 get_max_iter() const { return max_iter.get_value(); } - inline bool are_params_fields_changed() { - return ( - width.is_edited() - || height.is_edited() - || center_f_x.is_edited() - || center_f_y.is_edited() - || one.is_edited() - ); - } - inline void reset_params_fields() { - width.reset(); - height.reset(); - center_f_x.reset(); - center_f_y.reset(); - one.reset(); - } - inline const MandelParams get_params() { - 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 update_params(const MandelParams ¶ms); + void reset_params_fields(); + bool are_params_fields_changed(); + inline const quint64 get_max_iter() const { + return max_iter->get_value(); } - inline bool clear_image() const { - return clear_image_checkbox->isChecked(); + MandelParams get_params() const; + inline bool is_clear_image_checked() const { + return clear_image->isChecked(); } public Q_SLOTS: -- 2.47.0